aboutsummaryrefslogtreecommitdiff
path: root/day1.zig
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-12-02 14:02:11 +0000
committerMartin Ashby <martin@ashbysoft.com>2023-12-02 14:02:11 +0000
commit9a5201154c28923c402d93e3ddee4858c10d8017 (patch)
tree1be2126e22afa231ebb06307a672128c1c70b294 /day1.zig
parentb580fd512a7659585a8a749d97cf4763b8343ccf (diff)
downloadaoc2023-9a5201154c28923c402d93e3ddee4858c10d8017.tar.gz
aoc2023-9a5201154c28923c402d93e3ddee4858c10d8017.tar.bz2
aoc2023-9a5201154c28923c402d93e3ddee4858c10d8017.tar.xz
aoc2023-9a5201154c28923c402d93e3ddee4858c10d8017.zip
Pull out some common parts for day2
Diffstat (limited to 'day1.zig')
-rw-r--r--day1.zig47
1 files changed, 5 insertions, 42 deletions
diff --git a/day1.zig b/day1.zig
index ed6d7e9..5258b4e 100644
--- a/day1.zig
+++ b/day1.zig
@@ -1,5 +1,6 @@
const std = @import("std");
const digits = "0123456789";
+const Setup = @import("common.zig").Setup;
const words = [_][]const u8{
"0",
@@ -47,51 +48,13 @@ const w2d = std.comptime_string_map.ComptimeStringMap(u8, .{
.{ "nine", 9 },
});
-const Input = struct {
- const Part = enum { pt1, pt2 };
- file: []const u8,
- part: Part,
-};
-
-pub fn getInput() !Input {
- var p = std.process.args();
- var in: Input = .{
- .file = "",
- .part = .pt1,
- };
- if (!p.skip()) return error.NoProgramName;
- if (p.next()) |pp| {
- if (std.mem.eql(u8, pp, "pt1")) {
- in.part = .pt1;
- } else if (std.mem.eql(u8, pp, "pt2")) {
- in.part = .pt2;
- } else {
- return error.BadPartSpecified;
- }
- } else {
- return error.NoPartSpecified;
- }
- if (p.next()) |pp| {
- in.file = pp;
- } else {
- return error.NoPartSpecified;
- }
- return in;
-}
-
pub fn main() !void {
- var gpa = std.heap.GeneralPurposeAllocator(.{}){};
- defer _ = gpa.deinit();
- const a = gpa.allocator();
- const in = try getInput();
- var f = try std.fs.cwd().openFile(in.file, .{});
- defer f.close();
- var c = try f.readToEndAlloc(a, std.math.maxInt(u32));
- defer a.free(c);
- var l = std.mem.splitScalar(u8, c, '\n');
+ var s = try Setup.get();
+ defer s.deinit();
+ var l = std.mem.splitScalar(u8, s.input, '\n');
var t: u64 = 0;
while (l.next()) |n| {
- switch (in.part) {
+ switch (s.part) {
.pt1 => {
const ix1 = std.mem.indexOfAny(u8, n, digits) orelse return error.NoDigits;
const ix2 = std.mem.lastIndexOfAny(u8, n, digits) orelse return error.NoDigits;