1
0

day14 part1

This commit is contained in:
xeals 2023-12-14 17:25:19 +11:00
parent b13d5bb5f9
commit b9bff3998e
Signed by: xeals
SSH Key Fingerprint: SHA256:pRv+8swQDA+/LuZ7NHj9m006BbKexlNK62OUA01ZZBc

78
src/day14.zig Normal file
View File

@ -0,0 +1,78 @@
const std = @import("std");
const util = @import("util.zig");
const mem = std.mem;
pub fn main() !void {
const input = @embedFile("data/day14.txt");
const sln = try solve(util.gpa, input);
std.debug.print("{d}\n", .{sln.a});
std.debug.print("{d}\n", .{sln.b});
}
const Solution = struct {
a: usize,
b: usize,
};
fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
const width = mem.indexOfScalar(u8, input, '\n').?;
var height = mem.count(u8, input, &[_]u8{'\n'});
if (input[input.len - 1] != '\n') height += 1;
var next_slot = try alloc.alloc(u32, width);
defer alloc.free(next_slot);
@memset(next_slot, 0);
var weight: usize = 0;
var it = util.splitLines(input);
var row: u32 = 0;
while (it.next()) |line| {
if (line.len == 0) continue;
for (line, 0..) |char, col| {
switch (char) {
'O' => {
std.debug.print("rock at {}x{} moves to {}\n", .{ col, row, height - next_slot[col] });
weight += height - next_slot[col];
next_slot[col] += 1;
std.debug.print("{any}\n", .{next_slot});
},
'#' => {
std.debug.print("cube at {}x{}\n", .{ col, row });
next_slot[col] = @as(u32, @intCast(row)) + 1;
std.debug.print("{any}\n", .{next_slot});
},
else => {},
}
}
row += 1;
}
return .{ .a = weight, .b = 0 };
}
test "silver" {
const input =
\\O....#....
\\O.OO#....#
\\.....##...
\\OO.#O....O
\\.O.....O#.
\\O.#..O.#.#
\\..O..#O..O
\\.......O..
\\#....###..
\\#OO..#....
;
const sln = try solve(std.testing.allocator, input);
try std.testing.expectEqual(@as(usize, 136), sln.a);
}
test "gold" {
const input =
\\
;
_ = input;
// const sln = try solve(std.testing.allocator, input);
// try std.testing.expectEqual(@as(usize, 0), sln.b);
}