diff options
Diffstat (limited to 'day2/solution.nim')
| -rw-r--r-- | day2/solution.nim | 53 |
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 @@ | |||
| 1 | import std/strutils | ||
| 2 | import std/sequtils | ||
| 3 | import std/tables | ||
| 4 | |||
| 5 | let content = readFile("./input.txt").split("\n") | ||
| 6 | let 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 | |||
| 17 | proc 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 | |||
| 27 | let 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 | |||
| 39 | echo solve(pt1) | ||
| 40 | |||
| 41 | let 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 | |||
| 53 | echo solve(pt2) | ||
