This commit is contained in:
parent
9035d0a8d4
commit
9b8ae4f350
@ -31,7 +31,9 @@ fn zeroed(seq: []const isize) bool {
|
|||||||
|
|
||||||
fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
|
fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
|
||||||
var it = util.splitLines(input);
|
var it = util.splitLines(input);
|
||||||
var sum: isize = 0;
|
var prev_sum: isize = 0;
|
||||||
|
var next_sum: isize = 0;
|
||||||
|
|
||||||
while (it.next()) |line| {
|
while (it.next()) |line| {
|
||||||
if (line.len == 0) continue;
|
if (line.len == 0) continue;
|
||||||
var seqs = std.ArrayList([]const isize).init(alloc);
|
var seqs = std.ArrayList([]const isize).init(alloc);
|
||||||
@ -41,17 +43,24 @@ fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
|
|||||||
while (!zeroed(seqs.items[seqs.items.len - 1])) {
|
while (!zeroed(seqs.items[seqs.items.len - 1])) {
|
||||||
try seqs.append(try drv(alloc, seqs.items[seqs.items.len - 1]));
|
try seqs.append(try drv(alloc, seqs.items[seqs.items.len - 1]));
|
||||||
}
|
}
|
||||||
// Solve next sequence
|
// Extrapolate
|
||||||
var predicted: isize = 0;
|
var next: isize = 0;
|
||||||
|
var prev: isize = 0;
|
||||||
|
for (seqs.items) |seq| {
|
||||||
|
next += seq[seq.len - 1];
|
||||||
|
}
|
||||||
|
for (0..seqs.items.len) |i| {
|
||||||
|
prev = seqs.items[seqs.items.len - i - 1][0] - prev;
|
||||||
|
}
|
||||||
for (seqs.items) |seq| {
|
for (seqs.items) |seq| {
|
||||||
predicted += seq[seq.len - 1];
|
|
||||||
// Clean up
|
// Clean up
|
||||||
alloc.free(seq);
|
alloc.free(seq);
|
||||||
}
|
}
|
||||||
sum += predicted;
|
prev_sum += prev;
|
||||||
|
next_sum += next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return .{ .a = sum, .b = 0 };
|
return .{ .a = next_sum, .b = prev_sum };
|
||||||
}
|
}
|
||||||
|
|
||||||
test "silver" {
|
test "silver" {
|
||||||
@ -66,8 +75,10 @@ test "silver" {
|
|||||||
|
|
||||||
test "gold" {
|
test "gold" {
|
||||||
const input =
|
const input =
|
||||||
\\
|
\\0 3 6 9 12 15
|
||||||
|
\\1 3 6 10 15 21
|
||||||
|
\\10 13 16 21 30 45
|
||||||
;
|
;
|
||||||
const sln = try solve(std.testing.allocator, input);
|
const sln = try solve(std.testing.allocator, input);
|
||||||
try std.testing.expectEqual(@as(isize, 0), sln.b);
|
try std.testing.expectEqual(@as(isize, 2), sln.b);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user