https://github.com/akkartik/mu/blob/main/linux/apps/advent2017/1a.mu

     1 # Advent of code 2017, problem 1a
     2 #   https://adventofcode.com/2017/day/1
     3 #
     4 # Mu solution by Sumeet Agarwal and Kartik Agaram
     5 #   https://archive.org/details/2021-06-02-akkartik-sumeet
     6 #
     7 # To build on Linux:
     8 #   $ git clone https://github.com/akkartik/mu
     9 #   $ cd mu/linux
    10 #   $ ./translate apps/advent2017/1a.mu            # emits a.elf
    11 # To run on Linux:
    12 #   Download https://adventofcode.com/2017/day/1/input
    13 #   $ ./a.elf < input
    14 # Type in the number returned at https://adventofcode.com/2017/day/1
    15
    16 fn main -> _/ebx: int {
    17   var input_stream: (stream byte 0x8000)
    18   var input_stream_addr/esi: (addr stream byte) <- address input_stream
    19
    20   var sum/edi: int <- copy 0
    21   read-line-from-real-keyboard input_stream_addr
    22
    23   var temp/eax: int <- read_digit input_stream_addr
    24   var first_digit/ebx: int <- copy temp
    25   var this_digit/edx: int <- copy temp
    26
    27   {
    28     var done?/eax: boolean <- stream-empty? input_stream_addr
    29     compare done?, 1
    30     break-if-=
    31
    32     var next_digit/eax: int <- read_digit input_stream_addr
    33     # hacky newline check
    34     compare next_digit, 0
    35     break-if-<
    36
    37     {
    38       compare this_digit, next_digit
    39       break-if-!=
    40       sum <- add this_digit
    41     }
    42
    43     this_digit <- copy next_digit
    44
    45     loop
    46   }
    47
    48   # the last iteration will need to compare the last number to the first
    49   {
    50     compare this_digit, first_digit
    51     break-if-!=
    52     sum <- add this_digit
    53   }
    54
    55   print-int32-decimal 0, sum
    56
    57   return 0/ok
    58 }
    59
    60 fn read_digit input_stream_addr: (addr stream byte) -> _/eax: int {
    61   var next_digit/eax: byte <- read-byte input_stream_addr
    62   next_digit <- subtract 0x30
    63   var next_digit/eax: int <- copy next_digit
    64   return next_digit
    65 }