2022-12-02 06:05:23 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
pub const input = @embedFile("input/day02.txt");
|
|
|
|
const test_input = @embedFile("input/day02_test1.txt");
|
|
|
|
|
|
|
|
const Choice = enum(usize) {
|
|
|
|
Rock = 1,
|
|
|
|
Paper = 2,
|
|
|
|
Scissors = 3,
|
2022-12-02 15:49:37 +00:00
|
|
|
fn from_opponent_char(char: u8) error{InvalidChar}!Choice {
|
2022-12-02 06:05:23 +00:00
|
|
|
return switch (char) {
|
|
|
|
'A' => Choice.Rock,
|
|
|
|
'B' => Choice.Paper,
|
|
|
|
'C' => Choice.Scissors,
|
2022-12-02 15:49:37 +00:00
|
|
|
else => error.InvalidChar,
|
2022-12-02 06:05:23 +00:00
|
|
|
};
|
|
|
|
}
|
2022-12-02 15:49:37 +00:00
|
|
|
fn from_me_char(char: u8) error{InvalidChar}!Choice {
|
2022-12-02 06:05:23 +00:00
|
|
|
return switch (char) {
|
|
|
|
'X' => Choice.Rock,
|
|
|
|
'Y' => Choice.Paper,
|
|
|
|
'Z' => Choice.Scissors,
|
2022-12-02 15:49:37 +00:00
|
|
|
else => error.InvalidChar,
|
2022-12-02 06:05:23 +00:00
|
|
|
};
|
|
|
|
}
|
2022-12-02 15:49:37 +00:00
|
|
|
fn from_outcome_char(self: Choice, outcome_char: u8) error{InvalidChar}!Choice {
|
2022-12-02 06:05:23 +00:00
|
|
|
return switch (outcome_char) {
|
|
|
|
'X' => self.lose_choice(),
|
|
|
|
'Y' => self,
|
|
|
|
'Z' => self.win_choice(),
|
2022-12-02 15:49:37 +00:00
|
|
|
else => error.InvalidChar,
|
2022-12-02 06:05:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
fn lose_choice(self: Choice) Choice {
|
|
|
|
return switch (self) {
|
|
|
|
Choice.Rock => Choice.Scissors,
|
|
|
|
Choice.Paper => Choice.Rock,
|
|
|
|
Choice.Scissors => Choice.Paper,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
fn win_choice(self: Choice) Choice {
|
|
|
|
return switch (self) {
|
|
|
|
Choice.Rock => Choice.Paper,
|
|
|
|
Choice.Paper => Choice.Scissors,
|
|
|
|
Choice.Scissors => Choice.Rock,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
fn beats(self: Choice, other: Choice) bool {
|
|
|
|
return if (other.win_choice() == self) true else false;
|
|
|
|
}
|
|
|
|
fn ties(self: Choice, other: Choice) bool {
|
|
|
|
return self == other;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-02 15:49:37 +00:00
|
|
|
pub fn solve_part1(data: []const u8) !usize {
|
2022-12-02 06:05:23 +00:00
|
|
|
var lines = std.mem.split(u8, data, "\n");
|
|
|
|
var score: usize = 0;
|
|
|
|
while (lines.next()) |line| {
|
|
|
|
if (std.mem.eql(u8, line, "")) break;
|
|
|
|
var player_choices = std.mem.split(u8, line, " ");
|
2022-12-02 15:49:37 +00:00
|
|
|
var opponent = try Choice.from_opponent_char(player_choices.next().?[0]);
|
|
|
|
var me = try Choice.from_me_char(player_choices.next().?[0]);
|
2022-12-02 06:05:23 +00:00
|
|
|
score += @enumToInt(me);
|
|
|
|
if (me.beats(opponent)) {
|
|
|
|
score += 6;
|
|
|
|
} else if (me.ties(opponent)) {
|
|
|
|
score += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
2022-12-02 15:49:37 +00:00
|
|
|
pub fn solve_part2(data: []const u8) !usize {
|
2022-12-02 06:05:23 +00:00
|
|
|
var lines = std.mem.split(u8, data, "\n");
|
|
|
|
var score: usize = 0;
|
|
|
|
while (lines.next()) |line| {
|
|
|
|
if (std.mem.eql(u8, line, "")) break;
|
|
|
|
var player_choices = std.mem.split(u8, line, " ");
|
2022-12-02 15:49:37 +00:00
|
|
|
var opponent = try Choice.from_opponent_char(player_choices.next().?[0]);
|
|
|
|
var me = try opponent.from_outcome_char(player_choices.next().?[0]);
|
2022-12-02 06:05:23 +00:00
|
|
|
score += @enumToInt(me);
|
|
|
|
if (me.beats(opponent)) {
|
|
|
|
score += 6;
|
|
|
|
} else if (me.ties(opponent)) {
|
|
|
|
score += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
test "solves part1" {
|
|
|
|
try std.testing.expectEqual(solve_part1(test_input), 15);
|
|
|
|
}
|
|
|
|
|
|
|
|
test "solves part2" {
|
|
|
|
try std.testing.expectEqual(solve_part2(test_input), 12);
|
|
|
|
}
|