2022 day 22 part 1

This commit is contained in:
Maciej Jur 2022-12-22 08:10:36 +01:00
parent 77fb4952f9
commit fe91698a1f
3 changed files with 255 additions and 14 deletions

202
2022/rust/inputs/day22.txt Normal file

File diff suppressed because one or more lines are too long

View file

@ -23,5 +23,6 @@ fn main() {
// solutions::day18::run(); // solutions::day18::run();
// solutions::day19::run(); // solutions::day19::run();
// solutions::day20::run(); // solutions::day20::run();
solutions::day21::run(); // solutions::day21::run();
solutions::day22::run();
} }

View file

@ -12,10 +12,12 @@ pub fn run() -> () {
} }
#[derive(Copy, Clone)]
enum Tile { enum Tile {
Empty, Wall Empty, Wall
} }
#[derive(Copy, Clone)]
enum Action { enum Action {
Move(usize), L, R Move(usize), L, R
} }
@ -36,6 +38,8 @@ impl Action {
} }
} }
#[derive(Copy, Clone)]
#[repr(usize)]
enum Facing { enum Facing {
R = 0, R = 0,
D = 1, D = 1,
@ -64,38 +68,72 @@ impl<'a> Dungeon<'a> {
cols.entry(c).and_modify(|mut x| *x = (x.0.min(r), x.1.max(r))).or_insert((r, r)); cols.entry(c).and_modify(|mut x| *x = (x.0.min(r), x.1.max(r))).or_insert((r, r));
(rows, cols) (rows, cols)
}); });
println!("{:?}", row_bounds);
Self { map, pos, dir, row_bounds, col_bounds } Self { map, pos, dir, row_bounds, col_bounds }
} }
fn act(&mut self, action: Action) { fn act(&mut self, action: &Action) {
match action { match action {
Action::Move(steps) => self.walk(steps), Action::Move(steps) => self.walk(*steps),
rotate => self.dir = rotate.apply(&self.dir), rotate => self.dir = rotate.apply(&self.dir),
} }
} }
fn walk(&mut self, steps: usize) { fn walk(&mut self, steps: usize) {
for _ in 0..steps { for _ in 0..steps {
match self.dir { let pos = self.next_pos();
Facing::R => {} match self.map[&pos] {
Facing::D => {} Tile::Wall => continue,
Facing::L => {} Tile::Empty => self.pos = pos,
Facing::U => {}
} }
todo!()
} }
} }
fn next_tile(&self) -> (Point, Tile) { fn next_pos(&self) -> Point {
todo!() let (row, col) = self.pos;
match self.dir {
Facing::R => {
let (min, max) = self.row_bounds[&row];
match col < max {
true => (row, col + 1),
false => (row, min),
}
},
Facing::D => {
let (min, max) = self.col_bounds[&col];
match row < max {
true => (row + 1, col),
false => (min, col),
}
},
Facing::L => {
let (min, max) = self.row_bounds[&row];
match min < col {
true => (row, col - 1),
false => (row, max),
}
},
Facing::U => {
let (min, max) = self.col_bounds[&col];
match min < row {
true => (row - 1, col),
false => (max, col),
}
},
}
}
fn get_password(&self) -> usize {
1000 * (self.pos.0 + 1) + 4 * (self.pos.1 + 1) + self.dir as usize
} }
} }
fn solve1((start, map, commands): &(Point, Map, Commands)) -> i32 { fn solve1((start, map, commands): &(Point, Map, Commands)) -> usize {
let mut dungeon = Dungeon::new(map, *start, Facing::R); let mut dungeon = Dungeon::new(map, *start, Facing::R);
1 for action in commands {
dungeon.act(action)
};
dungeon.get_password()
} }
fn solve2(data: &(Point, Map, Commands)) -> i32 { fn solve2(data: &(Point, Map, Commands)) -> i32 {