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
|
# https://adventofcode.com/2020/day/3
#
# To run (on Linux):
# $ git clone https://github.com/akkartik/mu
# $ cd mu
# $ ./translate advent2020/3a.mu
# $ ./a.elf < input
#
# You'll need to register to download the 'input' file for yourself.
fn main -> _/ebx: int {
# represent trees in a 2D array of ints
# wasteful since each tree is just one bit
var trees-storage: (array int 0x2800) # 10k ints
var trees/esi: (addr array int) <- address trees-storage
var trees-length/ecx: int <- copy 0
var num-rows: int
var width: int
# phase 1: parse each row of trees from stdin
{
var line-storage: (stream byte 0x40) # 64 bytes
var line/edx: (addr stream byte) <- address line-storage
{
# read line from stdin
clear-stream line
read-line-from-real-keyboard line
# if line is empty (not even a newline), quit
var done?/eax: boolean <- stream-empty? line
compare done?, 0/false
break-if-!=
# wastefully recompute width on every line
# zero error-checking; we assume input lines are all equally long
copy-to width, 0
# turn each byte into a tree and append it
$main:line-loop: {
var done?/eax: boolean <- stream-empty? line
compare done?, 0/false
break-if-!=
#? print-int32-decimal 0, num-rows
#? print-string 0, " "
#? print-int32-decimal 0, width
#? print-string 0, "\n"
var dest/ebx: (addr int) <- index trees, trees-length
var c/eax: byte <- read-byte line
# newline comes only at end of line
compare c, 0xa/newline
break-if-=
# '#' = tree
compare c, 0x23/hash
{
break-if-!=
copy-to *dest, 1
}
# anything else = no tree
{
break-if-=
copy-to *dest, 0
}
increment width
trees-length <- increment
loop
}
increment num-rows
loop
}
}
# phase 2: compute
print-int32-decimal 0, num-rows
print-string 0, "x"
print-int32-decimal 0, width
print-string 0, "\n"
var row/ecx: int <- copy 0
var col/edx: int <- copy 0
var num-trees-hit/edi: int <- copy 0
{
compare row, num-rows
break-if->=
var curr/eax: int <- index2d trees, row, col, width
compare curr, 0
{
break-if-=
num-trees-hit <- increment
}
# right 3, down 1
col <- add 3
row <- add 1
loop
}
print-int32-decimal 0, num-trees-hit
print-string 0, "\n"
return 0
}
fn index2d _arr: (addr array int), _row: int, _col: int, width: int -> _/eax: int {
# handle repeating columns of trees
var dummy/eax: int <- copy 0
var col/edx: int <- copy 0
dummy, col <- integer-divide _col, width
# compute index
var index/eax: int <- copy _row
index <- multiply width
index <- add col
# look up array
var arr/esi: (addr array int) <- copy _arr
var src/eax: (addr int) <- index arr, index
return *src
}
|