Skip to content

Commit ccbeee9

Browse files
committed
Year 2025 Day 2
1 parent fed604d commit ccbeee9

File tree

7 files changed

+79
-4
lines changed

7 files changed

+79
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
7878
| Day | Problem | Solution | Benchmark (μs) |
7979
| --- | --- | --- | --: |
8080
| 1 | [Secret Entrance](https://adventofcode.com/2025/day/1) | [Source](src/year2025/day01.rs) | 25 |
81+
| 2 | [Gift Shop](https://adventofcode.com/2025/day/2) | [Source](src/year2025/day02.rs) | 1016 |
8182

8283
## 2024
8384

benches/benchmark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,5 @@ benchmark!(year2024
9595
);
9696

9797
benchmark!(year2025
98-
day01
98+
day01, day02
9999
);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,5 @@ library!(year2024 "Locate the Chief Historian in time for the big Christmas slei
7474
);
7575

7676
library!(year2025 "Finish the North Pole decorations in time for Christmas."
77-
day01
77+
day01, day02
7878
);

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,5 @@ run!(year2024
141141
);
142142

143143
run!(year2025
144-
day01
144+
day01, day02
145145
);

src/year2025/day02.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use crate::util::iter::*;
2+
use crate::util::parse::*;
3+
4+
type Input = (u64, u64);
5+
6+
pub fn parse(input: &str) -> Input {
7+
let ranges: Vec<_> = input.iter_unsigned::<u64>().chunk::<2>().collect();
8+
let mut invalid = Vec::with_capacity(100_000);
9+
10+
for digits in (2..=10).step_by(2) {
11+
let size = digits / 2;
12+
let start = 10_u64.pow(size - 1);
13+
let end = start * 10;
14+
15+
for n in start..end {
16+
invalid.push(end * n + n);
17+
}
18+
}
19+
20+
let part_one = sum(&ranges, &invalid);
21+
22+
for digits in 2_u32..=10 {
23+
for size in 1..=digits / 2 {
24+
let repeat = digits / size;
25+
if !digits.is_multiple_of(size) || repeat == 2 {
26+
continue;
27+
}
28+
29+
let start = 10_u64.pow(size - 1);
30+
let end = start * 10;
31+
32+
for n in start..end {
33+
invalid.push((0..repeat).fold(0, |acc, _| end * acc + n));
34+
}
35+
}
36+
}
37+
38+
invalid.sort_unstable();
39+
invalid.dedup();
40+
41+
let part_two = sum(&ranges, &invalid);
42+
(part_one, part_two)
43+
}
44+
45+
pub fn part1(input: &Input) -> u64 {
46+
input.0
47+
}
48+
49+
pub fn part2(input: &Input) -> u64 {
50+
input.1
51+
}
52+
53+
fn sum(ranges: &[[u64; 2]], invalid: &[u64]) -> u64 {
54+
let index = |n: u64| invalid.binary_search(&n).unwrap_or_else(|i| i);
55+
ranges.iter().flat_map(|&[from, to]| invalid[index(from)..index(to + 1)].iter()).sum()
56+
}

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,5 @@ test!(year2024
8787
);
8888

8989
test!(year2025
90-
day01
90+
day01, day02
9191
);

tests/year2025/day02.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use aoc::year2025::day02::*;
2+
3+
const EXAMPLE: &str = "
4+
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,
5+
1698522-1698528,446443-446449,38593856-38593862,565653-565659,
6+
824824821-824824827,2121212118-2121212124";
7+
8+
#[test]
9+
fn part1_test() {
10+
let input = parse(EXAMPLE);
11+
assert_eq!(part1(&input), 1227775554);
12+
}
13+
14+
#[test]
15+
fn part2_test() {
16+
let input = parse(EXAMPLE);
17+
assert_eq!(part2(&input), 4174379265);
18+
}

0 commit comments

Comments
 (0)