Created
January 21, 2026 18:18
-
-
Save icub3d/7944dcc4ea750cc44d428a4211db2a07 to your computer and use it in GitHub Desktop.
Solution for Advent of Code 2017 Day 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::time::Instant; | |
| const INPUT: &str = include_str!("inputs/day05.txt"); | |
| fn parse(input: &str) -> Vec<isize> { | |
| input.lines().map(|l| l.parse::<isize>().unwrap()).collect() | |
| } | |
| fn p1(input: &str) -> usize { | |
| let mut input = parse(input); | |
| let mut pc = 0isize; | |
| let mut steps = 0usize; | |
| while pc >= 0 && pc < input.len() as isize { | |
| steps += 1; | |
| let cur_pc = pc; | |
| pc += input[pc as usize]; | |
| input[cur_pc as usize] += 1; | |
| } | |
| steps | |
| } | |
| fn p2(input: &str) -> usize { | |
| let mut input = parse(input); | |
| let mut pc = 0isize; | |
| let mut steps = 0usize; | |
| while pc >= 0 && pc < input.len() as isize { | |
| steps += 1; | |
| let cur_pc = pc; | |
| pc += input[pc as usize]; | |
| if input[cur_pc as usize] >= 3 { | |
| input[cur_pc as usize] -= 1; | |
| } else { | |
| input[cur_pc as usize] += 1; | |
| } | |
| } | |
| steps | |
| } | |
| fn main() { | |
| let now = Instant::now(); | |
| let solution = p1(INPUT); | |
| println!("p1 {:?} {}", now.elapsed(), solution); | |
| let now = Instant::now(); | |
| let solution = p2(INPUT); | |
| println!("p2 {:?} {}", now.elapsed(), solution); | |
| } | |
| #[cfg(test)] | |
| mod tests { | |
| use super::*; | |
| #[test] | |
| fn test_p1() { | |
| let input = "0\n3\n0\n1\n-3"; | |
| assert_eq!(p1(input), 5); | |
| } | |
| #[test] | |
| fn test_p2() { | |
| let input = "0\n3\n0\n1\n-3"; | |
| assert_eq!(p2(input), 10); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment