about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2022-12-02 16:10:15 +0900
committerNguyễn Gia Phong <mcsinyx@disroot.org>2022-12-02 16:10:15 +0900
commitada3a69b15ffd1f2c76044726cbf05a415c9d8d4 (patch)
tree022cf151c41f89568bf44dccc63a084276b171fd
parentff0bb53c15ddb51c94bcf9400c68f0858a48a6e2 (diff)
downloadcp-ada3a69b15ffd1f2c76044726cbf05a415c9d8d4.tar.gz
[aoc/2022] Finish day 2
-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)!;
+};