Factor-out parsing the input file

This commit is contained in:
Jack Jackson 2024-12-01 22:49:30 -08:00
parent d751cada30
commit 09ff76600a

View File

@ -2,9 +2,44 @@ const std = @import("std");
const print = std.debug.print;
const util = @import("util.zig");
pub fn main() !u32 {
var arr1: [1000]u32 = undefined;
var arr2: [1000]u32 = undefined;
pub fn part_one() !u32 {
const arrays = try parseInputFileToArrays();
std.debug.print("{any}", .{arrays.first});
const arr1 = arrays.first;
const arr2 = arrays.second;
// Everything below this is the actual logic
const stdout = std.io.getStdOut().writer();
std.mem.sort(u32, arr1, {}, comptime std.sort.asc(u32));
std.mem.sort(u32, arr2, {}, comptime std.sort.asc(u32));
var sum: u32 = 0;
for (0..arr1.len) |index| {
const val1 = arr1[index];
const val2 = arr2[index];
if (val1 > val2) {
sum += (val1 -% val2);
} else {
sum += (val2 -% val1);
}
}
try stdout.print("{}", .{sum});
// Only used for testing - but later we'll have this return to an actual higher-level `main` function and have
// _that_ do printing.
return sum;
}
pub fn part_two() !u32 {
return 0;
}
fn parseInputFileToArrays() !struct { first: []u32, second: []u32 } {
// Wow this _sucks_. But there's no way (that I know of?) to find the number of lines in a file without parsing it,
// and we can't parse it without having created the array already, so... :shrug
const isTestCase = true;
const lengthOfArray: usize = if (isTestCase) 6 else 1000;
var arr1: [lengthOfArray]u32 = undefined;
var arr2: [lengthOfArray]u32 = undefined;
// Reading the input...
// Making use of example here: https://cookbook.ziglang.cc/01-01-read-file-line-by-line.html
@ -12,7 +47,7 @@ pub fn main() !u32 {
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const path = try util.getInputFile("01", true);
const path = try util.getInputFile("01", isTestCase);
std.debug.print("Path is {s}\n", .{path});
const file = try std.fs.cwd().openFile(path, .{});
defer file.close();
@ -47,26 +82,7 @@ pub fn main() !u32 {
},
else => return err, // Propagate error
}
// Everything below this is the actual logic
const stdout = std.io.getStdOut().writer();
std.mem.sort(u32, &arr1, {}, comptime std.sort.asc(u32));
std.mem.sort(u32, &arr2, {}, comptime std.sort.asc(u32));
var sum: u32 = 0;
for (0..arr1.len) |index| {
const val1 = arr1[index];
const val2 = arr2[index];
if (val1 > val2) {
sum += (val1 -% val2);
} else {
sum += (val2 -% val1);
}
}
try stdout.print("{}", .{sum});
// Only used for testing - but later we'll have this return to an actual higher-level `main` function and have
// _that_ do printing.
return sum;
return .{ .first = &arr1, .second = &arr2 };
}
fn parseLineToNumbers(line: []u8) struct { first: u32, second: u32 } {
@ -98,9 +114,10 @@ fn parseLineToNumbers(line: []u8) struct { first: u32, second: u32 } {
const expect = std.testing.expect;
test {
const filePath = try util.getInputFile("01", true);
const stdout = std.io.getStdOut().writer();
try stdout.print("{s}\n\n", .{filePath});
try expect(try main() == 11);
test "part one" {
try expect(try part_one() == 11);
}
// test "part two" {
// try expect(try part_two() == 31);
// }