about summary refs log tree commit diff
path: root/aoc/2022/02/part-one.ha
diff options
context:
space:
mode:
Diffstat (limited to 'aoc/2022/02/part-one.ha')
-rw-r--r--aoc/2022/02/part-one.ha30
1 files changed, 30 insertions, 0 deletions
diff --git a/aoc/2022/02/part-one.ha b/aoc/2022/02/part-one.ha
new file mode 100644
index 0000000..7a49de0
--- /dev/null
+++ b/aoc/2022/02/part-one.ha
@@ -0,0 +1,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 them = 'D' - (match (bufio::scanbyte(os::stdin)!) {
+		case let byte: u8 =>
+			yield byte;
+		case io::EOF =>
+			break;
+		});
+		scanbyte(os::stdin);
+		const us = scanbyte(os::stdin) - 'W';
+		scanbyte(os::stdin);
+		score += us + (us + them) % 3 * 3;
+	};
+	fmt::println(score)!;
+};