summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/day17.rs87
-rw-r--r--src/main.rs6
2 files changed, 91 insertions, 2 deletions
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<Vec<char>>) -> Self {
+ // Check we _can_ move
+ // Then move
+ self
+ }
+ pub fn solidify(self, grid: &mut Vec<Vec<char>>) {
+ // as it says, transform squares covered by ourselves into solid rock
+ }
+}
+
+fn rock_height(grid: &Vec<Vec<char>>) -> 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<Vec<char>> = (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!"));
}