-
Notifications
You must be signed in to change notification settings - Fork 1
/
day5.toit
37 lines (35 loc) · 1.16 KB
/
day5.toit
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
import .aoc
import .resources
main:
part --one_at_a_time=true
part --one_at_a_time=false
part --one_at_a_time/bool:
// A list of lists. Each element is a list of crates from bottom to top.
stacks := []
INPUT5.trim.split "\n": | line |
if line.contains "[":
for i := 1; i < line.size; i += 4:
letter := line[i]
if 'A' <= letter <= 'Z':
stack_number := (i / 4) + 1
// Make sure the stack list is long enough.
while stacks.size <= stack_number:
stacks.add []
// This is inefficient, but not called very often.
stacks[stack_number] = [letter] + stacks[stack_number]
else if line.starts_with "move":
split6 line " ": | _ cc _ f _ t |
crate_count := int.parse cc
from := int.parse f
to := int.parse t
if one_at_a_time:
crate_count.repeat:
top := stacks[from].remove_last
stacks[to].add top
else:
src := stacks[from]
stacks[to] += src[src.size - crate_count..]
stacks[from] = src[..src.size - crate_count]
answer := ""
stacks[1..].do: answer += "$(%c it.last)"
print answer