summaryrefslogtreecommitdiff
path: root/2023
diff options
context:
space:
mode:
authornekineki <nekineki@nekineki.net>2023-12-21 07:50:40 +0100
committernekineki <nekineki@nekineki.net>2023-12-21 07:50:40 +0100
commitdec2a99c5f3df0c9d5147af84cbc411427726620 (patch)
treeeb9766fe7459acb9f8cec8bfb2bbefa34a69a211 /2023
parent23d0915b239bef4234c5b9326eb2ecbdd4fb7a75 (diff)
day21 part1
Diffstat (limited to '2023')
-rw-r--r--2023/day21.rs82
-rw-r--r--2023/in/day21.pzl131
-rw-r--r--2023/in/day21.ref11
3 files changed, 224 insertions, 0 deletions
diff --git a/2023/day21.rs b/2023/day21.rs
new file mode 100644
index 0000000..c533177
--- /dev/null
+++ b/2023/day21.rs
@@ -0,0 +1,82 @@
+#![allow(dead_code)]
+#![allow(unused_variables)]
+#![allow(unused_mut)]
+use std::collections::HashSet;
+use std::env;
+use std::fs::File;
+use std::io::Read;
+
+// #[derive(Hash, Eq, Debug, PartialEq, Clone, Copy)]
+// enum Field {
+// Empty,
+// Rock,
+// Start,
+// }
+
+type Matrix<T> = Vec<Vec<T>>;
+
+fn bfs(steps: usize, x0: usize, y0: usize, m: &Matrix<bool>) -> usize {
+ let mut hs: HashSet<(usize, usize)> = HashSet::new();
+ let mut hs_next: HashSet<(usize, usize)> = HashSet::new();
+ hs.insert((x0, y0));
+
+ for s in 0..steps {
+ for (x, y) in hs {
+ if x + 1 < m[0].len() && m[y][x + 1] {
+ hs_next.insert((x + 1, y));
+ }
+ if x as i32 - 1 >= 0 && m[y][x - 1] {
+ hs_next.insert((x - 1, y));
+ }
+ if y + 1 < m.len() && m[y + 1][x] {
+ hs_next.insert((x, y + 1));
+ }
+ if y as i32 - 1 >= 0 && m[y - 1][x] {
+ hs_next.insert((x, y - 1));
+ }
+ }
+ hs = hs_next;
+ hs_next = HashSet::new();
+ }
+ return hs.len();
+}
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+ let filename = if args.len() == 1 {
+ "in/".to_owned() + args[0].split('/').last().unwrap() + ".pzl"
+ } else {
+ args[1].clone()
+ };
+ let mut f = File::open(filename).expect("cannot open file");
+ let mut content = String::new();
+ f.read_to_string(&mut content).expect("cannot read file");
+ let lines = content.trim_end().split("\n");
+
+ let mut x0: usize = 0;
+ let mut y0: usize = 0;
+ let mut m: Matrix<bool> = Vec::new();
+ for (y, line) in lines.enumerate() {
+ let mut r: Vec<bool> = Vec::new();
+ for (x, c) in line.chars().enumerate() {
+ match c {
+ '.' => r.push(true),
+ '#' => r.push(false),
+ 'S' => {
+ r.push(true);
+ x0 = x;
+ y0 = y;
+ }
+ _ => panic!(),
+ };
+ }
+ m.push(r);
+ }
+
+ let res1 = bfs(64, x0, y0, &m);
+
+ let mut res2 = 0;
+
+ println!("res1: {}", res1);
+ println!("res2: {}", res2);
+}
diff --git a/2023/in/day21.pzl b/2023/in/day21.pzl
new file mode 100644
index 0000000..2363d5c
--- /dev/null
+++ b/2023/in/day21.pzl
@@ -0,0 +1,131 @@
+...................................................................................................................................
+....#...........#...#...........................#....................................#....##........#..........#.....#.............
+..........#...##..#.....................#.#.#..................................#.#..#..#............#........#.....................
+....#................##.....................#..........#........................##.#...#..#......#...#......#....#.................
+.#..#.................................#.................................................#..#............#..#.##.#.#.........###....
+..#...#.#.......#......#...........#................#............................................##.........#...#...#....##........
+................#.#..........#..........##..#..............................#...#................#.#......##.......#............#...
+........#.#...#..#.##.#.....#......................#.#.............................#....#......#.#.#...#........#..................
+.#...##.........#..#..........#....#.....#...........................................#........#.......#.......#...........#........
+.........##............#.......#..#.......#...#.#.....................................#..###.......###.........#.....#...#.....#...
+.#..#.......#....#..#............#.....#............................#................#.........##...................#..#.#..#......
+.#.................###..#...........#.........#................##....#...........#...........#.....................................
+.....##.........#..#..........#.....##....#................#..##...................#......#.....#..#.....#.#.......................
+..........#......#..............#..#...#..............................................#...........#.#........#............#.#......
+...........#.#......#.....#.#.....#.....................#...........#....#.............#.#......##...#......#.....##...............
+..............#...##....##.....#......#...............#.#.#...#.....#.................#.#.............#...............#....#.#.#...
+.##.#........#.........#.#.#....#....#.......#.........................#...............#..........#.....##....#...............##...
+.....#.......##............#.........#....#.................#.................#.......#...............#......................#...#.
+...............#..........................#........#..#.......##..#....#..#...#................#....#...#.........#.#.#..#......#..
+..........#..#..#..#.##.........#......#.......................#.....#.........#......................#...#.##......#....#....#....
+....#.........#......#................#..#.............##.#......................#...........#..#.............#....##........#.....
+.............#...#.................#...#...........#...#.#......#......#.......................#.................#....#.#....##....
+.......#.#.....#..........#.....#.##............#.#......#........#...#.......##................#................#.......#.........
+.#.....................#..#.....#.#...........................#..................#...........#....#......##..............#.....##..
+.....#..............#....#.....................#..##...........................#..................#...................#.........#..
+....#..#.##........#.....#.........................................#......#.......................#....#....#...#...#....#...#.....
+...................#....###....#............#..#.............#.#......#........##.................#...#....#.....#.##..............
+......#...#........#....#.....#................#...............................#........................##..#..#......##........#..
+.#.....#..........#....#........#................#....#...#..#..#.............#..#..........................#.........#..........#.
+.......................#.....#.................#.....................................##.#.........#..................#.....#....#..
+.#..................#....#....#...........#....#...#..#..#.............#....#....#####....#.........#.......#..#..###...........##.
+....#.......#.#............#.#.................##.............#.#.#....#...##...#...#...............##.###...........##......#...#.
+.....#...#...#.........#.....................#....##..#........#......#.................#.#...................#.#..............#...
+........##.....##.........#.#......................................#..........#.....#.#.....#.#..............#..........#..........
+........#....#.........#..#..............##.....#.....#.....#............#............#....................#.....#.....#.......#.#.
+......##...........................#............#.....#.#..............#...........#.......................................#.......
+.#.......#...#....#...............................#....##...#..........#.##........##....#................##...........#...........
+.#.##.........#.................#................#.#..#.#................#.................................#.......................
+.#..#..............#.##.........##.....................................#.....#........#.#..##..#..##............#..........#...#...
+....#.##...........###.........#.....#........##...#...#......##..#...............#.#.......#...#.#..........#..........#..........
+..........#.....#.....................#..##..#.#.##.............#..............#.............#......##...........##....#.......#...
+.....#.......#..................#..........#.......#........#..........##.....#....#...........................#..............#....
+.........#.......................##...............#......#.....#...#..........#.#..#...................#...................#.##....
+.........#..............................##......##...#..............#...##......#..............#.....#.......................#.....
+....#............................#...#..#.#.......#.............#...............#.....#..................#........#.........#......
+..#.....................##........#......#...#....#.#..............#...............#.........#...#....#.###..........#....#..#..#..
+...........#...#.................#.........#..##.#......#.#.................#..#.......#..............................#..........#.
+.....#.#.................##.#.....#....#.........#.....#..#...#.......##..#................................##.................#....
+.......#..............#.......#................#.......#..............##.....#...#...#.............................................
+.#......#.................#......#.......#.........#...........#...#.......#...#...#......#.......#.#.....#...#..................#.
+..#...#...............#.......#.#...................#.....#.#........................#......#....##.#..............................
+...##....#......................................#......##.......................#..#...##...............................#...#......
+.##..........................................#...............#.......................##.....#...............#.#....................
+..#..#...................................##..#.....................................#........##.....#....###......##............#...
+...#............#.......#.........#..#.....#.......#.........##........................#...#...#.#..#....#.#.##....#........##.#...
+.....#...........#.#....#..............#.....#..............#.................#.......#....#................#....#.................
+....#............#......#.............#....#.#...##..#....#.#...#......#.......#...........#..........#.......#....................
+...##..............#......#...#........#.........#...#....#............#..#..........#.#..#.......#...#.........#.....#............
+.............#.##.........#...#....##.#...#...........##...........##..#....#.......#............##................#...............
+.............#..##...#.#.#.#..#.#............#...#......#...#.....#.........###....#..............#............#.#.....#........##.
+..........#.................#.....#.........#.#......#.#...........#.......#...............#..#..........#........#.....##.......#.
+....................#................#......................#..............#........##...#......#....#...........#.#....#..........
+..................#.....#.#.#....#...#..........................#...#...........................................#....#...#.........
+......#..........#.#..#........#.....#.......................#.........#......#.............#..............................#.......
+...........##....#..#........#...#............#...............#.#.##.#.#.#............#...#........#.....#..#.#...#......#.........
+.................................................................S.................................................................
+.....#........#......#...........#..#............#..............#........###........#......#............#.....#....#...#...........
+..................................#..#................#........#.........#...#.#.......#..#..........#...........#......#.#........
+............#.........###........#........##........................#...#.....#......#..##..#.......#..###..............#..........
+..........#..............................................#...............#..............#............#.#...#..............#........
+...........#...#..#...##...........#............#.....#.#..#..#........#..#...............#...............#.....#.....#.##.........
+.....................#....#..........#..#..................#..#.........#......#..................#.................#..#...........
+............#....#.#...............#.....#..........##..............#...#.#....#....#......#.......................................
+.#...........#............#..#..#....................................#.......#......#.....#....#.............#.....................
+..#.#...........#.............#...............#...#.....#.......#.............#.........#.#.........#..........#...................
+..............#................##.#.#.#.#......#...#...........#....#.#.....................#...........##.#..#.............#.##...
+...#...............#..........................##........#..#.....................#...........#...#...#.#...#......##...............
+........#..........#...#..........#...#.....#............#.......................#.#..........#...#..........................##....
+.......#..............#.............#.......##.#..#...#........#.....#..#.....#....#............................#........#...#.....
+..........#.............#...........#........#..#...###....#..........#...#....#..............#.##.............#.........#...#.##..
+.........................#.#..........#.....#.........##.#..............#.....##.........#.##.....#.....#..........................
+....#....#.................#..........#...#.....##.#...#....#.........##.....#.##.............................................#....
+...#....#.#............#...........................#......................#..#..#...###............#...#...........................
+..............#............#................#...#.#..........................#....................#...#.#............#.....#.....#.
+.......#.......#........#..................#.#....#...#..............#......#...............#.......##...#.................#.......
+.....#.#..............................#.#...#..................#...........#.##................#...................#..#.#..........
+.....#..........#..........#.#.....#.........#....................#..#..#.......#......................##................#.....#...
+....#..................................#....#........#.........#..........#...#.........#..#..........#.............#.......#..#...
+.#.................#........#..#.#......#..#.#.....#..#.........#......#......#.#........#.....#.#....#............#.......#.##....
+...............#................#..........#...#.#...#...#....#.#..#...........#....#..............................#............#..
+.........#...........#.........#................................#...#..#........................................#....#.#...........
+...#..................#...........#........#................#.#....#............#...#........................#...#.................
+.#..............##.....#.......#......#..............#..........................#.#...........#............#...........#....#......
+........#....#......................#................#........#...................#.........................#.#....#............##.
+...........#.........#.#...........##......##..#.#..#.....................#.#......#...........#.............#.....................
+...........#...##.........#...........................#......##.......#......#...............#.#...........#.##...#..#..#..#..#....
+....#...##..#.#..#.#....#...........#.........#.....#.........#.#.....................#..................#..........#........##.#..
+.....##....#..........#.#...........#....#..............#................#.......#....................##......#....................
+.#..#..#........#....................#..#.#....#........##.....##.#......#.....#......#....#.....................#......#..###.#...
+.........#...#...............#.........#..#............#.#....#.........#.............#...#.................#..............##......
+...##........#.#.#.....##..#........................#....#...#...........#..........#.#............#.#...#.............#...........
+...##..............#......##.................#...........#..........#..............##......................#...#.....#....#..#.....
+........#........#........#.................###.................................#....................##.#.#...#...............#..#.
+.......#.......................................#..........#..........................#.#............#.........#..#..##.............
+.........#.........................#................#................#...#.......#..#..#.......#.........###.......................
+........#.....#......###.#...............................#........#....#.....#..##.............................#..#...#........#...
+...........#.#....#..........#.#...#..........####.#......####.#....................#.............................#................
+...##.............#..#........#................#......................#...#......#..#........#......#..................#...#.#...#.
+.#........#.......#.......#.......#................#........#..#.....#...........#.........#.......#.#........#........##...#..#...
+..........#.......#............#.........................#.................#....#...........##.........#...#................#....#.
+.........##.#.....#...#..##..............................#..................................##......##..#..........................
+........#......###..#....#....#.##....#..............#........#...#.........................................#........#...........#.
+.......#.....#.#..#....#..........#...#.#..#....................#........#..............#......#..#..#....#............#.......#...
+...........#.##..#......##.#...............#.............#.##.............#.......................#...........##...................
+.###.....................#.#..........................................#.#.................#........#............#.........#........
+.....#....#......#..#.....#............#..............................................#....##.......#........#..#..........#.......
+.......#......................................................##...#.................#.......................##....................
+......#.#....#...#...............#.#...#..#...............##......#..#............#.....#.......#.................#..##..#.#.....#.
+............#........#..#.........#....#......##.#........#....#..#...........................#...............#............##......
+.....#....#.##.......................#....................#.#.................................................#....##....#...#.....
+..............###..#.....#........#.........##.#...........#.#.#...#.............#......#...............#.##...........##..........
+........................#..#.........#.....#.......#.........#.....#................#.#.#..........................#.#......##.....
+.......##........#......#........#.##.#.#........#......................................#......#...#.#...#.......#..#.....#........
+.#.....####..........#......#.......................#.............#...........#.....#.......#.#.....................#..............
+...#.....#...##.............#...............#..##..................#................#..........#...#........#.......#..............
+.#.........#..#...#.#....#....#.......#...........#...............#...............#...#........#..........................#........
+...............#....#........###.......#....................................#.................#...........#.........#..#...........
+.......#......#..#.....#.........#.........#............#.......................#..#........#...#..#.....#.......#.............#...
+........................#.............#........#..........#.................#......##.#..#...#.............#...........#...#....#..
+....#.....#....#......#...#...#...................#..##.................#..........##.##...............#..........##...............
+...................................................................................................................................
diff --git a/2023/in/day21.ref b/2023/in/day21.ref
new file mode 100644
index 0000000..9e1d842
--- /dev/null
+++ b/2023/in/day21.ref
@@ -0,0 +1,11 @@
+...........
+.....###.#.
+.###.##..#.
+..#.#...#..
+....#.#....
+.##..S####.
+.##..#...#.
+.......##..
+.##.#.####.
+.##..##.##.
+...........