77 lines
2.1 KiB
Rust
77 lines
2.1 KiB
Rust
|
use std::{fs, env, collections::HashSet};
|
||
|
|
||
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||
|
enum Direction {
|
||
|
East,
|
||
|
South,
|
||
|
Empty
|
||
|
}
|
||
|
|
||
|
fn read_input(path: &str) -> String {
|
||
|
return fs::read_to_string(path).expect("Cannot read file.");
|
||
|
}
|
||
|
|
||
|
fn parse_input(s: &str) -> Vec<Vec<Direction>> {
|
||
|
let mut f: Vec<Vec<Direction>> = vec![];
|
||
|
for (r, l) in s.lines().enumerate() {
|
||
|
f.push(vec![]);
|
||
|
for c in l.chars() {
|
||
|
f[r].push(match c {
|
||
|
'>' => { Direction::East }
|
||
|
'v' => { Direction::South }
|
||
|
_ => { Direction::Empty }
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
return f;
|
||
|
}
|
||
|
|
||
|
fn get_next(i: usize, m: usize) -> usize {
|
||
|
if i == m - 1 { return 0; }
|
||
|
return i + 1;
|
||
|
}
|
||
|
|
||
|
fn apply_step(v: &mut Vec<Vec<Direction>>) -> usize {
|
||
|
let mut f = HashSet::new();
|
||
|
let mut o = v.clone();
|
||
|
for r in 0..v.len() {
|
||
|
for c in 0..v[r].len() {
|
||
|
let n = get_next(c, v[r].len());
|
||
|
if v[r][c] != Direction::East { continue; }
|
||
|
if v[r][n] != Direction::Empty { continue; }
|
||
|
if o[r][n] != Direction::Empty { continue; }
|
||
|
if f.contains(&(r, c)) { continue; }
|
||
|
f.insert((r, n));
|
||
|
v[r][n] = Direction::East;
|
||
|
v[r][c] = Direction::Empty;
|
||
|
}
|
||
|
}
|
||
|
o = v.clone();
|
||
|
for r in 0..v.len() {
|
||
|
let n = get_next(r, v.len());
|
||
|
for c in 0..v[r].len() {
|
||
|
if v[r][c] != Direction::South { continue; }
|
||
|
if v[n][c] != Direction::Empty { continue; }
|
||
|
if o[n][c] != Direction::Empty { continue; }
|
||
|
if f.contains(&(r, c)) { continue; }
|
||
|
f.insert((n, c));
|
||
|
v[n][c] = Direction::South;
|
||
|
v[r][c] = Direction::Empty;
|
||
|
}
|
||
|
}
|
||
|
return f.len();
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let args: Vec<String> = env::args().collect();
|
||
|
for arg in args.iter().skip(1) {
|
||
|
let input = read_input(&arg);
|
||
|
let mut vec_in = parse_input(&input);
|
||
|
let mut i = 1;
|
||
|
while apply_step(&mut vec_in) != 0 { i += 1; }
|
||
|
println!("[{}]", &arg);
|
||
|
println!("\t[Part 1] => Answer is '{}'.", i);
|
||
|
}
|
||
|
}
|
||
|
|