aoc

ref: 2d089d12959983e13751c8662655aedc025dd684

008/main.py


 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
W = 25
H = 6


def get_layers(data):
    layers = []

    while len(data):
        layers.append(data[:W * H])
        data = data[W * H:]

    return layers


def main():
    data = open('input').read()
    layers = get_layers(data)
    x = {}

    for layer in layers:
        assert len(layer) == W * H
        zeros = filter(lambda x: x == '0', layer)
        x[len(list(zeros))] = layer

    k = list(x.keys())
    k.sort()

    smallest = x[k[0]]
    print(smallest)

    ones = len(list(filter(lambda x: x == '1', smallest)))
    twos = len(list(filter(lambda x: x == '2', smallest)))

    print(ones * twos)


if __name__ == '__main__':
    main()