Mercurial > lbo > hg > aoc22
view 05/05.jl @ 5:dc901991442c
Intermittent solution for day 05
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Tue, 06 Dec 2022 23:08:24 +0100 |
parents | |
children | dea8d8f60e25 |
line wrap: on
line source
const input = "05/input.txt"; mutable struct Crates stacks::Vector{Vector{Char}} end using ParserCombinator crate = ( ( (E"[" + p"[A-Z]" + E"]") |> s -> s[1][1]) | (E" " |> _ -> nothing)) + (E" " | Eos()) crate_row = Repeat(crate) + Eos() crate_base = Repeat(P"\s*"+ p"\d" + P"\s*", 1, 100) + Eos() function parse_stacks(lines::Vector{String})::Crates stacks = nothing # start is the line on which instructions start. start = 3 for l in lines try cs = parse_one(chomp(l), crate_row); if isnothing(stacks) stacks = Crates([Vector{Char}() for i in 1:length(cs)]) end for (i, e) in enumerate(cs) # inefficient but saves reversing if !isnothing(e) pushfirst!(stacks.stacks[i], e); end end start += 1 catch e @assert typeof(e) == ParserException break end end println(stacks) stacks end struct Instr n::Int from::Int to::Int end instruction = E"move " + PInt() + E" from " + PInt() + E" to " + PInt() + Eos() function parse_instructions(lines::Vector{String})::Vector{Instr} v = Vector{Instr}(); for l in lines try i = parse_one(chomp(l), instruction); push!(v, Instr(i[1], i[2], i[3])); catch e @assert typeof(e) == ParserException end end @show v v end function act(init::Crates, instr::Vector{Instr})::Crates for i in instr end end function run_05(f::String) open(f; read=true) do fh ls = collect(eachline(fh)); crates = parse_stacks(ls); instrs = parse_instructions(ls); end end run_05(input);