-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht18.py
More file actions
128 lines (90 loc) · 3.21 KB
/
t18.py
File metadata and controls
128 lines (90 loc) · 3.21 KB
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
#!python3
import sys
from attr import attrs, attrib
from collections import defaultdict, Counter
@attrs
class Lumber_collector:
field = attrib(default=defaultdict(lambda: '.'))
def print_field(self):
# print(self.field.items())
prev_x = 0
row = ''
for x, y in self.field:
if x != prev_x:
prev_x = x
print(row)
row = ''
row += self.field[x, y]
print(row, '\n')
return 0
def load_field(self, inp):
inp = open(sys.argv[1]).read().split('\n')
data = list(map(str, inp))
# first load the input to a grid
for i in (range(len(data))):
for j in (range(len(data[i]))):
self.field[i, j] = data[i][j]
return 1
def print_result(self):
trees = len([square for square in self.field
if self.field[square] in ['|']])
lumbers = len([square for square in self.field
if self.field[square] in ['#']])
print('trees', trees, 'lumbers', lumbers, 'total', trees*lumbers)
def run_round(self):
field_c = dict(self.field)
for x, y in field_c:
# open acre
tree_count = 0
lumber_count = 0
for a in [-1, 0, 1]:
for b in [-1, 0, 1]:
if a == b == 0:
continue
if field_c.get((x+a, y+b), '') == '|':
tree_count += 1
if field_c.get((x+a, y+b), '') == '#':
lumber_count += 1
if field_c[x, y] == '.' and tree_count >= 3:
self.field[x, y] = '|'
if field_c[x, y] == '|' and lumber_count >= 3:
self.field[x, y] = '#'
if field_c[x, y] == '#':
if lumber_count < 1 or tree_count < 1:
self.field[x, y] = '.'
return 1
def main(self):
assert len(sys.argv) == 2
self.load_field(sys.argv[1])
results = dict()
seen_again = None
first_repeated = None
loop = []
for i in range(1, 1_000_000_000):
# if i%1000 == 0:
# print('Round', i)
self.run_round()
if i == 10:
print("Round 1 :")
self.print_result()
if seen_again and tuple(self.field.items()) == tuple(first_repeated):
print('Seen again at ', i)
self.print_field()
index = (1_000_000_000 - seen_again) % len(loop)
print('index', index)
print('Result 2 :', loop[index])
break
if results.get(tuple(self.field.items()), None):
if not seen_again:
print("Found at i", i)
seen_again = i
first_repeated = tuple(self.field.items())
else:
results[tuple(self.field.items())] = i
if seen_again:
c = Counter(self.field.values())
r = c['#'] * c["|"]
loop.append(r)
if __name__ == '__main__':
lc = Lumber_collector()
lc.main()