Add solution for 2023 day 07 part 1

This commit is contained in:
Patrick Auernig 2023-12-11 14:40:44 +01:00
parent dbb917c8ef
commit c438d7d665
6 changed files with 1159 additions and 2 deletions

View File

@ -10,7 +10,7 @@
| [04] | ✓ | ✓ | [Zig] |
| [05] | ✓ | ✓ | [Zig] |
| [06] | ✓ | ✓ | [Zig] |
| [07] | | | |
| [07] | ✓ | | [Zig] |
| [08] | | | |
| [09] | | | |
| [10] | | | |

115
2023/day-07/common.zig Normal file
View File

@ -0,0 +1,115 @@
const std = @import("std");
const alloc = std.heap.page_allocator;
const List = std.ArrayList;
const OccuranceMap = std.AutoHashMap(Card, u8);
pub const Hand = struct {
cards: [5]Card,
bid: u64,
type: HandType,
};
pub const HandType = enum {
Five,
Four,
FullHouse,
Three,
TwoPair,
OnePair,
HighCard,
pub fn as_u8(self: HandType) u8 {
return switch (self) {
.Five => 7,
.Four => 6,
.FullHouse => 5,
.Three => 4,
.TwoPair => 3,
.OnePair => 2,
.HighCard => 1,
};
}
};
pub const Card = u8;
pub fn parse(data: []const u8) !List(Hand) {
var lines = std.mem.tokenizeScalar(u8, data, '\n');
var hands = List(Hand).init(alloc);
while (lines.next()) |line| {
var line_parts = std.mem.tokenizeScalar(u8, line, ' ');
const cards_part = line_parts.next().?;
var cards: [5]Card = undefined;
var occurances = OccuranceMap.init(alloc);
defer occurances.deinit();
for (cards_part, 0..) |card, i| {
const card_num = switch (card) {
'A' => 14,
'K' => 13,
'Q' => 12,
'J' => 11,
'T' => 10,
'2'...'9' => card - 48,
else => unreachable,
};
cards[i] = card_num;
if (occurances.get(card_num)) |val| {
try occurances.put(card_num, val + 1);
} else {
try occurances.put(card_num, 1);
}
}
const hand_type = determine_hand_type(&occurances);
const bid_part = line_parts.next().?;
const bid = try std.fmt.parseInt(u64, bid_part, 10);
const hand = .{
.cards = cards,
.bid = bid,
.type = hand_type,
};
try hands.append(hand);
}
return hands;
}
fn determine_hand_type(occurances: *const OccuranceMap) HandType {
switch (occurances.count()) {
1 => return HandType.Five,
2 => {
var iter = occurances.valueIterator();
while (iter.next()) |val| {
switch (val.*) {
1, 4 => return HandType.Four,
else => return HandType.FullHouse,
}
}
},
3 => {
var iter = occurances.valueIterator();
while (iter.next()) |val| {
switch (val.*) {
3 => return HandType.Three,
2 => return HandType.TwoPair,
else => continue,
}
unreachable;
}
},
4 => return HandType.OnePair,
5 => return HandType.HighCard,
else => unreachable,
}
unreachable;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483

37
2023/day-07/part_one.zig Normal file
View File

@ -0,0 +1,37 @@
const std = @import("std");
const common = @import("common.zig");
const Hand = common.Hand;
const data = @embedFile("inputs/puzzle.txt");
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
const hands = try common.parse(data);
defer hands.deinit();
var sum: u64 = 0;
std.mem.sort(Hand, hands.items, {}, compare_hands);
for (hands.items, 1..) |hand, rank| {
const value = hand.bid * rank;
sum += value;
}
try stdout.print("{d}\n", .{sum});
}
fn compare_hands(_: void, lhs: Hand, rhs: Hand) bool {
if (lhs.type == rhs.type) {
for (lhs.cards, rhs.cards) |lcard, rcard| {
if (lcard != rcard) {
return lcard < rcard;
}
}
} else {
return lhs.type.as_u8() < rhs.type.as_u8();
}
return false;
}

View File

@ -10,4 +10,4 @@
- [2020](2020/README.md) (20% completed)
- [2021](2021/README.md) (68% completed)
- [2022](2022/README.md) (54% completed)
- [2023](2023/README.md) (24% completed)
- [2023](2023/README.md) (26% completed)