FEAT: Day 13!!!

This commit is contained in:
2024-03-25 23:24:16 +00:00
parent dc88c159ed
commit ef5c179af9
4 changed files with 178 additions and 7 deletions

View File

@ -0,0 +1,162 @@
const std = @import("std");
const print = std.debug.print;
fn get_grid_col(allocator: std.mem.Allocator, input: [][]u8, colIndex: usize) ![]u8 {
var col = try allocator.alloc(u8, input.len);
for (input, 0..) |line, index| {
col[index] = line[colIndex];
}
return col;
}
fn single_tile(allocator: std.mem.Allocator, input: [][]u8, part1val: usize) !usize {
var ok = true;
var index_found: usize = 0;
for (0..input.len - 1) |index| {
var down_counter: usize = index;
var up_counter: usize = index + 1;
ok = true;
while (down_counter >= 0 and up_counter < input.len) {
if (!std.mem.eql(u8, input[down_counter], input[up_counter])) {
ok = false;
break;
}
if (down_counter == 0) {
break;
}
down_counter -= 1;
up_counter += 1;
}
if (ok and (index + 1) * 100 != part1val) {
index_found = index;
break;
} else {
ok = false;
}
}
if (ok) {
// horizon found
return (index_found + 1) * 100;
}
ok = true;
index_found = 0;
for (0..input[0].len - 1) |index| {
var down_counter: usize = index;
var up_counter: usize = index + 1;
ok = true;
while (down_counter >= 0 and up_counter < input[0].len) {
var col1 = try get_grid_col(allocator, input, down_counter);
var col2 = try get_grid_col(allocator, input, up_counter);
defer allocator.free(col1);
defer allocator.free(col2);
if (!std.mem.eql(u8, col1, col2)) {
ok = false;
break;
}
if (down_counter == 0) {
break;
}
down_counter -= 1;
up_counter += 1;
}
if (ok and index + 1 != part1val) {
index_found = index;
break;
} else {
ok = false;
}
}
if (ok) {
return index_found + 1;
}
return 0;
}
pub fn solve(input: [][]u8) !void {
var start_pointer: usize = 0;
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var allocator = gpa.allocator();
var counter: usize = 0;
var part1_results = try allocator.alloc(usize, 1000);
var part1: usize = 0;
var part2: usize = 0;
for (input, 0..) |line, index| {
if (line.len == 0) {
// found our empty line.
var num = try single_tile(allocator, input[start_pointer..index], 0);
part1_results[counter] = num;
part1 += num;
start_pointer = index + 1;
counter += 1;
}
}
start_pointer = 0;
counter = 0;
for (input, 0..) |line, index| {
if (line.len == 0) {
// found our empty line.
var temp: u8 = 0;
topLoop: for (start_pointer..index) |i| {
for (0..input[start_pointer].len) |j| {
temp = input[i][j];
if (input[i][j] == '.') {
temp = '.';
input[i][j] = '#';
} else {
temp = '#';
input[i][j] = '.';
}
var num = try single_tile(allocator, input[start_pointer..index], part1_results[counter]);
input[i][j] = temp;
if (num == 0) {
continue;
}
part2 += num;
break :topLoop;
}
}
start_pointer = index + 1;
counter += 1;
}
}
// 28996 tried
print("Part 1: {}\n", .{part1});
print("Part 2: {}\n", .{part2});
}

View File

@ -0,0 +1,3 @@
pub fn solve(input: [][]const u8) !void {
_ = input;
}

View File

@ -9,7 +9,9 @@ const std = @import("std");
// const day9 = @import("./day9/day9.zig");
// const day10 = @import("./day10/day10.zig");
// const day11 = @import("./day11/day11.zig");
const day = @import("./day12/day12.zig");
// const day = @import("./day12/day12.zig");
// const day14 = @import("./day14/day14.zig");
const day13 = @import("./day13/day13.zig");
// const day15 = @import("./day15/day15.zig");
// const day16 = @import("./day16/day16.zig");
// const day18 = @import("./day18/day18.zig");
@ -21,8 +23,8 @@ const utils = @import("utils.zig");
pub fn main() !void {
const allocator = std.heap.page_allocator;
var input = try utils.getInput("./src/day12/input.txt", allocator);
var input = try utils.getInput("./src/day13/input.txt", allocator);
defer allocator.free(input);
try day.solve(input);
try day13.solve(input);
}

View File

@ -2,19 +2,23 @@ const std = @import("std");
const mem = std.mem;
const ArrayList = std.ArrayList;
pub fn getInput(fileName: []const u8, allocator: mem.Allocator) !([][]const u8) {
pub fn getInput(fileName: []const u8, allocator: mem.Allocator) !([][]u8) {
const file = try std.fs.cwd().openFile(fileName, .{});
var content = try file.reader().readAllAlloc(allocator, 999999);
var lines = ArrayList([]const u8).init(allocator);
var lines = ArrayList([]u8).init(allocator);
defer lines.deinit();
var iterator = mem.tokenize(u8, content, "\n");
var iterator = mem.split(u8, content, "\n");
while (iterator.next()) |line| {
try lines.append(line);
var mutableSlice = try allocator.alloc(u8, line.len);
std.mem.copy(u8, mutableSlice, line);
try lines.append(mutableSlice);
}
var input = try lines.toOwnedSlice();
return input;
}