This commit is contained in:
Rupus Reinefjord 2022-12-03 00:32:16 +01:00
parent 1d0e9ec064
commit 50fa518ffc
2 changed files with 86 additions and 0 deletions

39
2022/day02/day02.nim Normal file
View file

@ -0,0 +1,39 @@
import std/[strutils, sugar, tables]
type
Shape = ref object
loses: Shape
beats: Shape
score: int
let rock = Shape(score: 1)
let paper = Shape(beats: rock, score: 2)
rock.loses = paper
let scissors = Shape(loses: rock, beats: paper, score: 3)
rock.beats = scissors
paper.loses = scissors
let charToMove =
{ "A": rock
, "B": paper
, "C": scissors
, "X": rock
, "Y": paper
, "Z": scissors
}.toTable
let input = readAll(stdin).strip().split('\n')
let rounds = collect:
for game in input:
let s = game.split(' ')
(charToMove[s[0]], charToMove[s[1]])
var score = 0
for (opponent, me) in rounds:
score += me.score
if me.beats == opponent:
score += 6
elif me == opponent:
score += 3
echo score

47
2022/day02/day02_2.nim Normal file
View file

@ -0,0 +1,47 @@
import std/[strutils, sugar, tables]
type
Shape = ref object
loses: Shape
beats: Shape
score: int
Strat = enum Lose, Draw, Win
let rock = Shape(score: 1)
let paper = Shape(beats: rock, score: 2)
rock.loses = paper
let scissors = Shape(loses: rock, beats: paper, score: 3)
rock.beats = scissors
paper.loses = scissors
let charToMove =
{ "A": rock
, "B": paper
, "C": scissors
}.toTable
let charToStrat =
{ "X": Lose
, "Y": Draw
, "Z": Win
}.toTable
let input = readAll(stdin).strip().split('\n')
let rounds = collect:
for game in input:
let s = game.split(' ')
(charToMove[s[0]], charToStrat[s[1]])
var score = 0
for (opponent, strat) in rounds:
case strat
of Lose:
score += opponent.beats.score
of Draw:
score += opponent.score
score += 3
of Win:
score += opponent.loses.score
score += 6
echo score