summaryrefslogtreecommitdiff
path: root/day3/part1/main.ha
blob: f7384ce085c9b9c2ffe7710810ce7f80564e9ddc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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)!;
};