Day1 Zig
This commit is contained in:
4352
day1/zig/input.txt
Normal file
4352
day1/zig/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
49
day1/zig/main.zig
Normal file
49
day1/zig/main.zig
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const Instruction = enum {
|
||||||
|
L,
|
||||||
|
R,
|
||||||
|
};
|
||||||
|
|
||||||
|
const MAGIC_NUMBER = 100;
|
||||||
|
|
||||||
|
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 current: i32 = 50;
|
||||||
|
var task1: u32 = 0;
|
||||||
|
var task2: u32 = 0;
|
||||||
|
|
||||||
|
while (try stdin.takeDelimiter('\n')) |line| {
|
||||||
|
var num = try std.fmt.parseInt(i32, line[1..], 10);
|
||||||
|
switch (line[0]) {
|
||||||
|
'L' => {
|
||||||
|
while (num > 0) : (num -= 1) {
|
||||||
|
current = @mod(current - 1, MAGIC_NUMBER);
|
||||||
|
if (current == 0) {
|
||||||
|
task2 += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'R' => {
|
||||||
|
while (num > 0) : (num -= 1) {
|
||||||
|
current = @mod(current + 1, MAGIC_NUMBER);
|
||||||
|
if (current == 0) {
|
||||||
|
task2 += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
else => unreachable,
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current == 0) {
|
||||||
|
task1 += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std.debug.print("{d}\n", .{task1});
|
||||||
|
std.debug.print("{d}\n", .{task2});
|
||||||
|
}
|
||||||
10
day1/zig/sample.txt
Normal file
10
day1/zig/sample.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
L68
|
||||||
|
L30
|
||||||
|
R48
|
||||||
|
L5
|
||||||
|
R60
|
||||||
|
L55
|
||||||
|
L1
|
||||||
|
L99
|
||||||
|
R14
|
||||||
|
L82
|
||||||
Reference in New Issue
Block a user