#![allow(dead_code)] #![allow(unused_variables)] #![allow(unused_mut)] use std::env; use std::fs::File; use std::io::Read; #[derive(Debug, PartialEq, Clone, Copy)] enum Field { Wall, Box, Empty, } fn sim(rx: i64, ry: i64, dx: i64, dy: i64, mut m: &mut Vec>) -> (i64, i64) { let mx = rx + dx; let my = ry + dy; let f = m[my as usize][mx as usize]; if f == Field::Empty { return (mx, my); } else if f == Field::Wall { return (rx, ry); } let mut x = mx + dx; let mut y = my + dy; loop { let f = m[y as usize][x as usize]; if f == Field::Wall { return (rx, ry); } if f == Field::Empty { m[my as usize][mx as usize] = Field::Empty; m[y as usize][x as usize] = Field::Box; return (mx, my); } x += dx; y += dy; } } fn gps(m: &Vec>) -> i64 { let mut ret = 0; for y in 0..m.len() { for x in 0..m[0].len() { if m[y][x] == Field::Box { ret += 100 * y + x; } } } return ret as i64; } fn print_m(m: &Vec>) { for l in m { for f in l { match f { Field::Box => print!("O"), Field::Empty => print!("."), Field::Wall => print!("#"), } } println!(); } } 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 (para1, para2) = content.trim_end().split_once("\n\n").unwrap(); let mut rx = 0; let mut ry = 0; let mut m: Vec> = Vec::new(); for (y, line) in para1.split("\n").enumerate() { let mut l: Vec = Vec::new(); for (x, c) in line.chars().enumerate() { let x = x as i64; let y = y as i64; if c == '@' { rx = x; ry = y; } if c == 'O' { l.push(Field::Box); } else if c == '#' { l.push(Field::Wall); } else { l.push(Field::Empty); } } m.push(l); } let mut dirs: Vec<(i64, i64)> = Vec::new(); for line in para2.split("\n") { for c in line.chars() { let d = match c { '<' => (-1, 0), '>' => (1, 0), '^' => (0, -1), 'v' => (0, 1), _ => panic!(), }; dirs.push(d); } } //println!("{:?}", dirs); for dir in dirs { (rx, ry) = sim(rx, ry, dir.0, dir.1, &mut m); //print_m(&m); } let res1 = gps(&m); let mut res2 = 0; println!("res1: {}", res1); println!("res2: {}", res2); //assert_eq!(res1, 1414416); //assert_eq!(res2, ); }