This commit is contained in:
Dusk 2022-12-05 18:45:45 +01:00
parent f226594ca5
commit 28d1882936
4 changed files with 713 additions and 0 deletions

9
day5/example Normal file
View File

@ -0,0 +1,9 @@
[D]
[N] [C]
[Z] [M] [P]
1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2

511
day5/input Normal file
View File

@ -0,0 +1,511 @@
[C] [N] [R]
[J] [T] [H] [P] [L]
[F] [S] [T] [B] [M] [D]
[C] [L] [J] [Z] [S] [L] [B]
[N] [Q] [G] [J] [J] [F] [F] [R]
[D] [V] [B] [L] [B] [Q] [D] [M] [T]
[B] [Z] [Z] [T] [V] [S] [V] [S] [D]
[W] [P] [P] [D] [G] [P] [B] [P] [V]
1 2 3 4 5 6 7 8 9
move 4 from 9 to 6
move 7 from 2 to 5
move 3 from 5 to 2
move 2 from 2 to 1
move 2 from 8 to 4
move 1 from 6 to 9
move 1 from 9 to 4
move 7 from 1 to 2
move 5 from 2 to 3
move 5 from 7 to 4
move 5 from 6 to 3
move 1 from 7 to 6
move 2 from 6 to 9
move 3 from 2 to 4
move 4 from 5 to 6
move 2 from 7 to 3
move 2 from 9 to 3
move 1 from 5 to 2
move 11 from 4 to 3
move 1 from 2 to 9
move 1 from 9 to 3
move 2 from 1 to 6
move 5 from 8 to 5
move 7 from 5 to 4
move 2 from 5 to 6
move 6 from 6 to 4
move 17 from 3 to 4
move 1 from 8 to 3
move 11 from 4 to 7
move 1 from 6 to 4
move 3 from 4 to 2
move 2 from 2 to 6
move 8 from 3 to 1
move 8 from 3 to 9
move 3 from 9 to 6
move 3 from 1 to 3
move 11 from 7 to 5
move 1 from 6 to 4
move 4 from 9 to 6
move 3 from 1 to 4
move 1 from 2 to 3
move 1 from 6 to 9
move 24 from 4 to 9
move 2 from 6 to 5
move 1 from 1 to 2
move 1 from 1 to 3
move 12 from 9 to 6
move 5 from 4 to 2
move 4 from 2 to 3
move 5 from 6 to 3
move 13 from 6 to 7
move 1 from 5 to 6
move 9 from 5 to 3
move 4 from 7 to 5
move 1 from 6 to 1
move 3 from 5 to 1
move 14 from 9 to 4
move 2 from 7 to 9
move 13 from 4 to 9
move 1 from 4 to 7
move 4 from 7 to 9
move 3 from 5 to 1
move 8 from 3 to 9
move 4 from 1 to 4
move 8 from 3 to 7
move 3 from 7 to 6
move 4 from 4 to 2
move 3 from 1 to 9
move 6 from 2 to 6
move 3 from 3 to 1
move 7 from 9 to 7
move 2 from 6 to 5
move 1 from 5 to 3
move 3 from 7 to 5
move 5 from 7 to 4
move 2 from 1 to 4
move 5 from 5 to 9
move 6 from 4 to 1
move 6 from 7 to 8
move 22 from 9 to 3
move 7 from 1 to 8
move 4 from 9 to 6
move 1 from 4 to 5
move 8 from 6 to 4
move 7 from 8 to 1
move 1 from 6 to 4
move 1 from 9 to 4
move 1 from 1 to 2
move 1 from 2 to 5
move 1 from 9 to 8
move 11 from 3 to 7
move 1 from 6 to 2
move 2 from 1 to 5
move 1 from 8 to 2
move 1 from 7 to 8
move 4 from 5 to 7
move 1 from 6 to 9
move 6 from 3 to 1
move 6 from 3 to 1
move 15 from 7 to 5
move 1 from 3 to 1
move 1 from 3 to 6
move 1 from 6 to 8
move 14 from 5 to 1
move 16 from 1 to 3
move 2 from 8 to 9
move 1 from 7 to 4
move 3 from 9 to 8
move 3 from 8 to 7
move 2 from 3 to 5
move 1 from 7 to 1
move 6 from 8 to 5
move 2 from 2 to 9
move 1 from 7 to 2
move 2 from 9 to 2
move 5 from 4 to 7
move 3 from 2 to 7
move 14 from 1 to 5
move 2 from 4 to 7
move 8 from 7 to 6
move 1 from 1 to 5
move 1 from 7 to 4
move 1 from 7 to 5
move 1 from 1 to 8
move 12 from 3 to 4
move 1 from 8 to 7
move 3 from 4 to 1
move 1 from 6 to 2
move 8 from 5 to 2
move 1 from 7 to 6
move 1 from 1 to 7
move 6 from 6 to 2
move 1 from 1 to 2
move 14 from 5 to 7
move 1 from 6 to 4
move 4 from 4 to 7
move 1 from 1 to 6
move 1 from 5 to 6
move 2 from 3 to 1
move 14 from 7 to 5
move 10 from 4 to 7
move 1 from 1 to 9
move 1 from 5 to 9
move 11 from 5 to 1
move 6 from 7 to 6
move 1 from 4 to 6
move 1 from 3 to 7
move 2 from 1 to 5
move 13 from 2 to 1
move 10 from 6 to 7
move 4 from 5 to 2
move 1 from 9 to 1
move 1 from 3 to 6
move 2 from 5 to 2
move 1 from 9 to 3
move 1 from 3 to 1
move 21 from 7 to 5
move 1 from 6 to 4
move 4 from 5 to 1
move 1 from 4 to 1
move 6 from 2 to 3
move 1 from 3 to 6
move 1 from 3 to 8
move 1 from 8 to 7
move 1 from 7 to 3
move 9 from 5 to 3
move 24 from 1 to 4
move 1 from 3 to 7
move 11 from 3 to 8
move 1 from 7 to 3
move 1 from 2 to 4
move 2 from 2 to 1
move 2 from 3 to 5
move 1 from 6 to 5
move 10 from 4 to 6
move 2 from 6 to 4
move 5 from 1 to 2
move 1 from 6 to 7
move 8 from 8 to 6
move 4 from 2 to 7
move 8 from 6 to 7
move 1 from 2 to 8
move 1 from 8 to 3
move 1 from 7 to 4
move 3 from 4 to 1
move 2 from 6 to 7
move 4 from 1 to 9
move 3 from 6 to 7
move 10 from 7 to 4
move 2 from 3 to 9
move 2 from 6 to 9
move 2 from 1 to 8
move 2 from 9 to 5
move 4 from 5 to 6
move 3 from 8 to 1
move 4 from 4 to 8
move 5 from 8 to 4
move 1 from 8 to 2
move 5 from 5 to 9
move 1 from 6 to 1
move 2 from 1 to 7
move 22 from 4 to 8
move 4 from 8 to 7
move 2 from 6 to 7
move 1 from 2 to 6
move 16 from 8 to 9
move 3 from 7 to 4
move 1 from 5 to 9
move 2 from 6 to 7
move 1 from 8 to 2
move 1 from 2 to 3
move 24 from 9 to 3
move 1 from 1 to 7
move 3 from 5 to 1
move 4 from 4 to 6
move 15 from 3 to 6
move 18 from 6 to 2
move 3 from 3 to 2
move 4 from 1 to 6
move 4 from 7 to 3
move 1 from 3 to 9
move 4 from 2 to 1
move 1 from 8 to 7
move 3 from 9 to 6
move 1 from 9 to 3
move 4 from 7 to 3
move 2 from 4 to 2
move 1 from 1 to 2
move 7 from 3 to 5
move 8 from 6 to 1
move 1 from 9 to 2
move 3 from 7 to 5
move 1 from 4 to 8
move 3 from 1 to 7
move 5 from 7 to 6
move 3 from 5 to 2
move 3 from 7 to 3
move 5 from 5 to 9
move 5 from 3 to 6
move 1 from 8 to 3
move 5 from 9 to 7
move 7 from 2 to 4
move 11 from 2 to 7
move 7 from 1 to 6
move 1 from 1 to 9
move 5 from 3 to 6
move 5 from 2 to 1
move 1 from 3 to 9
move 1 from 3 to 7
move 6 from 6 to 2
move 10 from 6 to 7
move 5 from 6 to 7
move 28 from 7 to 8
move 2 from 9 to 1
move 1 from 6 to 3
move 4 from 7 to 5
move 1 from 3 to 6
move 7 from 2 to 7
move 6 from 7 to 3
move 1 from 5 to 9
move 1 from 6 to 2
move 1 from 7 to 3
move 1 from 9 to 1
move 4 from 5 to 2
move 5 from 3 to 5
move 2 from 2 to 8
move 4 from 4 to 7
move 1 from 4 to 7
move 2 from 3 to 6
move 5 from 7 to 1
move 2 from 5 to 8
move 2 from 5 to 8
move 2 from 5 to 3
move 2 from 3 to 1
move 2 from 6 to 7
move 31 from 8 to 3
move 2 from 8 to 5
move 2 from 7 to 4
move 7 from 1 to 4
move 2 from 5 to 1
move 3 from 2 to 8
move 2 from 4 to 6
move 3 from 1 to 2
move 6 from 4 to 8
move 1 from 1 to 8
move 1 from 6 to 5
move 11 from 8 to 9
move 1 from 6 to 8
move 1 from 4 to 1
move 1 from 8 to 7
move 1 from 5 to 8
move 3 from 2 to 1
move 2 from 4 to 3
move 1 from 8 to 1
move 7 from 3 to 6
move 12 from 3 to 2
move 1 from 7 to 9
move 4 from 6 to 1
move 1 from 6 to 3
move 12 from 9 to 3
move 1 from 6 to 4
move 1 from 1 to 7
move 1 from 4 to 1
move 1 from 7 to 2
move 1 from 6 to 5
move 1 from 5 to 6
move 5 from 3 to 1
move 1 from 6 to 4
move 7 from 2 to 1
move 3 from 2 to 6
move 1 from 4 to 5
move 3 from 3 to 2
move 4 from 2 to 8
move 1 from 6 to 4
move 1 from 4 to 9
move 1 from 5 to 1
move 11 from 1 to 5
move 10 from 1 to 8
move 2 from 6 to 4
move 1 from 2 to 9
move 1 from 2 to 4
move 18 from 3 to 5
move 4 from 1 to 4
move 3 from 1 to 2
move 14 from 8 to 5
move 2 from 2 to 6
move 1 from 3 to 2
move 2 from 2 to 7
move 3 from 4 to 1
move 2 from 4 to 3
move 2 from 3 to 4
move 2 from 6 to 9
move 1 from 7 to 1
move 3 from 1 to 4
move 4 from 9 to 7
move 31 from 5 to 2
move 25 from 2 to 4
move 13 from 4 to 2
move 10 from 2 to 3
move 2 from 5 to 7
move 5 from 2 to 9
move 7 from 5 to 7
move 5 from 7 to 4
move 1 from 5 to 8
move 2 from 7 to 3
move 11 from 4 to 8
move 1 from 7 to 3
move 1 from 1 to 4
move 2 from 5 to 3
move 3 from 2 to 9
move 8 from 9 to 6
move 10 from 8 to 2
move 5 from 3 to 2
move 1 from 7 to 3
move 3 from 7 to 3
move 15 from 2 to 1
move 11 from 1 to 3
move 1 from 8 to 2
move 8 from 6 to 5
move 1 from 2 to 6
move 1 from 6 to 1
move 12 from 3 to 7
move 1 from 2 to 9
move 2 from 4 to 1
move 3 from 1 to 8
move 1 from 8 to 7
move 3 from 3 to 4
move 1 from 4 to 7
move 15 from 7 to 9
move 1 from 7 to 5
move 4 from 1 to 8
move 6 from 8 to 6
move 1 from 6 to 2
move 5 from 5 to 1
move 2 from 6 to 8
move 1 from 2 to 7
move 1 from 8 to 2
move 1 from 7 to 1
move 1 from 5 to 8
move 6 from 3 to 1
move 4 from 3 to 8
move 7 from 8 to 5
move 1 from 2 to 4
move 2 from 4 to 2
move 3 from 6 to 4
move 5 from 9 to 3
move 4 from 1 to 4
move 10 from 5 to 9
move 8 from 1 to 7
move 1 from 2 to 1
move 1 from 1 to 9
move 20 from 9 to 2
move 12 from 2 to 3
move 17 from 4 to 3
move 6 from 7 to 2
move 5 from 3 to 8
move 20 from 3 to 5
move 2 from 9 to 4
move 3 from 3 to 1
move 1 from 7 to 1
move 6 from 3 to 6
move 4 from 2 to 3
move 4 from 5 to 3
move 1 from 1 to 9
move 6 from 6 to 1
move 3 from 8 to 4
move 1 from 9 to 8
move 2 from 2 to 1
move 3 from 3 to 2
move 1 from 3 to 6
move 1 from 7 to 4
move 3 from 3 to 6
move 6 from 1 to 5
move 9 from 2 to 4
move 3 from 2 to 5
move 2 from 6 to 5
move 16 from 4 to 8
move 18 from 8 to 6
move 1 from 4 to 5
move 2 from 6 to 7
move 4 from 1 to 7
move 22 from 5 to 6
move 1 from 4 to 9
move 4 from 7 to 6
move 11 from 6 to 5
move 9 from 5 to 2
move 2 from 2 to 3
move 2 from 7 to 2
move 1 from 1 to 7
move 9 from 6 to 2
move 1 from 5 to 1
move 1 from 8 to 9
move 18 from 6 to 8
move 1 from 7 to 4
move 4 from 5 to 1
move 2 from 5 to 2
move 2 from 2 to 5
move 1 from 9 to 5
move 1 from 5 to 9
move 1 from 9 to 1
move 1 from 9 to 2
move 1 from 4 to 8
move 4 from 1 to 4
move 2 from 6 to 5
move 1 from 1 to 9
move 3 from 6 to 7
move 1 from 6 to 9
move 1 from 9 to 8
move 2 from 5 to 9
move 3 from 3 to 5
move 7 from 2 to 3
move 1 from 1 to 3
move 2 from 5 to 9
move 1 from 5 to 7
move 10 from 8 to 3
move 10 from 8 to 9
move 3 from 4 to 3
move 9 from 2 to 1
move 4 from 9 to 6
move 5 from 1 to 9
move 2 from 5 to 9
move 1 from 6 to 4
move 4 from 7 to 2
move 7 from 2 to 9
move 3 from 6 to 8
move 1 from 1 to 3
move 2 from 8 to 5
move 1 from 8 to 1
move 18 from 3 to 6
move 15 from 9 to 2
move 8 from 9 to 1
move 2 from 9 to 2
move 2 from 4 to 9
move 2 from 9 to 7
move 12 from 6 to 3
move 7 from 1 to 7
move 12 from 2 to 5
move 7 from 3 to 2
move 4 from 3 to 4
move 2 from 7 to 6
move 7 from 7 to 8
move 1 from 4 to 2
move 4 from 1 to 8
move 5 from 3 to 1
move 9 from 8 to 3
move 1 from 8 to 7
move 2 from 1 to 2
move 4 from 6 to 7
move 11 from 2 to 5
move 2 from 4 to 6
move 1 from 8 to 2
move 7 from 3 to 2
move 1 from 2 to 4
move 4 from 6 to 1
move 7 from 5 to 8
move 2 from 3 to 1
move 7 from 2 to 3
move 6 from 5 to 1
move 1 from 4 to 2
move 8 from 1 to 6
move 3 from 2 to 9

95
day5/puzzle1.zig Normal file
View File

@ -0,0 +1,95 @@
const std = @import("std");
const mem = std.mem;
const fmt = std.fmt;
const CharList = std.ArrayList(u8);
const input = @embedFile("input");
const n_crates = 9;
fn parseCrates(input_iterator: *mem.SplitIterator(u8), comptime n: u8, allocator: mem.Allocator) []CharList {
// Return value
var crates = allocator.alloc(CharList, n) catch unreachable;
for (crates) |*crate| {
crate.* = CharList.init(allocator);
}
// Make sure it's pointing to the start
input_iterator.reset();
while (input_iterator.next()) |line| {
// Parse until empty line
if (line.len == 0) break;
// Keep track of which crate we are in
var index: usize = 0;
for (line) |char, i| {
switch (char) {
'A'...'Z' => crates[index].insert(0, char) catch unreachable,
else => index = i / 4,
}
}
}
return crates;
}
fn destroyCrates(crates: []CharList) void {
for (crates) |*crate| {
crate.*.deinit();
}
}
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var input_iterator = mem.split(u8, input, "\n");
var crates = parseCrates(&input_iterator, n_crates, allocator);
//defer destroyCrates(crates);
while (input_iterator.next()) |line| {
if (line.len == 0) continue;
// Print the state
for (crates) |crate, i| {
std.debug.print("{d} : {s}\n", .{ i, crate.items });
}
// Parsing the line to get action
var line_iterator = mem.split(u8, line, " ");
// n, src, dst
var numbers = [_]u8{undefined} ** 3;
var index: usize = 0;
while (line_iterator.next()) |word| {
// Get a number and asign or go to next word
numbers[index] = fmt.parseInt(u8, word, 10) catch continue;
if (index == 3) {
std.debug.print("Word parsing error, index out of bounds", .{});
unreachable;
}
index += 1;
}
// Performing the action
var i: usize = 0;
const n = numbers[0];
const src = numbers[1] - 1;
const dst = numbers[2] - 1;
std.debug.print("Move {} from {} to {}\n", .{n, src, dst});
while (i < n) : (i += 1) {
crates[dst].append(crates[src].pop()) catch unreachable;
}
}
// Printing the final state
for (crates) |crate, i| {
std.debug.print("{d} : {s}\n", .{ i, crate.items });
}
}

98
day5/puzzle2.zig Normal file
View File

@ -0,0 +1,98 @@
const std = @import("std");
const mem = std.mem;
const fmt = std.fmt;
const CharList = std.ArrayList(u8);
const input = @embedFile("input");
const n_crates = 9;
fn parseCrates(input_iterator: *mem.SplitIterator(u8), comptime n: u8, allocator: mem.Allocator) []CharList {
// Return value
var crates = allocator.alloc(CharList, n) catch unreachable;
for (crates) |*crate| {
crate.* = CharList.init(allocator);
}
// Make sure it's pointing to the start
input_iterator.reset();
while (input_iterator.next()) |line| {
// Parse until empty line
if (line.len == 0) break;
// Keep track of which crate we are in
var index: usize = 0;
for (line) |char, i| {
switch (char) {
'A'...'Z' => crates[index].insert(0, char) catch unreachable,
else => index = i / 4,
}
}
}
return crates;
}
fn destroyCrates(crates: []CharList) void {
for (crates) |*crate| {
crate.*.deinit();
}
}
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var input_iterator = mem.split(u8, input, "\n");
var crates = parseCrates(&input_iterator, n_crates, allocator);
//defer destroyCrates(crates);
while (input_iterator.next()) |line| {
if (line.len == 0) continue;
// Print the state
for (crates) |crate, i| {
std.debug.print("{d} : {s}\n", .{ i, crate.items });
}
// Parsing the line to get action
var line_iterator = mem.split(u8, line, " ");
// n, src, dst
var numbers = [_]u8{undefined} ** 3;
var index: usize = 0;
while (line_iterator.next()) |word| {
// Get a number and asign or go to next word
numbers[index] = fmt.parseInt(u8, word, 10) catch continue;
if (index == 3) {
std.debug.print("Word parsing error, index out of bounds", .{});
unreachable;
}
index += 1;
}
// Performing the action
var i: usize = 0;
const n = numbers[0];
const src = numbers[1] - 1;
const dst = numbers[2] - 1;
std.debug.print("Move {} from {} to {}\n", .{n, src, dst});
var tmp_crates = CharList.init(allocator);
while (i < n) : (i += 1) {
tmp_crates.insert(0, crates[src].pop()) catch unreachable;
}
crates[dst].appendSlice(tmp_crates.items) catch unreachable;
}
// Printing the final state
for (crates) |crate, i| {
std.debug.print("{d} : {s}\n", .{ i, crate.items });
}
}