Dag 1 advent of code 25 afgetikt

This commit is contained in:
Philippe Zwietering
2026-01-05 16:48:32 +01:00
parent afe7131f31
commit 4ad2ec5240
4 changed files with 4297 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
[package]
name = "main"
version = "0.1.0"
edition = "2024"
[dependencies]

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,113 @@
#[derive(Debug)]
enum Dir {
Left,
Right,
}
fn parse_input(input: &str) -> Vec<(Dir, i32)> {
input
.lines()
.into_iter()
.map(|line| {
let dir = match line.chars().next().unwrap() {
'L' => Dir::Left,
'R' => Dir::Right,
_ => unreachable!(),
};
(dir, line[1..].parse::<i32>().unwrap())
})
.collect()
}
fn solve_1(input: &str) -> i32 {
let rots = parse_input(input);
rots.iter()
.fold((0, 50), |(zeroes, dial), x| {
let new_dial = (dial
+ match x.0 {
Dir::Left => -1,
Dir::Right => 1,
} * x.1)
% 100;
(if new_dial == 0 { zeroes + 1 } else { zeroes }, new_dial)
})
.0
}
fn solve_2(input: &str) -> i32 {
let rots = parse_input(input);
rots.iter()
.fold((0, 50), |(zeroes, dial), x| {
let full_rots = x.1 / 100;
let remainder = x.1 % 100;
let new_dial = match x.0 {
Dir::Left => {
if remainder <= dial {
dial - remainder
} else {
100 - (remainder - dial)
}
}
Dir::Right => {
if remainder + dial >= 100 {
remainder + dial - 100
} else {
remainder + dial
}
}
};
let carry = match x.0 {
Dir::Left => {
if remainder >= dial && dial != 0 {
1
} else {
0
}
}
Dir::Right => {
if remainder + dial >= 100 {
1
} else {
0
}
}
};
(zeroes + full_rots + carry, new_dial)
})
.0
}
fn main() {
println!("Hello, this is Patrick!");
let input = include_str!("../input.txt");
let result_1 = solve_1(input);
println!("Number of zeroes: {}", result_1);
let result_2 = solve_2(input);
println!("Number of zero clicks: {}", result_2);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1() {
let test_input = include_str!("../test.txt");
assert_eq!(solve_1(test_input), 3);
}
#[test]
fn test_2() {
let test_input = include_str!("../test.txt");
assert_eq!(solve_2(test_input), 6);
}
}

View File

@@ -0,0 +1,10 @@
L68
L30
R48
L5
R60
L55
L1
L99
R14
L82