{ datagubbe }

datagubbe.se » advent of shell

Advent of Shell

I solved Advent of Code 2020, day 1, part 1 in pure AmigaShell. No ARexx! No cheating!


Forget head, tail, sed and other such Unix frivolities - or syntactic sugar like "for" and "while". Conditional branching with labels should be enough for everyone!

The tools used are mostly sort, which always sorts in ascending order, and eval, which evaluates mathematical expressions and bitwise logical operations.

For fetching values from the data, I opted for the line editor, edit, which I use to extract a value from a row into a temporary file which is then read into a variable. I also use edit to treat the data file as a list, deleting the first value after each completed loop.

It does take a while to run on my 14 MHz Amiga 1200. I didn't bother timing it, but probably at least a minute.

See a photo of it running (JPG, ~350K)

The code

Click here to show the code with comments.

copy 1.dat t:
cd t:
sort 1.dat 1.sort numeric
copy 1.sort 1.outer
echo "d 2 1000" > to_next
echo "d 1 1" > rem_1st

resident c:edit pure=force
resident c:eval pure=force
resident c:type pure=force
resident c:list pure=force

skip outer

lab outer
  edit 1.outer v.outer with to_next > nil:
  set outersize=`list 1.outer lformat="%L"`
  if $outersize eq "empty"
    skip fail
  edit 1.outer with rem_1st > nil:
  set outerval=`type v.outer`
  copy 1.sort 1.inner
  skip inner

lab inner
  edit 1.inner v.inner with to_next > nil:
  set innersize=`list v.inner lformat="%L"`
  if $innersize eq "empty"
    skip outer back
  edit 1.inner with rem_1st > nil:
  set innerval=`type v.inner`
  set res=`eval $innerval + $outerval`
  if val $res gt 2020
    skip outer back
  if val $res eq 2020
    set finalres=`eval $innerval * $outerval`
    skip end
  skip inner back

lab fail
  echo "No matches found in data."

lab end
  echo "Final result: $finalres"

Download the code (text, ~1K)