day02 parsing
This commit is contained in:
parent
4e13fe6e80
commit
4a39266b11
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,89 @@
|
|||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
const expect = testing.expect;
|
||||
const ArrayList = std.ArrayList;
|
||||
const utils = @import("utils.zig");
|
||||
|
||||
fn parse_input(str_input: []const u8, allocator: std.mem.Allocator) !ArrayList(ArrayList(u32)) {
|
||||
var lines = std.mem.splitSequence(u8, str_input, "\n");
|
||||
var array = ArrayList(ArrayList(u32)).init(allocator);
|
||||
|
||||
while (lines.next()) |line| {
|
||||
var current_line_array = ArrayList(u32).init(allocator);
|
||||
|
||||
var current_number: u32 = 0;
|
||||
for (0..line.len) |i| {
|
||||
const char: u8 = line[i];
|
||||
switch (char) {
|
||||
'0'...'9' => {
|
||||
current_number = current_number * 10 + (char - '0');
|
||||
},
|
||||
else => {
|
||||
try current_line_array.append(current_number);
|
||||
current_number = 0;
|
||||
},
|
||||
}
|
||||
}
|
||||
try current_line_array.append(current_number);
|
||||
try array.append(current_line_array);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
fn count_safe_reports(lines: ArrayList(ArrayList(u32))) u32 {
|
||||
var count: u32 = 0;
|
||||
outer: for (lines.items, 0..) |line, line_n| {
|
||||
var previous_element = line.items[0];
|
||||
for (line.items) |element| {
|
||||
// FIXME: not all conditions covered:
|
||||
// [ ] - Need to go only increasing or decreasing for each line
|
||||
// [ ] - Could not stay the same value
|
||||
// [x] - Difference between element is <= 3
|
||||
if ((@max(previous_element, element) - @min(previous_element, element)) > 3) {
|
||||
std.debug.print("Line #{d} is not safe ({d} - {d} > 3)\n", .{ line_n, previous_element, element });
|
||||
continue :outer;
|
||||
}
|
||||
previous_element = element;
|
||||
}
|
||||
count += 1;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
const example_input =
|
||||
\\7 6 4 2 1
|
||||
\\1 2 7 8 9
|
||||
\\9 7 6 2 1
|
||||
\\1 3 2 4 5
|
||||
\\8 6 4 4 1
|
||||
\\1 3 6 7 9
|
||||
;
|
||||
const test_allocator = std.testing.allocator;
|
||||
|
||||
test "day02_parsing" {
|
||||
const expected_parsed_result = [6][5]u32{
|
||||
[5]u32{ 7, 6, 4, 2, 1 },
|
||||
[5]u32{ 1, 2, 7, 8, 9 },
|
||||
[5]u32{ 9, 7, 6, 2, 1 },
|
||||
[5]u32{ 1, 3, 2, 4, 5 },
|
||||
[5]u32{ 8, 6, 4, 4, 1 },
|
||||
[5]u32{ 1, 3, 6, 7, 9 },
|
||||
};
|
||||
const parsed_array = try parse_input(example_input, test_allocator);
|
||||
defer utils.deinit_2d_list(parsed_array);
|
||||
|
||||
for (parsed_array.items, 0..) |outer, outer_index| {
|
||||
for (outer.items, 0..) |inner, inner_index| {
|
||||
try expect(inner == expected_parsed_result[outer_index][inner_index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test "day02_solving" {
|
||||
const parsed_array = try parse_input(example_input, test_allocator);
|
||||
defer utils.deinit_2d_list(parsed_array);
|
||||
|
||||
try expect(count_safe_reports(parsed_array) == 2);
|
||||
}
|
||||
|
|
@ -11,3 +11,8 @@ pub fn load_dataset(dataset_path: []const u8, allocator: std.mem.Allocator) ![]u
|
|||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
pub fn deinit_2d_list(outer_list: anytype) void {
|
||||
for (outer_list.items) |line_array| line_array.deinit();
|
||||
outer_list.deinit();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue