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 | |
| download | aoc24-92524aa912db99becc1b3446d5b68b9cc089c721.tar.gz | |
Initial commit
| -rw-r--r-- | 1/1.ha | 49 | ||||
| -rw-r--r-- | 1/in | 6 | ||||
| -rw-r--r-- | 2/2.ha | 56 | ||||
| -rw-r--r-- | 2/in | 6 | ||||
| -rw-r--r-- | 3/3.ha | 31 | ||||
| -rw-r--r-- | 3/in | 1 |
6 files changed, 149 insertions, 0 deletions
@@ -0,0 +1,49 @@ +use fmt; +use bufio; +use os; +use shlex; +use strings; +use strconv; +use sort; +use sort::cmp; + +export fn main() void = { + const sc = bufio::newscanner(os::stdin); + 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)!; +}; @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 @@ -0,0 +1,56 @@ +use fmt; +use bufio; +use os; +use strings; +use strconv; + +export fn main() void = { + const sc = bufio::newscanner(os::stdin); + defer bufio::finish(&sc); + + let safereports = 0; + for (const line => bufio::scan_line(&sc)!) { + const spl = strings::split(line, " "); + defer free(spl); + let safe = true; + let inc: (bool | void) = void; + for (let i = 1z; i < len(spl); i += 1) { + const s1 = strconv::stoi(spl[i-1])!; + const s2 = strconv::stoi(spl[i])!; + const diff = if (s1 < s2) { + match (inc) { + case void => + inc = true; + case let inc: bool => + if (!inc) { + safe = false; + break; + }; + }; + yield s2 - s1; + } else { + match (inc) { + case void => + inc = false; + case let inc: bool => + if (inc) { + safe = false; + break; + }; + }; + yield s1 - s2; + }; + if (diff <= 0 || diff > 3) { + safe = false; + break; + }; + }; + if (safe) { + fmt::printfln("{}: Safe", line)!; + safereports += 1; + } else { + fmt::printfln("{}: Unsafe", line)!; + }; + }; + fmt::printfln("{} reports are safe", safereports)!; +}; @@ -0,0 +1,6 @@ +7 6 4 2 1 +1 2 7 8 9 +9 7 6 2 1 +1 3 2 4 5 +8 6 4 4 1 +1 3 6 7 9 @@ -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)!; +}; @@ -0,0 +1 @@ +xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) |
