about summary refs log tree commit diff
path: root/aoc/2022/02/part-two.ha
blob: da7ab523215272470ead7b8b4e54e355e4199c47 (plain) (blame)
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
use bufio;
use fmt;
use io;
use os;

fn scanbyte(file: io::handle) u8 = {
	match (bufio::scanbyte(os::stdin)!) {
	case let byte: u8 =>
		return byte;
	case io::EOF =>
		fmt::fatal("Unexpected EOF");
	};
};

export fn main() void = {
	let score: u16 = 0;
	for (true) {
		const opponent = match (bufio::scanbyte(os::stdin)!) {
		case let byte: u8 =>
			yield byte;
		case io::EOF =>
			break;
		} - '?';
		scanbyte(os::stdin);
		const outcome = scanbyte(os::stdin) - 'X';
		scanbyte(os::stdin);
		score += (opponent + outcome) % 3 + 1 + outcome * 3;
	};
	fmt::println(score)!;
};