advent_of_code/2021/6/6_2.py

40 lines
832 B
Python
Raw Normal View History

2021-12-07 04:13:24 +01:00
#!/usr/bin/env python
# 2021 - Advent Of Code - 6 part 2
def parse_file(file):
2022-04-05 13:27:24 +02:00
with open(file, encoding="utf-8") as f:
2021-12-07 04:13:24 +01:00
line = f.readline()
list_state = [int(x) for x in line.split(',')]
state = [0] * 9
2021-12-07 04:13:24 +01:00
for c in range(0, 9):
state[c] = list_state.count(c)
return state
def iterate(state):
new_fish = state[0]
state[0] = state[1]
state[1] = state[2]
state[2] = state[3]
state[3] = state[4]
state[4] = state[5]
state[5] = state[6]
state[6] = state[7] + new_fish
state[7] = state[8]
state[8] = new_fish
# init_state = parse_file('input_example.txt')
init_state = parse_file('input.txt')
print(f'Initial state: {init_state}')
DAYS = 256
for d in range(DAYS):
iterate(init_state)
print(f'There\'s {sum(init_state)} lanternfish after {DAYS} days')