https://github.com/akkartik/mu/blob/main/linux/advent2020/2b.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 _pos1/eax: int <- parse-decimal-int-from-slice slice
30 var pos1/ebx: int <- copy _pos1
31 var dash/eax: byte <- read-byte line
32
33 next-token line, 0x20, slice
34 var _pos2/eax: int <- parse-decimal-int-from-slice slice
35 var pos2/esi: int <- copy _pos2
36 print-int32-decimal 0, pos1
37 print-string 0, " "
38 print-int32-decimal 0, pos2
39 print-string 0, "\n"
40 compare pos1, pos2
41 {
42 break-if-<=
43 print-string 0, "out of order!\n"
44 return 1
45 }
46
47 skip-chars-matching-whitespace line
48 var letter/eax: byte <- read-byte line
49
50 {
51 var colon/eax: byte <- read-byte line
52 }
53 skip-chars-matching-whitespace line
54
55 var valid?/eax: boolean <- valid? pos1, pos2, letter, line
56 compare valid?, 0/false
57 {
58 break-if-=
59 print-string 0, "valid!\n"
60 valid-password-count <- increment
61 }
62 loop
63 }
64 print-int32-decimal 0, valid-password-count
65 print-string 0, "\n"
66 return 0
67 }
68
69
70
71
72 fn valid? pos1: int, pos2: int, letter: byte, password: (addr stream byte) -> _/eax: boolean {
73 var i/esi: int <- copy 1
74 var letter-count/edi: int <- copy 0
75
76
77
78
79
80
81
82
83 {
84
85
86
87 var done?/eax: boolean <- stream-empty? password
88 compare done?, 0/false
89 break-if-!=
90 var c/eax: byte <- read-byte password
91
92
93
94
95
96 compare c, letter
97 {
98 break-if-!=
99 compare i, pos1
100 {
101 break-if-!=
102 letter-count <- increment
103
104 }
105 compare i, pos2
106 {
107 break-if-!=
108 letter-count <- increment
109
110 }
111 }
112 i <- increment
113 loop
114 }
115
116 compare letter-count, 1
117 {
118 break-if-!=
119 return 1/true
120 }
121 return 0/false
122 }