Solve 2016:18 p1-2 "Like a Rogue"
Figured out that the center position did not matter, as long as left hand side tile and right hand side tile on previous row are not equal. Also tried to find a recurring pattern to speed p2 up a bit, but it seems it does not have a recurring pattern in the 400 000s first rows.
This commit is contained in:
parent
ffc5088813
commit
ae942ce803
1 changed files with 32 additions and 0 deletions
32
2016-python2/output/day_18.py
Normal file
32
2016-python2/output/day_18.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
def solve(data):
|
||||
cols = len(data)
|
||||
prevrow = [t == "." for t in data]
|
||||
p1 = sum(prevrow) + sum(
|
||||
sum(prevrow := [issafe(prevrow, i) for i in range(cols)]) for _ in range(39)
|
||||
)
|
||||
p2 = p1 + sum(
|
||||
sum(prevrow := [issafe(prevrow, i) for i in range(cols)])
|
||||
for _ in range(400_000 - 40)
|
||||
)
|
||||
return p1, p2
|
||||
|
||||
|
||||
def issafe(row, i):
|
||||
match i:
|
||||
case 0:
|
||||
return row[1]
|
||||
case n if n == len(row) - 1:
|
||||
return row[-2]
|
||||
case _:
|
||||
lt, rt = row[i - 1], row[i + 1]
|
||||
return not lt != rt
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("./input/18.txt", "r") as f:
|
||||
inp = f.read().strip()
|
||||
|
||||
p1, p2 = solve(inp)
|
||||
|
||||
print(p1)
|
||||
print(p2)
|
||||
Loading…
Add table
Reference in a new issue