Day 11 - Take two
This commit is contained in:
parent
e348f7077f
commit
97e4d5284c
4 changed files with 95 additions and 4 deletions
|
@ -29,6 +29,25 @@ class PuzzleSolver:
|
||||||
|
|
||||||
return n
|
return n
|
||||||
|
|
||||||
|
def occupied_in_sight(self, ix, iy):
|
||||||
|
directions = [( 1, -1), ( 1, 0), ( 1, 1), ( 0, 1), (-1, 1), (-1, 0), (-1, -1), ( 0, -1)]
|
||||||
|
n = 0
|
||||||
|
for dx, dy in directions:
|
||||||
|
x, y = ix, iy
|
||||||
|
x += dx
|
||||||
|
y += dy
|
||||||
|
while 0 <= y < self.height and 0 <= x < self.width:
|
||||||
|
s = self.get(x, y)
|
||||||
|
if s == "#":
|
||||||
|
n += 1
|
||||||
|
break
|
||||||
|
if s == "L":
|
||||||
|
break
|
||||||
|
x += dx
|
||||||
|
y += dy
|
||||||
|
|
||||||
|
return n
|
||||||
|
|
||||||
def get(self, x, y):
|
def get(self, x, y):
|
||||||
return self.data[y][x]
|
return self.data[y][x]
|
||||||
|
|
||||||
|
@ -50,11 +69,43 @@ class PuzzleSolver:
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
def mutate_in_sight(self):
|
||||||
|
ret = copy.deepcopy(self.data)
|
||||||
|
for y in range(self.height):
|
||||||
|
for x in range(self.width):
|
||||||
|
s = self.get(x, y)
|
||||||
|
if s == ".":
|
||||||
|
continue
|
||||||
|
|
||||||
|
n = self.occupied_in_sight(x, y)
|
||||||
|
if s == "L":
|
||||||
|
if n == 0:
|
||||||
|
ret[y][x] = "#"
|
||||||
|
elif s == "#":
|
||||||
|
if n >= 5:
|
||||||
|
ret[y][x] = "L"
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
def print(self):
|
def print(self):
|
||||||
for y in range(self.height):
|
for y in range(self.height):
|
||||||
print("".join(self.data[y]))
|
print("".join(self.data[y]))
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
def solve_in_sight(self):
|
||||||
|
stable = False
|
||||||
|
rounds = 0
|
||||||
|
self.print()
|
||||||
|
while not stable:
|
||||||
|
x = self.mutate_in_sight()
|
||||||
|
stable = x == self.data
|
||||||
|
self.data = x
|
||||||
|
rounds += 1
|
||||||
|
|
||||||
|
self.print()
|
||||||
|
|
||||||
|
return rounds
|
||||||
|
|
||||||
def solve(self):
|
def solve(self):
|
||||||
stable = False
|
stable = False
|
||||||
rounds = 0
|
rounds = 0
|
||||||
|
@ -73,12 +124,33 @@ class PuzzleSolver:
|
||||||
return sum([x.count("#") for x in self.data])
|
return sum([x.count("#") for x in self.data])
|
||||||
|
|
||||||
|
|
||||||
|
#ps = PuzzleSolver("test.txt")
|
||||||
|
#rounds = ps.solve()
|
||||||
|
#cs = PuzzleSolver("test-1.txt")
|
||||||
|
#assert ps.data == cs.data
|
||||||
|
#
|
||||||
|
#ps = PuzzleSolver("data.txt")
|
||||||
|
#rounds = ps.solve()
|
||||||
|
#n = ps.count_occupied()
|
||||||
|
#print(f"There are {n} occupied seats.")
|
||||||
|
|
||||||
|
ps = PuzzleSolver("test-2.txt")
|
||||||
|
assert ps.get(3, 4) == "L"
|
||||||
|
assert ps.occupied_in_sight(3, 4) == 8
|
||||||
|
|
||||||
|
ps = PuzzleSolver("test-3.txt")
|
||||||
|
assert ps.get(3, 3) == "L"
|
||||||
|
assert ps.occupied_in_sight(3, 3) == 0
|
||||||
|
|
||||||
|
ps = PuzzleSolver("test-4.txt")
|
||||||
|
assert ps.get(1, 1) == "L"
|
||||||
|
assert ps.occupied_in_sight(1, 1) == 0
|
||||||
|
|
||||||
ps = PuzzleSolver("test.txt")
|
ps = PuzzleSolver("test.txt")
|
||||||
rounds = ps.solve()
|
ps.solve_in_sight()
|
||||||
cs = PuzzleSolver("test-1.txt")
|
assert ps.count_occupied() == 26
|
||||||
assert ps.data == cs.data
|
|
||||||
|
|
||||||
ps = PuzzleSolver("data.txt")
|
ps = PuzzleSolver("data.txt")
|
||||||
rounds = ps.solve()
|
ps.solve_in_sight()
|
||||||
n = ps.count_occupied()
|
n = ps.count_occupied()
|
||||||
print(f"There are {n} occupied seats.")
|
print(f"There are {n} occupied seats.")
|
||||||
|
|
9
2020/11/test-2.txt
Normal file
9
2020/11/test-2.txt
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
.......#.
|
||||||
|
...#.....
|
||||||
|
.#.......
|
||||||
|
.........
|
||||||
|
..#L....#
|
||||||
|
....#....
|
||||||
|
.........
|
||||||
|
#........
|
||||||
|
...#.....
|
7
2020/11/test-3.txt
Normal file
7
2020/11/test-3.txt
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
.##.##.
|
||||||
|
#.#.#.#
|
||||||
|
##...##
|
||||||
|
...L...
|
||||||
|
##...##
|
||||||
|
#.#.#.#
|
||||||
|
.##.##.
|
3
2020/11/test-4.txt
Normal file
3
2020/11/test-4.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
.............
|
||||||
|
.L.L.#.#.#.#.
|
||||||
|
.............
|
Loading…
Reference in a new issue