Advent Of Code 2022 - Day 25
See Day 25 for a detailed description of the problem.
Continuing to solve the Advent of Code 2022 problems (see Advent of Code - Day 1).
Links:
To run the example code in this post save the code into file such as
advent.jactland take your input from the Advent of Code site (e.g.advent.txt) and run it like this:$ cat advent.txt | java -jar jactl-{{ site.content.jactl_version }}.jar advent.jactl
This is the last challenge for Advent of Code 2022 and day 25 has only one part, unlike the other days.
A fairly simple challenge for the last day. The goal is to take a list of numbers encoded in a code they have called SNAFU, sum them, and then output the sum as a SNAFU number.
In SNAFU the numbers are base 5 but the twist is that the digits are =, -, 0, 1, 2 with = meaning -2
and - meaning -1.
Decoding these numbers is pretty straightforward but encoding is a bit trickier.
In the end, not a lot of code for this challenge.
def n = stream(nextLine).map{ it.reduce(0L){ v,d -> v*5 + ['2':2,'1':1,'0':0,'-':-1,'=':-2][d] } }.sum()
for (def dval=0, value=''; ; n /= 5) {
  return value if n <= 0 && dval <= 2;
  dval = (n % 5) + (dval > 2 ? 1 : 0)
  value = '012=-'[dval % 5] + value
}
