https://github.com/akkartik/mu/blob/main/linux/advent2020/4a.mu

     1 # https://adventofcode.com/2020/day/4
     2 #
     3 # To run (on Linux):
     4 #   $ git clone https://github.com/akkartik/mu
     5 #   $ cd mu
     6 #   $ ./translate advent2020/4a.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 curr-passport-field-count/esi: int <- copy 0
    13   var valid-passport-count/edi: int <- copy 0
    14   var line-storage: (stream byte 0x100)  # 256 bytes
    15   var line/ecx: (addr stream byte) <- address line-storage
    16   var slice-storage: slice
    17   var slice/edx: (addr slice) <- address slice-storage
    18   $main:line-loop: {
    19     # read line from stdin
    20     clear-stream line
    21     read-line-from-real-keyboard line
    22     # if line is empty (not even a newline), quit
    23     var done?/eax: boolean <- stream-empty? line
    24     compare done?, 0/false
    25     break-if-!=
    26     print-stream-to-real-screen line
    27     # if line has just a newline, process passport
    28     skip-chars-matching-whitespace line
    29     var new-passport?/eax: boolean <- stream-empty? line
    30     {
    31       compare new-passport?, 0/false
    32       break-if-=
    33       compare curr-passport-field-count, 7
    34       {
    35         break-if-!=
    36         valid-passport-count <- increment
    37         print-string 0, "=> "
    38         print-int32-decimal 0, valid-passport-count
    39         print-string 0, "\n"
    40       }
    41       curr-passport-field-count <- copy 0
    42       loop $main:line-loop
    43     }
    44     $main:word-loop: {
    45       next-word line, slice
    46       var done?/eax: boolean <- slice-empty? slice
    47       compare done?, 0/false
    48       break-if-!=
    49       print-string 0, "  "
    50       print-slice-to-real-screen slice
    51       # treat cid as optional
    52       var optional?/eax: boolean <- slice-starts-with? slice, "cid:"
    53       compare optional?, 0/false
    54       {
    55         break-if-!=
    56         # otherwise assume there are no invalid fields and no duplicate fields
    57         curr-passport-field-count <- increment
    58         print-string 0, " => "
    59         print-int32-decimal 0, curr-passport-field-count
    60       }
    61       print-string 0, "\n"
    62       loop
    63     }
    64     loop
    65   }
    66   # process final passport
    67   compare curr-passport-field-count, 7
    68   {
    69     break-if-!=
    70     valid-passport-count <- increment
    71   }
    72   print-int32-decimal 0, valid-passport-count
    73   print-string 0, "\n"
    74   return 0
    75 }