summaryrefslogtreecommitdiff
path: root/day1/part1/main.ha
blob: 4f9b2b23202732cb0a2e8fe0bc92e0615e4b73cf (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
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)!;
};