summaryrefslogtreecommitdiff
path: root/2023/day21.rs
diff options
context:
space:
mode:
Diffstat (limited to '2023/day21.rs')
-rw-r--r--2023/day21.rs82
1 files changed, 82 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);
+}