summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2023-12-14 14:04:44 +0200
committerJustin Wernick <justin@worthe-it.co.za>2023-12-14 14:04:44 +0200
commit107850cb7f8b9b5a047c32279ec2f7673bf054b0 (patch)
tree0e46222abcc4e27327994bb345f0bd7230933ff3
parente37434b49728141f92a3002fdb0a2f07108db28f (diff)
Day 13 part 2
-rw-r--r--2023/src/bin/day_13.rs33
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);
}