mirror of
https://github.com/thegeneralist01/aoc
synced 2026-01-09 14:40:25 +01:00
day x
This commit is contained in:
parent
8b6cd0c4ae
commit
aefb095e66
6 changed files with 1813 additions and 0 deletions
133
day5/day5_part1.zig
Normal file
133
day5/day5_part1.zig
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
const std = @import("std");
|
||||
const in = @embedFile("in");
|
||||
|
||||
fn array_includes(comptime T: type, haystack: []T, needle: T) bool {
|
||||
for (haystack) |item| {
|
||||
if (item == needle) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const ally = gpa.allocator();
|
||||
defer _ = gpa.deinit();
|
||||
|
||||
var lines = std.ArrayList(std.ArrayList(u32)).init(ally);
|
||||
var incorrect_line_idxs = std.ArrayList(u32).init(ally);
|
||||
defer {
|
||||
for (lines.items) |*line| {
|
||||
line.deinit();
|
||||
}
|
||||
lines.deinit();
|
||||
incorrect_line_idxs.deinit();
|
||||
}
|
||||
|
||||
var dependencies = std.AutoHashMap(u32, std.ArrayList(u32)).init(ally);
|
||||
defer {
|
||||
var iter = dependencies.iterator();
|
||||
while (iter.next()) |entry| {
|
||||
entry.value_ptr.deinit();
|
||||
}
|
||||
dependencies.deinit();
|
||||
}
|
||||
|
||||
var in_lines = std.mem.tokenize(u8, in, "\n");
|
||||
var parsing_dependencies = false;
|
||||
var i: u32 = 0;
|
||||
while (in_lines.next()) |line| {
|
||||
if (std.mem.indexOf(u8, line, "|")) |_| {
|
||||
parsing_dependencies = true;
|
||||
} else {
|
||||
parsing_dependencies = false;
|
||||
}
|
||||
|
||||
if (line.len > 0 and parsing_dependencies) {
|
||||
var dependance = std.mem.split(u8, line, "|");
|
||||
const num1 = try std.fmt.parseInt(u32, dependance.next().?, 10);
|
||||
const num2 = try std.fmt.parseInt(u32, dependance.next().?, 10);
|
||||
|
||||
var entry = try dependencies.getOrPut(num2);
|
||||
if (!entry.found_existing) {
|
||||
entry.value_ptr.* = std.ArrayList(u32).init(ally);
|
||||
}
|
||||
|
||||
try entry.value_ptr.append(num1);
|
||||
} else if (line.len > 0 and !parsing_dependencies) {
|
||||
var ints = std.mem.split(u8, line, ",");
|
||||
var intLine = std.ArrayList(u32).init(ally);
|
||||
while (ints.next()) |int| {
|
||||
try intLine.append(try std.fmt.parseInt(u32, int, 10));
|
||||
}
|
||||
try lines.append(intLine);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
// Part 1
|
||||
var middle_page_number_sum: u32 = 0;
|
||||
for (lines.items, 0..) |line, lidx| {
|
||||
var is_correct = true;
|
||||
outer: for (line.items, 0..) |num1, j| {
|
||||
for (line.items[0..j]) |num2| {
|
||||
const deps = dependencies.get(num1);
|
||||
|
||||
if (deps == null) {
|
||||
is_correct = false;
|
||||
break :outer;
|
||||
}
|
||||
|
||||
var found = false;
|
||||
for (deps.?.items) |dep| {
|
||||
if (dep == num2) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
is_correct = false;
|
||||
break :outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_correct) {
|
||||
middle_page_number_sum += line.items[line.items.len / 2];
|
||||
} else {
|
||||
try incorrect_line_idxs.append(@intCast(lidx));
|
||||
}
|
||||
}
|
||||
std.debug.print("Part1: {}\n", .{middle_page_number_sum});
|
||||
|
||||
// Part 2
|
||||
middle_page_number_sum = 0;
|
||||
for (incorrect_line_idxs.items) |idx| {
|
||||
const line = lines.items[idx];
|
||||
var fixed_line = std.ArrayList(u32).init(ally);
|
||||
defer fixed_line.deinit();
|
||||
|
||||
for (line.items) |num| {
|
||||
const deps = dependencies.get(num);
|
||||
|
||||
if (deps == null) {
|
||||
try fixed_line.insert(0, num);
|
||||
continue;
|
||||
}
|
||||
|
||||
var j: usize = 0;
|
||||
for (fixed_line.items) |comparison_num| {
|
||||
if (array_includes(u32, deps.?.items, comparison_num)) {
|
||||
j += 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
try fixed_line.insert(j, num);
|
||||
}
|
||||
|
||||
middle_page_number_sum += fixed_line.items[fixed_line.items.len / 2];
|
||||
}
|
||||
|
||||
std.debug.print("Part 2: {}\n", .{middle_page_number_sum});
|
||||
}
|
||||
28
day5/test
Normal file
28
day5/test
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
47|53
|
||||
97|13
|
||||
97|61
|
||||
97|47
|
||||
75|29
|
||||
61|13
|
||||
75|53
|
||||
29|13
|
||||
97|29
|
||||
53|29
|
||||
61|53
|
||||
97|53
|
||||
61|29
|
||||
47|13
|
||||
75|47
|
||||
97|75
|
||||
47|61
|
||||
75|61
|
||||
47|29
|
||||
75|13
|
||||
53|13
|
||||
|
||||
75,47,61,53,29
|
||||
97,61,53,29,13
|
||||
75,29,13
|
||||
75,97,47,61,53
|
||||
61,13,29
|
||||
97,13,75,29,47
|
||||
134
day6/day6_part1.zig
Normal file
134
day6/day6_part1.zig
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
const std = @import("std");
|
||||
const in = @embedFile("in");
|
||||
|
||||
const Direction = struct {
|
||||
x: i32,
|
||||
y: i32,
|
||||
|
||||
const Left = Direction{ .x = -1, .y = 0 };
|
||||
const Right = Direction{ .x = 1, .y = 0 };
|
||||
const Up = Direction{ .x = 0, .y = -1 };
|
||||
const Down = Direction{ .x = 0, .y = 1 };
|
||||
|
||||
pub fn eql(self: Direction, other: Direction) bool {
|
||||
return self.x == other.x and self.y == other.y;
|
||||
}
|
||||
};
|
||||
|
||||
fn array_includes(comptime T: type, haystack: []T, needle: T) bool {
|
||||
for (haystack) |item| {
|
||||
if (item == needle) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
fn turn_right(direction: Direction) Direction {
|
||||
if (direction.eql(Direction.Up)) {
|
||||
return Direction.Right;
|
||||
} else if (direction.eql(Direction.Right)) {
|
||||
return Direction.Down;
|
||||
} else if (direction.eql(Direction.Down)) {
|
||||
return Direction.Left;
|
||||
} else if (direction.eql(Direction.Left)) {
|
||||
return Direction.Up;
|
||||
}
|
||||
unreachable;
|
||||
}
|
||||
|
||||
fn find_guard(lines: std.ArrayList(std.ArrayList(i8))) [2]i32 {
|
||||
for (lines.items, 0..) |line, y| {
|
||||
for (line.items, 0..) |item, x| {
|
||||
if (item == 1) {
|
||||
return .{ @intCast(x), @intCast(y) };
|
||||
}
|
||||
}
|
||||
}
|
||||
unreachable;
|
||||
}
|
||||
|
||||
fn print_lines(lines: std.ArrayList(std.ArrayList(i8))) void {
|
||||
for (lines.items) |line| {
|
||||
for (line.items) |item| {
|
||||
switch (item) {
|
||||
-1 => std.debug.print("#", .{}),
|
||||
0 => std.debug.print(".", .{}),
|
||||
1 => std.debug.print("^", .{}),
|
||||
2 => std.debug.print("V", .{}),
|
||||
else => std.debug.print(" ", .{}),
|
||||
}
|
||||
}
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
}
|
||||
|
||||
fn count_visited(lines: std.ArrayList(std.ArrayList(i8))) u32 {
|
||||
var count: u32 = 0;
|
||||
for (lines.items) |line| {
|
||||
for (line.items) |e| {
|
||||
if (e == 2 or e == 1) count += 1;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
fn move(lines: std.ArrayList(std.ArrayList(i8)), direction: Direction) void {
|
||||
// print_lines(lines);
|
||||
// std.debug.print("\n", .{});
|
||||
|
||||
const pos = find_guard(lines);
|
||||
var x = pos[0];
|
||||
var y = pos[1];
|
||||
lines.items[@intCast(y)].items[@intCast(x)] = 2; // 2 for already visited
|
||||
x += direction.x;
|
||||
y += direction.y;
|
||||
|
||||
if (y < 0 or y >= lines.items.len or x < 0 or x >= lines.items[@intCast(y)].items.len) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (lines.items[@intCast(y)].items[@intCast(x)] == -1) {
|
||||
x -= direction.x;
|
||||
y -= direction.y;
|
||||
lines.items[@intCast(y)].items[@intCast(x)] = 1;
|
||||
return move(lines, turn_right(direction));
|
||||
}
|
||||
|
||||
lines.items[@intCast(y)].items[@intCast(x)] = 1;
|
||||
return move(lines, direction);
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
const ally = gpa.allocator();
|
||||
defer _ = gpa.deinit();
|
||||
|
||||
var lines = std.ArrayList(std.ArrayList(i8)).init(ally);
|
||||
defer {
|
||||
for (lines.items) |*line| {
|
||||
line.deinit();
|
||||
}
|
||||
lines.deinit();
|
||||
}
|
||||
|
||||
var in_lines = std.mem.tokenize(u8, in, "\n");
|
||||
while (in_lines.next()) |linestr| {
|
||||
var line = std.ArrayList(i8).init(ally);
|
||||
for (linestr) |c| {
|
||||
const value: i8 = switch (c) {
|
||||
'#' => -1,
|
||||
'.' => 0,
|
||||
'^' => 1,
|
||||
else => -2,
|
||||
};
|
||||
try line.append(value);
|
||||
}
|
||||
try lines.append(line);
|
||||
}
|
||||
|
||||
move(lines, Direction.Up);
|
||||
const moves = count_visited(lines);
|
||||
|
||||
std.debug.print("{d}\n", .{moves});
|
||||
}
|
||||
130
day6/in
Normal file
130
day6/in
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
..#....##....................#...#...#..........................................................................................#.
|
||||
.....#..............................................#....#.....#.....#..............................#.......#...#.............#...
|
||||
............#.#.........................................#...................#..#...........................###..........#.....#...
|
||||
.....#.....#.............#...........................................................#.#.................#.........#..............
|
||||
.......................#...........................#................#...........#......#.....................#...#.......#....#...
|
||||
...#........................................#....................#.......#........##.##.........#.........................#.......
|
||||
......#...........#....................#...........#.............................#........................#....................#..
|
||||
...........................#...................................#...............#.........................................#........
|
||||
........#...........#................................................................#............#...............................
|
||||
.........................#......#.......#..............#......#.................#..................#...................#..........
|
||||
............#......................................................#..............................#........#....#.................
|
||||
........#.....................................................................#..............#....................................
|
||||
..#............................#.................#.#............#.................................................................
|
||||
...#................#.....................#.................................................#...................#.................
|
||||
......#...................#........................................................................................#..#...........
|
||||
.....................##..#........................................#...................#............#.....#........................
|
||||
.......#.................#..........#...........................#.......#..............#..........................................
|
||||
.............#........#........#..........#................#.........#.##.................................#.......#...........#...
|
||||
.#..............................................................#..#.....................................#.................##.....
|
||||
.........................#.#.......#.#..................................................#....................#.......#.........#..
|
||||
.............#................................................#..#................#.............#.....#...........................
|
||||
.............................................#..............#.....#..............#.........#......................................
|
||||
................#............#............................#...........#.#.........................................................
|
||||
...#.................................#.......#.........................................#..........................................
|
||||
...............................#.....................................................................#.....#.........##...........
|
||||
...................#...................................#.....................#.............................#..#...#...............
|
||||
......#.......................................................#............................#......#.................#.#...........
|
||||
...........................................#........................#.........#........................##......#..................
|
||||
....#.......................#...................#.....#....................#......................................................
|
||||
.....#......#....#..........#............#........#...............................................................................
|
||||
.........#.................................................................................#........#.........................#...
|
||||
........................#................................#........#...#......#........#..........#.#.......#.........#.........#.#
|
||||
.##......#..#......#............#.#.................................#..............................#.........#....................
|
||||
......#..............................#..........#.........#.....#....................................#...........................#
|
||||
..#.........................#.#...#............................................#.#.......................................##.......
|
||||
#.........................#......................#.............#..#.#...#....#.....#.#...#......#........................#........
|
||||
.#............................#.................................#....#..............................#........#..........#.........
|
||||
..........#.........#.....#.....#......................#............................................#.............................
|
||||
.........................#...#.............................#..............................................#......#.#..............
|
||||
....................#................................................................................................#............
|
||||
...#........#......................#..#...........#..#.........#.................................................#................
|
||||
...................#....................#..............#......................#..............#........#......................#....
|
||||
.............#..................................................................................#................#.....##.........
|
||||
.#.............................#........#................#..#..#.....#...........###..............................................
|
||||
.......................................................#.........................#.............................#..................
|
||||
...........................##.............#.........................................................#.......................#.....
|
||||
......#.......##.........#..................................................................#.#..#.........#.........#.......#....
|
||||
..#...........#..........#........#....#..........#..................#.............#.............#................................
|
||||
#................................##...........#......#....##......#..#.....................................#.........#..#.........
|
||||
...#...#.#.......................#......................................#.........#......#...............#........................
|
||||
..#...#.......................................................#..................#.........................................#......
|
||||
...#.....#.....#..........#....................#..................#.........................#...............#...#..#....#.........
|
||||
..........#................................#...................#..#...........#.........#....................................#....
|
||||
..............#........#............................#..................................................................#........#.
|
||||
............................................................#.....##.................................#............................
|
||||
...##..............................................#..........................................#.............................#.##..
|
||||
...#...........................................................................#......#.....................#.#...................
|
||||
.........#.............................................................................##.....................................#.#.
|
||||
...............#.....#........#..#................................................................................................
|
||||
#...........................#.#..............................#........^......#.................................#.....#............
|
||||
...#...#.........................................#......................#.#.......................................................
|
||||
.#......#...........................#.............................................................................................
|
||||
.....#.........#.......#..#........#.....#.....................#......#........................................#...#..............
|
||||
....#....................................................................................................#........................
|
||||
#..............#.......................#.........................................#...............................#.#..............
|
||||
................................#..............................#.......................................................#....#.....
|
||||
....#............................#............................................................#.....#........#...................#
|
||||
...........#...#............................#....................#.......................................................#........
|
||||
.#.....................................................................................................#.....#....................
|
||||
.#.......#..............#.....................#.....................#............#........................#.......................
|
||||
............................#...#................................................#................................#...............
|
||||
..........#...............#..........................#................#.........................#.......#.#.......................
|
||||
..............................#.............................................................................................#.....
|
||||
..............................................#....#...............................................#.....#........................
|
||||
.........#...................................................#...............#.....#................#.............................
|
||||
...........................................#..............#....................................................#........#.#.......
|
||||
..........................#...........#.#...#.....................................#......................#...........#..#.........
|
||||
....#...............#..............#.............................#....#.....#....#................................................
|
||||
....#.#...........#..............#.................................................................#..............................
|
||||
.....#.......#..........................................................................................#.#.......................
|
||||
................................................#..#..............................................................................
|
||||
.#....#..............................#..........#................#.............................................................#..
|
||||
....#.......................#........#.....#..............#...............#..#........................................#...........
|
||||
...........................................................................#......................................................
|
||||
...#.........#..#.......................................................#..#..............................#.......................
|
||||
.............#...................................................................#.......................#...#...........#........
|
||||
.....#........................#....................#...................#................................#.........................
|
||||
.........#......#...........................................#.......#....................#........................................
|
||||
...........................................................#...........................#.............................#............
|
||||
..........................#..............#........................................#......#.........#.#.............#........#.....
|
||||
.........#..............#..#...........................#......#.......................#..#..#.............#.......................
|
||||
...#.................#...............................................................................................#............
|
||||
................#.....#.#...........................#......................#....................................#...#.#...........
|
||||
..................#.....#...............................................................#............................#....#..#....
|
||||
...............................#................................................#.............#..........................#........
|
||||
..#...........#................................................#.........................................#...............##......#
|
||||
...............................#..................##..#...........................................#................#..............
|
||||
...........................#........#.........#....................................................#......#......#................
|
||||
..................................#................#..................#.....#..................#...................#.....#........
|
||||
.......#...#................................................................##...........................................#........
|
||||
..............#..#.......#............................#...................#....#...........................#..#......#............
|
||||
...............#............#................................................................................#..............#.....
|
||||
...............#.....#........................#.........#.........#...#................#....#.........#...........................
|
||||
................................#........................................................................#.........#..............
|
||||
.............#..................#...#................................#..............#................................#.........#..
|
||||
.....................#.#.............................#......#..................................#.........##....#.................#
|
||||
......#.........#...#.................................#..........#.#......................#.......#........#......................
|
||||
.....#....................#......................#.........#.....................#.........................................#......
|
||||
.........##.....................#....#.#....#....................................................................................#
|
||||
.................................................#.#....#....................#...............##..#................................
|
||||
..##.......#........#.......................#.....................................................................#...........#.##
|
||||
..#...........##........##......#..........#....................#.....#.....#..................#.....#............................
|
||||
..........................#.......................#.....#.........................................................................
|
||||
.#...........................#..............#...#...........#.....#.......#....#............#.....#..................#....#......#
|
||||
.................#.......................#....#...#..................#......#............................#.....................#..
|
||||
.....##..#........#........................#......#........................#....#......................##.........................
|
||||
...#....................................................................................................#.......................#.
|
||||
.......................#...#..............#..................#............#.#.........#....#...#..................................
|
||||
...........#.......................#.......................#.......#....#.....#..................#...................#............
|
||||
..................#......................................................................#.......#..........................#.....
|
||||
.......#..................#.............#..#........##........#..##........##.....##...#................#..#.......#..............
|
||||
........#................#..............................#..#......................................................................
|
||||
...................#....#.........................#...........#.#......##.........#.#..................#..#.#....#........#.......
|
||||
.............#......#.....................................................#.......................................................
|
||||
.........................#.......................#..................#.........................#...............................#..#
|
||||
..#...........................#............#.................##...#................#.................#................#...........
|
||||
.....#...............#.....#..........#..................................#.#........................#.....###.#...................
|
||||
...............................................#.............#......................................................#.............
|
||||
......#......................#..............#.................#.#........#.................#......................................
|
||||
........................#.......................................#....................#.......#.#..#...............................
|
||||
10
day6/test
Normal file
10
day6/test
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
....#.....
|
||||
.........#
|
||||
..........
|
||||
..#.......
|
||||
.......#..
|
||||
..........
|
||||
.#..^.....
|
||||
........#.
|
||||
#.........
|
||||
......#...
|
||||
Loading…
Add table
Add a link
Reference in a new issue