Add solution for 2023 day 05 part 2
This commit is contained in:
parent
8563c691ba
commit
7944889184
@ -8,7 +8,7 @@
|
||||
| [02] | ✓ | ✓ | [Zig] |
|
||||
| [03] | ✓ | ✓ | [Zig] |
|
||||
| [04] | ✓ | ✓ | [Zig] |
|
||||
| [05] | ✓ | | [Zig] |
|
||||
| [05] | ✓ | ✓ | [Zig] |
|
||||
| [06] | | | |
|
||||
| [07] | | | |
|
||||
| [08] | | | |
|
||||
|
@ -1,7 +1,7 @@
|
||||
const std = @import("std");
|
||||
const alloc = std.heap.page_allocator;
|
||||
const List = std.ArrayList;
|
||||
const HashMap = std.AutoHashMap(MappingKey, Mapping);
|
||||
pub const Mappings = std.AutoHashMap(MappingKey, Mapping);
|
||||
pub const Idx = u64;
|
||||
|
||||
pub const SeedList = List(Idx);
|
||||
@ -40,7 +40,7 @@ pub const Mapping = struct {
|
||||
};
|
||||
|
||||
pub const Input = struct {
|
||||
mappings: HashMap,
|
||||
mappings: Mappings,
|
||||
seeds: SeedList,
|
||||
};
|
||||
|
||||
@ -52,7 +52,7 @@ pub const MappingRange = struct {
|
||||
pub fn parse(data: []const u8) !Input {
|
||||
var lines = std.mem.tokenizeSequence(u8, data, "\n");
|
||||
|
||||
var mappings = HashMap.init(alloc);
|
||||
var mappings = Mappings.init(alloc);
|
||||
var seeds: SeedList = undefined;
|
||||
var mapping_types: [2]MappingType = undefined;
|
||||
var dest_ranges = List(MappingRange).init(alloc);
|
||||
|
67
2023/day-05/part_two.zig
Normal file
67
2023/day-05/part_two.zig
Normal file
@ -0,0 +1,67 @@
|
||||
const std = @import("std");
|
||||
const common = @import("common.zig");
|
||||
const Idx = common.Idx;
|
||||
const AtomicIdx = std.atomic.Value(Idx);
|
||||
const AtomicOrder = std.builtin.AtomicOrder;
|
||||
const Mappings = common.Mappings;
|
||||
const List = std.ArrayList;
|
||||
const alloc = std.heap.page_allocator;
|
||||
|
||||
const data = @embedFile("inputs/puzzle.txt");
|
||||
|
||||
pub fn main() !void {
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
|
||||
const parsed = try common.parse(data);
|
||||
const mappings = parsed.mappings;
|
||||
const seeds = parsed.seeds;
|
||||
|
||||
var chunk = std.mem.window(Idx, seeds.items, 2, 2);
|
||||
var threads = List(std.Thread).init(alloc);
|
||||
|
||||
var min_loc = AtomicIdx.init(18_446_744_073_709_551_615);
|
||||
|
||||
// Just brute force this bitch
|
||||
while (chunk.next()) |pair| {
|
||||
const start = pair[0];
|
||||
const end = pair[0] + pair[1];
|
||||
|
||||
const thread = try std.Thread.spawn(.{}, get_min_location, .{ &mappings, &min_loc, start, end });
|
||||
try threads.append(thread);
|
||||
}
|
||||
|
||||
for (threads.items) |thread| {
|
||||
thread.join();
|
||||
}
|
||||
|
||||
try stdout.print("{d}\n", .{min_loc.raw});
|
||||
}
|
||||
|
||||
fn get_min_location(mappings: *const Mappings, min_loc: *AtomicIdx, start: Idx, end: Idx) !void {
|
||||
for (start..end) |seed| {
|
||||
const ss_map = mappings.get(.{ .Seed, .Soil }).?;
|
||||
const soil = ss_map.get(seed);
|
||||
|
||||
const sf_map = mappings.get(.{ .Soil, .Fertilizer }).?;
|
||||
const fert = sf_map.get(soil);
|
||||
|
||||
const fw_map = mappings.get(.{ .Fertilizer, .Water }).?;
|
||||
const water = fw_map.get(fert);
|
||||
|
||||
const wl_map = mappings.get(.{ .Water, .Light }).?;
|
||||
const light = wl_map.get(water);
|
||||
|
||||
const lt_map = mappings.get(.{ .Light, .Temperature }).?;
|
||||
const temp = lt_map.get(light);
|
||||
|
||||
const th_map = mappings.get(.{ .Temperature, .Humidity }).?;
|
||||
const humi = th_map.get(temp);
|
||||
|
||||
const hl_map = mappings.get(.{ .Humidity, .Location }).?;
|
||||
const loc = hl_map.get(humi);
|
||||
|
||||
if (loc < min_loc.load(AtomicOrder.SeqCst)) {
|
||||
min_loc.store(loc, AtomicOrder.SeqCst);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user