Skip to content

Commit cea5453

Browse files
committed
2025 02.2 | most basic optimisation pass
6s -> 4s, that's 33% faster!
1 parent 3ab24d3 commit cea5453

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/aoc/y2025/day02.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,18 @@ pub fn part_2(data: crate::DataIn) -> crate::AoCResult<String> {
6262
let len = numstr.len();
6363
let max_len = len / 2;
6464

65-
// This takes 6 seconds but whatever 😭
66-
(1..=max_len).any(|size| {
67-
let substr = &numstr[0..size];
68-
numstr
69-
.chars()
70-
.chunks(size)
71-
.into_iter()
72-
.all(|window| window.collect::<String>() == substr)
73-
})
65+
// This takes 4 seconds but whatever 😭
66+
(1..=max_len)
67+
// Filter out any number that can't make valid windows
68+
.filter(|size| len % size == 0)
69+
.any(|size| {
70+
let substr = &numstr[0..size];
71+
numstr
72+
.chars()
73+
.chunks(size)
74+
.into_iter()
75+
.all(|window| window.collect::<String>() == substr)
76+
})
7477
})
7578
})
7679
.inspect(|serial| log::debug!("Found invalid serial number {serial}"))

0 commit comments

Comments
 (0)