summaryrefslogtreecommitdiff
path: root/2023/day21.rs
blob: bcad5acaa6132352bf8b1cbee9232baa73cb0e38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#![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<T> = Vec<Vec<T>>;

fn bfs(steps: usize, x0: usize, y0: usize, m: &Matrix<bool>) -> 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<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 x0: usize = 0;
    let mut y0: usize = 0;
    let mut m: Matrix<bool> = Vec::new();
    for (y, line) in lines.enumerate() {
        let mut r: Vec<bool> = 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);
}