Clean up part 2, solve_day fn
This commit is contained in:
parent
e17b7a8e01
commit
2fbada26bf
@ -3,35 +3,32 @@ const std = @import("std");
|
|||||||
pub const input = @embedFile("input/day02.txt");
|
pub const input = @embedFile("input/day02.txt");
|
||||||
const test_input = @embedFile("input/day02_test1.txt");
|
const test_input = @embedFile("input/day02_test1.txt");
|
||||||
|
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
|
|
||||||
const allocator = gpa.allocator();
|
|
||||||
|
|
||||||
const Choice = enum(usize) {
|
const Choice = enum(usize) {
|
||||||
Rock = 1,
|
Rock = 1,
|
||||||
Paper = 2,
|
Paper = 2,
|
||||||
Scissors = 3,
|
Scissors = 3,
|
||||||
fn from_opponent_char(char: u8) Choice {
|
fn from_opponent_char(char: u8) error{InvalidChar}!Choice {
|
||||||
return switch (char) {
|
return switch (char) {
|
||||||
'A' => Choice.Rock,
|
'A' => Choice.Rock,
|
||||||
'B' => Choice.Paper,
|
'B' => Choice.Paper,
|
||||||
'C' => Choice.Scissors,
|
'C' => Choice.Scissors,
|
||||||
else => @panic("invalid opponent char!"),
|
else => error.InvalidChar,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
fn from_me_char(char: u8) Choice {
|
fn from_me_char(char: u8) error{InvalidChar}!Choice {
|
||||||
return switch (char) {
|
return switch (char) {
|
||||||
'X' => Choice.Rock,
|
'X' => Choice.Rock,
|
||||||
'Y' => Choice.Paper,
|
'Y' => Choice.Paper,
|
||||||
'Z' => Choice.Scissors,
|
'Z' => Choice.Scissors,
|
||||||
else => @panic("invalid me char!"),
|
else => error.InvalidChar,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
fn from_outcome(self: Choice, outcome_char: u8) Choice {
|
fn from_outcome_char(self: Choice, outcome_char: u8) error{InvalidChar}!Choice {
|
||||||
return switch (outcome_char) {
|
return switch (outcome_char) {
|
||||||
'X' => self.lose_choice(),
|
'X' => self.lose_choice(),
|
||||||
'Y' => self,
|
'Y' => self,
|
||||||
'Z' => self.win_choice(),
|
'Z' => self.win_choice(),
|
||||||
else => @panic("invalid outcome char!"),
|
else => error.InvalidChar,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
fn lose_choice(self: Choice) Choice {
|
fn lose_choice(self: Choice) Choice {
|
||||||
@ -56,14 +53,14 @@ const Choice = enum(usize) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn solve_part1(data: []const u8) usize {
|
pub fn solve_part1(data: []const u8) !usize {
|
||||||
var lines = std.mem.split(u8, data, "\n");
|
var lines = std.mem.split(u8, data, "\n");
|
||||||
var score: usize = 0;
|
var score: usize = 0;
|
||||||
while (lines.next()) |line| {
|
while (lines.next()) |line| {
|
||||||
if (std.mem.eql(u8, line, "")) break;
|
if (std.mem.eql(u8, line, "")) break;
|
||||||
var player_choices = std.mem.split(u8, line, " ");
|
var player_choices = std.mem.split(u8, line, " ");
|
||||||
var opponent = Choice.from_opponent_char(player_choices.next().?[0]);
|
var opponent = try Choice.from_opponent_char(player_choices.next().?[0]);
|
||||||
var me = Choice.from_me_char(player_choices.next().?[0]);
|
var me = try Choice.from_me_char(player_choices.next().?[0]);
|
||||||
score += @enumToInt(me);
|
score += @enumToInt(me);
|
||||||
if (me.beats(opponent)) {
|
if (me.beats(opponent)) {
|
||||||
score += 6;
|
score += 6;
|
||||||
@ -74,14 +71,14 @@ pub fn solve_part1(data: []const u8) usize {
|
|||||||
return score;
|
return score;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn solve_part2(data: []const u8) usize {
|
pub fn solve_part2(data: []const u8) !usize {
|
||||||
var lines = std.mem.split(u8, data, "\n");
|
var lines = std.mem.split(u8, data, "\n");
|
||||||
var score: usize = 0;
|
var score: usize = 0;
|
||||||
while (lines.next()) |line| {
|
while (lines.next()) |line| {
|
||||||
if (std.mem.eql(u8, line, "")) break;
|
if (std.mem.eql(u8, line, "")) break;
|
||||||
var player_choices = std.mem.split(u8, line, " ");
|
var player_choices = std.mem.split(u8, line, " ");
|
||||||
var opponent = Choice.from_opponent_char(player_choices.next().?[0]);
|
var opponent = try Choice.from_opponent_char(player_choices.next().?[0]);
|
||||||
var me = opponent.from_outcome(player_choices.next().?[0]);
|
var me = try opponent.from_outcome_char(player_choices.next().?[0]);
|
||||||
score += @enumToInt(me);
|
score += @enumToInt(me);
|
||||||
if (me.beats(opponent)) {
|
if (me.beats(opponent)) {
|
||||||
score += 6;
|
score += 6;
|
||||||
|
49
src/main.zig
49
src/main.zig
@ -4,43 +4,30 @@ const day01 = @import("day01.zig");
|
|||||||
const day02 = @import("day02.zig");
|
const day02 = @import("day02.zig");
|
||||||
const utils = @import("utils.zig");
|
const utils = @import("utils.zig");
|
||||||
|
|
||||||
|
fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.time.Timer) !void {
|
||||||
|
try stdout.print("Day {}\n", .{day_num});
|
||||||
|
|
||||||
|
const part1_start = timer.read();
|
||||||
|
var part1 = try day.solve_part1(day.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 = try day.solve_part2(day.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() });
|
||||||
|
}
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
const stdout_file = std.io.getStdOut().writer();
|
const stdout_file = std.io.getStdOut().writer();
|
||||||
var bw = std.io.bufferedWriter(stdout_file);
|
var bw = std.io.bufferedWriter(stdout_file);
|
||||||
const stdout = bw.writer();
|
const stdout = bw.writer();
|
||||||
var timer = std.time.Timer.start() catch @panic("need timer to benchmark solutions");
|
var timer = std.time.Timer.start() catch @panic("need timer to benchmark solutions");
|
||||||
|
|
||||||
{
|
try solve_day(1, day01, &stdout, &timer);
|
||||||
try stdout.print("Day 1\n", .{});
|
try solve_day(2, day02, &stdout, &timer);
|
||||||
|
|
||||||
const part1_start = timer.read();
|
|
||||||
var part1 = try day01.solve_part1(day01.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 = try day01.solve_part2(day01.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 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();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user