summaryrefslogtreecommitdiff
path: root/2/2.ha
diff options
context:
space:
mode:
authorJulian Hurst <julian.hurst@digdash.com>2024-12-03 16:42:45 +0100
committerJulian Hurst <julian.hurst@digdash.com>2024-12-03 16:42:45 +0100
commit92524aa912db99becc1b3446d5b68b9cc089c721 (patch)
treec8f1c994b6b2217f1167a29382e41852d624684e /2/2.ha
downloadaoc24-92524aa912db99becc1b3446d5b68b9cc089c721.tar.gz
Initial commit
Diffstat (limited to '2/2.ha')
-rw-r--r--2/2.ha56
1 files changed, 56 insertions, 0 deletions
diff --git a/2/2.ha b/2/2.ha
new file mode 100644
index 0000000..83ba3b8
--- /dev/null
+++ b/2/2.ha
@@ -0,0 +1,56 @@
+use fmt;
+use bufio;
+use os;
+use strings;
+use strconv;
+
+export fn main() void = {
+ const sc = bufio::newscanner(os::stdin);
+ defer bufio::finish(&sc);
+
+ let safereports = 0;
+ for (const line => bufio::scan_line(&sc)!) {
+ const spl = strings::split(line, " ");
+ defer free(spl);
+ let safe = true;
+ let inc: (bool | void) = void;
+ for (let i = 1z; i < len(spl); i += 1) {
+ const s1 = strconv::stoi(spl[i-1])!;
+ const s2 = strconv::stoi(spl[i])!;
+ const diff = if (s1 < s2) {
+ match (inc) {
+ case void =>
+ inc = true;
+ case let inc: bool =>
+ if (!inc) {
+ safe = false;
+ break;
+ };
+ };
+ yield s2 - s1;
+ } else {
+ match (inc) {
+ case void =>
+ inc = false;
+ case let inc: bool =>
+ if (inc) {
+ safe = false;
+ break;
+ };
+ };
+ yield s1 - s2;
+ };
+ if (diff <= 0 || diff > 3) {
+ safe = false;
+ break;
+ };
+ };
+ if (safe) {
+ fmt::printfln("{}: Safe", line)!;
+ safereports += 1;
+ } else {
+ fmt::printfln("{}: Unsafe", line)!;
+ };
+ };
+ fmt::printfln("{} reports are safe", safereports)!;
+};