-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02_2.nim
88 lines (75 loc) · 1.8 KB
/
day02_2.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
if paramCount() != 1:
echo "Usage: ./dayXX <input file>"
quit(1)
let filename = paramStr(1)
if not fileExists(filename):
echo "File not found: ", filename
quit(1)
## RUN: TEST
# RUN: FULL
import std/strutils
import std/sequtils
type
play = enum
rock, paper, scissors
result = enum
loose, draw, win
strategy = tuple
play: play
result: result
var games: seq[strategy]
for line in lines(filename):
let parts = line.split(" ").map(proc (x: string): char = x[0]);
var
p: play
r: result
case parts[0]:
of 'A': p = rock
of 'B': p = paper
of 'C': p = scissors
else: raise newException(ValueError, "Invalid token: " & parts[0])
case parts[1]:
of 'X': r = loose
of 'Y': r = draw
of 'Z': r = win
else: raise newException(ValueError, "Invalid token: " & parts[1])
games.add((play: p, result: r))
proc calculate_play(x: play, y: result): play =
case y
of win:
case x
of rock: paper
of paper: scissors
of scissors: rock
of draw:
x
of loose:
case x
of rock: scissors
of paper: rock
of scissors: paper
proc calcualte_result(x,y: play): int =
case x
of rock:
case y
of rock: 3
of paper: 6
of scissors: 0
of paper:
case y
of rock: 0
of paper: 3
of scissors: 6
of scissors:
case y
of rock: 6
of paper: 0
of scissors: 3
let bonus: array[play, int] = [1,2,3]
var scores = seq[int](@[])
for game in games:
let play = calculate_play(game.play, game.result)
scores.add(calcualte_result(game.play, play) + bonus[play])
# scores.add(result(game[0], game[1]) + bonus[game[1]])
echo scores.foldl(a + b)