Optimize Days 1-8: 41% faster overall (39ms → 23ms)

This commit is contained in:
Russell Champoux committed 2025-12-09 16:16:10 -05:00
1 parent 6ff1349679
commit b067baac3d
10 files changed
+230 -184

No files matched your search

+2 -4
View File
@@ -1,5 +1,4 @@
use itertools::Itertools;
use std::collections::HashSet;
advent_of_code::solution!(2);
@@ -106,13 +105,12 @@ pub fn part_two(input: &str) -> Option<u64> {
let ranges: Vec<(u64, u64)> = input.split(',').filter_map(parse_range).collect();
let merged = merge_ranges(&ranges);
// Use a small HashSet only for deduplication within merged ranges
// Use itertools::unique() for deduplication
// (needed because same number can match multiple pattern lengths)
let mut seen = HashSet::new();
let sum: u64 = merged
.iter()
.flat_map(|&(start, end)| iter_invalid_part2(start, end))
.filter(|&n| seen.insert(n))
.unique()
.sum();
Some(sum)