https://github.com/akkartik/mu/blob/main/linux/apps/advent2020/2a.mu
1
2
3
4
5
6
7
8
9
10
11 fn main -> _/ebx: int {
12 var valid-password-count/edi: int <- copy 0
13 var line-storage: (stream byte 0x100)
14 var line/edx: (addr stream byte) <- address line-storage
15 var slice-storage: slice
16 var slice/ecx: (addr slice) <- address slice-storage
17 {
18
19 clear-stream line
20 read-line-from-real-keyboard line
21
22 var done?/eax: boolean <- stream-empty? line
23 compare done?, 0/false
24 break-if-!=
25 print-stream-to-real-screen line
26
27 next-token line, 0x2d, slice
28
29 var _start/eax: int <- parse-decimal-int-from-slice slice
30 var start/ebx: int <- copy _start
31 var dash/eax: byte <- read-byte line
32
33 next-token line, 0x20, slice
34 var _end/eax: int <- parse-decimal-int-from-slice slice
35 var end/esi: int <- copy _end
36 print-int32-decimal 0, start
37 print-string 0, " "
38 print-int32-decimal 0, end
39 print-string 0, "\n"
40
41 skip-chars-matching-whitespace line
42 var letter/eax: byte <- read-byte line
43
44 {
45 var colon/eax: byte <- read-byte line
46 }
47 skip-chars-matching-whitespace line
48
49 var valid?/eax: boolean <- valid? start, end, letter, line
50 compare valid?, 0/false
51 {
52 break-if-=
53 print-string 0, "valid!\n"
54 valid-password-count <- increment
55 }
56 loop
57 }
58 print-int32-decimal 0, valid-password-count
59 print-string 0, "\n"
60 return 0
61 }
62
63 fn valid? start: int, end: int, letter: byte, password: (addr stream byte) -> _/eax: boolean {
64 var letter-count/edi: int <- copy 0
65
66
67
68 {
69 var done?/eax: boolean <- stream-empty? password
70 compare done?, 0/false
71 break-if-!=
72 var c/eax: byte <- read-byte password
73 compare c, letter
74 {
75 break-if-!=
76 letter-count <- increment
77 }
78 loop
79 }
80
81 compare letter-count, start
82 {
83 break-if->=
84 return 0/false
85 }
86 compare letter-count, end
87 {
88 break-if-<=
89 return 0/false
90 }
91 return 1/true
92 }