diff options
Diffstat (limited to '2023/day18.rs')
| -rw-r--r-- | 2023/day18.rs | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/2023/day18.rs b/2023/day18.rs new file mode 100644 index 0000000..66e3831 --- /dev/null +++ b/2023/day18.rs @@ -0,0 +1,129 @@ +#![allow(dead_code)] +#![allow(unused_variables)] +#![allow(unused_mut)] +use std::env; +use std::fs::File; +use std::io::Read; + +#[derive(Debug, PartialEq, Clone)] +struct Line { + dir: char, + len: usize, + color: String, +} + +fn flood(mut m: Vec<Vec<usize>>) -> (Vec<Vec<usize>>, usize) { + let mut changes = 0; + for y in 1..m.len() - 1 { + for x in 1..m[0].len() - 1 { + if m[y][x] == 2 { + for (yy, xx) in [(y - 1, x), (y + 1, x), (y, x + 1), (y, x - 1)] { + if m[yy][xx] == 0 { + changes += 1; + m[yy][xx] = 2; + } + } + } + } + } + + (m, changes) +} + +fn print_m(m: &Vec<Vec<usize>>) { + for l in m { + for a in l { + if a == &0 { + print!("."); + } else if a == &1 { + print!("#"); + } else { + print!("{}", a); + } + } + println!(); + } +} + +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 path: Vec<Line> = Vec::new(); + for line in lines { + let (a, ar) = line.split_once(' ').unwrap(); + let (b, c) = ar.split_once(' ').unwrap(); + let a = a.chars().next().unwrap(); + let b = b.parse().unwrap(); + // let c:u32 = c[2..c.len()-1].parse().unwrap(); + path.push(Line { + dir: a, + len: b, + color: c.to_string(), + }); + } + + let mut res1 = 0; + + let mut field = vec![vec![0; 600]; 600]; + let x0 = 300; + let y0 = 300; + let mut x = x0; + let mut y = y0; + field[y0 + 1][x0 + 1] = 2; + res1 += 1; + + for l in path { + match l.dir { + 'R' => { + for i in x..x + l.len { + field[y][i] = 1; + } + x += l.len; + res1 += l.len; + } + 'L' => { + for i in x - l.len..=x { + field[y][i] = 1; + } + x -= l.len; + res1 += l.len; + } + 'U' => { + for i in y - l.len..=y { + field[i][x] = 1; + } + y -= l.len; + res1 += l.len; + } + 'D' => { + for i in y..y + l.len { + field[i][x] = 1; + } + y += l.len; + res1 += l.len; + } + _ => panic!(), + } + } + + let mut changes = 1; + while changes != 0 { + (field, changes) = flood(field); + res1 += changes; + // print_m(&field); + } + + let mut res2 = 0; + + println!("res1: {}", res1); + println!("res2: {}", res2); +} |
