summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPolesznyák Márk <contact@pml68.dev>2025-12-04 06:10:22 +0100
committerPolesznyák Márk <contact@pml68.dev>2025-12-04 06:10:22 +0100
commit5a5e11835c5206bf480c128e2a14f365ee5ff09e (patch)
tree1caff7e585783334ebb1b521fad84405809188e7
parentfeat: day2 part2 complete (diff)
downloadaoc-5a5e11835c5206bf480c128e2a14f365ee5ff09e.tar.gz
feat: day3 part1 complete
-rw-r--r--day3/part1/main.ha47
1 files changed, 47 insertions, 0 deletions
diff --git a/day3/part1/main.ha b/day3/part1/main.ha
new file mode 100644
index 0000000..f7384ce
--- /dev/null
+++ b/day3/part1/main.ha
@@ -0,0 +1,47 @@
+use fmt;
+use io;
+use os;
+use strconv::{stou8};
+use strings::{fromutf8_unsafe, split, sub};
+
+fn joltage(bank: str) u8 = {
+ let first: u8 = 1;
+ let second: u8 = 1;
+
+ let first_index: size = 0;
+
+ for (let i: size = 0; i < len(bank); i += 1) {
+ let val = stou8(sub(bank, i, i + 1))!;
+
+ if (val > first && i < len(bank) - 1) {
+ first = val;
+ first_index = i;
+ };
+ };
+
+ for (let i: size = first_index + 1; i < len(bank); i += 1) {
+ let val = stou8(sub(bank, i, i + 1))!;
+
+ if (val > second)
+ second = val;
+ };
+
+ return first * 10 + second;
+};
+
+export fn main() void = {
+ let handle = os::open("input.txt")!;
+ defer io::close(handle)!;
+
+ let buf = io::drain(handle)!;
+ defer free(buf);
+
+ let lines = split(fromutf8_unsafe(buf), "\n")!;
+ let output = 0;
+
+ for (let line .. lines)
+ if (line != "")
+ output += joltage(line): int;
+
+ fmt::printfln("Answer: {}", output)!;
+};