-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.wsf
63 lines (55 loc) · 1.34 KB
/
3.wsf
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
58
59
60
61
62
63
# Advent of Code 2022
# Day 3: Rucksack Reorganization
# https://adventofcode.com/2022/day/3
"@array" import
"@mem" import
"@string" import
0 ^ readi retrieve # count
0 # score
.read_loop:
swap 3- ^ jn .read_break swap
108 call string.read_line 2/
0 108 ^2 call index
54 108 ^2 + ^2 call index
0 54 call intersect
0 54 call array.sum +
jmp .read_loop
.read_break:
drop
printi '\n' printc
end
# priority converts an item in the alphabet a-zA-Z to its priority in
# 1-54.
# (item -- priority)
priority:
# (1 - x/96)*26 + x%32
1 ^1 96/ - 26* swap 32% +
ret
# (table addr len -- )
index:
^2 54 call mem.clear
^1 + swap
.index_loop:
^ ^2 j= .index_break
^ retrieve call priority # priority
^ 1- ^4 + # offset
swap store # table[offset] = priority
1+
jmp .index_loop
.index_break:
2drop ret
# (table addr len -- score)
intersect:
^ ^2 + swap ^1 + swap
0 # score
.intersect_loop:
^1 ^3 j= .intersect_break
^1 retrieve call priority # priority2
^ 1- ^4 + # offset
^ retrieve # priority1 = table[offset]
^1 0 store # table[offset] = 0
* + # score += priority1
swap 1+ swap
jmp .intersect_loop
.intersect_break:
2slide ret