mirror of
https://github.com/kristoferssolo/Advent-of-Code.git
synced 2025-10-21 18:00:35 +00:00
day 13 part 2
This commit is contained in:
parent
380a2060f8
commit
7f99a0ed41
@ -1,19 +1,150 @@
|
|||||||
|
use std::iter::from_fn;
|
||||||
|
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
enum Fold {
|
||||||
|
Horizontal(usize),
|
||||||
|
Vertical(usize),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn detect_fold(input: &str) -> Option<Fold> {
|
||||||
|
detect_vertical_fold(input).or(detect_horizontal_fold(input))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn detect_vertical_fold(input: &str) -> Option<Fold> {
|
||||||
|
let mut columns_iter_collection = input.lines().map(|line| line.chars()).collect::<Vec<_>>();
|
||||||
|
let columns = from_fn(move || {
|
||||||
|
let mut items = Vec::new();
|
||||||
|
for iter in &mut columns_iter_collection {
|
||||||
|
match iter.next() {
|
||||||
|
Some(item) => items.push(item),
|
||||||
|
None => return None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(items)
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
let result = columns
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.tuple_windows()
|
||||||
|
.filter(|((_, line_a), (_, line_b))| {
|
||||||
|
line_a == line_b
|
||||||
|
|| line_a
|
||||||
|
.iter()
|
||||||
|
.zip(line_b.iter())
|
||||||
|
.filter(|(a, b)| a != b)
|
||||||
|
.count()
|
||||||
|
<= 1
|
||||||
|
})
|
||||||
|
.find_map(|((index_a, _), (index_b, _))| {
|
||||||
|
let lines_a = (&columns[0..=index_a]).iter().rev();
|
||||||
|
let lines_b = (&columns[index_b..]).iter();
|
||||||
|
(lines_a
|
||||||
|
.flatten()
|
||||||
|
.zip(lines_b.flatten())
|
||||||
|
.filter(|(a, b)| a != b)
|
||||||
|
.count()
|
||||||
|
== 1)
|
||||||
|
.then_some(index_a + 1)
|
||||||
|
});
|
||||||
|
result.map(|num| Fold::Vertical(num))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn detect_horizontal_fold(input: &str) -> Option<Fold> {
|
||||||
|
let lines = input.lines().collect::<Vec<_>>();
|
||||||
|
let result = input
|
||||||
|
.lines()
|
||||||
|
.enumerate()
|
||||||
|
.tuple_windows()
|
||||||
|
.filter(|((_, line_a), (_, line_b))| {
|
||||||
|
line_a == line_b
|
||||||
|
|| line_a
|
||||||
|
.chars()
|
||||||
|
.zip(line_b.chars())
|
||||||
|
.filter(|(a, b)| a != b)
|
||||||
|
.count()
|
||||||
|
<= 1
|
||||||
|
})
|
||||||
|
.find_map(|((index_a, _), (index_b, _))| {
|
||||||
|
let lines_a = (&lines[0..=index_a]).iter().map(|line| line.chars()).rev();
|
||||||
|
let lines_b = (&lines[index_b..]).iter().map(|line| line.chars());
|
||||||
|
(lines_a
|
||||||
|
.flatten()
|
||||||
|
.zip(lines_b.flatten())
|
||||||
|
.filter(|(a, b)| a != b)
|
||||||
|
.count()
|
||||||
|
== 1)
|
||||||
|
.then_some(index_a + 1)
|
||||||
|
});
|
||||||
|
result.map(|num| Fold::Horizontal(num))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn process(input: &str) -> Result<usize> {
|
pub fn process(input: &str) -> Result<usize> {
|
||||||
todo!("day xx - part 2");
|
let (horizontal, vertical) =
|
||||||
Ok(0)
|
input
|
||||||
|
.split("\n\n")
|
||||||
|
.flat_map(detect_fold)
|
||||||
|
.fold((0, 0), |mut acc, item| match item {
|
||||||
|
Fold::Horizontal(num) => {
|
||||||
|
acc.0 += 100 * num;
|
||||||
|
acc
|
||||||
|
}
|
||||||
|
Fold::Vertical(num) => {
|
||||||
|
acc.1 += num;
|
||||||
|
acc
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Ok(horizontal + vertical)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_process() -> Result<()> {
|
fn test_process() -> Result<()> {
|
||||||
let input = "";
|
let input = "#.##..##.
|
||||||
todo!("haven't built test yet");
|
..#.##.#.
|
||||||
assert_eq!(0, process(input)?);
|
##......#
|
||||||
|
##......#
|
||||||
|
..#.##.#.
|
||||||
|
..##..##.
|
||||||
|
#.#.##.#.
|
||||||
|
|
||||||
|
#...##..#
|
||||||
|
#....#..#
|
||||||
|
..##..###
|
||||||
|
#####.##.
|
||||||
|
#####.##.
|
||||||
|
..##..###
|
||||||
|
#....#..#";
|
||||||
|
assert_eq!(400, process(input)?);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_horizontal() -> Result<()> {
|
||||||
|
let input = "#...##..#
|
||||||
|
#....#..#
|
||||||
|
..##..###
|
||||||
|
#####.##.
|
||||||
|
#####.##.
|
||||||
|
..##..###
|
||||||
|
#....#..#";
|
||||||
|
assert_eq!(Some(Fold::Horizontal(1)), detect_fold(input));
|
||||||
|
|
||||||
|
let input = "#.##..##.
|
||||||
|
..#.##.#.
|
||||||
|
##......#
|
||||||
|
##......#
|
||||||
|
..#.##.#.
|
||||||
|
..##..##.
|
||||||
|
#.#.##.#.";
|
||||||
|
assert_eq!(Some(Fold::Horizontal(3)), detect_fold(input));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user