From 60e71790250903659b6973a6531b26e5505a8226 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Fri, 2 Dec 2022 23:25:43 +1030 Subject: [PATCH] 2022 Day 2 Nim --- 2022/day2.nim | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2022/day2.nim diff --git a/2022/day2.nim b/2022/day2.nim new file mode 100644 index 0000000..7891213 --- /dev/null +++ b/2022/day2.nim @@ -0,0 +1,28 @@ +from std/math import floorMod +import sequtils +import strutils +import tables + +proc `%%` (a: int, b: int): int = floorMod(a, b) + +let moveMap = {'A': 1, 'B': 2, 'C': 3, 'X': 1, 'Y': 2, 'Z': 3}.toTable +let rounds = splitlines strip readFile "day2-input" + +# Part 1 +proc round1Score(round: string): int = + let them = moveMap[round[0]] + let us = moveMap[round[2]] + let winScore = [3, 6, 0][(us - them) %% 3] + us + winScore + +echo rounds.map(round1Score).foldl(a + b) + +# Part 2 +proc round2Score(round: string): int = + let them: int = moveMap[round[0]] + let usDelta = moveMap[round[2]] - 2 + let us: int = ((them + usDelta - 1) %% 3) + 1 + let winScore = [3, 6, 0][(us - them) %% 3] + us + winScore + +echo rounds.map(round2Score).foldl(a + b)