day 5, part 1 (bad but working solution)

main
Inga 🏳‍🌈 1 year ago
parent faf1d82e4b
commit 97c6961e5f
  1. 49
      day05-easy/src/main.zig

@ -21,8 +21,11 @@ fn StackList(comptime T: type, comptime capacity_type: type, comptime capacity:
return false;
}
fn getSlice(self: *const Self) []const T {
return self.mem[0..self.length];
fn getSlice(self: *Self) []T {
//var mem_full_slice = &self.mem;
//var mem_slice: []T = mem_full_slice[0..self.length];
//return mem_slice;
return (&self.mem)[0..self.length];
}
fn reset(self: *Self) void {
@ -64,28 +67,34 @@ const State = struct {
}
fn addMapping(self: *State, destination_start: u64, source_start: u64, range_length: u64) void {
var current = self.getCurrent();
const previous = self.getPrevious().*;
const source_end = source_start + range_length;
std.debug.print("processing mapping from {d}-{d} (length {d}) to {d}\n", .{ source_start, source_end, range_length, destination_start });
for (previous.getSlice()) |value| {
if (value >= source_start and value < source_end) {
const new_value = (value - source_start) + destination_start;
std.debug.print("adding new value {d}\n", .{new_value});
current.*.add(new_value);
//std.debug.print("processing mapping from {d}-{d} (length {d}) to {d}\n", .{ source_start, source_end, range_length, destination_start });
for (self.getPrevious().*.getSlice()) |*value| {
if (value.* >= source_start and value.* < source_end) {
const new_value = (value.* - source_start) + destination_start;
//std.debug.print("adding new value {d}\n", .{new_value});
self.add(new_value);
value.* = std.math.maxInt(u64);
}
}
}
fn swap(self: *State) void {
fn finishMappings(self: *State) void {
//std.debug.print("previous values: {any}\n", .{self.getPrevious().*.getSlice()});
for (self.getPrevious().*.getSlice()) |value| {
if (value != std.math.maxInt(u64)) {
self.add(value);
}
}
self.is_a_current = !self.is_a_current;
self.getCurrent().*.reset();
}
fn getMinCurrentValue(self: *State) u64 {
var current = self.getCurrent().*;
fn getMinPreviousValue(self: *State) u64 {
var previous = self.getPrevious().*;
var result: u32 = std.math.maxInt(u32);
for (current.getSlice()) |value| {
for (previous.getSlice()) |value| {
result = @min(result, value);
}
@ -93,7 +102,8 @@ const State = struct {
}
fn debug(self: *State) void {
std.debug.print("Current state: {any}, previous state: {any}\n", .{ self.getCurrent().*.getSlice(), self.getPrevious().*.getSlice() });
_ = self;
//std.debug.print("Current state: {any}, previous state: {any}\n", .{ self.getCurrent().*.getSlice(), self.getPrevious().*.getSlice() });
}
};
@ -164,18 +174,19 @@ pub fn main() !void {
state.debug();
_ = try reader.readUntilDelimiterOrEof(&line_buffer, '\n');
_ = try reader.readUntilDelimiterOrEof(&line_buffer, '\n');
state.swap();
state.finishMappings();
state.debug();
while (try reader.readUntilDelimiterOrEof(&line_buffer, '\n')) |line| {
if (line.len != 0) {
parseMappingLine(line, &state);
} else {
state.debug();
_ = try reader.readUntilDelimiterOrEof(&line_buffer, '\n');
state.swap();
state.finishMappings();
state.debug();
}
}
state.finishMappings();
state.debug();
try stdout.print("{d}\n", .{state.getMinCurrentValue()});
try stdout.print("{d}\n", .{state.getMinPreviousValue()});
}

Loading…
Cancel
Save