diff options
| author | Julian Hurst <julian.hurst@digdash.com> | 2024-12-03 16:42:45 +0100 |
|---|---|---|
| committer | Julian Hurst <julian.hurst@digdash.com> | 2024-12-03 16:42:45 +0100 |
| commit | 92524aa912db99becc1b3446d5b68b9cc089c721 (patch) | |
| tree | c8f1c994b6b2217f1167a29382e41852d624684e /3/3.ha | |
| download | aoc24-92524aa912db99becc1b3446d5b68b9cc089c721.tar.gz | |
Initial commit
Diffstat (limited to '3/3.ha')
| -rw-r--r-- | 3/3.ha | 31 |
1 files changed, 31 insertions, 0 deletions
@@ -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)!; +}; |
