Skip to content

Commit f44b5f7

Browse files
committed
Day 02
1 parent 22fcdc8 commit f44b5f7

File tree

3 files changed

+85
-6
lines changed

3 files changed

+85
-6
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,20 +194,24 @@ By default, `cargo time` does not write to the readme. In order to do so, append
194194

195195
<!--- advent_readme_stars table --->
196196

197+
## 2025 Results
198+
197199
| Day | Part 1 | Part 2 |
198200
| :------------------------------------------: | :----: | :----: |
199201
| [Day 1](https://adventofcode.com/2025/day/1) |||
202+
| [Day 2](https://adventofcode.com/2025/day/2) |||
200203

201204
<!--- advent_readme_stars table --->
202205

203-
## Benchmarks
204-
205206
<!--- benchmarking table --->
206207

207-
| Day | Part 1 | Part 2 |
208-
| :----------------------: | :------: | :------: |
209-
| [Day 1](./src/bin/01.rs) | `82.6µs` | `93.5µs` |
208+
## Benchmarks
209+
210+
| Day | Part 1 | Part 2 |
211+
| :----------------------: | :------: | :-------: |
212+
| [Day 1](./src/bin/01.rs) | `51.1µs` | `38.7µs` |
213+
| [Day 2](./src/bin/02.rs) | `27.1ms` | `117.0ms` |
210214

211-
**Total: 0.18ms**
215+
**Total: 144.19ms**
212216

213217
<!--- benchmarking table --->

data/examples/02.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
11-22,95-115,998-1012,1188511880-1188511890,222220-222224, 1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124

src/bin/02.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
advent_of_code::solution!(2);
2+
3+
pub fn part_one(input: &str) -> Option<u64> {
4+
let mut result = 0;
5+
for range in input.split(',') {
6+
let (first_id, last_id) = get_ids(range)?;
7+
result += (first_id..=last_id)
8+
.filter(|&id| is_invalid(id.to_string()))
9+
.sum::<u64>();
10+
}
11+
12+
Some(result)
13+
}
14+
15+
pub fn part_two(input: &str) -> Option<u64> {
16+
let mut result = 0;
17+
for range in input.split(',') {
18+
let (first_id, last_id) = get_ids(range)?;
19+
result += (first_id..=last_id)
20+
.filter(|&id| is_invalid_strict(id.to_string()))
21+
.sum::<u64>();
22+
}
23+
Some(result)
24+
}
25+
26+
fn get_ids(range: &str) -> Option<(u64, u64)> {
27+
let ids: Vec<&str> = range.trim().split('-').collect();
28+
let first = ids[0].parse::<u64>().ok()?;
29+
let last = ids[1].parse::<u64>().ok()?;
30+
Some((first, last))
31+
}
32+
33+
fn is_invalid(id: String) -> bool {
34+
let length = id.len();
35+
if !length.is_multiple_of(2) {
36+
return false;
37+
}
38+
39+
let mid = length / 2;
40+
id[0..mid] == id[mid..]
41+
}
42+
43+
fn is_invalid_strict(id: String) -> bool {
44+
let length = id.len();
45+
for i in 1..=length / 2 {
46+
if !length.is_multiple_of(i) {
47+
continue;
48+
}
49+
50+
let candidate = &id[0..i];
51+
if candidate.repeat(length / i) == id {
52+
return true;
53+
}
54+
}
55+
56+
false
57+
}
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
63+
#[test]
64+
fn test_part_one() {
65+
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
66+
assert_eq!(result, Some(1227775554));
67+
}
68+
69+
#[test]
70+
fn test_part_two() {
71+
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
72+
assert_eq!(result, Some(4174379265));
73+
}
74+
}

0 commit comments

Comments
 (0)