Dyota's blog

Advent of Code, in Power Query

Day 1

let
    Source = ...,

    // replace the "gaps" with a separator character, to delimit each elf
    putSeparators = List.Transform(
        Source, 
        each if Text.Length(_) = 0 then "#" else _
    ),

    // combine everything separated by $, to delimit each food item
    combine = Text.Combine(putSeparators, "$"),

    resplit = Text.Split(combine, "#"),

    // sum calories per elf
    sumGroups = List.Transform(
        resplit, 
        each List.Sum(List.Transform(Text.Split(_, "$"), each Number.From(_)))
    ),

    // this is the answer to Part 1
    // Which elf is carrying the most calories? 
    max = List.Max(sumGroups),

    // this is the answer to Part 2
    // From the top three elves by calories carried, what is the total calories they are carrying?
    topThree = List.Sum(List.MaxN(sumGroups, 3))
in
    topThree

#powerquery