summaryrefslogtreecommitdiff
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
downloadaoc24-92524aa912db99becc1b3446d5b68b9cc089c721.tar.gz
Initial commit
-rw-r--r--1/1.ha49
-rw-r--r--1/in6
-rw-r--r--2/2.ha56
-rw-r--r--2/in6
-rw-r--r--3/3.ha31
-rw-r--r--3/in1
6 files changed, 149 insertions, 0 deletions
diff --git a/1/1.ha b/1/1.ha
new file mode 100644
index 0000000..a6a5d94
--- /dev/null
+++ b/1/1.ha
@@ -0,0 +1,49 @@
+use fmt;
+use bufio;
+use os;
+use shlex;
+use strings;
+use strconv;
+use sort;
+use sort::cmp;
+
+export fn main() void = {
+ const sc = bufio::newscanner(os::stdin);
+ defer bufio::finish(&sc);
+
+ let l1: []int = [];
+ let l2: []int = [];
+ defer free(l1);
+ defer free(l2);
+ for (const line => bufio::scan_line(&sc)!) {
+ const tok = strings::tokenize(line, " ");
+ let isfirst = true;
+ for (const t => strings::next_token(&tok)) {
+ if (t == "") {
+ continue;
+ };
+ const x = strconv::stoi(t)!;
+ if (isfirst) {
+ append(l1, x);
+ isfirst = false;
+ } else {
+ append(l2, x);
+ };
+ };
+ };
+ sort::sort(l1, size(int), &cmp::ints);
+ sort::sort(l2, size(int), &cmp::ints);
+ let sum = 0;
+ for (let i = 0z; i < len(l1); i += 1) {
+ const l = l1[i];
+ const j = l2[i];
+ const diff = if (l > j) {
+ yield (l, j, l - j);
+ } else {
+ yield (j, l, j - l);
+ };
+ fmt::printfln("{} - {} = {}", diff.0, diff.1, diff.2)!;
+ sum += diff.2;
+ };
+ fmt::println(sum)!;
+};
diff --git a/1/in b/1/in
new file mode 100644
index 0000000..f0aa1c6
--- /dev/null
+++ b/1/in
@@ -0,0 +1,6 @@
+3 4
+4 3
+2 5
+1 3
+3 9
+3 3
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)!;
+};
diff --git a/2/in b/2/in
new file mode 100644
index 0000000..b49c10d
--- /dev/null
+++ b/2/in
@@ -0,0 +1,6 @@
+7 6 4 2 1
+1 2 7 8 9
+9 7 6 2 1
+1 3 2 4 5
+8 6 4 4 1
+1 3 6 7 9
diff --git a/3/3.ha b/3/3.ha
new file mode 100644
index 0000000..f4d3ac5
--- /dev/null
+++ b/3/3.ha
@@ -0,0 +1,31 @@
+use fmt;
+use io;
+use os;
+use regex;
+use strings;
+use strconv;
+
+export fn main() void = {
+ const in = io::drain(os::stdin)!;
+ const sin = strings::trim(strings::fromutf8_unsafe(in));
+ const re = regex::compile(`mul\([0-9]+,[0-9]+\)`)!;
+ defer regex::finish(&re);
+
+ const results = regex::findall(&re, sin);
+ defer regex::result_freeall(results);
+ let sum = 0;
+ for (const result .. results) {
+ for (let r .. result) {
+ const idx1 = strings::index(r.content, ',');
+ const idx2 = strings::index(r.content, ')');
+ const arg1 = strings::sub(r.content, 4, idx1: size);
+ const arg2 = strings::sub(r.content, idx1: size + 1, idx2: size);
+
+ const a1 = strconv::stoi(arg1)!;
+ const a2 = strconv::stoi(arg2)!;
+ sum += a1 * a2;
+ fmt::printfln("{} * {} = {}", a1, a2, a1 * a2)!;
+ };
+ };
+ fmt::printfln("{}", sum)!;
+};
diff --git a/3/in b/3/in
new file mode 100644
index 0000000..f274bda
--- /dev/null
+++ b/3/in
@@ -0,0 +1 @@
+xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))