summaryrefslogtreecommitdiff
path: root/day3/part1/main.ha
diff options
context:
space:
mode:
Diffstat (limited to 'day3/part1/main.ha')
-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)!;
+};