1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
use crate::input::Input; pub fn solve(input: &mut Input) -> Result<u32, String> { let jump_change_computer = |offset| { if input.is_part_one() || offset < 3 { 1 } else { -1 } }; let mut jumps: Vec<i32> = input .text .lines() .enumerate() .map(|(line_index, line)| { line.parse::<i32>().map_err(|error| { format!( "Invalid input at line {}: {}", line_index + 1, error.to_string() ) }) }) .collect::<Result<_, _>>()?; let mut position: i32 = 0; for step in 1..100_000_000 { let old_position = position; position += jumps[position as usize]; if position < 0 || position as usize >= jumps.len() { return Ok(step); } jumps[old_position as usize] += jump_change_computer(jumps[old_position as usize]); } Err("No solution found".to_string()) } #[test] fn test() { use crate::{test_part_one, test_part_two}; let real_input = include_str!("day05_input.txt"); test_part_one!(real_input => 374_269); test_part_two!(real_input => 27_720_699); }