Untitled
unknown
plain_text
24 days ago
2.1 kB
2
Indexable
Never
const std = @import("std"); const fname = "input.txt"; const Keypad = struct { row: u8 = 2, column: u8 = 2, pub fn move(self: *Keypad, dir: u8) void { switch (dir) { 'U' => if (self.row > 1) { self.row -= 1; }, 'D' => if (self.row < 3) { self.row += 1; }, 'L' => if (self.column > 1) { self.column -= 1; }, 'R' => if (self.column < 3) { self.column += 1; }, else => unreachable } } pub fn getVal(self: *Keypad) u8 { switch (self.row) { 1 => switch (self.column) { 1 => return 1, 2 => return 2, 3 => return 3, else => unreachable }, 2 => switch (self.column) { 1 => return 4, 2 => return 5, 3 => return 6, else => unreachable }, 3 => switch (self.column) { 1 => return 7, 2 => return 8, 3 => return 9, else => unreachable }, else => unreachable } } }; pub fn main() !void { const file = try std.fs.cwd().openFile(fname, .{}); defer file.close(); const reader = file.reader(); var line: [100]u8 = undefined; var keypad: Keypad = undefined; while (try reader.readUntilDelimiterOrEof(&line, '\n')) |currLine| { var atEOL: bool = false; for (currLine) |c| { if (isValidMove(c)) { keypad.move(c); } else { if (!atEOL) { std.debug.print("Key: {i}\n", keypad.getVal()); } atEOL = true; } } } } pub fn isValidMove(move: u8) bool { switch(move) { 'U' => return true, 'D' => return true, 'L' => return true, 'R' => return true, else => return false } }
Leave a Comment