Skip to content

Instantly share code, notes, and snippets.

@hovsater
Last active December 3, 2022 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hovsater/b542f03028cf54e487dcb1b889a165ce to your computer and use it in GitHub Desktop.
Save hovsater/b542f03028cf54e487dcb1b889a165ce to your computer and use it in GitHub Desktop.
Solution to Advent of Code 2022, Day 1.
module Day01 exposing (partOne, partTwo)
decreasingOrder : comparable -> comparable -> Order
decreasingOrder a b =
case compare a b of
LT ->
GT
EQ ->
EQ
GT ->
LT
type alias CalorieCount =
Int
type alias ElfInventory =
List CalorieCount
parseList : String -> List ElfInventory
parseList list =
list
|> String.split "\n\n"
|> List.map (String.split "\n" >> List.filterMap String.toInt)
partOne : String -> Int
partOne input =
input
|> parseList
|> List.map List.sum
|> List.maximum
|> Maybe.withDefault 0
partTwo : String -> Int
partTwo input =
input
|> parseList
|> List.map List.sum
|> List.sortWith decreasingOrder
|> List.take 3
|> List.sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment