#![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 = Vec>; fn bfs(steps: usize, x0: usize, y0: usize, m: &Matrix) -> 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 = 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 = Vec::new(); for (y, line) in lines.enumerate() { let mut r: Vec = 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 = 0; // let res1 = bfs(64, x0, y0, &m); let steps = 26501365; let mut res2 = bfs(264, x0, y0, &m); println!("res1: {}", res1); println!("res2: {}", res2); }