Skip to content

Commit 5eb50f3

Browse files
committed
Year 2025 Day 2
1 parent fed604d commit 5eb50f3

File tree

7 files changed

+71
-4
lines changed

7 files changed

+71
-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) | 11000 |
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 mut invalid_p1 = BTreeSet::new();
9+
let mut invalid_p2 = BTreeSet::new();
10+
11+
for digits in 2..=10 {
12+
for size in 1..=digits / 2 {
13+
if digits % size != 0 {
14+
continue;
15+
}
16+
17+
let repeat = digits / size;
18+
let start = 10_u64.pow(size - 1);
19+
let end = start * 10;
20+
21+
if repeat == 2 {
22+
for n in start..end {
23+
invalid_p1.insert(end * n + n);
24+
invalid_p2.insert(end * n + n);
25+
}
26+
} else {
27+
for n in start..end {
28+
invalid_p2.insert((0..repeat).fold(0, |acc, _| end * acc + n));
29+
}
30+
}
31+
}
32+
}
33+
34+
let mut part_one = 0;
35+
let mut part_two = 0;
36+
37+
for [from, to] in input.iter_unsigned::<u64>().chunk::<2>() {
38+
part_one += invalid_p1.range(from..=to).sum::<u64>();
39+
part_two += invalid_p2.range(from..=to).sum::<u64>();
40+
}
41+
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+
}

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)