DEV Community

JoeStrout
JoeStrout

Posted on

Advent of Code (in MiniScript), Day 1

I've decided to participate in the fun annual Advent of Code challenges. I'll be posting solutions written in MiniScript, which is a great language for these sorts of tasks. But so as to not spoil anybody's fun, I won't post them until at least a day or two after each challenge drops. I encourage you to try them yourself before looking at my solutions!

(Yes, that's why I'm posting them late... not because I tend to procrastinate. Really!)

Each challenge consists of two parts, and you only get to see the second part after correctly solving the first. Note that the input file (and so, the correct answers) are randomly generated for each participant. So if you follow along at home — which I urge you to do! — your answers will be different from mine.

In Day 1, we are given an input file that contains one number per line, with occasional blank lines. Each batch of numbers between the blanks represents snack calories carried by one elf. We are tasked with finding the maximum number of calories any elf has.

I chose to do this with command-line MiniScript. My solution was straightforward: read each line, adding up a curTotal for the current elf as we go; and when we hit a blank (or null, indicating end-of-file), update our max total.

f = file.open("input.txt")

maxTotal = 0
curTotal = 0
while true
    line = f.readLine
    print line
    if not line then
        // done with elf
        if curTotal > maxTotal then maxTotal = curTotal
        print "Total: " + curTotal + "  Max: " + maxTotal
        curTotal = 0
    else
        curTotal = curTotal + val(line)
    end if
    if line == null then exit // end of file
end while
Enter fullscreen mode Exit fullscreen mode

For my input file, the last max total shown was 71471, so I pasted that in on the Advent of Code site, and it was correct!

This unlocked Part B, in which we have to keep track of the top 3 totals (and then report the some of those top 3). This sounds much harder, but really isn't. Instead of a single maxTotal variable, we keep a top3 list. After each elf, we just push the new total onto the list, sort it, and pull the lowest one off the front. That way, the list always contains the biggest 3 numbers.

My first attempt almost worked, but didn't print the sum of the top 3. So I changed exit to break in my while loop, and added an additional print top3.sum at the end.

f = file.open("input.txt")

top3 = [0,0,0]
curTotal = 0
while true
    line = f.readLine
    print line
    if not line then
        // done with elf
        top3.push curTotal
        top3.sort
        top3.pull
        print "Total: " + curTotal + "  Top 3: " + top3
        curTotal = 0
    else
        curTotal = curTotal + val(line)
    end if
    if line == null then break // end of file
end while

print "Sum of top 3: " + top3.sum
Enter fullscreen mode Exit fullscreen mode

So the last two lines of output looked like

Total: 35525 Top 3: [69195, 70523, 71471]
Sum of top 3: 211189

And again, this answer was correct!

As you can see, MiniScript is a fun and easy language for this sort of problem. If you're new to it, check out the 1-page quick reference and see for yourself! The Advent of Code challenges are quirky and fun. I'll be posting more solutions over the rest of the month. But please give them a try yourself! And if you have questions, comments, or notes about a different approach, please post them in the comments below. Happy coding!

Top comments (0)