Clean up day03 code & and use tokenize in day02
This commit is contained in:
parent
453a9ebeb8
commit
a1c8610cc3
@ -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);
|
||||||
|
@ -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");
|
||||||
|
|
||||||
|
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 {
|
fn ascii_to_priority(char: u8) error{InvalidChar}!u8 {
|
||||||
if (char >= 97 and char <= 122) {
|
return switch (char) {
|
||||||
return char - 96;
|
'a'...'z' => char - 'a' + 1,
|
||||||
} else if (char >= 65 and char <= 90) {
|
'A'...'Z' => char - 'A' + 27,
|
||||||
return char - 64 + 26;
|
else => error.InvalidChar,
|
||||||
} else {
|
};
|
||||||
return error.InvalidChar;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const PrioritySet = std.StaticBitSet(26 * 2 + 1);
|
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user