Advent of Code (in MiniScript), Day 6
Tags: miniscript, adventofcode, programming
Published: 2022-12-06 15:43:34

Last night was [Day 6](https://adventofcode.com/2022/day/6) of the [Advent of Code](https://adventofcode.com/2022/day/6) challenge.  This one was considerably simpler than [the previous day](https://dev.to/joestrout/advent-of-code-in-miniscript-day-5-46b5)!  As before, I tackled this one using [MiniScript](https://miniscript.org/) (more specifically, [Mini Micro](https://miniscript.org/MiniMicro/index.html)).

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](https://github.com/JoeStrout/AoC-2022/blob/main/starter/dayN.ms) 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
```

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, [push](https://miniscript.org/wiki/Push)ing each character with a little for-loop.  If all four characters are unique, the [len](https://miniscript.org/wiki/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.

{% spoiler 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
```
{% endspoiler %}

## 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](https://dev.to/search?q=JoeStrout%20%23adventofcode), 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](https://miniscript.org/MiniMicro/index.html) and give it a try?