Skip to content

Commit 918507e

Browse files
committed
Completed day 19 of year 2023
1 parent c28ebb3 commit 918507e

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

2023/19.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# -*- coding: utf-8 -*-
33

44
from typing import List, Tuple, Dict
5+
from copy import deepcopy
56
import re
67

78
def parse(data:str) -> Tuple[Dict[str, List], List[Dict]]:
@@ -31,7 +32,7 @@ def parse(data:str) -> Tuple[Dict[str, List], List[Dict]]:
3132
return workflow, partList
3233

3334
split_data = parse
34-
completed = 1
35+
completed = True
3536
raw_data = None # Not To be touched
3637

3738
def part1(data:Tuple[Dict[str, List], List[Dict]]):
@@ -59,7 +60,53 @@ def isAccepted(part, workflowName):
5960

6061
return acc
6162

63+
def part2(data:List[str]):
64+
workflow = data[0] # We don't need the second thing!
65+
66+
queue = [{'x': [1, 4000], 'm': [1, 4000], 'a': [1, 4000], 's': [1, 4000], 'workflow': 'in'}] # I am not writting the entire obj (︶^︶)
67+
accepted = 0
6268

69+
while queue:
70+
item = queue.pop(0)
71+
if item['workflow'] == 'A':
72+
# numpy.prod doesn't work for odd reasons!
73+
processed = [item[k][1] - item[k][0] + 1 for k in 'xmas']
74+
accepted += processed[0] * processed[1] * processed[2] * processed[3]
75+
continue
76+
elif item['workflow'] == 'R':
77+
continue
78+
rules = workflow[item['workflow']]
6379

64-
def part2(data:List[str]):
65-
...
80+
for rule in rules:
81+
# There are three possibility!
82+
# 1. Its > and entire thing is above. In which case, break and append the entire thing back with the different workflow
83+
# 2. Its < and entire thing is below. In which case, break and append the entire thing back with the different workflow
84+
# 3. The value is in the middle of the min and max. In which case, we edit the item to that what didn't match and continue
85+
# 3.a We take the part which matched and add to the queue
86+
# 3.b We edit the item currently under inspection to the condition that would fail the rule
87+
88+
if rule['isLast']:
89+
queue.append({**item, 'workflow': rule['action']}) # Last rule
90+
continue
91+
92+
minN, maxN = item[rule['key']]
93+
if rule['operator'] == '>' and minN > rule['value']:
94+
queue.append({**item, 'workflow': rule['action']}) # The entire item works
95+
break
96+
elif rule['operator'] == '<' and maxN < rule['value']:
97+
queue.append({**item, 'workflow': rule['action']}) # The entire item works
98+
break
99+
elif rule['operator'] == '>' and maxN > rule['value'] and minN <= rule['value']: # Some of the item works
100+
newItem = deepcopy(item)
101+
newItem['workflow'] = rule['action']
102+
newItem[rule['key']][0] = rule['value'] + 1
103+
queue.append(newItem)
104+
item[rule['key']][1] = rule['value']
105+
elif rule['operator'] == '<' and minN < rule['value'] and maxN >= rule['value']: # The entire item works
106+
newItem = deepcopy(item)
107+
newItem['workflow'] = rule['action']
108+
newItem[rule['key']][1] = rule['value'] - 1
109+
queue.append(newItem)
110+
item[rule['key']][0] = rule['value']
111+
112+
return accepted

0 commit comments

Comments
 (0)