Dag 1 advent of code 25 afgetikt
This commit is contained in:
6
advent_of_code/2025/1/Cargo.toml
Normal file
6
advent_of_code/2025/1/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "main"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
4168
advent_of_code/2025/1/input.txt
Normal file
4168
advent_of_code/2025/1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
113
advent_of_code/2025/1/src/main.rs
Normal file
113
advent_of_code/2025/1/src/main.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
10
advent_of_code/2025/1/test.txt
Normal file
10
advent_of_code/2025/1/test.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
L68
|
||||
L30
|
||||
R48
|
||||
L5
|
||||
R60
|
||||
L55
|
||||
L1
|
||||
L99
|
||||
R14
|
||||
L82
|
||||
Reference in New Issue
Block a user