Skip to content

Commit a2b514a

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

File tree

7 files changed

+73
-4
lines changed

7 files changed

+73
-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) | 2 |
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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//! # Gift Shop
2+
use crate::util::iter::*;
3+
use crate::util::parse::*;
4+
5+
type Pair = [usize; 2];
6+
7+
const FIRST: [Pair; 5] = [[2, 1], [4, 2], [6, 3], [8, 4], [10, 5]];
8+
const SECOND: [Pair; 6] = [[3, 1], [5, 1], [6, 2], [7, 1], [9, 3], [10, 2]];
9+
const THIRD: [Pair; 2] = [[6, 1], [10, 1]];
10+
11+
pub fn parse(input: &str) -> Pair {
12+
let ranges: Vec<_> = input.iter_unsigned::<usize>().chunk::<2>().collect();
13+
let first = sum(&FIRST, &ranges);
14+
let second = sum(&SECOND, &ranges);
15+
let third = sum(&THIRD, &ranges);
16+
[first, first + second - third]
17+
}
18+
19+
pub fn part1(input: &Pair) -> usize {
20+
input[0]
21+
}
22+
23+
pub fn part2(input: &Pair) -> usize {
24+
input[1]
25+
}
26+
27+
fn sum(ranges: &[Pair], ids: &[Pair]) -> usize {
28+
let mut result = 0;
29+
30+
for &[digits, size] in ranges {
31+
let repeat = digits / size;
32+
let power = 10_usize.pow(size as u32);
33+
let factor = (0..repeat).fold(0, |acc, _| acc * power + 1);
34+
let start = factor * (power / 10);
35+
let end = factor * (power - 1);
36+
37+
for &[from, to] in ids {
38+
let lower = from.next_multiple_of(factor).max(start);
39+
let upper = to.min(end);
40+
41+
if lower <= upper {
42+
let n = (upper - lower) / factor;
43+
let triangular = n * (n + 1) / 2;
44+
result += lower * (n + 1) + factor * triangular;
45+
}
46+
}
47+
}
48+
49+
result
50+
}

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)