Compare commits
3 commits
6d677300ee
...
59157c5a11
Author | SHA1 | Date | |
---|---|---|---|
59157c5a11 | |||
462ab21364 | |||
be7c251662 |
1 changed files with 49 additions and 33 deletions
|
@ -1,44 +1,60 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Allocator = std.mem.Allocator;
|
const gpa = @import("util.zig").gpa;
|
||||||
const List = std.ArrayList;
|
const ArrayList = std.ArrayList;
|
||||||
const Map = std.AutoHashMap;
|
const tokenize = std.mem.tokenize;
|
||||||
const StrMap = std.StringHashMap;
|
const parseInt = std.fmt.parseInt;
|
||||||
const BitSet = std.DynamicBitSet;
|
const print = std.debug.print;
|
||||||
const Str = []const u8;
|
|
||||||
|
|
||||||
const util = @import("util.zig");
|
const data = @embedFile("input");
|
||||||
const gpa = util.gpa;
|
|
||||||
|
|
||||||
const data = @embedFile("../data/day02.txt");
|
const Rule = struct {
|
||||||
|
a: u8, b: u8, char: u8, password: []const u8
|
||||||
|
};
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
|
const rules = try parse();
|
||||||
|
print("Part 1: {d}\n", .{part1(rules)});
|
||||||
|
print("Part 2: {d}\n", .{part2(rules)});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Useful stdlib functions
|
|
||||||
const tokenize = std.mem.tokenize;
|
|
||||||
const split = std.mem.split;
|
|
||||||
const indexOf = std.mem.indexOfScalar;
|
|
||||||
const indexOfAny = std.mem.indexOfAny;
|
|
||||||
const indexOfStr = std.mem.indexOfPosLinear;
|
|
||||||
const lastIndexOf = std.mem.lastIndexOfScalar;
|
|
||||||
const lastIndexOfAny = std.mem.lastIndexOfAny;
|
|
||||||
const lastIndexOfStr = std.mem.lastIndexOfLinear;
|
|
||||||
const trim = std.mem.trim;
|
|
||||||
const sliceMin = std.mem.min;
|
|
||||||
const sliceMax = std.mem.max;
|
|
||||||
|
|
||||||
const parseInt = std.fmt.parseInt;
|
fn parse() ![]Rule {
|
||||||
const parseFloat = std.fmt.parseFloat;
|
var it = tokenize(u8, data, "\n");
|
||||||
|
var rules = ArrayList(Rule).init(gpa);
|
||||||
|
while (it.next()) |line| {
|
||||||
|
var line_it = tokenize(u8, line, ": -");
|
||||||
|
const a = try parseInt(u8, line_it.next().?, 10);
|
||||||
|
const b = try parseInt(u8, line_it.next().?, 10);
|
||||||
|
const char = line_it.next().?[0];
|
||||||
|
const password = line_it.next().?;
|
||||||
|
try rules.append(Rule{.a = a, .b = b, .char = char, .password = password});
|
||||||
|
}
|
||||||
|
return rules.toOwnedSlice();
|
||||||
|
}
|
||||||
|
|
||||||
const min = std.math.min;
|
fn part1(rules: []Rule) !u16 {
|
||||||
const min3 = std.math.min3;
|
var valid_pw_count: u16 = 0;
|
||||||
const max = std.math.max;
|
for (rules) |rule| {
|
||||||
const max3 = std.math.max3;
|
var count: u8 = 0;
|
||||||
|
for (rule.password) |password_char| {
|
||||||
|
if (rule.char == password_char)
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
const print = std.debug.print;
|
if (rule.a <= count and count <= rule.b)
|
||||||
const assert = std.debug.assert;
|
valid_pw_count += 1;
|
||||||
|
}
|
||||||
|
return valid_pw_count;
|
||||||
|
}
|
||||||
|
|
||||||
const sort = std.sort.sort;
|
fn part2(rules: []Rule) !u16 {
|
||||||
const asc = std.sort.asc;
|
var valid_pw_count: u16 = 0;
|
||||||
const desc = std.sort.desc;
|
for (rules) |rule| {
|
||||||
|
const a_match = rule.password[rule.a-1] == rule.char;
|
||||||
|
const b_match = rule.password[rule.b-1] == rule.char;
|
||||||
|
// No xor in zig?
|
||||||
|
if (a_match and !b_match or !a_match and b_match)
|
||||||
|
valid_pw_count += 1;
|
||||||
|
}
|
||||||
|
return valid_pw_count;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue