2022 Day 2 Nim

This commit is contained in:
Luke Hubmayer-Werner 2022-12-02 23:25:43 +10:30
parent cdb80d9b9d
commit 60e7179025
1 changed files with 28 additions and 0 deletions

28
2022/day2.nim Normal file
View File

@ -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)