parent
1dea96cca9
commit
cb77b4797a
@ -0,0 +1,70 @@ |
||||
const std = @import("std"); |
||||
|
||||
// Although this function looks imperative, note that its job is to |
||||
// declaratively construct a build graph that will be executed by an external |
||||
// runner. |
||||
pub fn build(b: *std.Build) void { |
||||
// Standard target options allows the person running `zig build` to choose |
||||
// what target to build for. Here we do not override the defaults, which |
||||
// means any target is allowed, and the default is native. Other options |
||||
// for restricting supported target set are available. |
||||
const target = b.standardTargetOptions(.{}); |
||||
|
||||
// Standard optimization options allow the person running `zig build` to select |
||||
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not |
||||
// set a preferred release mode, allowing the user to decide how to optimize. |
||||
const optimize = b.standardOptimizeOption(.{}); |
||||
|
||||
const exe = b.addExecutable(.{ |
||||
.name = "day07-hard", |
||||
// In this case the main source file is merely a path, however, in more |
||||
// complicated build scripts, this could be a generated file. |
||||
.root_source_file = .{ .path = "src/main.zig" }, |
||||
.target = target, |
||||
.optimize = optimize, |
||||
}); |
||||
|
||||
// This declares intent for the executable to be installed into the |
||||
// standard location when the user invokes the "install" step (the default |
||||
// step when running `zig build`). |
||||
b.installArtifact(exe); |
||||
|
||||
// This *creates* a Run step in the build graph, to be executed when another |
||||
// step is evaluated that depends on it. The next line below will establish |
||||
// such a dependency. |
||||
const run_cmd = b.addRunArtifact(exe); |
||||
|
||||
// By making the run step depend on the install step, it will be run from the |
||||
// installation directory rather than directly from within the cache directory. |
||||
// This is not necessary, however, if the application depends on other installed |
||||
// files, this ensures they will be present and in the expected location. |
||||
run_cmd.step.dependOn(b.getInstallStep()); |
||||
|
||||
// This allows the user to pass arguments to the application in the build |
||||
// command itself, like this: `zig build run -- arg1 arg2 etc` |
||||
if (b.args) |args| { |
||||
run_cmd.addArgs(args); |
||||
} |
||||
|
||||
// This creates a build step. It will be visible in the `zig build --help` menu, |
||||
// and can be selected like this: `zig build run` |
||||
// This will evaluate the `run` step rather than the default, which is "install". |
||||
const run_step = b.step("run", "Run the app"); |
||||
run_step.dependOn(&run_cmd.step); |
||||
|
||||
// Creates a step for unit testing. This only builds the test executable |
||||
// but does not run it. |
||||
const unit_tests = b.addTest(.{ |
||||
.root_source_file = .{ .path = "src/main.zig" }, |
||||
.target = target, |
||||
.optimize = optimize, |
||||
}); |
||||
|
||||
const run_unit_tests = b.addRunArtifact(unit_tests); |
||||
|
||||
// Similar to creating the run step earlier, this exposes a `test` step to |
||||
// the `zig build --help` menu, providing a way for the user to request |
||||
// running the unit tests. |
||||
const test_step = b.step("test", "Run unit tests"); |
||||
test_step.dependOn(&run_unit_tests.step); |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,5 @@ |
||||
32T3K 765 |
||||
T55J5 684 |
||||
KK677 28 |
||||
KTJJT 220 |
||||
QQQJA 483 |
@ -0,0 +1,219 @@ |
||||
const std = @import("std"); |
||||
|
||||
fn StackList(comptime T: type, comptime capacity_type: type, comptime capacity: capacity_type) type { |
||||
return struct { |
||||
const Self = @This(); |
||||
mem: [capacity]T, |
||||
length: capacity_type, |
||||
|
||||
fn add(self: *Self, value: T) void { |
||||
self.mem[self.length] = value; |
||||
self.length += 1; |
||||
} |
||||
|
||||
fn has(self: *Self, needle: T) bool { |
||||
for (0..self.length) |i| { |
||||
if (self.mem[i] == needle) { |
||||
return true; |
||||
} |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
fn getMutableSlice(self: *Self) []T { |
||||
return (&self.mem)[0..self.length]; |
||||
} |
||||
|
||||
fn getSlice(self: *const Self) []const T { |
||||
return self.mem[0..self.length]; |
||||
} |
||||
|
||||
fn init() Self { |
||||
return Self{ |
||||
.mem = undefined, |
||||
.length = 0, |
||||
}; |
||||
} |
||||
}; |
||||
} |
||||
|
||||
const Rank = enum(u8) { |
||||
Five = 6, |
||||
Four = 5, |
||||
FullHouse = 4, |
||||
Three = 3, |
||||
TwoPair = 2, |
||||
OnePair = 1, |
||||
HighCard = 0, |
||||
}; |
||||
|
||||
const Hand = packed struct(u64) { |
||||
bid: u16, |
||||
card5: u8, |
||||
card4: u8, |
||||
card3: u8, |
||||
card2: u8, |
||||
card1: u8, |
||||
rank: Rank, |
||||
}; |
||||
|
||||
const HandSet = StackList(Hand, usize, 1000); |
||||
|
||||
fn parseCard(char: u8) u8 { |
||||
return switch (char) { |
||||
'2'...'9' => char - '0', |
||||
'T' => 10, |
||||
'J' => 1, |
||||
'Q' => 12, |
||||
'K' => 13, |
||||
'A' => 14, |
||||
else => unreachable, |
||||
}; |
||||
} |
||||
|
||||
fn getRank(cards: [5]u8) Rank { |
||||
var quantities = std.mem.zeroes([6]u8); |
||||
var jokers: u8 = 0; |
||||
for (cards) |first_card| { |
||||
if (first_card == 1) { |
||||
jokers += 1; |
||||
continue; |
||||
} |
||||
|
||||
var matches: u4 = 0; |
||||
for (cards) |second_card| { |
||||
if (first_card == second_card) { |
||||
matches += 1; |
||||
} |
||||
} |
||||
|
||||
quantities[matches] += 1; |
||||
} |
||||
|
||||
if (jokers >= 4) { |
||||
return .Five; |
||||
} |
||||
|
||||
if (jokers == 3) { |
||||
if (quantities[2] != 0) { |
||||
return .Five; |
||||
} |
||||
|
||||
return .Four; |
||||
} |
||||
|
||||
if (jokers == 2) { |
||||
if (quantities[3] != 0) { |
||||
return .Five; |
||||
} |
||||
|
||||
if (quantities[2] != 0) { |
||||
return .Four; |
||||
} |
||||
|
||||
return .Three; |
||||
} |
||||
|
||||
if (jokers == 1) { |
||||
if (quantities[4] != 0) { |
||||
return .Five; |
||||
} |
||||
|
||||
if (quantities[3] != 0) { |
||||
return .Four; |
||||
} |
||||
|
||||
if (quantities[2] > 2) { |
||||
return .FullHouse; |
||||
} |
||||
|
||||
if (quantities[2] > 0) { |
||||
return .Three; |
||||
} |
||||
|
||||
return .OnePair; |
||||
} |
||||
|
||||
if (quantities[5] != 0) { |
||||
return .Five; |
||||
} |
||||
|
||||
if (quantities[4] != 0) { |
||||
return .Four; |
||||
} |
||||
|
||||
if (quantities[3] != 0) { |
||||
if (quantities[2] != 0) { |
||||
return .FullHouse; |
||||
} else { |
||||
return .Three; |
||||
} |
||||
} |
||||
|
||||
if (quantities[2] > 2) { |
||||
return .TwoPair; |
||||
} |
||||
|
||||
if (quantities[2] != 0) { |
||||
return .OnePair; |
||||
} |
||||
|
||||
return .HighCard; |
||||
} |
||||
|
||||
fn addDigit(result: anytype, digit: u8) void { |
||||
result.* = (result.* * 10) + (digit - '0'); |
||||
} |
||||
|
||||
fn parseHand(line: []u8) Hand { |
||||
var cards: [5]u8 = undefined; |
||||
var i: usize = 0; |
||||
while (i < 5) : (i += 1) { |
||||
cards[i] = parseCard(line[i]); |
||||
} |
||||
|
||||
var bid: u16 = 0; |
||||
i = 6; |
||||
while (i < line.len) : (i += 1) { |
||||
addDigit(&bid, line[i]); |
||||
} |
||||
|
||||
return .{ |
||||
.rank = getRank(cards), |
||||
.card1 = cards[0], |
||||
.card2 = cards[1], |
||||
.card3 = cards[2], |
||||
.card4 = cards[3], |
||||
.card5 = cards[4], |
||||
.bid = bid, |
||||
}; |
||||
} |
||||
|
||||
fn cmpByValue(context: void, a: Hand, b: Hand) bool { |
||||
return std.sort.asc(u64)(context, @bitCast(a), @bitCast(b)); |
||||
} |
||||
|
||||
fn solve(hands: *HandSet) u64 { |
||||
std.sort.heap(Hand, hands.getMutableSlice(), {}, cmpByValue); |
||||
var result: u64 = 0; |
||||
for (hands.getSlice(), 1..) |hand, i| { |
||||
result += i * hand.bid; |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
pub fn main() !void { |
||||
const stdout = std.io.getStdOut().writer(); |
||||
|
||||
const raw_in = std.io.getStdIn(); |
||||
var buffered_reader = std.io.bufferedReader(raw_in.reader()); |
||||
var reader = buffered_reader.reader(); |
||||
|
||||
var hands = HandSet.init(); |
||||
var line_buffer: [1000]u8 = undefined; |
||||
while (try reader.readUntilDelimiterOrEof(&line_buffer, '\n')) |line| { |
||||
hands.add(parseHand(line)); |
||||
} |
||||
try stdout.print("{d}\n", .{solve(&hands)}); |
||||
} |
Loading…
Reference in new issue