Solve day 2

This commit is contained in:
Tyler Hallada 2022-12-02 01:05:23 -05:00
parent 1b78bd2980
commit e17b7a8e01
4 changed files with 2622 additions and 0 deletions

101
src/day02.zig Normal file
View File

@ -0,0 +1,101 @@
const std = @import("std");
pub const input = @embedFile("input/day02.txt");
const test_input = @embedFile("input/day02_test1.txt");
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
const allocator = gpa.allocator();
const Choice = enum(usize) {
Rock = 1,
Paper = 2,
Scissors = 3,
fn from_opponent_char(char: u8) Choice {
return switch (char) {
'A' => Choice.Rock,
'B' => Choice.Paper,
'C' => Choice.Scissors,
else => @panic("invalid opponent char!"),
};
}
fn from_me_char(char: u8) Choice {
return switch (char) {
'X' => Choice.Rock,
'Y' => Choice.Paper,
'Z' => Choice.Scissors,
else => @panic("invalid me char!"),
};
}
fn from_outcome(self: Choice, outcome_char: u8) Choice {
return switch (outcome_char) {
'X' => self.lose_choice(),
'Y' => self,
'Z' => self.win_choice(),
else => @panic("invalid outcome char!"),
};
}
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;
}
};
pub fn solve_part1(data: []const u8) usize {
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, " ");
var opponent = Choice.from_opponent_char(player_choices.next().?[0]);
var me = Choice.from_me_char(player_choices.next().?[0]);
score += @enumToInt(me);
if (me.beats(opponent)) {
score += 6;
} else if (me.ties(opponent)) {
score += 3;
}
}
return score;
}
pub fn solve_part2(data: []const u8) usize {
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, " ");
var opponent = Choice.from_opponent_char(player_choices.next().?[0]);
var me = opponent.from_outcome(player_choices.next().?[0]);
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);
}

2500
src/input/day02.txt Executable file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,3 @@
A Y
B X
C Z

View File

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const day01 = @import("day01.zig"); const day01 = @import("day01.zig");
const day02 = @import("day02.zig");
const utils = @import("utils.zig"); const utils = @import("utils.zig");
pub fn main() !void { pub fn main() !void {
@ -25,6 +26,22 @@ pub fn main() !void {
try stdout.print("Part 2: {} ({d:.2} {s})\n", .{ part2, part2_time.value, part2_time.unit.abbr() }); try stdout.print("Part 2: {} ({d:.2} {s})\n", .{ part2, part2_time.value, part2_time.unit.abbr() });
} }
{
try stdout.print("\nDay 2\n", .{});
const part1_start = timer.read();
var part1 = day02.solve_part1(day02.input);
const part1_end = timer.read();
const part1_time = utils.HumanTime.new(part1_end - part1_start);
try stdout.print("Part 1: {} ({d:.2} {s})\n", .{ part1, part1_time.value, part1_time.unit.abbr() });
const part2_start = timer.read();
var part2 = day02.solve_part2(day02.input);
const part2_end = timer.read();
const part2_time = utils.HumanTime.new(part2_end - part2_start);
try stdout.print("Part 2: {} ({d:.2} {s})\n", .{ part2, part2_time.value, part2_time.unit.abbr() });
}
try bw.flush(); try bw.flush();
} }
@ -32,5 +49,6 @@ test {
std.testing.refAllDecls(@This()); std.testing.refAllDecls(@This());
_ = day01; _ = day01;
_ = day02;
_ = utils; _ = utils;
} }