kill me now
This commit is contained in:
parent
09bdd6d669
commit
deb74bc8f9
60
2018/12.py
60
2018/12.py
@ -1,3 +1,6 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
def to_int(bitlist):
|
def to_int(bitlist):
|
||||||
out = 0
|
out = 0
|
||||||
for bit in bitlist:
|
for bit in bitlist:
|
||||||
@ -5,37 +8,42 @@ def to_int(bitlist):
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
def get_slice(index, pots):
|
def grow(init, num_gens, progress=False):
|
||||||
for i in range(index - 2, index + 3):
|
first, last = min(init), max(init)
|
||||||
if i < 0:
|
current_gen = set(init)
|
||||||
yield 0
|
|
||||||
yield pots[i]
|
|
||||||
|
|
||||||
|
|
||||||
def grow(init, num_gens):
|
|
||||||
current_gen = init
|
|
||||||
for g in range(num_gens):
|
for g in range(num_gens):
|
||||||
while current_gen[:2] != [0, 0]:
|
next_gen = set()
|
||||||
current_gen.insert(0, 0)
|
for i in range(first - 2, last + 3):
|
||||||
while current_gen[-2:] != [0, 0]:
|
key = to_int(1 if b in current_gen else 0 for b in range(i - 2, i + 3))
|
||||||
current_gen.append(0)
|
|
||||||
|
|
||||||
next_gen = []
|
|
||||||
for i, b in enumerate(current_gen):
|
|
||||||
key = to_int(get_slice(i, current_gen))
|
|
||||||
if key in positives:
|
if key in positives:
|
||||||
next_gen.append(1)
|
next_gen.add(i)
|
||||||
else:
|
if i < first:
|
||||||
next_gen.append(0)
|
first = i
|
||||||
|
if i > last:
|
||||||
|
last = i
|
||||||
current_gen = next_gen
|
current_gen = next_gen
|
||||||
|
|
||||||
|
if progress:
|
||||||
|
print('Generations:', g + 1, end='\r')
|
||||||
|
|
||||||
|
if progress: print('Generations:', g + 1)
|
||||||
return current_gen
|
return current_gen
|
||||||
|
|
||||||
|
|
||||||
|
def test(n):
|
||||||
|
pots = grow(initial, n)
|
||||||
|
first, last = min(pots), max(pots)
|
||||||
|
chars = []
|
||||||
|
for i in range(first - 1, last + 2):
|
||||||
|
chars.append('#' if i in pots else '.')
|
||||||
|
print(''.join(chars))
|
||||||
|
|
||||||
|
|
||||||
with open('data/12.txt') as f:
|
with open('data/12.txt') as f:
|
||||||
initial = list(next(f)[15:-1])
|
initial = re.search('[#.]+', next(f)).group(0)
|
||||||
initial = list(map(lambda p: int(p == '#'), initial))
|
initial = {i for i, p in enumerate(initial) if p == '#'}
|
||||||
next(f)
|
next(f)
|
||||||
|
|
||||||
positives, negatives = set(), set()
|
positives, negatives = set(), set()
|
||||||
for line in f:
|
for line in f:
|
||||||
state, result = line.strip().split(' => ')
|
state, result = line.strip().split(' => ')
|
||||||
@ -46,7 +54,9 @@ with open('data/12.txt') as f:
|
|||||||
negatives.add(s)
|
negatives.add(s)
|
||||||
|
|
||||||
|
|
||||||
def test(n):
|
pots = grow(initial, 20)
|
||||||
result = grow(initial, n)
|
print(f'Part 1: {sum(pots)}\n')
|
||||||
print(''.join('#' if p else '.' for p in result))
|
|
||||||
|
pots = grow(initial, 5 * 10 ** 10, True)
|
||||||
|
print(f'Part 2: {pots}\n')
|
||||||
|
|
||||||
|
16
2018/data/test.txt
Normal file
16
2018/data/test.txt
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
initial state: #..#.#..##......###...###
|
||||||
|
|
||||||
|
...## => #
|
||||||
|
..#.. => #
|
||||||
|
.#... => #
|
||||||
|
.#.#. => #
|
||||||
|
.#.## => #
|
||||||
|
.##.. => #
|
||||||
|
.#### => #
|
||||||
|
#.#.# => #
|
||||||
|
#.### => #
|
||||||
|
##.#. => #
|
||||||
|
##.## => #
|
||||||
|
###.. => #
|
||||||
|
###.# => #
|
||||||
|
####. => #
|
Loading…
x
Reference in New Issue
Block a user