From 6ad1fa74d6be7c1d83de53960074a90d13351c84 Mon Sep 17 00:00:00 2001 From: Jos van Goor Date: Mon, 1 Dec 2025 22:21:40 +0100 Subject: [PATCH] Day 1 Part 1 done, working on Part 2 --- advent_of_code_2025/src/day1/part1.rs | 34 ++++++++++++++++++++-- advent_of_code_2025/src/day1/part2.rs | 42 ++++++++++++++++++++++++++- advent_of_code_2025/src/day1/test.txt | 10 +++++++ advent_of_code_2025/src/main.rs | 3 +- 4 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 advent_of_code_2025/src/day1/test.txt diff --git a/advent_of_code_2025/src/day1/part1.rs b/advent_of_code_2025/src/day1/part1.rs index 6197ad3..32f9bbe 100644 --- a/advent_of_code_2025/src/day1/part1.rs +++ b/advent_of_code_2025/src/day1/part1.rs @@ -1,4 +1,32 @@ - pub fn solve(input: &str) { - -} \ No newline at end of file + let instructions = input + .lines() + .map(|str| { + let distance = str.chars().next().map(|ch| &str[ch.len_utf8()..]).unwrap().parse::().unwrap(); + if str.starts_with('L') { -distance } else { distance } + }) + .collect::>(); + + let mut counter = 0; + let mut dial = 50; + + for instruction in instructions { + dial += instruction; + + match dial { + ..=-1 => { + dial += 100 * (dial / -100); + } + 100.. => { + dial %= 100; + } + _ => {} + } + + if dial == 0 { + counter += 1; + } + } + + println!("part 1 counter: {}", counter); +} diff --git a/advent_of_code_2025/src/day1/part2.rs b/advent_of_code_2025/src/day1/part2.rs index 6197ad3..d4a56c6 100644 --- a/advent_of_code_2025/src/day1/part2.rs +++ b/advent_of_code_2025/src/day1/part2.rs @@ -1,4 +1,44 @@ pub fn solve(input: &str) { - + let instructions = input.lines().map(|str| { + let distance = unsafe { str::from_utf8_unchecked( &str.bytes().collect::>()[1..]).parse::().unwrap() }; + if str.bytes().next().unwrap() == b'L' { + -distance + } else { + distance + } + }).collect::>(); + + let mut counter = 0; + let mut dial = 50; + + println!("instr min: {}, max: {}", instructions.iter().min().unwrap(), instructions.iter().max().unwrap()); + + for instruction in instructions { + // let old = dial; + // let new = dial + instruction; + + dial += instruction; + + match dial { + ..=-1 => { + counter += dial / -100; + dial += 100 * (dial / -100); }, + 100.. => { + counter += dial / 100; + dial %= 100; + }, + _ => {} + } + + // println!("{old} -> {new} = {dial}"); + + if dial == 0 { + // println!("incremented!"); + counter += 1; + } + } + + println!("counter: {}", counter); + } \ No newline at end of file diff --git a/advent_of_code_2025/src/day1/test.txt b/advent_of_code_2025/src/day1/test.txt new file mode 100644 index 0000000..d03fad7 --- /dev/null +++ b/advent_of_code_2025/src/day1/test.txt @@ -0,0 +1,10 @@ +L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82 \ No newline at end of file diff --git a/advent_of_code_2025/src/main.rs b/advent_of_code_2025/src/main.rs index 5694437..5421055 100644 --- a/advent_of_code_2025/src/main.rs +++ b/advent_of_code_2025/src/main.rs @@ -1,4 +1,5 @@ use paste::paste; mod utility; -solve_day!{1} +// solve_day!{1} +test_day!{1}