-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.krk
76 lines (69 loc) · 1.87 KB
/
day11.krk
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
#!/usr/bin/env kuroko
'''
Template
'''
import fileio
import kuroko
let data
with fileio.open(kuroko.argv[1] if len(kuroko.argv) > 1 else kuroko.argv[0].replace('.krk','.txt'),'r') as f:
data = f.readlines()
let lines = [line.strip() for line in data]
let octos = [[int(x) for x in line] for line in lines]
def neighbors(x,y):
let out = []
if x > 0:
if y > 0:
out.append((x-1,y-1))
out.append((x-1,y))
if y < len(octos[0]) - 1:
out.append((x-1,y+1))
if y > 0:
out.append((x,y-1))
if y < len(octos[0]) - 1:
out.append((x,y+1))
if x < len(octos) - 1:
if y > 0:
out.append((x+1,y-1))
out.append((x+1,y))
if y < len(octos[0]) - 1:
out.append((x+1,y+1))
return out
def flash(flashed, x, y):
if flashed[x][y]:
return 0
let count = 1
flashed[x][y] = True
for _x, _y in neighbors(x,y):
octos[_x][_y] += 1
if not flashed[_x][_y] and octos[_x][_y] > 9:
count += flash(flashed,_x,_y)
return count
def step_once(step):
let flashed = [[False] * len(lines[0]) for i in range(len(lines))]
for x in range(len(octos)):
for y in range(len(octos[x])):
octos[x][y] += 1
let count = 0
for x in range(len(octos)):
for y in range(len(octos[x])):
if octos[x][y] > 9:
count += flash(flashed,x,y)
for x in range(len(octos)):
for y in range(len(octos[x])):
if flashed[x][y]:
octos[x][y] = 0
#print(count,"flashed on step",step)
if count == 100:
print("everyone flashed on step", step)
return None
return count
let total = 0
let step = 1
while True:
let c = step_once(step)
if c is None:
break
total += c
if step == 100:
print(total,"total on step", step)
step += 1