From 190f522242b2e91eb3213e828a26886aa7847f3a Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Tue, 3 Dec 2024 18:47:51 +0100 Subject: Refactor and add tests Folders need to have 'sensible' names for hare test to detect them. --- three/3.ha | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 three/3.ha (limited to 'three/3.ha') 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; +}; -- cgit v1.2.3