Created
December 11, 2020 14:47
-
-
Save kung-foo/027cd7c087916a609d1d6472e22fa3df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import sys | |
import random | |
from itertools import combinations_with_replacement, permutations, product | |
src = open("input.txt", "r").readlines() | |
src2 = """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""".splitlines() | |
src = [r.strip() for r in src] | |
directions = list(product([-1, 0, 1], repeat=2)) | |
directions.remove((0,0)) | |
def blank(): | |
return [["." for _ in src[0]] for _ in src] | |
def print_boat(boat): | |
for y in range(len(boat)): | |
for x in range(len(boat[y])): | |
print(boat[y][x], end="") | |
print() | |
mx = len(src[0]) | |
my = len(src) | |
def adj(boat, x, y, sight=1): | |
a = 0 | |
for d in directions: | |
for s in range(1, sight + 1): | |
nx = x + s * d[0] | |
ny = y + s * d[1] | |
if nx < 0 or nx >= mx: | |
break | |
if ny < 0 or ny >= my: | |
break | |
if boat[ny][nx] == "L": | |
break | |
if boat[ny][nx] == "#": | |
a += 1 | |
break | |
return a | |
def life(boat, visible, sight=1): | |
boat2 = blank() | |
for y in range(len(boat)): | |
for x in range(len(boat[y])): | |
c = boat[y][x] | |
if c == ".": | |
continue | |
o = adj(boat, x, y, sight) | |
if c == "L" and o == 0: | |
boat2[y][x] = "#" | |
elif c == "#" and o >= visible: | |
boat2[y][x] = "L" | |
else: | |
boat2[y][x] = c | |
return boat2 | |
c = 0 | |
boat = src | |
while True: | |
new_boat = life(boat, 5, len(src)) | |
if new_boat == boat: | |
break | |
boat = new_boat | |
c += 1 | |
print(c) | |
o = 0 | |
for y in range(len(new_boat)): | |
for x in range(len(new_boat[y])): | |
if new_boat[y][x] == "#": | |
o += 1 | |
print(o) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment