Skip to content

Commit

Permalink
Day 21a
Browse files Browse the repository at this point in the history
  • Loading branch information
lpenz committed Dec 21, 2023
1 parent 41f7933 commit 3bace98
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ members = [
"day17",
"day18",
"day20",
"day21",
]

10 changes: 10 additions & 0 deletions day21/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "day21"
version = "0.1.0"
edition = "2021"

[dependencies]
aoc = { path = "../aoc" }
color-eyre = "0.6.2"
nom = "7.1.3"
sqrid = "0.0.23"
131 changes: 131 additions & 0 deletions day21/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
...................................................................................................................................
....#...#.......................#.#......###..#.............#................#.....#.....#.#...#.........#.#..........#......#.....
...#.##.#...#.....................#.......#.......#....#...................#....#...........#.#...........#....#...#...............
.#........#.#................#.....#.....#.........#.......................#..#...#........#..#..........##............#...........
.....#.#.....#.....#..........#........#.........#...#..........................#....#.#......#....#.#####.................#.#.....
...........................#...........#...#..#...#...............#.......#......#.#......#.......#.......#.........#.........#....
......#...#.....##.......................#.....................#..............#..#...##...#....#....##....##....#..#...............
..................#...........#.................................#..#.......................#....#.....#................#...........
.#....##......#.............#.#.....#...#..#...#.....#.......#.....#.#..........#..##..........#.........#.........................
...........#.......#.....#................#.....#..............................#.#....#...............#..........................#.
............#.#..............#.......#...................................................#......##....#.......#...##...............
...................#.#......#.....................#........##.#....#...#............#...................#.....##.....#..#.#..#...#.
..........#......#...#...#..#.................#..............#.....................#.........#.#........#.......#.#..............#.
......#.......#...#.#............#.....#.#...................#...........##.......................#...#...##......#..#......#......
.#.............#.....#......#.....#.........#..............##..................................#..#..#.#........#.....#............
......#...............#......#............................#.............##..#.........##.........#.#......#......#..............#..
.........#..............#..................................#.................#..........##.......#........#........##............#.
.....#..........#.....#.#..................##..........#....#.....#...#...#..#........#.............##.......#.....#........##.##..
......#........#.....................#................#......................#...........#...#......#...##.........................
..............#................#..#...................#...###...............................#.#..##........##......##...#.....#....
....#...............##.....#.#........#..............#..#.......#..#..#........#............#..##....#......#.....###..............
.#......................#..#...#....#...........#...#...............#........#............#...#.##....#................##....#...#.
......#.......#.#...#......##.....................#...............#......#.....................##.....#.#.....##.................#.
......##................#....#...#...#.............#..................#.........#...............#...........##.................#...
....#.....#...#.#....#....#.......#.#.........#...###........#.........#.....................#.....#.##...#..##......#...#.........
.................#....#...#....#.#......................#...........#.....#.....#.#.##........#..........#......#..#....#..........
....#...........#.......#....................#.#...#.........#.............#..#..................##............#.#.................
...................#.#.......#...##............#....#...........#.......#........................#...........#.##....##....##......
......#..#.#...#..............#...............................#.....#.#.............#..#............#........#..............#.#....
..#....#...........#...##..##..#........#..#.....#....#...#...#...#....#.#............................#.#.#........................
......#................#.#................#..#....##........#........#....#...#..#.#......#..........##....#...#......#.#.#...#..#.
...#.#..##..#......#......................#................#....................#............................##...#......#..#......
....#...#.##...##.......#................#.#..#........#.#.............#............##.#..........................##......##.......
...#.........###.#.....#............#.........#................#..............#............................#......#.....#..#.......
...###....#.........................#..#...#............................#.....##..#..........#...........#.....#................#..
.............##.....#...............#..........#.....#.............#..#...#..#........#.#.#.............#............#...........#.
.......#..........#..............#.......#..#........#.#...............#................#....#...............#.....................
.............#..#.....................##.....#.#...............................#..#..#..#....#...##............#..#......#.........
................#...#.#.............#............#...........................#...........##...#..#..........#.......#..#........#..
.........#.#.........#...........#.........#..........#...........#.............#..........#..................#...#....#....#..#...
...#.#.....#...#...............#..#......#.......##........#..#......#..#.....#..........#...#.#.............#...#....#.....#......
..#...#..##.....#............#.#......#...#................................#.##.............###....#................##....#......#.
...................................#.#...#.......#.....##....#.#......#..#.............#...........#..#........#.................#.
.......#..................#..#................#.........................#.................#..........#..#.....................#....
..........#...#.##..................#......##..##......#......#......#..##.....#..............#....#.............#...#..##.#...#...
................#.........................#.......#...............#.......#..#....##...#......#....#................#............#.
..#....#..##.#...........#.........##..#..#..#.................##......#...##..................##....#...............#.#..##.......
......#......#...........#....#...#....#.#......................#...........#................#.........#.#..........#.#............
.....#....#..#.......##....................#................#.......#...........#.......#..............#..................#........
..#........#........#.#.....##........#..#.....#.....#......#......#.#............##....#..............#..............#...#........
..#.##........................#..##.#.........#.....#...#............#..............#...#.........#...........#............##......
...#........................#.#....##.#.......#.#.............#....#...#......#.....#.....#..#......#...##...............#.........
......#...........#.................#......#.#......#.......##.#.....#.....#..............#...................#..............#.....
.......#..............#..........#..........#...........#...#........................#..#....#............................#....#...
................#..#.......#....#..##............####....#..#...#................#..............#................................#.
...#..#............#..#........#.......#......#..##...............#..#.#...#......#.........#.................#.#...........#.#....
..#...............#...#..........................#.#.......#..#..........#...#.......#..#....#.#..............................#..#.
...#................................#...#.....#...#........#.......#..#..#..............#...#.....#.......#........................
............##.............#.......#...........#...##......#......#.................#.......###...........#......#...............#.
.......................#...###.........#.....#.................#....##..#............#...........##....#.........#.#..##...........
...........##.....................#....#..#....................#.....................#..........#.....##........#................#.
........#........#.....##........##.........##..#.#..................##..........##...#...##..............#......#.................
...................#.........#.##..#..#.#..#...........##................#.......#......#...#.#........#.#..#......................
.................#..........##.............#.....#.......##..#..#.......##..##...............#..............#......#.##............
............#....#.............##...#.....#..................###..#..#......#............#............##..............#....#.......
.................................................................S.................................................................
............#......#....#........#..#..#.#..................#..#......#................#...........#.#..#....#......#..#.....#.....
................#.......................#...##.........#...............#.#...................##.........#....#..#...........#......
...........#..##..#...........#........##...........#..##..#..#..........#......#..........#....#..#........#.........##...........
.........##..#...#.............#....#...........#...#...#......#..#...............#........#..............#.#....#.................
.#............................#..............#....##.....#..#..#............#.#....#..#......#.....#........#.....#................
...................#...#...........#............#.....##........#....#....#..#....#.........#.#....#.#.....##..#..#....#...........
................#.#...#....#.#....##..#..##...#.......#.#............#.........................###.#.................##............
.#.#.........................#.....................#.......................#.#...#..##...#..#.#.......#...............#.......#..#.
..............#...###...........#....#..#.................#..#......#.#........#.#...#...#..#..#...#.#..#.....................#.#..
.....................................##...............#.#.......#.......#..........#......#.........#..#............#..............
......................#..#.#.#...........#.##.....#...............#...#.........#.....##............##......................#....#.
...................#....#..#.#...#...#.....#.........#..#.......#...........#..#......................#.......#...........#........
.....................#...#.....#.#......#....##..##.##.........#..........#...........###......#......#.......#...............#....
..#.....................#..............#.#..#............#.....#.........#.#..............#................................#.......
.#........#...........##...........................#........#......##............#......#......#......#............................
...........#...................#...##......#...........#...................#...................#.#......#.............#..#...#.....
..........#..........##........##..#.#....#.....#...........##.......#...........#.......##.#........#...#............#..........#.
.........#...........................#...#.##...........#.............##....#...#...#.............#.....#...#.........#............
..#.....#...........................#....#.................#..#....................##..#.##.##....#......#..................#......
.....##...................#......#.#..#.............#........#......#..#.........................#....................#....#...#...
.#...................................#....#........#...#.#...#...........#.#.......#..#..#.......##.#...................#....#.#.#.
.#......#....#....#........................#....................#...#..#...#..#...........#......#..#..............................
..........#.......#..........#.........##...##.....#.......#....#..........#..#......#..................................##.........
...#...........#...............#.#....##......#.....................#....#..........##..#..##....##.#.........#......#.......#.....
.#.................#...........#......#...#........#.........#.##..............#.......#...#.#...#...#..........#..#.#.............
.....#..#...#...#...................#...#..#..#.......#..#................#............#......#...................##..#..........#.
......#.......#...#..............#.##.....#..#........#....#..........#..###...................................#............#......
..#..........#...#..##..#...........##.........##....#..#...#..........#.##.......#.......#..#..#............#.#...#.......##..#...
..#..#....#..##........##...........#.#..................#......#.#.#........#..........#.................................#..#.....
.................#...........................#..#....................#..#.#.#..#....#....#..............#....###.....#...#..##.#...
......#.....#.........#.......................##.....#.......#.#..##....#.....#....#...##...#..#..........#....#..#................
..........#........##...........................#.#..........#................#.....#.#...##.......................#.......#..#....
.........#.....#...........#...........................................................................#.....#...............#.....
.................#.....#..................#...#....#.#............#..##..........#......#.##.............###......#..........#.....
.#...................................................#..#..#.#........#...............#....................#.............#..#......
..#..#......#..#........#..#..............##...........#.#........#.........#..#....................................#.....#........
.#.....#..#.#..##....#...#......................#..........###.#..##.......#.........................#....#........#..#............
..#.......#.#.......#........#.#.............#..........#.........#...#.....................................#........#.##.......#..
...#......#.#......##.##......##.#...........##....#.....#......................#...............#.....##.............#..........#..
.#.#....#...................................##.................#.............##.#................#...#.#...#.............#.....###.
..#..........#.##.###....#...#.......#..............#.......#.................#.#....#..........#.#...#...#..#........#............
.......##.........#....#....##..##............##.........#...........#...##.#.....##..........#.........#............#.#....#.##...
....#...###...........#......#.................#.....#.#.................#.................#......#.............................#..
...#..#...................#..#....#...#.............#.......#.#.........###...#...................#....##..#...........#......#....
.#...............#.............#..##............................#..........##.................#.#.#.##....#.......#.#...#..#.......
..#...........#.....................#..#...........#..#.##..#......####.........#..............................##........###.##.#..
.......#.....#..........#.#.........................#...........#.....#...#....#..............#..........##.#................#.....
.#.....#....#...#....#..........#....................#................#....#...........#....#.....................##...#....#....#.
................##...#....#........#..##.............#............##.#....##............#........#.##............#.#.........#.....
......#.#.#.#....#........................#..................#.#........................#........#.#....................#.....#.#..
......#..#.........#........#....#...#...#...#...............##.......#.#.............................................##...#....#..
......#.#......................#....#..#....................#.......#................#..#..#.#..#...............#.........##.#.....
............#.....................###.......................##.....#.....#........#...#........#.........#..#........#.........#.#.
....#......#..#.................#........#..#...#.........#.....#.#.................#.............#..#.....#........#....#.........
.#..#......#..#.....................#.....#......#..............................#..#..................#..#..##...........#.........
...#.............#...........#...................#.#........#.....#............#............................#..#...................
.....#....#.........#..#.#..............#.#..#.##....#.......##..........................#....#...........#...............###......
......#................#..#....................#.....#........................#.#....#.......#................#.............#...#..
..........#...........#.........#.................#.........................#........#..#.............#......#..........#..#.......
.....#...#...................#.....#.........##..#.....................................#.....#.#....#........#...#..#....#.........
..#.#.......#...##..#........#..##...##.#...#...........#.........................#.....#........#..................#....#.....#.#.
.#.........................#.............#.......................................#..........##....#..#####.#.#.................#...
.#.#...........#.....#.##.....#...............#....#.....#.#.................#..#.....#...#...........#......#...#............#..#.
...#...........#..#............#.......#.#.........#..##................#.#.......#............#.................#.....##..........
...................................................................................................................................
66 changes: 66 additions & 0 deletions day21/src/bin/day21a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (C) 2023 Leandro Lisboa Penz <lpenz@lpenz.org>
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.

use day21::*;

use std::collections::HashSet;

pub use sqrid::Dir;
pub type Sqrid = sqrid::sqrid_create!(131, 131, false);
// pub type Sqrid = sqrid::sqrid_create!(11, 11, false);
pub type Pos = sqrid::pos_create!(Sqrid);
pub type Grid = sqrid::grid_create!(Sqrid, Cell);

pub type GridDebug = sqrid::grid_create!(Sqrid, char);

pub type Steps = i32;

fn bfs(
size: u16,
grid: &Grid,
pos: Pos,
visited: &mut HashSet<(Pos, Steps)>,
end: &mut HashSet<Pos>,
stepsleft: i32,
) {
if visited.contains(&(pos, stepsleft)) {
return;
}
if stepsleft == 0 {
end.insert(pos);
return;
}
for dir in Dir::iter::<false>() {
let Ok(newpos) = pos + dir else { continue };
let newpos_t = newpos.tuple();
if newpos_t.0 >= size || newpos_t.1 >= size {
continue;
}
if grid[newpos] == Cell::Rock {
continue;
}
bfs(size, grid, newpos, visited, end, stepsleft - 1);
}
visited.insert((pos, stepsleft));
}

fn process(size: u16, steps: Steps, bufin: impl BufRead) -> Result<usize> {
let input = parser::parse(bufin)?;
let grid = Grid::try_from(input)?;
let start = Pos::iter().find(|p| grid[p] == Cell::Start).unwrap();
let mut visited = HashSet::<(Pos, Steps)>::new();
let mut end = HashSet::<Pos>::new();
bfs(size, &grid, start, &mut visited, &mut end, steps);
Ok(end.len())
}

#[test]
fn test() -> Result<()> {
assert_eq!(process(11, 6, EXAMPLE.as_bytes())?, 16);
Ok(())
}

fn main() -> Result<()> {
do_main(|| process(131, 64, stdin().lock()))
}
Loading

0 comments on commit 3bace98

Please sign in to comment.