about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--aoc/2022/02/part-one.ha30
-rw-r--r--aoc/2022/02/part-two.ha30
2 files changed, 60 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)!;
+};
diff --git a/aoc/2022/02/part-two.ha b/aoc/2022/02/part-two.ha
new file mode 100644
index 0000000..da7ab52
--- /dev/null
+++ b/aoc/2022/02/part-two.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 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)!;
+};