The Julia Github repo has this to say about the language:

Julia is a high-level, high-performance dynamic language for technical computing.

github.com/JuliaLang/julia

Like many languages focussed on numerical computing, Julia has 1-based array indexing. I don’t think this is a major issue, but I imagine it would be something that would occasionaly trip up folks coming from 0-based languages while they adjust.

Visual Studio Code support for Julia is excellent.

The Code#

One interesting feature is the use of Unicode operators. While using the ÷ symbol for integer division looks neat, it presents a visual tripping hazard, and at a quick glance, n÷2+1 looks suspiciously similar to n+2+1 in equal_halves(…). For those on Macs, you can type ÷ directly with Option-/.

Unicode quirks aside, the code is clean and pretty easy to read. The standard libraries seem complete and accessible. Arrays / vectors are column-major, hence the use of vcat in the mapreduce call.

The approach is pretty straightforward. The code produces a gigantic list of numbers, nearly 20 million of them. For part 1, we just check straightforwardly whether the length is even and the two halves match.

For part 2, I originally used a regular expression in Kotlin to initially solve the problem. It turns out that backtracking regexes are much slower in Julia than they are in Kotlin— I assume the underlying Java libraries have some optimisations for specific cases that improved the performance for the regexes I used.

Fortunately, many posts in the reddit solutions code pointed out that a string consists of a repeated substring if it is a nontrivial rotation of itself, and that is the algorithm implemented here.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function equal_halves(s)
    n = length(s)
    n > 0 && iseven(n) && view(s, 1:n÷2) == view(s, n÷2+1:n)
end

function is_repeated_unit(s)
    length(s) > 1 && occursin(s, (s^2)[2:end-1])
end

function main()
    line = strip(read("y25d02.txt", String))

    nums = mapreduce(vcat, split(line, ',')) do part
        a, b = parse.(Int, split(part, '-'))
        a:b
    end

    result1 = sum(n for n in nums if equal_halves(string(n)))
    println("Result1: ", result1)

    result2 = sum(n for n in nums if is_repeated_unit(string(n)))
    println("Result2: ", result2)
end

main()

Install Julia and run#

The modern way to manage Julia versions is juliaup. Regular package managers work fine too.

# I installed via Homebrew
brew install julia

# Website recommends running random scripts on your machine
curl -fsSL https://install.julialang.org | sh

# Execute the code
julia d02.jl