solve_day2 :: (test: bool) {
contents := read_entire_file(ifx test then "inputs/day2_test.txt" else "inputs/day2.txt");
lines := split(contents, "\n");
t: Table(string, u64);
table_add(*t, "A X", 3 + 1);
table_add(*t, "A Y", 6 + 2);
table_add(*t, "A Z", 0 + 3);
table_add(*t, "B X", 0 + 1);
table_add(*t, "B Y", 3 + 2);
table_add(*t, "B Z", 6 + 3);
table_add(*t, "C X", 6 + 1);
table_add(*t, "C Y", 0 + 2);
table_add(*t, "C Z", 3 + 3);
part1: u64 = 0;
part2: u64 = 0;
for line, line_index: lines {
han, s := table_find(*t, line);
part1 += han;
line[2] = translate_hand(line[0], line[2]);
han, s = table_find(*t, line);
part2 += han;
}
print("Part 1: %\n", part1);
print("Part 2: %\n", part2);
}
translate_hand :: (hand: u8, outcome: u8) -> u8 {
if outcome == {
case #char "X"; // LOSS
if hand == {
case #char "A"; return #char "Z";
case #char "B"; return #char "X";
case #char "C"; return #char "Y";
}
case #char "Y"; // DRAW
if hand == {
case #char "A"; return #char "X";
case #char "B"; return #char "Y";
case #char "C"; return #char "Z";
}
case #char "Z"; // WIN
if hand == {
case #char "A"; return #char "Y";
case #char "B"; return #char "Z";
case #char "C"; return #char "X";
}
}
return #char "Æ";
}
#scope_file