Complete day 11

This commit is contained in:
Peter J. Holzer 2020-12-13 20:58:10 +01:00 committed by Peter J. Holzer
parent 164b77d657
commit 784bcafbd4
4 changed files with 243 additions and 0 deletions

98
11/input Normal file
View File

@ -0,0 +1,98 @@
LLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLL.LLLLL.LLLL.LL.L.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLL
LLLLLLLLLL.LLLLLL.L.LLLLLLL.LLLLL.LLLLL.LLLL.LLLLLLLLLLLLL..LLLLLLLLLLLLLLLL.LLLLL.LLLLLLLL
LLLLLLLLLLLLLLLLLLLLLLLLLLL..L.LLLLLLLLLLLLLLL.LL.LLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLL.LLLLL
LLLLLLLLLL.LL..LLLL.LLLLLLL.LLLLLLLLLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLL.LLLLL.LLLLLLLL
LLLLLLLLLL.LLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLL..LLLLLLLL
LL.L..L.L..L.LL..L.....LLLL..L.L.LL..L...LLLLLL...L.....L.....LL.L...LLLLL..L.LL..L..L.LL..
LLLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLL.LLLLL.LLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLL
LLLLLLLLLL..LLLLLLL.LLLLLLL.LLLLL.L.LLL.LLLL.LLLL.LLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLL
L.LLLLLLLLLL.LLL..LLLLLLLLL.LLLLL.LLLLL..LLLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLLL.LLLL..LLLLLLLL
LLLLLLLLLL.LLLLLLLLLLLLLLL..LLLLLLLLLLL.L.LL.LLLL.LLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLL.LLLLLLLL
LLLLLLLL.L.LL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LL
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLL.LLLL.LLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLL.LLLLLLLL
LLLLLLLL.L.LLLLLLLL.LLLLLLL.L.LLL.LLLLL.LLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLL.LLLLL.LLLLLLLL
LLLLLLLLLL.LLL.LLLL.LLLLLLL.LL.LL.LLLLL.LLLL.LLLL.LLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLL.
LLLLLLLLLL.LLLLLLL..LLLLLLL.LLLLL.LLLLL.LLLLLLLLL.LLL.LLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLLLLL
LLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLL.LLLLL.LLL..LLLL.LLLLLLL.L.LLLLLLLLL..LLLLLLLLLLLLLLLLLLL.

10
11/input.example Normal file
View File

@ -0,0 +1,10 @@
L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL

61
11/part1 Executable file
View File

@ -0,0 +1,61 @@
#!/usr/bin/python3
from pprint import pprint
def read_area():
area = []
with open("input") as fh:
for ln in fh:
area.append(list(ln.strip()))
return area
def neighbours(area, i, j):
count = 0
for i1 in (-1, 0, 1):
if i + i1 < 0:
continue
if i + i1 >= len(area):
continue
for j1 in (-1, 0, 1):
if i1 == j1 == 0:
continue
if j + j1 < 0:
continue
if j + j1 >= len(area[i + i1]):
continue
count += area[i + i1][j + j1] == "#"
return count
def new_occ(area, i, j):
if area[i][j] == ",":
return "."
if area[i][j] == "L" and neighbours(area, i, j) == 0:
return "#"
if area[i][j] == "#" and neighbours(area, i, j) >= 4:
return "L"
return area[i][j]
def next_tick(area):
new_area = []
for i in range(len(area)):
new_row = []
for j in range(len(area[i])):
new_row.append(new_occ(area, i, j))
new_area.append(new_row)
return new_area
def count_occupied(area):
sum = 0
for row in area:
for seat in row:
sum += seat == "#"
return sum
area = read_area()
while True:
new_area = next_tick(area)
if new_area == area:
break
area = new_area
print(count_occupied(area))

74
11/part2 Executable file
View File

@ -0,0 +1,74 @@
#!/usr/bin/python3
from pprint import pprint
def read_area():
area = []
with open("input") as fh:
for ln in fh:
area.append(list(ln.strip()))
return area
def neighbours(area, i, j):
count = 0
for i1 in (-1, 0, 1):
if i + i1 < 0:
continue
if i + i1 >= len(area):
continue
for j1 in (-1, 0, 1):
if i1 == j1 == 0:
continue
if j + j1 < 0:
continue
if j + j1 >= len(area[i + i1]):
continue
n = 1
while True:
i2 = i + n * i1
if not (0 <= i2 < len(area)):
break
j2 = j + n * j1
if not (0 <= j2 < len(area[i2])):
break
if area[i2][j2] == "L":
break
if area[i2][j2] == "#":
count += 1
break
n += 1
return count
def new_occ(area, i, j):
if area[i][j] == ",":
return "."
if area[i][j] == "L" and neighbours(area, i, j) == 0:
return "#"
if area[i][j] == "#" and neighbours(area, i, j) >= 5:
return "L"
return area[i][j]
def next_tick(area):
new_area = []
for i in range(len(area)):
new_row = []
for j in range(len(area[i])):
new_row.append(new_occ(area, i, j))
new_area.append(new_row)
return new_area
def count_occupied(area):
sum = 0
for row in area:
for seat in row:
sum += seat == "#"
return sum
area = read_area()
while True:
new_area = next_tick(area)
if new_area == area:
break
area = new_area
print(count_occupied(area))