WIP day 11
Zig orderedRemove + append is doing very strange things.
This commit is contained in:
parent
99bdcaaf75
commit
818968eab9
@ -4,9 +4,6 @@ pub const input = @embedFile("input/day10.txt");
|
||||
const test_input1 = @embedFile("input/day10_test1.txt");
|
||||
const test_input2 = @embedFile("input/day10_test2.txt");
|
||||
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
fn signal_strength(cycle: usize, x: isize) isize {
|
||||
if (cycle == 20 or cycle == 60 or cycle == 100 or cycle == 140 or cycle == 180 or cycle == 220) {
|
||||
return @intCast(isize, cycle) * x;
|
||||
|
145
src/day11.zig
Normal file
145
src/day11.zig
Normal file
@ -0,0 +1,145 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub const input = @embedFile("input/day11.txt");
|
||||
const test_input1 = @embedFile("input/day11_test1.txt");
|
||||
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const Op = enum {
|
||||
Add,
|
||||
Mult,
|
||||
fn from_char(char: u8) error{InvalidChar}!Op {
|
||||
return switch (char) {
|
||||
'+' => Op.Add,
|
||||
'*' => Op.Mult,
|
||||
else => error.InvalidChar,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const Operation = struct {
|
||||
op: Op,
|
||||
value: ?u8,
|
||||
};
|
||||
|
||||
const Monkey = struct {
|
||||
num: u8,
|
||||
items: std.ArrayList(usize),
|
||||
operation: Operation,
|
||||
div_test: u8,
|
||||
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");
|
||||
var num_line = lines.next().?;
|
||||
num_line = std.mem.trimRight(u8, num_line, ":");
|
||||
const num = try std.fmt.parseInt(u8, num_line[7..], 10);
|
||||
const items_line = lines.next().?;
|
||||
var items_input = std.mem.tokenize(u8, items_line[18..], ", ");
|
||||
var items = std.ArrayList(usize).init(allocator);
|
||||
while (items_input.next()) |item| {
|
||||
try items.append(try std.fmt.parseInt(usize, item, 10));
|
||||
}
|
||||
const op_line = lines.next().?;
|
||||
const op = try Op.from_char(op_line[23]);
|
||||
const value = if (std.mem.eql(u8, op_line[25..], "old")) null else try std.fmt.parseInt(u8, op_line[25..], 10);
|
||||
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{
|
||||
.num = num,
|
||||
.items = items,
|
||||
.operation = Operation{
|
||||
.op = op,
|
||||
.value = value,
|
||||
},
|
||||
.div_test = div_test,
|
||||
.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});
|
||||
} 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});
|
||||
}
|
||||
}
|
||||
monkey.inspect_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
var inspect_counts = std.ArrayList(usize).init(allocator);
|
||||
var m: usize = 0;
|
||||
while (m < monkeys.items.len) : (m += 1) {
|
||||
try inspect_counts.append(monkeys.items[m].inspect_count);
|
||||
}
|
||||
var sorted_inspect_counts = try inspect_counts.toOwnedSlice();
|
||||
std.sort.sort(usize, sorted_inspect_counts, {}, std.sort.desc(usize));
|
||||
|
||||
return sorted_inspect_counts[0] * sorted_inspect_counts[1];
|
||||
}
|
||||
|
||||
pub fn solve_part2(data: []const u8) !usize {
|
||||
var lines = std.mem.tokenize(u8, data, "\n");
|
||||
_ = lines;
|
||||
return 0;
|
||||
}
|
||||
|
||||
test "solves part1" {
|
||||
try std.testing.expectEqual(solve_part1(test_input1), 10605);
|
||||
}
|
||||
|
||||
test "solves part2" {
|
||||
try std.testing.expectEqual(solve_part2(test_input1), 0);
|
||||
}
|
27
src/input/day11_test1.txt
Normal file
27
src/input/day11_test1.txt
Normal file
@ -0,0 +1,27 @@
|
||||
Monkey 0:
|
||||
Starting items: 79, 98
|
||||
Operation: new = old * 19
|
||||
Test: divisible by 23
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 1:
|
||||
Starting items: 54, 65, 75, 74
|
||||
Operation: new = old + 6
|
||||
Test: divisible by 19
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 0
|
||||
|
||||
Monkey 2:
|
||||
Starting items: 79, 60, 97
|
||||
Operation: new = old * old
|
||||
Test: divisible by 13
|
||||
If true: throw to monkey 1
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 3:
|
||||
Starting items: 74
|
||||
Operation: new = old + 3
|
||||
Test: divisible by 17
|
||||
If true: throw to monkey 0
|
||||
If false: throw to monkey 1
|
@ -10,6 +10,7 @@ const day07 = @import("day07.zig");
|
||||
const day08 = @import("day08.zig");
|
||||
const day09 = @import("day09.zig");
|
||||
const day10 = @import("day10.zig");
|
||||
const day11 = @import("day11.zig");
|
||||
const utils = @import("utils.zig");
|
||||
|
||||
fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.time.Timer) !void {
|
||||
@ -52,6 +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 bw.flush();
|
||||
}
|
||||
@ -69,5 +71,6 @@ test {
|
||||
_ = day08;
|
||||
_ = day09;
|
||||
_ = day10;
|
||||
_ = day11;
|
||||
_ = utils;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user