Skip to content

Commit c896978

Browse files
committed
Completed day 19 of year 2024
1 parent 1f41eff commit c896978

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

2024/19.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from heapq import heappop, heappush
5+
6+
7+
def parse(data):
8+
available, required = data.split('\n\n')
9+
available = available.split(', ')
10+
required = required.split('\n')
11+
12+
return available, required
13+
14+
split_data = parse
15+
completed = True
16+
raw_data = None # Not To be touched
17+
18+
def Astar(design:str, available):
19+
queue = [len(design)] # heuristic, unique number
20+
while len(queue) > 0:
21+
lenRemaining = heappop(queue)
22+
23+
for avail in available:
24+
if not design[-lenRemaining:].startswith(avail): continue
25+
if lenRemaining - len(avail) == 0: return True
26+
heappush(queue, lenRemaining-len(avail))
27+
28+
return False
29+
30+
def part1(data):
31+
available, required = data
32+
33+
cummilation = 0
34+
35+
for design in required:
36+
cummilation += int(Astar(design, available)) # True/False -> 1/0
37+
return cummilation
38+
39+
def possibilityFinder(design:str, available):
40+
queue = {len(design): 1}
41+
42+
for i in range(len(design), 0, -1):
43+
if not queue.get(i): continue
44+
45+
for avail in available:
46+
if not design[-i:].startswith(avail): continue
47+
queue[i-len(avail)] = queue.get(i-len(avail), 0) + queue[i]
48+
49+
return queue.get(0, 0)
50+
51+
def part2(data):
52+
available, required = data
53+
54+
cummilation = 0
55+
56+
for design in required:
57+
cummilation += possibilityFinder(design, available)
58+
return cummilation

0 commit comments

Comments
 (0)