aboutsummaryrefslogtreecommitdiff
path: root/2022/18/common.rs
blob: ef6c8c8aba207154bb3dcd0fbe2f8777970ad6e3 (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
pub const MAX_LEN: usize = 20;
pub const DIR: [(i8, i8, i8); 6] = [
    (1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1)
];

fn inb(i: i8, j: i8, k: i8) -> bool {
    i >= 0 && i < MAX_LEN as i8 &&
    j >= 0 && j < MAX_LEN as i8 &&
    k >= 0 && k < MAX_LEN as i8
}

pub struct Map {
    cell: [[[bool; MAX_LEN]; MAX_LEN]; MAX_LEN]
}

impl Map {
    pub fn new() -> Map {
        Map { cell: [[[false; MAX_LEN]; MAX_LEN]; MAX_LEN] }
    }

    pub fn from_stdin() -> Map {
        let mut map = Map::new();
        let mut line = String::new();
        while std::io::stdin().read_line(&mut line).unwrap() > 0 {
            let i = 0;
            let j = line.find(',').unwrap();
            let x = line[i..j].parse::<usize>().unwrap();
            let i = j+1;
            let j = line[i..].find(',').unwrap() + i;
            let y = line[i..j].parse::<usize>().unwrap();
            let i = j+1;
            let j = line.find('\n').unwrap();
            let z = line[i..j].parse::<usize>().unwrap();
            map.set(x, y, z, true);
            line.clear();
        }
        map
    }

    pub fn get(&self, x: i8, y: i8, z: i8) -> bool {
        inb(x, y, z) && self.cell[x as usize][y as usize][z as usize]
    }

    pub fn set(&mut self, x: usize, y: usize, z: usize, b: bool) {
        self.cell[x][y][z] = b
    }

    fn free_area(&self, x: usize, y: usize, z: usize) -> usize {
        let mut r = 0;
        for d in DIR {
            let (i, j, k) = (x as i8 + d.0, y as i8 + d.1, z as i8 + d.2);
            if !self.get(i, j, k) {
                r += 1
            }
        }
        r
    }

    pub fn total_free_area(&self) -> usize {
        let mut r = 0;
        for i in 0..MAX_LEN {
            for j in 0..MAX_LEN {
                for k in 0..MAX_LEN {
                    if self.get(i as i8, j as i8, k as i8) {
                        r += self.free_area(i, j, k);
                    }
                }
            }
        }
        r
    }

    #[allow(dead_code)]
    fn fill_from(&mut self, x: i8, y: i8, z: i8) {
        if !inb(x, y, z) || self.get(x, y, z) { return; }
        self.set(x as usize, y as usize, z as usize, true);
        for d in DIR {
            self.fill_from(x + d.0, y + d.1, z + d.2);
        }
    }

    #[allow(dead_code)]
    pub fn fill_exterior(&mut self) {
        for i in 0..MAX_LEN {
            for j in 0..MAX_LEN {
                self.fill_from(0 as i8, i as i8, j as i8);
                self.fill_from((MAX_LEN-1) as i8, i as i8, j as i8);
                self.fill_from(i as i8, 0 as i8, j as i8);
                self.fill_from(i as i8, (MAX_LEN-1) as i8, j as i8);
                self.fill_from(i as i8, j as i8, 0 as i8);
                self.fill_from(i as i8, j as i8, (MAX_LEN-1) as i8);
            }
        }
    }
}

Generated with cgit - Back to sebastiano.tronto.net