Mercurial > lbo > hg > aoc22
view 2022/03/03.jl @ 63:f2355e1a8e8c
Day 12 Part 1
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 23 Dec 2023 14:14:45 +0100 |
parents | 05ddc45b4210 |
children |
line wrap: on
line source
const input = "03/input.txt" function parse(fh::IO)::Vector{String} readlines(fh) end function run(v::Vector{String})::Int count = 0; for s in v l = length(s); a, b = s[1:div(l, 2)], s[div(l, 2)+1:end]; c = intersect(a, b); @assert length(c) == 1; c = c[1]; score = islowercase(c) ? 1+c-'a' : 27+c-'A'; count += score; end count end function runbadge(v::Vector{String})::Int count = 0; for i in 0:div(length(v), 3)-1 group = v[1+3i:1+3i+2]; shared = intersect(group...); @assert length(shared) == 1; c = shared[1]; score = islowercase(c) ? 1+c-'a' : 27+c-'A'; count += score; end count end function run_03(f::String)::Tuple{Int,Int} open(f; read=true) do fh p = parse(fh); (run(p), runbadge(p)) end end println(run_03(input));