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