aboutsummaryrefslogtreecommitdiffstats
path: root/day02/solution.nim
blob: 8a794d30dc14e23e5b3f58fec99efd689431088a (plain) (blame)
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
import std/strutils
import std/sequtils

let content = readFile("./input.txt").strip().split("\n")
let rounds = map(
    content,
    # Split line into pair of characters
    proc(round: string): tuple[other: char, self: char] = 
       var splitted = round.split(" ")
       (splitted[0][0], splitted[1][0]))

# example: rounds = [('A', 'Z'), ('C', 'Y')]
# echo rounds

proc solve(dScore: array[3, array[3, int]]): int = 
    var score = 0
    for round in rounds:
        let
            i = ord(round.other) - ord('A')
            j = ord(round.self) - ord('X')
        score += dScore[i][j]

    return score

let pt1 = [
    # A = Rock
    # X Y Z
    [1 + 3, 2 + 6, 3 + 0],
    # B = Paper
    # X Y Z
    [1 + 0, 2 + 3, 3 + 6],
    # C = Scissors
    # X Y Z
    [1 + 6, 2 + 0, 3 + 3],
]

echo solve(pt1)

let pt2 = [
    # A = Rock
    # X Y Z
    [3 + 0, 1 + 3, 2 + 6],
    # B = Paper
    # X Y Z
    [1 + 0, 2 + 3, 3 + 6],
    # C = Scissors
    # X Y Z
    [2 + 0, 3 + 3, 1 + 6],
]

echo solve(pt2)