Advent of Code 2018 - Day 1

Day 1

Let's do this in Julia!

This is the first time I have done Advent of Code. It looked fun last year and so I wanted to have a go this time around. Coincidentally a colleague challenged me to learn a new language, I chose Julia. So the Advent of Code looked like a perfect chance to get stuck in.

Julia Logo

Hello World

Fortunately I didn't have to think too much about the first program - it has to be Hello World. My work laptop is a Macbook and didn't have Julia installed by default so I downloded the .dmg package for version 1.0.2 and installed it. Starting if from Launchpad brings up a terminal window with the colourful banner and the REPL running. Running

println("Hello World")

gives the expected result and instant gratification. Since I'm a command line kind of person I want to run a julia script throuch the interpreter by invoking it from the shell. The Introducing Julia wikibook is invaluable here. To run this Julia script

#!/usr/bin/env julia

println("Kia Ora Koutou")

In the shell I did

ls /Applications/Julia-*.app/Contents/Resources/julia/bin/julia
export PATH=$PATH:/Applications/Julia-1.0.app/Contents/Resources/julia/bin/
chmod a+x kiaora.jl
./kiaora.jl

First Puzzle

Starting from a base frquency of zero and given a series of offests find the resulting frequency. There are a number of "test vectors", test input and the expected output.

Ideally, I'd learn the standard Julia testing framework, write my tests, write my implementation and then once they are passing try the challenge input. However this is quite a simple problem and I want to get up to speed on some of the more basic language features before jumping in to testing. (Hopefully my RSE colleagues won't disown me just yet.)

Essentially the problem is to read in a file, treat the input as signed integers and add them all up. Here's my solution using the readdlm function from the DelimitedFiles module

#!/usr/bin/env julia

# Advent of Code, Day 1, Puzzle a
using DelimitedFiles
println( sum( readdlm("input.txt", Int64) ) )

Nice and simple!

Second Puzzle

The next step is to find the first frequency that is repeated for a second time. We need to look at the array of cumulative sums. My approach is to iterate through the array storing the frequencies we have already seen in a Set.

The list of frequency deltas may need to be applied more than once. I'm not particularly happy with my hack of hard coding the number of repeats, but I'm optimising for learning language features rather than perfomance or perfect readability.

#!/usr/bin/env julia

# Advent of Code 2018, Day 1, Puzzle b
# find first repeated element in cumulative sum of (possibly repeated) deltas

using DelimitedFiles
deltas = readdlm("input.txt",Int64)
seen = Set(0)
# TODO: rework to loop over repetitions rather than arbitrary number 400
for freq in cumsum(repeat(deltas[:,1],400))
    if in(freq, seen)
        println(freq)
        break
    else
        push!(seen,freq)
    end
end

First Impressions

These problems seem to be squarely in Julia's realm and coming from a backround of C, Fortran and Python the language seems to do what I expect it to with no fuss. Should be a fun month!

links

social