Files
adventofcode/2017/05/src/main.rs
2017-12-05 08:01:36 +01:00

88 lines
1.6 KiB
Rust

use std::io::{self, Read};
fn process_one(mut data: Vec<i64>) -> usize {
let mut index: i64 = 0;
let mut steps = 0;
loop {
let addr = data[index as usize];
data[index as usize] += 1;
index += addr;
steps += 1;
if index < 0 || index as usize >= data.len() {
break;
}
}
steps
}
fn process_two(mut data: Vec<i64>) -> usize {
let mut index: i64 = 0;
let mut steps = 0;
loop {
if index < 0 || index as usize >= data.len() {
break;
}
let addr = data[index as usize];
data[index as usize] += if addr < 3 { 1 } else { -1 };
index += addr;
steps += 1;
}
steps
}
fn parse(input: &str) -> Vec<i64> {
input.lines().filter_map(|line| line.parse().ok()).collect()
}
fn main() {
let mut input = String::new();
io::stdin()
.read_to_string(&mut input)
.expect("faild to read input");
let data = parse(&input);
println!("part_one={}", process_one(data.clone()));
println!("part_two={}", process_two(data));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse() {
let input = "0\n\
3\n\
0\n\
1\n\
-3";
assert_eq!(parse(input), vec![0, 3, 0, 1, -3]);
}
#[test]
fn test_process_one() {
assert_eq!(process_one(vec![0, 3, 0, 1, -3]), 5);
}
#[test]
fn test_process_two() {
assert_eq!(process_two(vec![0, 3, 0, 1, -3]), 10);
}
}