diff options
| author | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2024-06-08 13:50:47 +0300 |
|---|---|---|
| committer | Orfeas <38209077+0xfea5@users.noreply.github.com> | 2024-06-08 13:50:47 +0300 |
| commit | 7be570c4a6e86fb7060f0bc06910ca57003dfe90 (patch) | |
| tree | a998f976596cb902cd8988d74f756b72660bc29c /day02/solution.nim | |
| parent | Day 21 (part1) (diff) | |
| download | aoc22-main.tar.gz aoc22-main.zip | |
Diffstat (limited to 'day02/solution.nim')
| -rw-r--r-- | day02/solution.nim | 51 |
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 @@ | |||
| 1 | import std/strutils | ||
| 2 | import std/sequtils | ||
| 3 | |||
| 4 | let content = readFile("./input.txt").strip().split("\n") | ||
| 5 | let 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 | |||
| 15 | proc 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 | |||
| 25 | let 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 | |||
| 37 | echo solve(pt1) | ||
| 38 | |||
| 39 | let 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 | |||
| 51 | echo solve(pt2) | ||
