diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2023-12-14 14:04:44 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2023-12-14 14:04:44 +0200 |
commit | 107850cb7f8b9b5a047c32279ec2f7673bf054b0 (patch) | |
tree | 0e46222abcc4e27327994bb345f0bd7230933ff3 /2023/src | |
parent | e37434b49728141f92a3002fdb0a2f07108db28f (diff) |
Day 13 part 2
Diffstat (limited to '2023/src')
-rw-r--r-- | 2023/src/bin/day_13.rs | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/2023/src/bin/day_13.rs b/2023/src/bin/day_13.rs index b2ef8aa..ad97602 100644 --- a/2023/src/bin/day_13.rs +++ b/2023/src/bin/day_13.rs @@ -10,7 +10,8 @@ use std::fs; fn main() -> Result<(), Box<dyn std::error::Error>> { let input = fs::read_to_string("inputs/day_13.txt")?; let parsed = ManyMaps::parser(&input).unwrap().1; - dbg!(&parsed.reflection_score_sum()); + dbg!(&parsed.reflection_score_sum(0)); + dbg!(&parsed.reflection_score_sum(1)); Ok(()) } @@ -32,8 +33,11 @@ impl ManyMaps { )(input) } - fn reflection_score_sum(&self) -> usize { - self.0.iter().map(|m| m.reflection_score()).sum() + fn reflection_score_sum(&self, expected_smudge_count: usize) -> usize { + self.0 + .iter() + .map(|m| m.reflection_score(expected_smudge_count)) + .sum() } } @@ -52,23 +56,24 @@ impl Map { })(input) } - fn reflection_score(&self) -> usize { - reflection_i(&self.cols) - .or_else(|| reflection_i(&self.rows).map(|y| y * 100)) + fn reflection_score(&self, expected_smudge_count: usize) -> usize { + reflection_i(&self.cols, expected_smudge_count) + .or_else(|| reflection_i(&self.rows, expected_smudge_count).map(|y| y * 100)) .expect("No reflection!") } } -fn reflection_i(rows: &[String]) -> Option<usize> { +fn reflection_i(rows: &[String], expected_smudge_count: usize) -> Option<usize> { for i in 1..rows.len() { - let mut is_reflection = true; - let mut d = 1; - while is_reflection && d <= i && d + i - 1 < rows.len() { - if rows[i - d] != rows[i + d - 1] { - is_reflection = false; - } - d += 1; + let mut smudge_count = 0; + for d in 1..=(i.min(rows.len() - i)) { + smudge_count += rows[i - d] + .chars() + .zip(rows[i + d - 1].chars()) + .filter(|(a, b)| a != b) + .count(); } + let is_reflection = smudge_count == expected_smudge_count; if is_reflection { return Some(i); } |