Advent of Code (in MiniScript), Day 6

JoeStrout - Dec 6 '22 - - Dev Community

Last night was Day 6 of the Advent of Code challenge. This one was considerably simpler than the previous day! As before, I tackled this one using MiniScript (more specifically, Mini Micro).

For Part A, all we have to do is find the position of a 4-character substring (of a big input string) where all four characters are different.

By this point I have a starter program which reads an input file line by line, has a place to calculate a result for each line, and then does some kind of summary of the results. I didn't need all that for this program, so I cut some stuff out, and (under time pressure) ignored other bits. But with a little more cleaning up, my solution boils down to this:

line = file.readLines("input.txt")[0]

for i in range(4, line.len)
    s = line[i-4:i]
    d = {}
    for c in s; d.push c; end for
    if d.len == 4 then
        print s + " after " + i
        exit
    end if
end for
Enter fullscreen mode Exit fullscreen mode

The input in this case is only one line, which is read in by the first line of code above. Then we iterate over that, starting at index 4, and grab the 4-character substring that precedes that position. To tell if they're all different, I use a map like a set, pushing each character with a little for-loop. If all four characters are unique, the len of this map will be 4; if any are duplicates, then it will be less. So, as soon as we see that length of 4, we print the substring (as a sanity check) and the index.

For the curious, here's the uncleaned version of the code I actually used last night.
import "stringUtil"
import "mapUtil"
import "listUtil"
import "mathUtil"
import "aoc"

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

resultA = []
resultB = []
while not f.atEnd
    line = f.readLine
    if line == null then break // end of file
    f.close
    for i in range(4, line.len)
        s = line[i-4:i]
        d = {}
        for c in s; d.push c; end for
        if d.len == 4 then
            print s + " after " + i
            exit
        end if
    end for
end while
f.close
Enter fullscreen mode Exit fullscreen mode

Part B

Part B of this challenge was trivial: instead of a unique 4-character substring, just find a unique 14-character substring. So I literally just changed 4 to 14 in three places, and ran it again. Done!

Results

I'm happy to say that, for the first time, I got into the top 1000! I finished 501st for Part A, and 362nd for Part B. Huzzah! 🥳

It's also interesting (to me, anyway) that the solution didn't require any of the library functions I had prepared — not even standard stuff in /sys/lib. Pure MiniScript was all that was needed.

Check out the other posts in this series, and stay tuned for more — I'm going to do the whole 25-step calendar if I can! I also encourage you to play along at home. The challenges are well-written and fun to do. Why not grab Mini Micro and give it a try?

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .