Clean up day03 code & and use tokenize in day02

This commit is contained in:
Tyler Hallada 2022-12-03 19:21:53 -05:00
parent 453a9ebeb8
commit a1c8610cc3
2 changed files with 26 additions and 31 deletions

View File

@ -54,11 +54,10 @@ 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.tokenize(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; var player_choices = std.mem.tokenize(u8, line, " ");
var player_choices = std.mem.split(u8, line, " ");
var opponent = try Choice.from_opponent_char(player_choices.next().?[0]); var opponent = try Choice.from_opponent_char(player_choices.next().?[0]);
var me = try 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);
@ -72,11 +71,10 @@ pub fn solve_part1(data: []const u8) !usize {
} }
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.tokenize(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; var player_choices = std.mem.tokenize(u8, line, " ");
var player_choices = std.mem.split(u8, line, " ");
var opponent = try Choice.from_opponent_char(player_choices.next().?[0]); var opponent = try Choice.from_opponent_char(player_choices.next().?[0]);
var me = try opponent.from_outcome_char(player_choices.next().?[0]); var me = try opponent.from_outcome_char(player_choices.next().?[0]);
score += @enumToInt(me); score += @enumToInt(me);

View File

@ -3,23 +3,21 @@ const std = @import("std");
pub const input = @embedFile("input/day03.txt"); pub const input = @embedFile("input/day03.txt");
const test_input = @embedFile("input/day03_test1.txt"); const test_input = @embedFile("input/day03_test1.txt");
fn ascii_to_priority(char: u8) error{InvalidChar}!u8 {
if (char >= 97 and char <= 122) {
return char - 96;
} else if (char >= 65 and char <= 90) {
return char - 64 + 26;
} else {
return error.InvalidChar;
}
}
const PrioritySet = std.StaticBitSet(26 * 2 + 1); const PrioritySet = std.StaticBitSet(26 * 2 + 1);
const EmptyGroup = [3]PrioritySet{ PrioritySet.initEmpty(), PrioritySet.initEmpty(), PrioritySet.initEmpty() };
fn ascii_to_priority(char: u8) error{InvalidChar}!u8 {
return switch (char) {
'a'...'z' => char - 'a' + 1,
'A'...'Z' => char - 'A' + 27,
else => error.InvalidChar,
};
}
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.tokenize(u8, data, "\n");
var sum: usize = 0; var sum: usize = 0;
while (lines.next()) |line| { while (lines.next()) |line| {
if (std.mem.eql(u8, line, "")) break;
var compartment1 = PrioritySet.initEmpty(); var compartment1 = PrioritySet.initEmpty();
var compartment2 = PrioritySet.initEmpty(); var compartment2 = PrioritySet.initEmpty();
var i: usize = 0; var i: usize = 0;
@ -38,18 +36,14 @@ pub fn solve_part1(data: []const u8) !usize {
} }
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.tokenize(u8, data, "\n");
var sum: usize = 0; var sum: usize = 0;
var i: usize = 0; var i: usize = 0;
var group = [3]PrioritySet{ PrioritySet.initEmpty(), PrioritySet.initEmpty(), PrioritySet.initEmpty() }; var group = EmptyGroup;
while (i < 3) { while (i < 3) : (i = (i + 1) % 3) {
const line = lines.next().?; const line = lines.next() orelse break;
if (std.mem.eql(u8, line, "")) break;
var j: usize = 0; for (line) |item| group[i].set(try ascii_to_priority(item));
while (j < line.len) : (j += 1) {
group[i].set(try ascii_to_priority(line[j]));
}
if (i == 2) { if (i == 2) {
group[0].setIntersection(group[1]); group[0].setIntersection(group[1]);
@ -57,16 +51,19 @@ pub fn solve_part2(data: []const u8) !usize {
const priority = group[0].findFirstSet().?; const priority = group[0].findFirstSet().?;
sum += priority; sum += priority;
i = 0; group = EmptyGroup;
group = [3]PrioritySet{ PrioritySet.initEmpty(), PrioritySet.initEmpty(), PrioritySet.initEmpty() };
continue;
} }
i += 1;
} }
return sum; return sum;
} }
test "ascii_to_priority" {
try std.testing.expectEqual(ascii_to_priority('a'), 1);
try std.testing.expectEqual(ascii_to_priority('z'), 26);
try std.testing.expectEqual(ascii_to_priority('A'), 27);
try std.testing.expectEqual(ascii_to_priority('Z'), 52);
}
test "solves part1" { test "solves part1" {
try std.testing.expectEqual(solve_part1(test_input), 157); try std.testing.expectEqual(solve_part1(test_input), 157);
} }