summaryrefslogtreecommitdiff
path: root/2023/day18.rs
blob: 66e38316052c8d26abd6e6f7eccceab72a152c82 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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);
}