Skip to content

Commit d191db7

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

File tree

7 files changed

+69
-4
lines changed

7 files changed

+69
-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) | 5090 |
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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use crate::util::iter::*;
2+
use crate::util::parse::*;
3+
use std::collections::BTreeSet;
4+
5+
type Input = (u64, u64);
6+
7+
pub fn parse(input: &str) -> Input {
8+
let ranges: Vec<_> = input.iter_unsigned::<u64>().chunk::<2>().collect();
9+
let mut invalid = BTreeSet::new();
10+
11+
for digits in (2..=10).step_by(2) {
12+
let size = digits / 2;
13+
let start = 10_u64.pow(size - 1);
14+
let end = start * 10;
15+
16+
for n in start..end {
17+
invalid.insert(end * n + n);
18+
}
19+
}
20+
21+
let part_one = ranges.iter().map(|&[from, to]| invalid.range(from..=to).sum::<u64>()).sum();
22+
23+
for digits in 2_u32..=10 {
24+
for size in 1..=digits / 2 {
25+
if !digits.is_multiple_of(size) || digits / size == 2 {
26+
continue;
27+
}
28+
29+
let repeat = digits / size;
30+
let start = 10_u64.pow(size - 1);
31+
let end = start * 10;
32+
33+
for n in start..end {
34+
invalid.insert((0..repeat).fold(0, |acc, _| end * acc + n));
35+
}
36+
}
37+
}
38+
39+
let part_two = ranges.iter().map(|&[from, to]| invalid.range(from..=to).sum::<u64>()).sum();
40+
(part_one, part_two)
41+
}
42+
43+
pub fn part1(input: &Input) -> u64 {
44+
input.0
45+
}
46+
47+
pub fn part2(input: &Input) -> u64 {
48+
input.1
49+
}

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

0 commit comments

Comments
 (0)