From ada3a69b15ffd1f2c76044726cbf05a415c9d8d4 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Fri, 2 Dec 2022 16:10:15 +0900 Subject: [aoc/2022] Finish day 2 --- aoc/2022/02/part-one.ha | 30 ++++++++++++++++++++++++++++++ aoc/2022/02/part-two.ha | 30 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 aoc/2022/02/part-one.ha create mode 100644 aoc/2022/02/part-two.ha 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)!; +}; -- cgit 1.4.1