more Nim syntax experimentation

This commit is contained in:
Luke Hubmayer-Werner 2022-12-02 23:47:20 +10:30
parent 46e18b1c1e
commit 04ab7a2c55
1 changed files with 8 additions and 8 deletions

View File

@ -4,26 +4,26 @@ import sequtils
import strutils
import tables
proc `%%` (a: int, b: int): int = floor_mod(a, b)
proc `%%` (a: int, b: int): int = floor_mod a, b
let move_map = {'A': 1, 'B': 2, 'C': 3, 'X': 1, 'Y': 2, 'Z': 3}.to_table
let move_map = to_table {'A': 1, 'B': 2, 'C': 3, 'X': 1, 'Y': 2, 'Z': 3}
let rounds = split_lines strip read_file "day2-input"
proc round_score(us: int, them: int): int = us + [3, 6, 0][(us-them) %% 3]
# Part 1
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
round_score us, them
echo rounds.map(round_1_score).fold_l(a + b)
# Part 2
proc round_2_score(round: string): int =
let them: int = move_map[round[0]]
let them = 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
let us = ((them + us_delta - 1) %% 3) + 1
round_score us, them
echo rounds.map(round_2_score).fold_l(a + b)