attemps from before

This commit is contained in:
2022-11-25 22:36:13 +01:00
commit 680da5cc2c
17 changed files with 5703 additions and 0 deletions
+76
View File
@@ -0,0 +1,76 @@
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);
}
+2000
View File
File diff suppressed because it is too large Load Diff
+8
View File
@@ -0,0 +1,8 @@
607
618
618
617
647
716
769
792