Finish day02, part 1
This commit is contained in:
parent
fd0a34c35e
commit
45b25120e0
2 changed files with 38 additions and 1 deletions
|
|
@ -2,6 +2,42 @@ const std = @import("std");
|
|||
|
||||
pub const title = "Day 02";
|
||||
|
||||
pub fn run(_: std.mem.Allocator) !void {
|
||||
pub fn run(allocator: std.mem.Allocator) !void {
|
||||
const input = @embedFile("./input/day02.txt");
|
||||
var ranges = std.mem.tokenizeScalar(u8, input, ',');
|
||||
|
||||
var accumulator: u64 = 0;
|
||||
|
||||
while (ranges.next()) |range| {
|
||||
var components = std.mem.tokenizeScalar(u8, range, '-');
|
||||
const start_s = std.mem.trim(u8, components.next() orelse continue, &std.ascii.whitespace);
|
||||
const end_s = std.mem.trim(u8, components.next() orelse continue, &std.ascii.whitespace);
|
||||
const start = try std.fmt.parseUnsigned(u64, start_s, 10);
|
||||
const end = try std.fmt.parseUnsigned(u64, end_s, 10);
|
||||
|
||||
std.debug.print("{d} to {d}\n", .{start, end});
|
||||
|
||||
outer: for (start..end+1) |id| {
|
||||
const id_s = try std.fmt.allocPrint(allocator, "{}", .{id});
|
||||
defer allocator.free(id_s);
|
||||
|
||||
if (id_s.len % 2 == 1) continue;
|
||||
|
||||
for (0..id_s.len / 2) |i| {
|
||||
if (id_s[i] != id_s[i + id_s.len / 2]) {
|
||||
continue :outer;
|
||||
}
|
||||
}
|
||||
accumulator += id;
|
||||
}
|
||||
}
|
||||
|
||||
var buffer: [64]u8 = undefined;
|
||||
var stdout_writer = std.fs.File.stdout().writer(&buffer);
|
||||
const stdout = &stdout_writer.interface;
|
||||
|
||||
try stdout.print("{d}\n", .{accumulator});
|
||||
|
||||
try stdout.flush();
|
||||
}
|
||||
|
||||
|
|
|
|||
1
src/days/input/day02.txt
Executable file
1
src/days/input/day02.txt
Executable file
|
|
@ -0,0 +1 @@
|
|||
52500467-52574194,655624494-655688785,551225-576932,8418349387-8418411293,678-1464,33-79,74691-118637,8787869169-8787890635,9898977468-9899009083,548472423-548598890,337245835-337375280,482823-543075,926266-991539,1642682920-1642753675,3834997-3940764,1519-2653,39697698-39890329,3-21,3251796-3429874,3467-9298,26220798-26290827,80-124,200638-280634,666386-710754,21329-64315,250-528,9202893-9264498,819775-903385,292490-356024,22-32,2663033-2791382,133-239,56514707-56704320,432810-458773,4949427889-4949576808
|
||||
Loading…
Add table
Add a link
Reference in a new issue