summaryrefslogtreecommitdiff
path: root/2024/day06.rs
blob: 7a31743c0fdeeafed05042293536ec20c3c071e6 (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
86
87
88
89
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
use std::collections::HashSet;
use std::env;
use std::fs::File;
use std::io::Read;

fn run(
    mut pos: (i32, i32),
    m: &Vec<Vec<bool>>,
) -> (bool, HashSet<(i32, i32)>, HashSet<((i32, i32), (i32, i32))>) {
    let mut hs: HashSet<(i32, i32)> = HashSet::new();
    let mut hs2: HashSet<((i32, i32), (i32, i32))> = HashSet::new();
    let mut circ = false;
    let mut dir = [(0, -1), (1, 0), (0, 1), (-1, 0)];
    loop {
        hs.insert(pos);
        hs2.insert((pos, dir[0]));
        let new_pos = (pos.0 + dir[0].0, pos.1 + dir[0].1);

        if hs2.contains(&(new_pos, dir[0])) {
            circ = true;
            break;
        }
        if new_pos.0 < 0
            || new_pos.1 < 0
            || new_pos.0 >= m[0].len().try_into().unwrap()
            || new_pos.1 >= m.len().try_into().unwrap()
        {
            break;
        }
        if m[new_pos.1 as usize][new_pos.0 as usize] {
            pos = new_pos;
            continue;
        } else {
            dir.rotate_left(1);
        }
    }

    return (circ, hs, hs2);
}

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 pos: (i32, i32) = (0, 0);
    let mut m: Vec<Vec<bool>> = Vec::new();
    for (y, line) in lines.enumerate() {
        let mut row = Vec::new();
        for (x, c) in line.chars().enumerate() {
            row.push(if c == '#' { false } else { true });
            if c == '^' {
                pos = (x.try_into().unwrap(), y.try_into().unwrap());
            }
        }
        m.push(row);
    }

    let (circ, hs, hs2) = run(pos, &m);
    let res1 = hs.len();

    let mut res2 = 0;
    for y in 0..m.len() {
        for x in 0..m[0].len() {
            let mut m2 = m.clone();
            m2[y][x] = false;
            let (circ, hs, hs2) = run(pos, &m2);
            if circ {
                res2 += 1;
                println!("circ {} {}", y, x);
            }
        }
    }

    println!("res1: {}", res1);
    println!("res2: {}", res2);
    assert_eq!(res1, 5531);
    assert_eq!(res2, 2165);
}