Skip to content

Instantly share code, notes, and snippets.

@icub3d
Created December 23, 2025 00:07
Show Gist options
  • Select an option

  • Save icub3d/93c7602a8ae96f81618434d57ac5d744 to your computer and use it in GitHub Desktop.

Select an option

Save icub3d/93c7602a8ae96f81618434d57ac5d744 to your computer and use it in GitHub Desktop.
Solution for Flip Flop 2025 Day 2
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