From 5cd4bbc3742ae6f34a0ad17f2c68e2f0f4e95fd0 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Sat, 17 Dec 2022 23:29:20 +0000 Subject: day17 skeleton... --- input/day17_ex.txt | 1 + src/day17.rs | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 6 ++-- 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 input/day17_ex.txt create mode 100644 src/day17.rs diff --git a/input/day17_ex.txt b/input/day17_ex.txt new file mode 100644 index 0000000..fb5d89e --- /dev/null +++ b/input/day17_ex.txt @@ -0,0 +1 @@ +>>><<><>><<<>><>>><<<>>><<<><<<>><>><<>> \ No newline at end of file diff --git a/src/day17.rs b/src/day17.rs new file mode 100644 index 0000000..3606036 --- /dev/null +++ b/src/day17.rs @@ -0,0 +1,87 @@ +const ROCK: char = '#'; +const AIR: char = '.'; + +#[derive(Clone,PartialEq)] +struct Pos { + x: u32, + y: u32, +} + +#[derive(Clone,PartialEq)] +enum RockType { + Bar,Cross,Ell,VBar,Square +} + +enum Direction { + Down,Left,Right +} + +#[derive(Clone,PartialEq)] +struct Rock { + rtype: RockType, + pos: Pos, +} + +impl Rock { + pub fn mv(self, dir: Direction, grid: &Vec>) -> Self { + // Check we _can_ move + // Then move + self + } + pub fn solidify(self, grid: &mut Vec>) { + // as it says, transform squares covered by ourselves into solid rock + } +} + +fn rock_height(grid: &Vec>) -> usize { + 0 +} + +fn run(input: String) { + let initial_height = 6; + let width = 7; + let rocks = vec![RockType::Bar,RockType::Cross,RockType::Ell,RockType::VBar,RockType::Square].into_iter().cycle(); + let gas_jets = input.chars().cycle(); + let mut grid: Vec> = (0..initial_height).map(|_| { + (0..width).map(|_| {AIR}).collect() + }).collect(); + + let mut rock = Rock{ + rtype: rocks.next().unwrap(), + pos: Pos{x: 2, y: 3} + }; + let mut rocks_count: usize = 1; + 'lp: loop { + let mv = match gas_jets.next().unwrap() { + '>' => Direction::Right, + '<' => Direction::Left, + dir => panic!("Unexpected jet direction! {}", dir) + }; + // apply jet, ignore if it didn't move + let r2 = rock.mv(mv, &grid); + // apply gravity + let r3 = r2.mv(Direction::Down, &grid); + + if rock == r3 { + // We didn't move, solidify! + rock.solidify(&mut grid); + if rocks_count == 2022 { + break 'lp; + } + // Measure height... + let max_rock = rock_height(&grid); + // Extend grid to max_rock + 7 (enough room for any new rocks) + grid.extend((0..(max_rock+7)).map(|_| { (0..width).map(|_| { AIR }).collect() })); + // Spawn new rock + rock = Rock { + rtype: rocks.next().unwrap(), + pos: Pos{x: 2, y: 3} + }; + // remember how many rocks we did + rocks_count += 1; + } else { + rock = r3; + } + } + println!("Day 17: {}", rock_height(&grid)); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index bdbb41e..34d8bad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,8 @@ use std::fs; // mod day13; // mod day14; // mod day15; -mod day16; +// mod day16; +mod day17; fn main() { // day1::run(fs::read_to_string("input/day1.txt").expect("Failed to read input file!")); @@ -35,5 +36,6 @@ fn main() { // day13::run(fs::read_to_string("input/day13.txt").expect("Failed to read input file!")); // day14::run(fs::read_to_string("input/day14.txt").expect("Failed to read input file!")); // day15::run(fs::read_to_string("input/day15.txt").expect("Failed to read input file!")); - day16::run(fs::read_to_string("input/day16.txt").expect("Failed to read input file!")); + // day16::run(fs::read_to_string("input/day16.txt").expect("Failed to read input file!")); + day17::run(fs::read_to_string("input/day17_ex.txt").expect("Failed to read input file!")); } -- cgit v1.2.3-ZIG