-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
191 lines (151 loc) · 4.51 KB
/
run.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import os
# change to dir of script
os.chdir(os.path.dirname(os.path.abspath(__file__)))
try:
with open("input.txt") as f:
data = f.read() # entire file as string
lines = data.splitlines()
except:
print("no input.txt")
data, lines = "", []
line_groups = data.split("\n\n") # lines split by double newlines
print(lines)
print(len(lines), "lines in input.txt")
def ans(answer):
# store answer to clipboard
print(answer, "| in clipboard")
os.system(f'echo "{answer}"| xclip -selection clipboard -in')
L, I, D, S = list, int, dict, set
P, E, R, M = print, enumerate, range, map
## end of boilerplate ##
SL = lambda arr: list(sorted(arr))
def line_transform(line):
# split = [line.split() for line in lines]
# return int(line)
return [c for c in line]
lines = [line_transform(line) for line in lines] # apply line_transform to each line
# . floor
# L empty
# # occuipied
import copy
seats = lines
seats_part2 = copy.deepcopy(lines)
def adjacent(_seats, x, y):
adj = 0
for dy in [-1, 0, 1]:
for dx in [-1, 0, 1]:
if dx == 0 and dy == 0:
continue
yy = y + dy
xx = x + dx
if yy < 0 or xx < 0:
continue
try:
if _seats[y + dy][x + dx] == "#":
adj += 1
except:
# print("bads ", y + dy, x + dx)
pass
return adj
import os
import png
def save_as_png(arrs, idx, folder):
trans = {"L": 0, ".": 150, "#": 255}
_arrs = copy.deepcopy(arrs)
for y in range(len(arrs)):
for x in range(len(arrs[0])):
_arrs[y][x] = trans[arrs[y][x]]
if not os.path.exists(folder):
os.mkdir(folder)
outfile = os.path.join(folder, str(idx).zfill(5) + ".png")
png.from_array(_arrs, "L").save(outfile)
# print([[sub[x]for x in (0,7)] for sub in l ])
def nice_print(arrs):
trans = str.maketrans("L#.", "□■ ")
ls = ["".join(line).translate(trans) for line in arrs]
print("\n".join(ls))
def occd(seats):
tot = 0
for y in range(len(seats)):
for x in range(len(seats[0])):
if seats[y][x] == "#":
tot += 1
return tot
def eq(s1, s2):
for y in range(len(seats)):
for x in range(len(seats[0])):
if s1[y][x] != s2[y][x]:
return False
return True
def round(_seats):
nu_seats = copy.deepcopy(_seats)
for y in range(len(_seats)):
for x in range(len(_seats[0])):
# print(adjacent(seats, x, y))
adj = adjacent(_seats, x, y)
cur = _seats[y][x]
if cur == ".":
continue
if cur == "L" and adj == 0:
nu_seats[y][x] = "#"
if cur == "#" and adj >= 4:
nu_seats[y][x] = "L"
return nu_seats
last = occd(seats)
for i in range(5000):
nice_print(seats)
save_as_png(seats, i, "pt1_frames")
nu_seats = round(seats)
if eq(nu_seats, seats):
ans(occd(nu_seats)) # 2261
input("continue to pt 2?")
break
seats = nu_seats
## part 2
def visible(_seats, x, y):
adj = 0
for dy in [-1, 0, 1]:
for dx in [-1, 0, 1]:
if dx == 0 and dy == 0:
continue
step = 1
yy = y + dy * step
xx = x + dx * step
seen = False
while yy in range(len(_seats)) and xx in range(len(_seats[0])) and not seen:
if _seats[yy][xx] == "#":
adj += 1
seen = True
break
elif _seats[yy][xx] == "L":
seen = True
break
step += 1
yy = y + dy * step
xx = x + dx * step
return adj
def round2(_seats):
nu_seats = copy.deepcopy(_seats)
for y in range(len(_seats)):
for x in range(len(_seats[0])):
# print(adjacent(seats, x, y))
adj = visible(_seats, x, y)
cur = _seats[y][x]
if cur == ".":
continue
if cur == "L" and adj == 0:
nu_seats[y][x] = "#"
if cur == "#" and adj >= 5:
nu_seats[y][x] = "L"
return nu_seats
seats = seats_part2
last = occd(seats)
for i in range(500000000):
save_as_png(seats, i, "pt2_frames")
nice_print(seats)
print()
nu_seats = round2(seats)
if eq(nu_seats, seats):
ans(occd(nu_seats)) # 2039
exit()
seats = nu_seats