day 8
This commit is contained in:
171
08/src/main.zig
Normal file
171
08/src/main.zig
Normal file
@@ -0,0 +1,171 @@
|
||||
// yea zig was fine today i just suck
|
||||
// and didn't want to think
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
const alloc = arena.allocator();
|
||||
|
||||
const test_data = @embedFile("test");
|
||||
const input_data = @embedFile("input");
|
||||
|
||||
pub fn main() !void {
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
|
||||
const value1 = try run1(input_data);
|
||||
try stdout.print("{d}\n", .{value1});
|
||||
|
||||
const value2 = try run2(input_data);
|
||||
try stdout.print("{d}\n", .{value2});
|
||||
}
|
||||
|
||||
test "test input part 1" {
|
||||
std.debug.print("\n", .{});
|
||||
const value = try run1(test_data);
|
||||
std.debug.print("{d}\n", .{value});
|
||||
try std.testing.expectEqual(14, value);
|
||||
}
|
||||
|
||||
test "test input part 2" {
|
||||
std.debug.print("\n",.{});
|
||||
const value = try run2(test_data);
|
||||
std.debug.print("{d}\n", .{value});
|
||||
try std.testing.expectEqual(34, value);
|
||||
}
|
||||
|
||||
fn parseIntFromReader(reader: anytype) anyerror!u64 {
|
||||
const f = try reader.readByte();
|
||||
if (f < '0' or f > '9') return error.BadCharacter;
|
||||
var v: u64 = f - '0';
|
||||
while (true) {
|
||||
const c = try reader.readByte();
|
||||
if (c < '0' or c > '9') {
|
||||
break;
|
||||
} else {
|
||||
v = (v * 10) + (c - '0');
|
||||
}
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
fn run2(s: []const u8) !u64 {
|
||||
var map: [256][256]u8 = [_][256]u8{[_]u8{0}**256}**256;
|
||||
var lines = std.mem.splitScalar(u8, s, '\n');
|
||||
var y: usize = 0;
|
||||
var x: usize = 0;
|
||||
var dim: usize = undefined;
|
||||
while (lines.next()) |line| {
|
||||
if (std.mem.eql(u8, line, "")) continue;
|
||||
std.mem.copyForwards(u8, &map[y], line);
|
||||
std.debug.print("{s}\n",.{line});
|
||||
dim = line.len;
|
||||
y += 1;
|
||||
}
|
||||
|
||||
std.debug.print("map max {}\n", .{dim});
|
||||
|
||||
var total: u64 = 0;
|
||||
|
||||
var antinodes: [256][256]u8 = [_][256]u8{[_]u8{0}**256}**256;
|
||||
|
||||
y = 0;
|
||||
while (y < dim) : (y += 1) {
|
||||
x = 0;
|
||||
while (x < dim) : (x += 1) {
|
||||
if (map[y][x] == '.') continue;
|
||||
const v = map[y][x];
|
||||
|
||||
var ix: usize = 0;
|
||||
var iy: usize = 0;
|
||||
while (iy < dim) : (iy += 1) {
|
||||
ix = 0;
|
||||
while (ix < dim) : (ix += 1) {
|
||||
if (iy == y and ix == x) continue;
|
||||
if (map[iy][ix] != v) continue;
|
||||
|
||||
const dy: isize = @as(isize, @intCast(iy)) - @as(isize, @intCast(y));
|
||||
const dx: isize = @as(isize, @intCast(ix)) - @as(isize, @intCast(x));
|
||||
var ay = @as(isize,@intCast(iy));
|
||||
var ax = @as(isize,@intCast(ix));
|
||||
while (ay >= 0 and ay < dim and ax >= 0 and ax < dim) {
|
||||
antinodes[@intCast(ay)][@intCast(ax)] = 1;
|
||||
ay += dy;
|
||||
ax += dx;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
y = 0;
|
||||
while (y < dim) : (y += 1) {
|
||||
x = 0;
|
||||
while (x < dim) : (x += 1) {
|
||||
total += antinodes[y][x];
|
||||
const c: u8 = if (antinodes[y][x] == 0) '.' else '#';
|
||||
std.debug.print("{c}",.{c});
|
||||
}
|
||||
std.debug.print("\n",.{});
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
fn run1(s: []const u8) !u64 {
|
||||
var map: [256][256]u8 = [_][256]u8{[_]u8{0}**256}**256;
|
||||
var lines = std.mem.splitScalar(u8, s, '\n');
|
||||
var y: usize = 0;
|
||||
var x: usize = 0;
|
||||
var dim: usize = undefined;
|
||||
while (lines.next()) |line| {
|
||||
if (std.mem.eql(u8, line, "")) continue;
|
||||
std.mem.copyForwards(u8, &map[y], line);
|
||||
std.debug.print("{s}\n",.{line});
|
||||
dim = line.len;
|
||||
y += 1;
|
||||
}
|
||||
|
||||
std.debug.print("map max {}\n", .{dim});
|
||||
|
||||
var total: u64 = 0;
|
||||
|
||||
var antinodes: [256][256]u8 = [_][256]u8{[_]u8{0}**256}**256;
|
||||
|
||||
y = 0;
|
||||
while (y < dim) : (y += 1) {
|
||||
x = 0;
|
||||
while (x < dim) : (x += 1) {
|
||||
if (map[y][x] == '.') continue;
|
||||
const v = map[y][x];
|
||||
|
||||
var ix: usize = 0;
|
||||
var iy: usize = 0;
|
||||
while (iy < dim) : (iy += 1) {
|
||||
ix = 0;
|
||||
while (ix < dim) : (ix += 1) {
|
||||
if (iy == y and ix == x) continue;
|
||||
if (map[iy][ix] != v) continue;
|
||||
|
||||
const ay: isize = @as(isize,@intCast(iy)) + (@as(isize, @intCast(iy)) - @as(isize, @intCast(y)));
|
||||
const ax: isize = @as(isize,@intCast(ix)) + (@as(isize, @intCast(ix)) - @as(isize, @intCast(x)));
|
||||
if (ay < 0 or ay >= dim) continue;
|
||||
if (ax < 0 or ax >= dim) continue;
|
||||
antinodes[@intCast(ay)][@intCast(ax)] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
y = 0;
|
||||
while (y < dim) : (y += 1) {
|
||||
x = 0;
|
||||
while (x < dim) : (x += 1) {
|
||||
total += antinodes[y][x];
|
||||
const c: u8 = if (antinodes[y][x] == 0) '.' else '#';
|
||||
std.debug.print("{c}",.{c});
|
||||
}
|
||||
std.debug.print("\n",.{});
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
Reference in New Issue
Block a user