1
0

Compare commits

...

2 Commits

Author SHA1 Message Date
faf81869d6
wtf
Some checks failed
Build and test / test (push) Failing after 2m35s
2023-12-05 17:50:38 +11:00
d85d4f0d26
day05 part2 OOM killed lmao 2023-12-05 16:46:39 +11:00

200
src/day05.zig Normal file
View File

@ -0,0 +1,200 @@
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,
};
const UsizeSet = std.AutoHashMap(usize, void);
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_values = UsizeSet.init(alloc);
var working_values = UsizeSet.init(alloc);
defer last_values.deinit();
defer working_values.deinit();
var low_loc: usize = std.math.maxInt(usize);
var seeds_index: usize = 0;
while (seeds_index < seeds.len) : (seeds_index += 2) {
last_values.clearRetainingCapacity();
working_values.clearRetainingCapacity();
std.debug.print("= seeding {d}/{d} =\n", .{ seeds_index, seeds.len });
const start = seeds[seeds_index];
const range = seeds[seeds_index + 1];
for (start..start + range) |seed| {
// std.debug.print("seed {d}\n", .{seed});
try working_values.put(seed, {});
}
// Reset parse input.
it = util.splitLines(input);
_ = it.next().?;
var current_component: []const u8 = undefined;
while (it.next()) |line| {
if (line.len == 0) {
std.debug.print("== swap ==\n", .{});
// Copy all remaining
var lv_it = last_values.iterator();
while (lv_it.next()) |entry| {
try working_values.put(entry.key_ptr.*, {});
}
// Swap working sets
mem.swap(UsizeSet, &last_values, &working_values);
working_values.clearRetainingCapacity();
} 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 {
std.debug.print("== {s} {d}/{d}\n", .{ current_component, working_values.count(), last_values.count() });
const map = try util.parseIntsScalar(usize, alloc, line, .{});
defer alloc.free(map);
var to_delete = UsizeSet.init(alloc);
defer to_delete.deinit();
var lv_it = last_values.iterator();
while (lv_it.next()) |entry| {
// if (transitioned[index]) {
// std.debug.print("{d} already mapped\n", .{index});
// continue :map_blk;
// }
const item = entry.key_ptr.*;
if (map[1] <= item and item <= map[1] + map[2]) {
const offset = @as(isize, @intCast(map[0])) - @as(isize, @intCast(map[1]));
const res: usize = @intCast(@as(isize, @intCast(item)) + offset);
// std.debug.print("{d} => {d}\n", .{ item, res });
try working_values.put(res, {});
try to_delete.put(item, {});
}
}
var td_it = to_delete.iterator();
while (td_it.next()) |entry| {
// std.debug.print("pop {d}\n", .{entry.key_ptr.*});
_ = last_values.remove(entry.key_ptr.*);
}
}
}
var lv_it = last_values.iterator();
while (lv_it.next()) |value| {
low_loc = @min(low_loc, value.key_ptr.*);
}
}
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);
}