advent_of_code_2022

My (attempted) solutions to the 2022 Advent of Code
git clone https://git.eamoncaddigan.net/advent_of_code_2022.git
Log | Files | Refs | README

commit 9bb419b8c48ecd581ce0c3a431046cdcd325ae79
parent 314b8485bf5fd474f161cf3a034319547a190c22
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Sat,  7 Jan 2023 20:03:33 -0800

Solution to day 20, part 1.

Diffstat:
Asrc/day_20.jl | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 78 insertions(+), 0 deletions(-)

diff --git a/src/day_20.jl b/src/day_20.jl @@ -0,0 +1,78 @@ +#!/usr/bin/env julia +# https://adventofcode.com/2022/day/20 +using AdventOfCode + +example = readlines(IOBuffer(""" + 1 + 2 + -3 + 3 + -2 + 0 + 4""")) +input = readlines("data/day_20.txt") + +mutable struct CoordFile + values::Vector{Int} + indices::Vector{Int} +end + +CoordFile(x::Vector{Int}) = CoordFile(x, collect(0:length(x)-1)) + +CoordFile(input::Vector{<:AbstractString}) = CoordFile(parse.(Int, input)) + +""" +Return an Array of the elements in order. +""" +Base.collect(coordfile::CoordFile) = coordfile.values[sortperm(coordfile.indices)] + +Base.length(coordfile::CoordFile) = length(coordfile.values) + +function Base.getindex(coordfile::CoordFile, i) + coordfile.values[findfirst(x->x==i, coordfile.indices)] +end + +""" +Move an element +""" +function moveelement!(coordfile::CoordFile, index::Int) + oldindex = coordfile.indices[index] + newindex = oldindex + + mod(coordfile.values[index], length(coordfile) - 1) + if newindex >= length(coordfile) + newindex -= length(coordfile) - 1 + end + for i = eachindex(coordfile.indices) + if coordfile.indices[i] > oldindex + coordfile.indices[i] -= 1 + end + if coordfile.indices[i] >= newindex + coordfile.indices[i] += 1 + end + end + coordfile.indices[index] = newindex + coordfile +end + +function coordinates(coordfile::CoordFile) + # Find the zero + zeroloc = coordfile.indices[findfirst(x->x==0, coordfile.values)] + mapreduce(x->coordfile[mod(zeroloc + x, length(coordfile))], + +, + 1000:1000:3000) +end + +function part_1(input) + coordfile = CoordFile(input) + for i = 1:length(coordfile) + moveelement!(coordfile, i) + end + coordinates(coordfile) +end +@assert part_1(example) == 3 +@info part_1(input) + +function part_2(input) + nothing +end +@info part_2(input)