From 1dce35c9580c009d8fd7bd04902cf4a3388ca721 Mon Sep 17 00:00:00 2001 From: Riari <3583774+Riari@users.noreply.github.com> Date: Fri, 22 Dec 2023 18:37:41 +0000 Subject: [PATCH] Work on day 22 part 1 --- data/examples/22.txt | 7 ++++ src/bin/22.rs | 83 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 data/examples/22.txt create mode 100644 src/bin/22.rs diff --git a/data/examples/22.txt b/data/examples/22.txt new file mode 100644 index 0000000..43a7fc5 --- /dev/null +++ b/data/examples/22.txt @@ -0,0 +1,7 @@ +1,0,1~1,2,1 +0,0,2~2,0,2 +0,2,3~2,2,3 +0,0,4~0,2,4 +2,0,5~2,2,5 +0,1,6~2,1,6 +1,1,8~1,1,9 diff --git a/src/bin/22.rs b/src/bin/22.rs new file mode 100644 index 0000000..80d0f66 --- /dev/null +++ b/src/bin/22.rs @@ -0,0 +1,83 @@ +use std::cmp::max; +use itertools::Itertools; + +advent_of_code::solution!(22); + +type XY = (usize, usize); +type XYZ = (usize, usize, usize); + +struct Brick { + start: XYZ, + end: XYZ, +} + +fn parse(input: &str) -> Vec { + let mut bricks = vec![]; + for line in input.lines() { + let (a, b) = line.split_once("~").unwrap(); + let start: XYZ = a.split(",").map(|s| s.parse().unwrap()).collect_tuple().unwrap(); + let end: XYZ = b.split(",").map(|s| s.parse().unwrap()).collect_tuple().unwrap(); + bricks.push(Brick { start, end }); + } + + bricks.sort_by(|a, b| a.start.2.cmp(&b.start.2)); + bricks +} + +pub fn part_one(input: &str) -> Option { + let mut bricks = parse(input); + let highest_brick = bricks.last().unwrap(); + let max_z = max(highest_brick.start.2, highest_brick.end.2); + + // This should probably be a vec of hashset of all the positions, not just start/end positions + let mut layers: Vec> = vec![vec![]; max_z]; + for i in 0..bricks.len() { + for z in bricks[i].start.2..=bricks[i].end.2 { + layers[z - 1].push((bricks[i].start.0, bricks[i].start.1)); + } + } + + let mut settled = false; + while !settled { + settled = true; + for brick in bricks.iter_mut() { + if brick.start.2 == 1 || brick.end.2 == 1 { + continue; + } + + // Update this: + // 1) iterate over the positions in the layer + // 2) for each position, check each component to see if it's contained by the brick's start/end range for that component + // 3) if it is, skip this brick + // 4) otherwise, update the brick position, add the positions it occupies to the new layer, and remove its positions from the layer it was in + if !layers[brick.start.2 - 2].contains(&(brick.start.0, brick.start.1)) { + brick.start.2 -= 1; + brick.end.2 -= 1; + settled = false; + } + } + } + + None +} + +pub fn part_two(input: &str) -> Option { + None +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part_one() { + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, None); + } + + #[test] + fn test_part_two() { + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, None); + } +}