Add solution for day 11
This commit is contained in:
parent
c87daab18b
commit
00bc105ac3
1 changed files with 55 additions and 0 deletions
55
src/day11.py
Normal file
55
src/day11.py
Normal file
|
@ -0,0 +1,55 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from util import print_color, read_input, Color
|
||||
|
||||
|
||||
def update_stone(stone):
|
||||
result = []
|
||||
stone_str = str(stone)
|
||||
if stone == 0:
|
||||
result.append(1)
|
||||
elif len(stone_str) % 2 == 0:
|
||||
mid = int(len(stone_str) / 2)
|
||||
result.append(int(stone_str[:mid]))
|
||||
result.append(int(stone_str[mid:]))
|
||||
else:
|
||||
result.append(stone * 2024)
|
||||
return result
|
||||
|
||||
|
||||
def blink(times, stone, cache):
|
||||
if times == 0:
|
||||
return 1
|
||||
if stone not in cache:
|
||||
cache[stone] = {}
|
||||
if times not in cache[stone]:
|
||||
cache[stone][times] = sum(map(lambda x: blink(times - 1, x, cache), update_stone(stone)))
|
||||
return cache[stone][times]
|
||||
|
||||
|
||||
def count_stones(line, blinks):
|
||||
stones = list(map(lambda x: int(x), line.split(' ')))
|
||||
stone_count = 0
|
||||
cache = {}
|
||||
for stone in stones:
|
||||
stone_count += blink(blinks, stone, cache)
|
||||
return stone_count
|
||||
|
||||
|
||||
def part1(input_lines):
|
||||
return count_stones(input_lines[0], 25)
|
||||
|
||||
|
||||
def part2(input_lines):
|
||||
return count_stones(input_lines[0], 75)
|
||||
|
||||
|
||||
test_input = read_input("_test")
|
||||
test_input_result = part1(test_input)
|
||||
print_color(test_input_result, Color.GREY)
|
||||
assert 55312 == test_input_result
|
||||
|
||||
real_input = read_input()
|
||||
print(part1(real_input))
|
||||
|
||||
print(part2(real_input))
|
Loading…
Reference in a new issue