Skip to content

Commit ba971d9

Browse files
committed
day06
1 parent 7917fc0 commit ba971d9

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

2021/06/__init__.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
def cast_input(inputs):
2+
return [
3+
int(string)
4+
for string in inputs.split(',')
5+
]
6+
7+
8+
class Lanternfish:
9+
def __init__(self, time=8):
10+
self.time = time
11+
12+
def count_down(self):
13+
if self.time:
14+
self.time -= 1
15+
return
16+
self.time = 6
17+
return True # spawn
18+
19+
def __repr__(self):
20+
return f'{self.time}'
21+
22+
23+
def naive_oo_part1(inputs):
24+
days = 80
25+
fishes = [Lanternfish(x) for x in inputs]
26+
for _ in range(days):
27+
new_fishes = []
28+
for fish in fishes:
29+
if fish.count_down():
30+
new_fishes.append(Lanternfish())
31+
fishes.extend(new_fishes)
32+
res = len(fishes)
33+
raise RuntimeError(f'{res}, slow')
34+
35+
36+
def cache_recur_part1(inputs, days=80):
37+
cache = {}
38+
39+
def f(time, day):
40+
if (time, day) in cache:
41+
return cache[(time, day)]
42+
if not day > 0:
43+
res = 1
44+
elif time == 0:
45+
res = f(6, day-1) + f(8, day-1)
46+
else:
47+
res = f(time-1, day-1)
48+
cache[(time, day)] = res
49+
return res
50+
return sum(
51+
f(x, days) for x in inputs
52+
)
53+
54+
55+
def cache_recur_part2(inputs):
56+
return cache_recur_part1(inputs, 256)

2021/06/input

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4,2,4,1,5,1,2,2,4,1,1,2,2,2,4,4,1,2,1,1,4,1,2,1,2,2,2,2,5,2,2,3,1,4,4,4,1,2,3,4,4,5,4,3,5,1,2,5,1,1,5,5,1,4,4,5,1,3,1,4,5,5,5,4,1,2,3,4,2,1,2,1,2,2,1,5,5,1,1,1,1,5,2,2,2,4,2,4,2,4,2,1,2,1,2,4,2,4,1,3,5,5,2,4,4,2,2,2,2,3,3,2,1,1,1,1,4,3,2,5,4,3,5,3,1,5,5,2,4,1,1,2,1,3,5,1,5,3,1,3,1,4,5,1,1,3,2,1,1,1,5,2,1,2,4,2,3,3,2,3,5,1,5,1,2,1,5,2,4,1,2,4,4,1,5,1,1,5,2,2,5,5,3,1,2,2,1,1,4,1,5,4,5,5,2,2,1,1,2,5,4,3,2,2,5,4,2,5,4,4,2,3,1,1,1,5,5,4,5,3,2,5,3,4,5,1,4,1,1,3,4,4,1,1,5,1,4,1,2,1,4,1,1,3,1,5,2,5,1,5,2,5,2,5,4,1,1,4,4,2,3,1,5,2,5,1,5,2,1,1,1,2,1,1,1,4,4,5,4,4,1,4,2,2,2,5,3,2,4,4,5,5,1,1,1,1,3,1,2,1

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy