🎄⌨️ Advent of Code 2018

Artifact [1cef99]

Artifact 1cef99dd7ff0a98effc0165145e7ce18924ddd163c5244b14574cf3c33c18829:

Instance of technote [46fcfd] - Day 3 thoughts and comments by joel on 2018-12-03 20:30:18.
C Day\s3\sthoughts\sand\scomments
D 2018-12-03T22:42:09.571
E 2018-12-03T20:30:18 46fcfd713085d9018b0c7fccb5ce15cc00f7ff93
P adc1bad42d5932d3492673b67d184e3fefb1d1e31c3014180f440ca70137de77
N text/x-markdown
T +bgcolor * #c0f0ff
U joel
W 2224
[Today’s problems](https://adventofcode.com/2018/day/3) pretty clearly called for the use of a two-dimensional array, or a matrix or whatever you like to call it. The LISPy way to do these is to think of the 2D array as a list of lists, but I pretty quickly got annoyed with Racket here.

    (define fabric (make-vector 5 (make-vector 5 0)))
    (define (poke-square x y val) 
        (vector-set! (vector-ref fabric y) x val))

    (poke-square 1 2 9)

Contrary to what you might think, this changes the value in position `2` of _every row_ to `9`. This is because `make-vector` fills `fabric` not with unique vectors but with identical _references_ to a single vector.

Racket does have a `math` package with functions for [arrays](https://docs.racket-lang.org/math/array.html) and [matrices](https://docs.racket-lang.org/math/matrices.html) but even these are cumbersome for the simple operation of fingering an individual bit somewhere in the middle. There is furthermore a warning at the top of the docs for each that using these libraries without using Typed Racket is 50⨉ slower. I didn’t feel like learning Typed Racket today.

I’m biting my tongue about how annoying it is that simple, native 2-D array support is MIA from such an advanced language, because invariably as soon as I say something somebody will point out something simple and perfect that I missed. (But when that happens, it’ll still be Racket’s fault that it took so long to find.)

## Just use pixels already

[My solution](/file/day03.rkt)

At any rate I decided to find the solution graphically, by drawing on a canvas using a semi-opaque brush. After drawing all the rectangles, you can then go back and read the alpha channel values for each pixel to find those that have been drawn over more than once.

This isn’t particularly fast. The solution for part 1 takes 10.8 seconds on my MacBook Pro, and the solution for part 2 takes 1.8 seconds. But it has the benefit of giving me, “for free”, a graphical representation of the inputs (and the Part 2 solution highlighted in red):

![Graphic representation of my input and part 2 answer](https://thenotepad.org/repos/aoc2018/doc/trunk/day03-results.png)
Z bef1769f51e0f228a25a4e263ee8469f