diff --git a/2022/day2.nim b/2022/day2.nim index 7891213..dbcd0e7 100644 --- a/2022/day2.nim +++ b/2022/day2.nim @@ -1,28 +1,29 @@ -from std/math import floorMod +{.hint[name]: off.} # snake_case > camelCase +from std/math import floor_mod import sequtils import strutils import tables -proc `%%` (a: int, b: int): int = floorMod(a, b) +proc `%%` (a: int, b: int): int = floor_mod(a, b) -let moveMap = {'A': 1, 'B': 2, 'C': 3, 'X': 1, 'Y': 2, 'Z': 3}.toTable -let rounds = splitlines strip readFile "day2-input" +let move_map = {'A': 1, 'B': 2, 'C': 3, 'X': 1, 'Y': 2, 'Z': 3}.to_table +let rounds = split_lines strip read_file "day2-input" # Part 1 -proc round1Score(round: string): int = - let them = moveMap[round[0]] - let us = moveMap[round[2]] - let winScore = [3, 6, 0][(us - them) %% 3] - us + winScore +proc round_1_score(round: string): int = + let them = move_map[round[0]] + let us = move_map[round[2]] + let win_score = [3, 6, 0][(us - them) %% 3] + us + win_score -echo rounds.map(round1Score).foldl(a + b) +echo rounds.map(round_1_score).fold_l(a + b) # Part 2 -proc round2Score(round: string): int = - let them: int = moveMap[round[0]] - let usDelta = moveMap[round[2]] - 2 - let us: int = ((them + usDelta - 1) %% 3) + 1 - let winScore = [3, 6, 0][(us - them) %% 3] - us + winScore +proc round_2_score(round: string): int = + let them: int = move_map[round[0]] + let us_delta = move_map[round[2]] - 2 + let us: int = ((them + us_delta - 1) %% 3) + 1 + let win_score = [3, 6, 0][(us - them) %% 3] + us + win_score -echo rounds.map(round2Score).foldl(a + b) +echo rounds.map(round_2_score).fold_l(a + b)