summaryrefslogtreecommitdiff
path: root/1/1.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 /1/1.ha
downloadaoc24-92524aa912db99becc1b3446d5b68b9cc089c721.tar.gz
Initial commit
Diffstat (limited to '1/1.ha')
-rw-r--r--1/1.ha49
1 files changed, 49 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)!;
+};