Solve day 11

This commit is contained in:
Tyler Hallada 2022-12-11 13:20:55 -05:00
parent 818968eab9
commit f65eeaa7ff
2 changed files with 86 additions and 63 deletions

View File

@ -31,13 +31,8 @@ const Monkey = struct {
true_monkey: u8,
false_monkey: u8,
inspect_count: usize,
};
pub fn solve_part1(data: []const u8) !usize {
var monkey_inputs = std.mem.split(u8, data, "\n\n");
var monkeys = std.ArrayList(Monkey).init(allocator);
while (monkey_inputs.next()) |monkey| {
var lines = std.mem.tokenize(u8, monkey, "\n");
fn parse(data: []const u8) !Monkey {
var lines = std.mem.tokenize(u8, data, "\n");
var num_line = lines.next().?;
num_line = std.mem.trimRight(u8, num_line, ":");
const num = try std.fmt.parseInt(u8, num_line[7..], 10);
@ -53,7 +48,7 @@ pub fn solve_part1(data: []const u8) !usize {
const div_test = try std.fmt.parseInt(u8, lines.next().?[21..], 10);
const true_monkey = try std.fmt.parseInt(u8, lines.next().?[29..], 10);
const false_monkey = try std.fmt.parseInt(u8, lines.next().?[30..], 10);
try monkeys.append(Monkey{
return Monkey{
.num = num,
.items = items,
.operation = Operation{
@ -64,61 +59,54 @@ pub fn solve_part1(data: []const u8) !usize {
.true_monkey = true_monkey,
.false_monkey = false_monkey,
.inspect_count = 0,
});
};
}
std.debug.print("monkeys: {any}", .{monkeys});
};
var round: usize = 0;
while (round < 20) : (round += 1) {
var m: usize = 0;
while (m < monkeys.items.len) : (m += 1) {
var monkey = monkeys.items[m];
std.debug.print("Monkey {d}\n", .{monkey.num});
var i: usize = 0;
std.debug.print("Monkey items {any}\n", .{monkey.items.items});
while (monkey.items.items.len > 0) : (i += 1) {
var item = monkey.items.orderedRemove(0);
std.debug.print("items: {any}\n", .{monkey.items});
std.debug.print("items: {any}\n", .{monkey.items.items});
std.debug.print(" Monkey inspects an item with a worry level of {d}\n", .{item});
switch (monkey.operation.op) {
Op.Add => {
if (monkey.operation.value) |value| {
item += @intCast(usize, value);
std.debug.print(" Worry level is added by {d} to {d}\n", .{ value, item });
} else {
item += item;
std.debug.print(" Worry level is added by old to {d}\n", .{item});
}
},
Op.Mult => {
if (monkey.operation.value) |value| {
item *= @intCast(usize, value);
std.debug.print(" Worry level is multiplied by {d} to {d}\n", .{ value, item });
} else {
item *= item;
std.debug.print(" Worry level is multiplied by old to {d}\n", .{item});
}
},
}
item /= 3;
std.debug.print(" Monkey gets bored with item. Worry level is divided by 3 to {d}\n", .{item});
if (item % monkey.div_test == 0) {
std.debug.print(" Current worry level is divisible by {d}\n", .{monkey.div_test});
try monkeys.items[monkey.true_monkey].items.append(item);
std.debug.print(" Item with worry level {d} is thrown to monkey {d}\n", .{ item, monkey.true_monkey });
std.debug.print("items: {any}\n", .{monkeys.items[monkey.true_monkey].items.items});
fn inspect_and_throw_items(monkeys: *std.ArrayList(Monkey), index: usize, div_3: bool, common_divisor: ?usize) !void {
var monkey = monkeys.items[index];
// std.debug.print("Monkey {d}\n", .{monkey.num});
var i: usize = 0;
while (monkeys.items[index].items.items.len > 0) : (i += 1) {
var item = monkeys.items[index].items.pop();
// std.debug.print(" Monkey inspects an item with a worry level of {d}\n", .{item});
switch (monkey.operation.op) {
Op.Add => {
if (monkey.operation.value) |value| {
item += @intCast(usize, value);
// std.debug.print(" Worry level is added by {d} to {d}\n", .{ value, item });
} else {
std.debug.print(" Current worry level is not divisible by {d}\n", .{monkey.div_test});
try monkeys.items[monkey.false_monkey].items.append(item);
std.debug.print(" Item with worry level {d} is thrown to monkey {d}\n", .{ item, monkey.false_monkey });
std.debug.print("items: {any}\n", .{monkeys.items[monkey.false_monkey].items.items});
item += item;
// std.debug.print(" Worry level is added by old to {d}\n", .{item});
}
}
monkey.inspect_count += 1;
},
Op.Mult => {
if (monkey.operation.value) |value| {
item *= @intCast(usize, value);
// std.debug.print(" Worry level is multiplied by {d} to {d}\n", .{ value, item });
} else {
item *= item;
// std.debug.print(" Worry level is multiplied by old to {d}\n", .{item});
}
},
}
if (div_3) item /= 3;
if (common_divisor) |divisor| item = item % divisor;
// std.debug.print(" Monkey gets bored with item. Worry level is divided by 3 to {d}\n", .{item});
if (item % monkey.div_test == 0) {
// std.debug.print(" Current worry level is divisible by {d}\n", .{monkey.div_test});
try monkeys.items[monkey.true_monkey].items.append(item);
// std.debug.print(" Item with worry level {d} is thrown to monkey {d}\n", .{ item, monkey.true_monkey });
} else {
// std.debug.print(" Current worry level is not divisible by {d}\n", .{monkey.div_test});
try monkeys.items[monkey.false_monkey].items.append(item);
// std.debug.print(" Item with worry level {d} is thrown to monkey {d}\n", .{ item, monkey.false_monkey });
}
monkeys.items[index].inspect_count += 1;
}
}
fn calc_monkey_business(monkeys: *std.ArrayList(Monkey)) !usize {
var inspect_counts = std.ArrayList(usize).init(allocator);
var m: usize = 0;
while (m < monkeys.items.len) : (m += 1) {
@ -130,10 +118,45 @@ pub fn solve_part1(data: []const u8) !usize {
return sorted_inspect_counts[0] * sorted_inspect_counts[1];
}
pub fn solve_part1(data: []const u8) !usize {
var monkey_inputs = std.mem.split(u8, data, "\n\n");
var monkeys = std.ArrayList(Monkey).init(allocator);
while (monkey_inputs.next()) |monkey| {
try monkeys.append(try Monkey.parse(monkey));
}
var round: usize = 0;
while (round < 20) : (round += 1) {
var m: usize = 0;
while (m < monkeys.items.len) : (m += 1) {
try inspect_and_throw_items(&monkeys, m, true, null);
}
}
return calc_monkey_business(&monkeys);
}
pub fn solve_part2(data: []const u8) !usize {
var lines = std.mem.tokenize(u8, data, "\n");
_ = lines;
return 0;
var monkey_inputs = std.mem.split(u8, data, "\n\n");
var monkeys = std.ArrayList(Monkey).init(allocator);
while (monkey_inputs.next()) |monkey| {
try monkeys.append(try Monkey.parse(monkey));
}
var common_divisor: usize = 1;
for (monkeys.items) |monkey| {
common_divisor *= monkey.div_test;
}
var round: usize = 0;
while (round < 10_000) : (round += 1) {
var m: usize = 0;
while (m < monkeys.items.len) : (m += 1) {
try inspect_and_throw_items(&monkeys, m, false, common_divisor);
}
}
return calc_monkey_business(&monkeys);
}
test "solves part1" {
@ -141,5 +164,5 @@ test "solves part1" {
}
test "solves part2" {
try std.testing.expectEqual(solve_part2(test_input1), 0);
try std.testing.expectEqual(solve_part2(test_input1), 2713310158);
}

View File

@ -20,7 +20,7 @@ fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.ti
var part1 = try day.solve_part1(day.input);
const part1_end = timer.read();
const part1_time = utils.HumanTime.new(part1_end - part1_start);
if (@TypeOf(part1) == usize or @TypeOf(part1) == isize) {
if (@TypeOf(part1) == usize or @TypeOf(part1) == isize or @TypeOf(part1) == u64 or @TypeOf(part1) == u128) {
try stdout.print("Part 1: {} ({d:.2} {s})\n", .{ part1, part1_time.value, part1_time.unit.abbr() });
} else {
try stdout.print("Part 1: \n{s} ({d:.2} {s})\n", .{ part1, part1_time.value, part1_time.unit.abbr() });
@ -30,7 +30,7 @@ fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.ti
var part2 = try day.solve_part2(day.input);
const part2_end = timer.read();
const part2_time = utils.HumanTime.new(part2_end - part2_start);
if (@TypeOf(part2) == usize or @TypeOf(part2) == isize) {
if (@TypeOf(part2) == usize or @TypeOf(part2) == isize or @TypeOf(part2) == u64 or @TypeOf(part2) == u128) {
try stdout.print("Part 2: {} ({d:.2} {s})\n", .{ part2, part2_time.value, part2_time.unit.abbr() });
} else {
try stdout.print("Part 2: \n{s} ({d:.2} {s})\n", .{ part2, part2_time.value, part2_time.unit.abbr() });
@ -53,7 +53,7 @@ pub fn main() !void {
try solve_day(8, day08, &stdout, &timer);
try solve_day(9, day09, &stdout, &timer);
try solve_day(10, day10, &stdout, &timer);
try solve_day(11, day10, &stdout, &timer);
try solve_day(11, day11, &stdout, &timer);
try bw.flush();
}