-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.wsf
81 lines (71 loc) · 1.59 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
# Advent of Code 2023
# Day 1: Historian Hysteria
# https://adventofcode.com/2024/day/1
# TODO: Part 2: Count occurrences by iterating right list in lock step,
# instead of with a map.
"@array" import
"@int" import
"@math" import
0 ^ readi retrieve # len
0 # i
.read_loop:
^
^ ' ' call int.read_until jz invalid_number
store
2dup +
^ call read_space ^ call read_space
^ '\n' call int.read_until jz invalid_number
store
1+
^ ^2 j< .read_loop
# .read_break:
drop
0 ^1 call array.quicksort
^ ^ call array.quicksort
# Part 1
# ^ len
0 # i
0 # sum of distances
.sum_distances_loop:
^1 retrieve ^3 ^3 + retrieve - call math.abs
+
swap 1+ swap
^1 ^3 j< .sum_distances_loop
# .sum_distances_break:
printi '\n' printc
drop
# Part 2
^ 2* ^1 retrieve 0 retrieve call math.min - # min table addr
# 0-fill heap to max addr
^1 1- retrieve ^2 2* 1- retrieve call math.max
^1 + 0 store
# ^ len
# ^ min table addr
0 # i
.count_loop:
# Increment count in table
^ ^3 + retrieve ^2 + ^ retrieve 1+ store
1+
^ ^3 j< .count_loop
drop
# ^ len
# ^ min table addr
0 # i
0 # sum of similarity scores
.sum_similarity_loop:
^1 retrieve ^ ^4 + retrieve *
+
swap 1+ swap
^1 ^4 j< .sum_similarity_loop
# .sum_similarity_break:
printi '\n' printc
3drop end
invalid_number:
"Invalid number\n" prints end
# (addr -- )
read_space:
^ readc retrieve
' ' j= .read_space_ok
"Expected space\n" prints end
.read_space_ok:
ret