summaryrefslogtreecommitdiff
path: root/three
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 /three
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 'three')
-rw-r--r--three/+test.ha8
-rw-r--r--three/3.ha36
-rw-r--r--three/in1
3 files changed, 45 insertions, 0 deletions
diff --git a/three/+test.ha b/three/+test.ha
new file mode 100644
index 0000000..5ec9745
--- /dev/null
+++ b/three/+test.ha
@@ -0,0 +1,8 @@
+use os;
+use io;
+
+@test fn testthree() void = {
+ const f = os::open("three/in")!;
+ defer io::close(f)!;
+ assert(do(f) == 161);
+};
diff --git a/three/3.ha b/three/3.ha
new file mode 100644
index 0000000..9668423
--- /dev/null
+++ b/three/3.ha
@@ -0,0 +1,36 @@
+use fmt;
+use io;
+use os;
+use regex;
+use strings;
+use strconv;
+
+export fn main() void = {
+ do(os::stdin);
+};
+
+fn do(h: io::handle) int = {
+ const in = io::drain(h)!;
+ 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)!;
+ return sum;
+};
diff --git a/three/in b/three/in
new file mode 100644
index 0000000..f274bda
--- /dev/null
+++ b/three/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))