-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.wsf
128 lines (120 loc) · 2.6 KB
/
1.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Advent of Code 2023
# Day 1: Trebuchet?!
# https://adventofcode.com/2023/day/1
2 't' store
3 'w' store
4 'o' store
5 'n' store
6 'e' store
7 'i' store
8 'g' store
9 'h' store
10 't' store
11 'h' store
12 'r' store
13 'e' store
14 'e' store
15 'f' store
16 'o' store
17 'u' store
18 'r' store
19 'f' store
20 'i' store
21 'v' store
22 'e' store
23 's' store
24 'i' store
25 'x' store
26 's' store
27 'e' store
28 'v' store
29 'e' store
30 'n' store
31 'i' store
32 'n' store
33 'e' store
0 ^ readi retrieve # number of lines
0 # sum
.read_lines:
swap 1- ^ jn .read_lines_break swap
34 # string write pointer
.read_line:
^ readc
^ retrieve '\n' j= .read_line_break
1+
jmp .read_line
.read_line_break:
0 -1 store # first digit
1 -1 store # second digit
33 # string read pointer
.parse_line:
1+
2dup j< .parse_line_break
^ retrieve '0' -
^ jn .parse_line_continue
^ 10 - jn .parsed_digit
drop
2dup - # length
4 3 call starts_with jz .parsed_digit_1
2 3 call starts_with jz .parsed_digit_2
10 5 call starts_with jz .parsed_digit_3
15 4 call starts_with jz .parsed_digit_4
19 4 call starts_with jz .parsed_digit_5
23 3 call starts_with jz .parsed_digit_6
27 5 call starts_with jz .parsed_digit_7
6 5 call starts_with jz .parsed_digit_8
30 4 call starts_with jz .parsed_digit_9
drop
.parse_line_continue:
drop
jmp .parse_line
.parsed_digit_1:
1 jmp .parsed_digit
.parsed_digit_2:
2 jmp .parsed_digit
.parsed_digit_3:
3 jmp .parsed_digit
.parsed_digit_4:
4 jmp .parsed_digit
.parsed_digit_5:
5 jmp .parsed_digit
.parsed_digit_6:
6 jmp .parsed_digit
.parsed_digit_7:
7 jmp .parsed_digit
.parsed_digit_8:
8 jmp .parsed_digit
.parsed_digit_9:
9
.parsed_digit:
1 ^1 store # store the second digit
0 retrieve jn .store_first_digit
drop
jmp .parse_line_continue
.store_first_digit:
0 swap store
jmp .parse_line_continue
.parse_line_break:
drop
0 retrieve 10*
1 retrieve +
+
jmp .read_lines
.read_lines_break:
drop
printi '\n' printc
end
# starts_with returns whether the string starts with the prefix.
# (addr len prefixaddr prefixlen -- addr len ?)
starts_with:
-1 # i
^3 ^2 j< .starts_with_false
.starts_with_loop:
1+
2dup j<= .starts_with_true # i >= prefixlen
^4 ^1 + retrieve ^3 ^2 + retrieve
j= .starts_with_loop
.starts_with_false:
-1 3slide ret
.starts_with_true:
0 3slide ret