Day3
This commit is contained in:
57
day3/zig/main.zig
Normal file
57
day3/zig/main.zig
Normal file
@@ -0,0 +1,57 @@
|
||||
const std = @import("std");
|
||||
|
||||
const Lv = struct { value: u8, position: usize };
|
||||
|
||||
pub fn main() !void {
|
||||
var stdin_buffer: [1024]u8 = undefined;
|
||||
var stdin_reader = std.fs.File.stdin().reader(&stdin_buffer);
|
||||
|
||||
const stdin = &stdin_reader.interface;
|
||||
|
||||
var task1: u32 = 0;
|
||||
var task2: u64 = 0;
|
||||
|
||||
while (try stdin.takeDelimiter('\n')) |line| {
|
||||
var highest_joltage: u32 = 0;
|
||||
for (line, 0..) |c1, i| {
|
||||
for (line[i + 1 ..]) |c2| {
|
||||
const c_arr = [_]u8{ c1, c2 };
|
||||
const num = try std.fmt.parseInt(u32, &c_arr, 10);
|
||||
|
||||
if (num > highest_joltage) {
|
||||
highest_joltage = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var chars: [12]u8 = undefined;
|
||||
var i: usize = 0;
|
||||
var current: usize = 0;
|
||||
|
||||
while (i < 12) : (i += 1) {
|
||||
const largest = find_largest(line[current .. line.len - 11 + i]);
|
||||
current += largest.position + 1;
|
||||
chars[i] = largest.value;
|
||||
}
|
||||
const num = try std.fmt.parseInt(u64, &chars, 10);
|
||||
|
||||
task2 += num;
|
||||
task1 += highest_joltage;
|
||||
}
|
||||
|
||||
std.debug.print("{d}\n", .{task1});
|
||||
std.debug.print("{d}\n", .{task2});
|
||||
}
|
||||
|
||||
fn find_largest(values: []const u8) Lv {
|
||||
var largest_value: u8 = 0;
|
||||
var largest_position: usize = 0;
|
||||
for (values, 0..) |value, i| {
|
||||
if (value > largest_value) {
|
||||
largest_position = i;
|
||||
largest_value = value;
|
||||
}
|
||||
}
|
||||
|
||||
return Lv{ .value = largest_value, .position = largest_position };
|
||||
}
|
||||
Reference in New Issue
Block a user