Skip to content

Commit

Permalink
Work on day 22 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Riari committed Dec 22, 2023
1 parent b39eb3a commit 1dce35c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
7 changes: 7 additions & 0 deletions data/examples/22.txt
Original file line number Diff line number Diff line change
@@ -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
83 changes: 83 additions & 0 deletions src/bin/22.rs
Original file line number Diff line number Diff line change
@@ -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<Brick> {
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<u32> {
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<XY>> = 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<u32> {
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);
}
}

0 comments on commit 1dce35c

Please sign in to comment.