view 2022/01/01.jl @ 69:64fc8f99bddd

Day 14 Part 1
author Lewin Bormann <lbo@spheniscida.de>
date Thu, 28 Dec 2023 12:08:35 +0100
parents 05ddc45b4210
children
line wrap: on
line source


const input = "01/input.txt"

function count_calories(data::Vector{Vector{Int}})::Int
    reduced = [sum(v) for v in data];
    maximum(reduced)
end

function top_three(data::Vector{Vector{Int}})::Int
    reduced = [sum(v) for v in data];
    sort!(reduced);
    sum(reduced[end-2:end])
end

function parse_input(filehandle::IO)::Vector{Vector{Int}}
    input = Vector{Vector{Int}}();
    while true
        current = Vector{Int}();
        while true
            line = readline(filehandle);
            if isempty(line)
                break;
            end
            push!(current, parse(Int, line));
        end
        if length(current) == 0
            break
        end
        push!(input, current);
    end
    input
end

function run_01(f::String)::Tuple{Int,Int}
    input = open(f; read=true) do fh
        parse_input(fh)
    end;
    count_calories(input), top_three(input)
end