QK4ZCTPGOONE5HB3DDYJIU74BX65DNEHEJSWXKYF5DPNGCJWS2KAC
Alice would gain 54 happiness units by sitting next to Bob.
Alice would lose 81 happiness units by sitting next to Carol.
Alice would lose 42 happiness units by sitting next to David.
Alice would gain 89 happiness units by sitting next to Eric.
Alice would lose 89 happiness units by sitting next to Frank.
Alice would gain 97 happiness units by sitting next to George.
Alice would lose 94 happiness units by sitting next to Mallory.
Bob would gain 3 happiness units by sitting next to Alice.
Bob would lose 70 happiness units by sitting next to Carol.
Bob would lose 31 happiness units by sitting next to David.
Bob would gain 72 happiness units by sitting next to Eric.
Bob would lose 25 happiness units by sitting next to Frank.
Bob would lose 95 happiness units by sitting next to George.
Bob would gain 11 happiness units by sitting next to Mallory.
Carol would lose 83 happiness units by sitting next to Alice.
Carol would gain 8 happiness units by sitting next to Bob.
Carol would gain 35 happiness units by sitting next to David.
Carol would gain 10 happiness units by sitting next to Eric.
Carol would gain 61 happiness units by sitting next to Frank.
Carol would gain 10 happiness units by sitting next to George.
Carol would gain 29 happiness units by sitting next to Mallory.
David would gain 67 happiness units by sitting next to Alice.
David would gain 25 happiness units by sitting next to Bob.
David would gain 48 happiness units by sitting next to Carol.
David would lose 65 happiness units by sitting next to Eric.
David would gain 8 happiness units by sitting next to Frank.
David would gain 84 happiness units by sitting next to George.
David would gain 9 happiness units by sitting next to Mallory.
Eric would lose 51 happiness units by sitting next to Alice.
Eric would lose 39 happiness units by sitting next to Bob.
Eric would gain 84 happiness units by sitting next to Carol.
Eric would lose 98 happiness units by sitting next to David.
Eric would lose 20 happiness units by sitting next to Frank.
Eric would lose 6 happiness units by sitting next to George.
Eric would gain 60 happiness units by sitting next to Mallory.
Frank would gain 51 happiness units by sitting next to Alice.
Frank would gain 79 happiness units by sitting next to Bob.
Frank would gain 88 happiness units by sitting next to Carol.
Frank would gain 33 happiness units by sitting next to David.
Frank would gain 43 happiness units by sitting next to Eric.
Frank would gain 77 happiness units by sitting next to George.
Frank would lose 3 happiness units by sitting next to Mallory.
George would lose 14 happiness units by sitting next to Alice.
George would lose 12 happiness units by sitting next to Bob.
George would lose 52 happiness units by sitting next to Carol.
George would gain 14 happiness units by sitting next to David.
George would lose 62 happiness units by sitting next to Eric.
George would lose 18 happiness units by sitting next to Frank.
George would lose 17 happiness units by sitting next to Mallory.
Mallory would lose 36 happiness units by sitting next to Alice.
Mallory would gain 76 happiness units by sitting next to Bob.
Mallory would lose 34 happiness units by sitting next to Carol.
Mallory would gain 37 happiness units by sitting next to David.
Mallory would gain 40 happiness units by sitting next to Eric.
Mallory would gain 18 happiness units by sitting next to Frank.
Mallory would gain 7 happiness units by sitting next to George.
const std = @import("std");
const Str = []const u8;
const PATH = "input/day13.txt";
pub fn first(allocator: std.mem.Allocator) !isize {
var tk = TableKnights{ .pairs = undefined };
tk.pairs = try parseInput(allocator, @embedFile(PATH));
defer tk.pairs.deinit();
tk.permute(0, tk.table.len);
return tk.max;
}
pub fn second(allocator: std.mem.Allocator) !isize {
var tk = TableKnights{ .pairs = undefined };
tk.pairs = try parseInput(allocator, @embedFile(PATH));
defer tk.pairs.deinit();
tk.permute(0, tk.table.len);
var min: isize = std.math.maxInt(isize);
// find the least good pair, replace that with myself (zeros)
var idx: usize = 0;
while (idx < tk.best.len) : (idx += 1) {
const val = tk.pairs.get(.{ tk.best[idx], tk.best[(idx + 1) % tk.best.len] }) orelse
tk.pairs.get(.{ tk.best[(idx + 1) % tk.best.len], tk.best[idx] }).?;
if (val < min) min = val;
}
return tk.max - min;
}
test "day13a" {
try std.testing.expectEqual(@as(isize, 709), try first(std.testing.allocator));
}
test "day13b" {
try std.testing.expectEqual(@as(isize, 668), try second(std.testing.allocator));
}
const Pairs = std.AutoHashMap([2]u8, isize);
const TableKnights = struct {
pairs: Pairs,
table: [8]u8 = .{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'M' },
best: [8]u8 = undefined,
max: isize = 0,
fn permute(self: *@This(), start: usize, end: usize) void {
if (start == end) {
const value = self.calValue();
if (value > self.max) {
self.max = value;
self.best = self.table;
}
} else {
var idx: usize = start;
while (idx < end) : (idx += 1) {
std.mem.swap(u8, &self.table[start], &self.table[idx]);
self.permute(start + 1, end);
std.mem.swap(u8, &self.table[start], &self.table[idx]);
}
}
}
fn calValue(self: @This()) isize {
var sum: isize = 0;
var idx: usize = 0;
while (idx < self.table.len) : (idx += 1) {
sum += self.pairs.get(.{ self.table[idx], self.table[(idx + 1) % self.table.len] }) orelse
self.pairs.get(.{ self.table[(idx + 1) % self.table.len], self.table[idx] }).?;
}
return sum;
}
};
fn parseInput(allocator: std.mem.Allocator, in: Str) !Pairs {
var pairs = Pairs.init(allocator);
var lines = std.mem.tokenize(u8, in, "\n");
while (lines.next()) |line| {
var words = std.mem.tokenize(u8, line, " ");
var pair: [2]u8 = undefined;
pair[0] = words.next().?[0];
_ = words.next(); // would
// lose of gain
const gain: isize = switch (words.next().?[0]) {
'g' => try std.fmt.parseUnsigned(isize, words.next().?, 10),
'l' => try std.fmt.parseUnsigned(isize, words.next().?, 10) * -1,
else => unreachable,
};
// drop "happiness units by sitting next to"
var idx: usize = 0;
while (idx < 6) : (idx += 1) {
_ = words.next();
}
pair[1] = words.next().?[0];
if (pairs.contains(pair)) {
pairs.getPtr(pair).?.* += gain;
} else if (pairs.contains(.{ pair[1], pair[0] })) {
pairs.getPtr(.{ pair[1], pair[0] }).?.* += gain;
} else {
try pairs.put(pair, gain);
}
}
std.debug.assert(pairs.count() == 8 * 7 / 2);
return pairs;
}