aboutsummaryrefslogtreecommitdiffstats
path: root/day02/solution.nim
diff options
context:
space:
mode:
Diffstat (limited to 'day02/solution.nim')
-rw-r--r--day02/solution.nim51
1 files changed, 51 insertions, 0 deletions
diff --git a/day02/solution.nim b/day02/solution.nim
new file mode 100644
index 0000000..8a794d3
--- /dev/null
+++ b/day02/solution.nim
@@ -0,0 +1,51 @@
1import std/strutils
2import std/sequtils
3
4let content = readFile("./input.txt").strip().split("\n")
5let rounds = map(
6 content,
7 # Split line into pair of characters
8 proc(round: string): tuple[other: char, self: char] =
9 var splitted = round.split(" ")
10 (splitted[0][0], splitted[1][0]))
11
12# example: rounds = [('A', 'Z'), ('C', 'Y')]
13# echo rounds
14
15proc solve(dScore: array[3, array[3, int]]): int =
16 var score = 0
17 for round in rounds:
18 let
19 i = ord(round.other) - ord('A')
20 j = ord(round.self) - ord('X')
21 score += dScore[i][j]
22
23 return score
24
25let pt1 = [
26 # A = Rock
27 # X Y Z
28 [1 + 3, 2 + 6, 3 + 0],
29 # B = Paper
30 # X Y Z
31 [1 + 0, 2 + 3, 3 + 6],
32 # C = Scissors
33 # X Y Z
34 [1 + 6, 2 + 0, 3 + 3],
35]
36
37echo solve(pt1)
38
39let pt2 = [
40 # A = Rock
41 # X Y Z
42 [3 + 0, 1 + 3, 2 + 6],
43 # B = Paper
44 # X Y Z
45 [1 + 0, 2 + 3, 3 + 6],
46 # C = Scissors
47 # X Y Z
48 [2 + 0, 3 + 3, 1 + 6],
49]
50
51echo solve(pt2)