aboutsummaryrefslogtreecommitdiffstats
path: root/day2/solution.nim
blob: 8bbd95b6044b9c26540424ffd5e9731537aa95ae (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
52
53
import std/strutils
import std/sequtils
import std/tables

let content = readFile("./input.txt").split("\n")
let rounds = map(
    # Filter out empty lines
    filter(content, proc(line: string): bool = not line.isEmptyOrWhitespace()),
    # Split line into pair of strings
    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)