Created
December 23, 2025 00:06
-
-
Save icub3d/e6f8c5ebcbb4796413d1610cfec105b6 to your computer and use it in GitHub Desktop.
Solution for Flip Flop 2025 Day 2
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; | |
| use cached::proc_macro::cached; | |
| use itertools::Itertools; | |
| const INPUT: &str = include_str!("inputs/day02.txt"); | |
| fn parse(input: &str) -> impl Iterator<Item = char> { | |
| input.trim().chars() | |
| } | |
| fn p1(input: &str) -> isize { | |
| parse(input) | |
| .scan(0isize, |acc, c| { | |
| *acc += match c { | |
| '^' => 1, | |
| _ => -1, | |
| }; | |
| Some(*acc) | |
| }) | |
| .max() | |
| .unwrap() | |
| } | |
| fn p2(input: &str) -> isize { | |
| parse(input) | |
| .scan((0isize, true, 0), |(acc, up, count), c| { | |
| // Update count. | |
| *count = match (c, *up) { | |
| ('^', true) => *count + 1, | |
| ('v', false) => *count + 1, | |
| _ => { | |
| *up = !*up; | |
| 1 | |
| } | |
| }; | |
| *acc += match c { | |
| '^' => *count, | |
| _ => -*count, | |
| }; | |
| Some(*acc) | |
| }) | |
| .max() | |
| .unwrap() | |
| } | |
| #[cached] | |
| fn fib(n: isize) -> isize { | |
| if n == 0 { | |
| 0 | |
| } else if n == 1 { | |
| 1 | |
| } else { | |
| fib(n - 1) + fib(n - 2) | |
| } | |
| } | |
| fn p3(input: &str) -> isize { | |
| parse(input) | |
| .chunk_by(|c| *c) | |
| .into_iter() | |
| .map(|(k, g)| (k, fib(g.count() as isize))) | |
| .scan(0isize, |acc, (k, v)| { | |
| *acc += match k { | |
| '^' => v, | |
| _ => -v, | |
| }; | |
| Some(*acc) | |
| }) | |
| .max() | |
| .unwrap() | |
| } | |
| 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); | |
| let now = Instant::now(); | |
| let solution = p3(INPUT); | |
| println!("p3 {:?} {}", now.elapsed(), solution); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment