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