summaryrefslogtreecommitdiff
path: root/2024/day06.rs
diff options
context:
space:
mode:
authornekineki <nekineki@nekineki.net>2024-12-06 06:47:58 +0100
committernekineki <nekineki@nekineki.net>2024-12-06 06:47:58 +0100
commit48b85f07ceefa0f56724abe47b1097694789e6a9 (patch)
tree7448571cbf890792dd174a7dc77e898f9b07da91 /2024/day06.rs
parent97501bb2b1c876f92bc44ea53ba4686d15145b60 (diff)
day06
Diffstat (limited to '2024/day06.rs')
-rw-r--r--2024/day06.rs89
1 files changed, 89 insertions, 0 deletions
diff --git a/2024/day06.rs b/2024/day06.rs
new file mode 100644
index 0000000..7a31743
--- /dev/null
+++ b/2024/day06.rs
@@ -0,0 +1,89 @@
+#![allow(dead_code)]
+#![allow(unused_variables)]
+#![allow(unused_mut)]
+use std::collections::HashSet;
+use std::env;
+use std::fs::File;
+use std::io::Read;
+
+fn run(
+ mut pos: (i32, i32),
+ m: &Vec<Vec<bool>>,
+) -> (bool, HashSet<(i32, i32)>, HashSet<((i32, i32), (i32, i32))>) {
+ let mut hs: HashSet<(i32, i32)> = HashSet::new();
+ let mut hs2: HashSet<((i32, i32), (i32, i32))> = HashSet::new();
+ let mut circ = false;
+ let mut dir = [(0, -1), (1, 0), (0, 1), (-1, 0)];
+ loop {
+ hs.insert(pos);
+ hs2.insert((pos, dir[0]));
+ let new_pos = (pos.0 + dir[0].0, pos.1 + dir[0].1);
+
+ if hs2.contains(&(new_pos, dir[0])) {
+ circ = true;
+ break;
+ }
+ if new_pos.0 < 0
+ || new_pos.1 < 0
+ || new_pos.0 >= m[0].len().try_into().unwrap()
+ || new_pos.1 >= m.len().try_into().unwrap()
+ {
+ break;
+ }
+ if m[new_pos.1 as usize][new_pos.0 as usize] {
+ pos = new_pos;
+ continue;
+ } else {
+ dir.rotate_left(1);
+ }
+ }
+
+ return (circ, hs, hs2);
+}
+
+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 pos: (i32, i32) = (0, 0);
+ let mut m: Vec<Vec<bool>> = Vec::new();
+ for (y, line) in lines.enumerate() {
+ let mut row = Vec::new();
+ for (x, c) in line.chars().enumerate() {
+ row.push(if c == '#' { false } else { true });
+ if c == '^' {
+ pos = (x.try_into().unwrap(), y.try_into().unwrap());
+ }
+ }
+ m.push(row);
+ }
+
+ let (circ, hs, hs2) = run(pos, &m);
+ let res1 = hs.len();
+
+ let mut res2 = 0;
+ for y in 0..m.len() {
+ for x in 0..m[0].len() {
+ let mut m2 = m.clone();
+ m2[y][x] = false;
+ let (circ, hs, hs2) = run(pos, &m2);
+ if circ {
+ res2 += 1;
+ println!("circ {} {}", y, x);
+ }
+ }
+ }
+
+ println!("res1: {}", res1);
+ println!("res2: {}", res2);
+ assert_eq!(res1, 5531);
+ assert_eq!(res2, 2165);
+}