This repository was archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpart1.py
75 lines (54 loc) · 1.55 KB
/
part1.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
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
import argparse
import collections
import os.path
from typing import Counter
from typing import Tuple
import pytest
from support import timing
INPUT_TXT = os.path.join(os.path.dirname(__file__), 'input.txt')
def compute(s: str) -> int:
space = {}
for y, line in enumerate(s.splitlines()):
for x, c in enumerate(line):
if c == '#':
space[(0, y, x)] = c
for _ in range(6):
marked: Counter[Tuple[int, int, int]] = collections.Counter()
for (z, y, x), c in space.items():
for z_i in (-1, 0, 1):
for y_i in (-1, 0, 1):
for x_i in (-1, 0, 1):
if z_i == y_i == x_i == 0:
continue
marked[(z + z_i, y + y_i, x + x_i)] += 1
new_space = {}
for k, v in marked.items():
if v == 3:
new_space[k] = '#'
for k in space:
if marked[k] in {2, 3}:
new_space[k] = '#'
space = new_space
return len(space)
INPUT_S = '''\
.#.
..#
###
'''
@pytest.mark.parametrize(
('input_s', 'expected'),
(
(INPUT_S, 112),
),
)
def test(input_s: str, expected: int) -> None:
assert compute(input_s) == expected
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument('data_file', nargs='?', default=INPUT_TXT)
args = parser.parse_args()
with open(args.data_file) as f, timing():
print(compute(f.read()))
return 0
if __name__ == '__main__':
exit(main())