You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.9 KiB
Zig

const input = @embedFile("input1");
const std = @import("std");
pub fn main() !void{
var increments: usize = 0;
var t = std.mem.tokenize(u8, input, "\n");
var window = .{
try std.fmt.parseInt(usize, t.next().?, 10),
try std.fmt.parseInt(usize, t.next().?, 10),
try std.fmt.parseInt(usize, t.next().?, 10),
};
while(t.next())|line|{
var sum1: usize = window[0] + window[1] + window[2];
window[0] = window[1];
window[1] = window[2];
window[2] = try std.fmt.parseInt(usize, line, 10);
var sum2: usize = window[0] + window[1] + window[2];
if ( sum1 < sum2) increments += 1;
}
std.debug.print("result: {}", .{increments});
}
test "part1" {
var t = std.mem.tokenize(u8, input, "\n");
var last: usize = std.math.maxInt(usize);
var increments: usize = 0;
while(t.next()) |line| {
const n = try std.fmt.parseInt(usize, line, 10);
defer last = n;
if ( n > last) increments += 1;
}
try std.testing.expect(1583 == increments);
}
test "part2"{
var increments: usize = 0;
var t = std.mem.tokenize(u8, input, "\n");
// Read the first window
var window = .{
try std.fmt.parseInt(usize, t.next().?, 10),
try std.fmt.parseInt(usize, t.next().?, 10),
try std.fmt.parseInt(usize, t.next().?, 10),
};
while(t.next())|line|{
//with this window, calculate the current window sum
var sum1: usize = window[0] + window[1] + window[2];
//slide window one over
window[0] = window[1];
window[1] = window[2];
window[2] = try std.fmt.parseInt(usize, line, 10);
var sum2: usize = window[0] + window[1] + window[2];
if ( sum1 < sum2){
increments += 1;
}
}
std.testing.expect(1627 == increments);
}