Skip to content

Commit 566cd14

Browse files
committed
Completed day 17 of year 2015
1 parent 984704d commit 566cd14

1 file changed

Lines changed: 38 additions & 17 deletions

File tree

2015/17.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,47 @@
11
split_data = True
2-
completed = False
2+
completed = True
33
raw_data = None # Not To be touched
44

5-
coms = 0
6-
7-
def loop(canfit, containers, listing=[]):
8-
global coms
9-
for container in containers:
10-
if container == canfit:
11-
print([*listing, container])
12-
coms += 1
13-
elif container < canfit:
14-
list2 = containers.copy()
15-
list2.remove(container)
16-
loop(canfit - container, list2, [*listing, container])
5+
from functools import cache
176

187
def part1(data):
198
"""The Code is supposed to run here"""
20-
containers = [int(container) for container in data]
21-
loop(25, containers)
22-
return coms
9+
containers = sorted([int(container) for container in data], reverse=True)
10+
11+
@cache
12+
def try_fitting(remaining:int, index:int):
13+
if remaining == 0:
14+
return 1
15+
elif remaining < 0 or index >= len(containers):
16+
return 0 # We ran out of containers and still didn't achieve our goals
17+
18+
use_it = try_fitting(remaining - containers[index], index+1) # We either use this container
19+
no_use = try_fitting(remaining, index+1) # or we don't use it.
20+
21+
# If the current container consumes too much, then it will automaticlly return 0
22+
return use_it + no_use
23+
24+
return try_fitting(150, 0)
2325

2426
def part2(data):
2527
"""The Code is supposed to run here"""
26-
containers = [int(container) for container in data]
28+
containers = [int(container) for container in data]
29+
30+
@cache
31+
def try_fitting(remaining:int, index:int, counter:int):
32+
if remaining == 0:
33+
return counter, 1
34+
elif remaining < 0 or index >= len(containers):
35+
return float('inf'), 0 # We ran out of containers and still didn't achieve our goals
36+
37+
use_it = try_fitting(remaining - containers[index], index+1, counter+1) # We either use this container
38+
no_use = try_fitting(remaining, index+1, counter) # or we don't use it.
39+
40+
if use_it[0] < no_use[0]:
41+
return use_it
42+
elif use_it[0] > no_use[0]:
43+
return no_use
44+
else:
45+
return use_it[0], use_it[1] + no_use[1]
46+
47+
return try_fitting(150, 0, 0)[1]

0 commit comments

Comments
 (0)