From 0c01e8805063e1dccdb4ce467b8f4fde11f02f86 Mon Sep 17 00:00:00 2001 From: Polesznyák Márk Date: Mon, 1 Dec 2025 10:49:09 +0100 Subject: feat: day1 part1 complete --- day1/part1/main.ha | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 day1/part1/main.ha (limited to 'day1/part1/main.ha') diff --git a/day1/part1/main.ha b/day1/part1/main.ha new file mode 100644 index 0000000..4f9b2b2 --- /dev/null +++ b/day1/part1/main.ha @@ -0,0 +1,43 @@ +use os; +use io; +use fmt; +use strings; +use strconv; + +export fn main() void = { + const handle = os::open("input.txt")!; + defer io::close(handle)!; + + let buf = io::drain(handle) as []u8; + defer free(buf); + + let input = strings::fromutf8_unsafe(buf[..]); + let lines = strings::split(input, "\n")!; + + let dial: i16 = 50; + let zeroes_seen: u16 = 0; + + for (let line .. lines[..]) { + if (strings::hasprefix(line, "L")) { + dial -= strconv::stoi16(strings::ltrim(line, 'L'))!; + } else if (strings::hasprefix(line, "R")) { + dial += strconv::stoi16(strings::ltrim(line, 'R'))!; + } else { + continue; + }; + + for (dial < 0 || dial > 99) { + if (dial < 0) { + dial += 100; + } else if (dial > 99) { + dial -= 100; + }; + }; + + if (dial == 0) { + zeroes_seen += 1; + }; + }; + + fmt::printfln("Password: {}", zeroes_seen)!; +}; -- cgit v1.2.3