Skip to content

Commit 40dae32

Browse files
committed
[2025] Day 2 first solution
1 parent aa446e7 commit 40dae32

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Thank you to [Eric Wastl](http://was.tl) for running this incredible yearly even
66
## [2025](https://adventofcode.com/2025) [![aoc_2025](https://github.com/connorslade/advent-of-code/actions/workflows/aoc_2025.yml/badge.svg)](https://github.com/connorslade/advent-of-code/actions/workflows/aoc_2025.yml)
77

88
- [Day 01: Secret Entrance](aoc_2025/src/day_01.rs)
9+
- [Day 02: Gift Shop](aoc_2025/src/day_02.rs)
910
<!-- MARKER -->
1011

1112
## [2024](https://adventofcode.com/2024) [![aoc_2024](https://github.com/connorslade/advent-of-code/actions/workflows/aoc_2024.yml/badge.svg)](https://github.com/connorslade/advent-of-code/actions/workflows/aoc_2024.yml)

aoc_2025/src/day_02.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use common::{Answer, solution};
2+
3+
solution!("Gift Shop", 2);
4+
5+
fn part_a(input: &str) -> Answer {
6+
let mut count = 0;
7+
8+
for range in input.trim().split(',') {
9+
let (start, end) = range.split_once('-').unwrap();
10+
let start = start.parse::<u64>().unwrap();
11+
let end = end.parse::<u64>().unwrap();
12+
13+
for id in start..=end {
14+
count += is_invalid(id) as u64 * id;
15+
}
16+
}
17+
18+
count.into()
19+
}
20+
21+
fn part_b(input: &str) -> Answer {
22+
let mut count = 0;
23+
24+
for range in input.trim().split(',') {
25+
let (start, end) = range.split_once('-').unwrap();
26+
let start = start.parse::<u64>().unwrap();
27+
let end = end.parse::<u64>().unwrap();
28+
29+
for id in start..=end {
30+
count += is_invalid2(id) as u64 * id;
31+
}
32+
}
33+
34+
count.into()
35+
}
36+
37+
fn is_invalid(id: u64) -> bool {
38+
let digits = (id.ilog10() + 1) / 2;
39+
let mask = 10_u64.pow(digits);
40+
id % mask == id / mask
41+
}
42+
43+
fn is_invalid2(id: u64) -> bool {
44+
let digits = id.ilog10() + 1;
45+
46+
'outer: for size in 1..=(digits / 2) {
47+
let mask = 10_u64.pow(size);
48+
let repeated = id % mask;
49+
if (digits / size) * size != digits || repeated == 0 || repeated.ilog10() + 1 != size {
50+
continue;
51+
}
52+
53+
let mut id = id;
54+
for _ in 0..(digits / size) {
55+
if id % mask != repeated {
56+
continue 'outer;
57+
}
58+
59+
id /= mask;
60+
}
61+
62+
return true;
63+
}
64+
65+
false
66+
}
67+
68+
#[cfg(test)]
69+
mod test {
70+
use indoc::indoc;
71+
72+
const CASE: &str = indoc! {"
73+
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124
74+
"};
75+
76+
#[test]
77+
fn part_a() {
78+
assert_eq!(super::part_a(CASE), 1227775554.into());
79+
}
80+
81+
#[test]
82+
fn part_b() {
83+
assert_eq!(super::part_b(CASE), 4174379265_u64.into());
84+
}
85+
}

aoc_2025/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use common::Solution;
22

33
mod day_01;
4+
mod day_02;
45
// [import_marker]
56

67
pub const SOLUTIONS: &[Solution] = &[
78
day_01::SOLUTION,
9+
day_02::SOLUTION,
810
// [list_marker]
911
];

0 commit comments

Comments
 (0)