AdventOfCode/2022/day2.nim

30 lines
806 B
Nim
Raw Normal View History

{.hint[name]: off.} # snake_case > camelCase
from std/math import floor_mod
2022-12-02 23:25:43 +10:30
import sequtils
import strutils
import tables
2022-12-02 23:47:20 +10:30
proc `%%` (a: int, b: int): int = floor_mod a, b
2022-12-02 23:25:43 +10:30
2022-12-02 23:47:20 +10:30
let move_map = to_table {'A': 1, 'B': 2, 'C': 3, 'X': 1, 'Y': 2, 'Z': 3}
2022-12-03 17:22:53 +10:30
let rounds = split_lines strip read_file "input/2"
2022-12-02 23:25:43 +10:30
2022-12-02 23:47:20 +10:30
proc round_score(us: int, them: int): int = us + [3, 6, 0][(us-them) %% 3]
2022-12-02 23:25:43 +10:30
# Part 1
proc round_1_score(round: string): int =
let them = move_map[round[0]]
let us = move_map[round[2]]
2022-12-02 23:47:20 +10:30
round_score us, them
2022-12-02 23:25:43 +10:30
echo rounds.map(round_1_score).fold_l(a + b)
2022-12-02 23:25:43 +10:30
# Part 2
proc round_2_score(round: string): int =
2022-12-02 23:47:20 +10:30
let them = move_map[round[0]]
let us_delta = move_map[round[2]] - 2
2022-12-02 23:47:20 +10:30
let us = ((them + us_delta - 1) %% 3) + 1
round_score us, them
2022-12-02 23:25:43 +10:30
echo rounds.map(round_2_score).fold_l(a + b)