-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path14.q
57 lines (44 loc) · 1 KB
/
14.q
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//--- Day 14: Space Stoichiometry ---
i:" => " vs/:i:read0 `:input/14.txt
tab:flip `res`qty`ing!flip (reverse each "JS"$/:" "vs'i[;1]),'flip enlist reverse@''"JS"$/:/:" "vs''", "vs/:i[;0]
leftovers:enlist[`]!enlist 0N
f:{
ore:0;
queue:enlist (`FUEL;x);
while[count queue;
pop:first queue;
queue:1_queue;
item:first pop;
required:last pop;
creates:first exec qty from tab where res=item;
if[(0<leftovers[item]) and leftovers[item]>=left:required mod creates;
required-:left;
leftovers[item]-:left
];
leftovers[item]+:(creates - required mod creates) mod creates;
$[`ORE~item;
ore+:required;
[
ing:raze exec ing from tab where res=item;
queue,:ing[;0],'(ceiling required%creates)*ing[;1]
]
]
];
ore
};
f 1
/301997
TARGET:1000000000000
MIN:1
MAX:TARGET
FUEL:1
LAST:0N
while[LAST<>FUEL;
LAST:FUEL;
$[TARGET<o:f FUEL:"j"$.5*MIN+MAX;
MAX:MIN|FUEL-1;
MIN:MAX&FUEL+1
]
];
$[o>TARGET;FUEL-1;FUEL]
/6216589