https://github.com/akkartik/mu/blob/main/linux/apps/advent2020/2a.mu
 1 # https://adventofcode.com/2020/day/2
 2 #
 3 # To run (on Linux):
 4 #   $ git clone https://github.com/akkartik/mu
 5 #   $ cd mu
 6 #   $ ./translate apps/advent2020/2a.mu
 7 #   $ ./a.elf < input
 8 #
 9 # You'll need to register to download the 'input' file for yourself.
10 
11 fn main -> _/ebx: int {
12   var valid-password-count/edi: int <- copy 0
13   var line-storage: (stream byte 0x100)  # 256 bytes
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     # read line from stdin
19     clear-stream line
20     read-line-from-real-keyboard line
21     # if line is empty (not even a newline), quit
22     var done?/eax: boolean <- stream-empty? line
23     compare done?, 0/false
24     break-if-!=
25     print-stream-to-real-screen line
26     # slice = next-token(line, '-')
27     next-token line, 0x2d, slice
28     # start = parse-int(slice)
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  # skip '-'
32     # slice = next-token(line, ' ')
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     # letter = next non-space
41     skip-chars-matching-whitespace line
42     var letter/eax: byte <- read-byte line
43     # skip some stuff
44     {
45       var colon/eax: byte <- read-byte line  # skip ':'
46     }
47     skip-chars-matching-whitespace line
48     # now check the rest of the line
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   # for every c in password
66   #   if (c == letter)
67   #     ++letter-count
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   # return (start <= letter-count <= end)
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 }