2
2
# -*- coding: utf-8 -*-
3
3
4
4
from typing import List , Tuple , Dict
5
+ from copy import deepcopy
5
6
import re
6
7
7
8
def parse (data :str ) -> Tuple [Dict [str , List ], List [Dict ]]:
@@ -31,7 +32,7 @@ def parse(data:str) -> Tuple[Dict[str, List], List[Dict]]:
31
32
return workflow , partList
32
33
33
34
split_data = parse
34
- completed = 1
35
+ completed = True
35
36
raw_data = None # Not To be touched
36
37
37
38
def part1 (data :Tuple [Dict [str , List ], List [Dict ]]):
@@ -59,7 +60,53 @@ def isAccepted(part, workflowName):
59
60
60
61
return acc
61
62
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
62
68
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' ]]
63
79
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