summaryrefslogtreecommitdiff
path: root/one
diff options
context:
space:
mode:
authorJulian Hurst <julian.hurst@digdash.com>2024-12-03 18:47:51 +0100
committerJulian Hurst <julian.hurst@digdash.com>2024-12-03 18:47:57 +0100
commit190f522242b2e91eb3213e828a26886aa7847f3a (patch)
tree04122a22aa1e92d6f3efe21436d2e75650c1c4c1 /one
parent92524aa912db99becc1b3446d5b68b9cc089c721 (diff)
downloadaoc24-190f522242b2e91eb3213e828a26886aa7847f3a.tar.gz
Refactor and add tests
Folders need to have 'sensible' names for hare test to detect them.
Diffstat (limited to 'one')
-rw-r--r--one/+test.ha8
-rw-r--r--one/1.ha55
-rw-r--r--one/in6
3 files changed, 69 insertions, 0 deletions
diff --git a/one/+test.ha b/one/+test.ha
new file mode 100644
index 0000000..7041261
--- /dev/null
+++ b/one/+test.ha
@@ -0,0 +1,8 @@
+use os;
+use io;
+
+@test fn testone() void = {
+ const f = os::open("one/in")!;
+ defer io::close(f)!;
+ assert(do(f) == 11);
+};
diff --git a/one/1.ha b/one/1.ha
new file mode 100644
index 0000000..72a1e1a
--- /dev/null
+++ b/one/1.ha
@@ -0,0 +1,55 @@
+use fmt;
+use bufio;
+use os;
+use shlex;
+use strings;
+use strconv;
+use sort;
+use sort::cmp;
+use io;
+
+export fn main() void = {
+ do(os::stdin);
+};
+
+fn do(h: io::handle) int = {
+ const sc = bufio::newscanner(h);
+ 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)!;
+ return sum;
+};
diff --git a/one/in b/one/in
new file mode 100644
index 0000000..f0aa1c6
--- /dev/null
+++ b/one/in
@@ -0,0 +1,6 @@
+3 4
+4 3
+2 5
+1 3
+3 9
+3 3