about summary refs log tree commit diff stats
path: root/linux/advent2020/4a.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-03-03 22:09:50 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-03-03 22:21:03 -0800
commit71e4f3812982dba2efb471283d310224e8db363e (patch)
treeea111a1acb8b8845dbda39c0e1b4bac1d198143b /linux/advent2020/4a.mu
parentc6b928be29ac8cdb4e4d6e1eaa20420ff03e5a4c (diff)
downloadmu-71e4f3812982dba2efb471283d310224e8db363e.tar.gz
7842 - new directory organization
Baremetal is now the default build target and therefore has its sources
at the top-level. Baremetal programs build using the phase-2 Mu toolchain
that requires a Linux kernel. This phase-2 codebase which used to be at
the top-level is now under the linux/ directory. Finally, the phase-2 toolchain,
while self-hosting, has a way to bootstrap from a C implementation, which
is now stored in linux/bootstrap. The bootstrap C implementation uses some
literate programming tools that are now in linux/bootstrap/tools.

So the whole thing has gotten inverted. Each directory should build one
artifact and include the main sources (along with standard library). Tools
used for building it are relegated to sub-directories, even though those
tools are often useful in their own right, and have had lots of interesting
programs written using them.

A couple of things have gotten dropped in this process:
  - I had old ways to run on just a Linux kernel, or with a Soso kernel.
    No more.
  - I had some old tooling for running a single test at the cursor. I haven't
    used that lately. Maybe I'll bring it back one day.

The reorg isn't done yet. Still to do:
  - redo documentation everywhere. All the README files, all other markdown,
    particularly vocabulary.md.
  - clean up how-to-run comments at the start of programs everywhere
  - rethink what to do with the html/ directory. Do we even want to keep
    supporting it?

In spite of these shortcomings, all the scripts at the top-level, linux/
and linux/bootstrap are working. The names of the scripts also feel reasonable.
This is a good milestone to take stock at.
Diffstat (limited to 'linux/advent2020/4a.mu')
-rw-r--r--linux/advent2020/4a.mu75
1 files changed, 75 insertions, 0 deletions
diff --git a/linux/advent2020/4a.mu b/linux/advent2020/4a.mu
new file mode 100644
index 00000000..e645da24
--- /dev/null
+++ b/linux/advent2020/4a.mu
@@ -0,0 +1,75 @@
+# https://adventofcode.com/2020/day/4
+#
+# To run (on Linux):
+#   $ git clone https://github.com/akkartik/mu
+#   $ cd mu
+#   $ ./translate_mu apps/advent2020/4a.mu
+#   $ ./a.elf < input
+#
+# You'll need to register to download the 'input' file for yourself.
+
+fn main -> _/ebx: int {
+  var curr-passport-field-count/esi: int <- copy 0
+  var valid-passport-count/edi: int <- copy 0
+  var line-storage: (stream byte 0x100)  # 256 bytes
+  var line/ecx: (addr stream byte) <- address line-storage
+  var slice-storage: slice
+  var slice/edx: (addr slice) <- address slice-storage
+  $main:line-loop: {
+    # read line from stdin
+    clear-stream line
+    read-line-from-real-keyboard line
+    # if line is empty (not even a newline), quit
+    var done?/eax: boolean <- stream-empty? line
+    compare done?, 0/false
+    break-if-!=
+    print-stream-to-real-screen line
+    # if line has just a newline, process passport
+    skip-chars-matching-whitespace line
+    var new-passport?/eax: boolean <- stream-empty? line
+    {
+      compare new-passport?, 0/false
+      break-if-=
+      compare curr-passport-field-count, 7
+      {
+        break-if-!=
+        valid-passport-count <- increment
+        print-string 0, "=> "
+        print-int32-decimal 0, valid-passport-count
+        print-string 0, "\n"
+      }
+      curr-passport-field-count <- copy 0
+      loop $main:line-loop
+    }
+    $main:word-loop: {
+      next-word line, slice
+      var done?/eax: boolean <- slice-empty? slice
+      compare done?, 0/false
+      break-if-!=
+      print-string 0, "  "
+      print-slice-to-real-screen slice
+      # treat cid as optional
+      var optional?/eax: boolean <- slice-starts-with? slice, "cid:"
+      compare optional?, 0/false
+      {
+        break-if-!=
+        # otherwise assume there are no invalid fields and no duplicate fields
+        curr-passport-field-count <- increment
+        print-string 0, " => "
+        print-int32-decimal 0, curr-passport-field-count
+      }
+      print-string 0, "\n"
+      loop
+    }
+    loop
+  }
+  # process final passport
+  compare curr-passport-field-count, 7
+  {
+    break-if-!=
+    valid-passport-count <- increment
+  }
+  print-int32-decimal 0, valid-passport-count
+  print-string 0, "\n"
+  return 0
+}