Skip to content

Commit 4e89c02

Browse files
committed
Year 2025 Day 2
1 parent fed604d commit 4e89c02

File tree

7 files changed

+66
-4
lines changed

7 files changed

+66
-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) | 40000 |
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use crate::util::iter::*;
2+
use crate::util::parse::*;
3+
use std::io::Write as _;
4+
5+
type Input = (u64, u64);
6+
7+
pub fn parse(input: &str) -> Input {
8+
let mut id = Vec::new();
9+
let mut part_one = 0;
10+
let mut part_two = 0;
11+
12+
for [from, to] in input.iter_unsigned::<u64>().chunk::<2>() {
13+
for number in from..=to {
14+
id.clear();
15+
let _unused = write!(&mut id, "{number}");
16+
let digits = id.len();
17+
18+
let half = digits / 2;
19+
if id[0..half] == id[half..] {
20+
part_one += number;
21+
part_two += number;
22+
continue;
23+
}
24+
25+
for size in 1..=digits / 2 {
26+
if digits % size == 0 {
27+
let first = &id[0..size];
28+
if (size..digits).step_by(size).all(|i| &id[i..i + size] == first) {
29+
part_two += number;
30+
break;
31+
}
32+
}
33+
}
34+
}
35+
}
36+
37+
(part_one, part_two)
38+
}
39+
40+
pub fn part1(input: &Input) -> u64 {
41+
input.0
42+
}
43+
44+
pub fn part2(input: &Input) -> u64 {
45+
input.1
46+
}

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)