Skip to content

Instantly share code, notes, and snippets.

@JesterXL
Created December 2, 2022 13:24
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 JesterXL/3e8b12ae456dafce5d159983659d53e3 to your computer and use it in GitHub Desktop.
Save JesterXL/3e8b12ae456dafce5d159983659d53e3 to your computer and use it in GitHub Desktop.
app "AoC Day 2 - Rock Paper Scissors"
packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.1.1/zAoiC9xtQPHywYk350_b7ust04BmWLW00sjb9ZPtSQk.tar.br" }
imports [pf.Stdout, pf.Path, pf.File, pf.Task]
provides [main] to pf
# saysYeah = \yup ->
# "\(yup) yeah!"
# expect saysYeah "cactus" == "cactus yeah!"
main =
task =
inputStr <- Path.fromStr "input_small.txt" |> File.readUtf8 |> Task.await
totalScore = parse inputStr
dbg totalScore
Stdout.line "Part 1: "
Task.onFail task \_ -> crash "Failed to read and parse input"
parse = \inputStr ->
Str.split inputStr "\n"
|> List.map parseLine
# |> List.dropIf isUnknownStrategy
# |> List.sum
|> sumScore
parseLine = \lineStr ->
when lineStr is
"A X" -> RockVsRockDraw (1 + 3)
"A Y" -> RockVsPaperWin (2 + 6)
"A Z" -> RockVsScissorsLoss (3 + 0)
"B X" -> PaperVsRockLoss (1 + 0)
"B Y" -> PaperVsPaperDraw (2 + 3)
"B Z" -> PaperVsScissorsWin (3 + 6)
"C X" -> ScissorsVsRockWin (1 + 6)
"C Y" -> ScissorsVsPaperLoss (2 + 0)
"C Z" -> ScissorsVsScissorsDraw (3 + 3)
x -> UnknownStrategy "C-C-C-Combo breaker! Unknown move: \(x)"
# isUnknownStrategy = \gameTag ->
# when gameTag is
# UnknownStrategy _ -> true
# _ -> false
sumScore = \rounds ->
List.walk rounds 0 \state, elem ->
when elem is
UnknownStrategy _ -> state
RockVsRockDraw score -> state + score
RockVsPaperWin score -> state + score
RockVsScissorsLoss score -> state + score
PaperVsRockLoss score -> state + score
PaperVsPaperDraw score -> state + score
PaperVsScissorsWin score -> state + score
ScissorsVsRockWin score -> state + score
ScissorsVsPaperLoss score -> state + score
ScissorsVsScissorsDraw score -> state + score
# sumScore = \rounds ->
# List.walk rounds 0 \state, elem ->
# when elem is
# UnknownStrategy _ -> state
# _ score -> state + score
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment