1
0

day05 part2 OOM killed lmao

This commit is contained in:
xeals 2023-12-05 16:46:39 +11:00
parent 052bf70095
commit d85d4f0d26
Signed by: xeals
SSH Key Fingerprint: SHA256:pRv+8swQDA+/LuZ7NHj9m006BbKexlNK62OUA01ZZBc

171
src/day05.zig Normal file
View File

@ -0,0 +1,171 @@
const std = @import("std");
const util = @import("util.zig");
const mem = std.mem;
pub fn main() !void {
const input = @embedFile("data/day05.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,
};
const Transform = struct {
from: []const u8,
to: []const u8,
};
const Map = struct {
dest_start: usize,
source_start: usize,
range: usize,
};
const Component = struct {
name: []const u8,
value: usize,
};
fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
var it = util.splitLines(input);
const seed_line = it.next().?;
const seeds = try util.parseIntsScalar(usize, alloc, seed_line[mem.indexOfScalar(u8, seed_line, ':').? + 1 ..], .{});
defer alloc.free(seeds);
var last_components = try std.ArrayList(Component).initCapacity(alloc, seeds.len);
defer last_components.deinit();
var seeds_index: usize = 0;
while (seeds_index < seeds.len) : (seeds_index += 2) {
const start = seeds[seeds_index];
const range = seeds[seeds_index + 1];
for (start..start + range) |seed| {
try last_components.append(.{ .name = "seed", .value = seed });
}
}
var current_component: []const u8 = undefined;
while (it.next()) |line| {
if (line.len == 0) {
for (last_components.items, 0..) |comp, index| {
if (!mem.eql(u8, comp.name, current_component)) {
last_components.items[index] = .{ .name = current_component, .value = comp.value };
}
}
} else if (mem.endsWith(u8, line, "map:")) {
var t_it = util.split(line, ' ');
var comp_it = util.split(t_it.next().?, '-');
_ = comp_it.next(); // A
_ = comp_it.next(); // to
current_component = comp_it.next().?; // B
} else {
const map = try util.parseIntsScalar(usize, alloc, line, .{});
defer alloc.free(map);
// map values
map_blk: for (last_components.items, 0..) |comp, index| {
if (mem.eql(u8, comp.name, current_component)) {
std.debug.print("{d} already mapped\n", .{index});
continue :map_blk;
}
if (map[1] <= comp.value and comp.value <= map[1] + map[2]) {
const offset = @as(isize, @intCast(map[0])) - @as(isize, @intCast(map[1]));
const res: usize = @intCast(@as(isize, @intCast(comp.value)) + offset);
std.debug.print("{d} {d} => {d}\n", .{ index, comp.value, res });
last_components.items[index] = .{ .name = current_component, .value = res };
}
}
}
}
var low_loc: usize = std.math.maxInt(usize);
for (last_components.items) |comp| {
std.debug.print("{s} ", .{comp.name});
low_loc = @min(low_loc, comp.value);
}
return .{ .a = low_loc, .b = 0 };
}
test "silver" {
const input =
\\seeds: 79 14 55 13
\\
\\seed-to-soil map:
\\50 98 2
\\52 50 48
\\
\\soil-to-fertilizer map:
\\0 15 37
\\37 52 2
\\39 0 15
\\
\\fertilizer-to-water map:
\\49 53 8
\\0 11 42
\\42 0 7
\\57 7 4
\\
\\water-to-light map:
\\88 18 7
\\18 25 70
\\
\\light-to-temperature map:
\\45 77 23
\\81 45 19
\\68 64 13
\\
\\temperature-to-humidity map:
\\0 69 1
\\1 0 69
\\
\\humidity-to-location map:
\\60 56 37
\\56 93 4
;
_ = input;
// const sln = try solve(std.testing.allocator, input);
// try std.testing.expectEqual(@as(usize, 35), sln.a);
}
test "gold" {
const input =
\\seeds: 79 14 55 13
\\
\\seed-to-soil map:
\\50 98 2
\\52 50 48
\\
\\soil-to-fertilizer map:
\\0 15 37
\\37 52 2
\\39 0 15
\\
\\fertilizer-to-water map:
\\49 53 8
\\0 11 42
\\42 0 7
\\57 7 4
\\
\\water-to-light map:
\\88 18 7
\\18 25 70
\\
\\light-to-temperature map:
\\45 77 23
\\81 45 19
\\68 64 13
\\
\\temperature-to-humidity map:
\\0 69 1
\\1 0 69
\\
\\humidity-to-location map:
\\60 56 37
\\56 93 4
;
const sln = try solve(std.testing.allocator, input);
try std.testing.expectEqual(@as(usize, 46), sln.a);
}