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. --- 1/1.ha | 49 ---------------------------------------------- 1/in | 6 ------ 2/2.ha | 56 ---------------------------------------------------- 2/in | 6 ------ 3/3.ha | 31 ----------------------------- 3/in | 1 - Makefile | 27 +++++++++++++++++++++++++ README | 1 + one/+test.ha | 8 ++++++++ one/1.ha | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ one/in | 6 ++++++ three/+test.ha | 8 ++++++++ three/3.ha | 36 ++++++++++++++++++++++++++++++++++ three/in | 1 + two/+test.ha | 8 ++++++++ two/2.ha | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ two/in | 6 ++++++ 17 files changed, 218 insertions(+), 149 deletions(-) delete mode 100644 1/1.ha delete mode 100644 1/in delete mode 100644 2/2.ha delete mode 100644 2/in delete mode 100644 3/3.ha delete mode 100644 3/in create mode 100644 Makefile create mode 100644 README create mode 100644 one/+test.ha create mode 100644 one/1.ha create mode 100644 one/in create mode 100644 three/+test.ha create mode 100644 three/3.ha create mode 100644 three/in create mode 100644 two/+test.ha create mode 100644 two/2.ha create mode 100644 two/in diff --git a/1/1.ha b/1/1.ha deleted file mode 100644 index a6a5d94..0000000 --- a/1/1.ha +++ /dev/null @@ -1,49 +0,0 @@ -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)!; -}; diff --git a/1/in b/1/in deleted file mode 100644 index f0aa1c6..0000000 --- a/1/in +++ /dev/null @@ -1,6 +0,0 @@ -3 4 -4 3 -2 5 -1 3 -3 9 -3 3 diff --git a/2/2.ha b/2/2.ha deleted file mode 100644 index 83ba3b8..0000000 --- a/2/2.ha +++ /dev/null @@ -1,56 +0,0 @@ -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)!; -}; diff --git a/2/in b/2/in deleted file mode 100644 index b49c10d..0000000 --- a/2/in +++ /dev/null @@ -1,6 +0,0 @@ -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 diff --git a/3/3.ha b/3/3.ha deleted file mode 100644 index f4d3ac5..0000000 --- a/3/3.ha +++ /dev/null @@ -1,31 +0,0 @@ -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)!; -}; diff --git a/3/in b/3/in deleted file mode 100644 index f274bda..0000000 --- a/3/in +++ /dev/null @@ -1 +0,0 @@ -xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..539e5f0 --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +.POSIX: +.SUFFIXES: +HARE=hare +HAREFLAGS= + +DESTDIR= +PREFIX=/usr/local +BINDIR=$(PREFIX)/bin + +all: example-cmd + +example-cmd: + $(HARE) build $(HAREFLAGS) -o $@ cmd/$@/ + +check: + $(HARE) test $(HAREFLAGS) + +clean: + rm -f example-cmd + +install: + install -Dm755 example-cmd $(DESTDIR)$(BINDIR)/example-cmd + +uninstall: + rm -f $(DESTDIR)$(BINDIR)/example-cmd + +.PHONY: all check clean install uninstall diff --git a/README b/README new file mode 100644 index 0000000..99070fa --- /dev/null +++ b/README @@ -0,0 +1 @@ +Advent of Code 2024 in Hare diff --git a/one/+test.ha b/one/+test.ha new file mode 100644 index 0000000..7041261 --- /dev/null +++ b/one/+test.ha @@ -0,0 +1,8 @@ +use os; +use io; + +@test fn testone() void = { + const f = os::open("one/in")!; + defer io::close(f)!; + assert(do(f) == 11); +}; diff --git a/one/1.ha b/one/1.ha new file mode 100644 index 0000000..72a1e1a --- /dev/null +++ b/one/1.ha @@ -0,0 +1,55 @@ +use fmt; +use bufio; +use os; +use shlex; +use strings; +use strconv; +use sort; +use sort::cmp; +use io; + +export fn main() void = { + do(os::stdin); +}; + +fn do(h: io::handle) int = { + const sc = bufio::newscanner(h); + 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)!; + return sum; +}; diff --git a/one/in b/one/in new file mode 100644 index 0000000..f0aa1c6 --- /dev/null +++ b/one/in @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 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)) diff --git a/two/+test.ha b/two/+test.ha new file mode 100644 index 0000000..8f61645 --- /dev/null +++ b/two/+test.ha @@ -0,0 +1,8 @@ +use os; +use io; + +@test fn testtwo() void = { + const f = os::open("two/in")!; + defer io::close(f)!; + assert(do(f) == 2); +}; diff --git a/two/2.ha b/two/2.ha new file mode 100644 index 0000000..aea9ea8 --- /dev/null +++ b/two/2.ha @@ -0,0 +1,62 @@ +use fmt; +use bufio; +use os; +use strings; +use strconv; +use io; + +export fn main() void = { + do(os::stdin); +}; + +fn do(h: io::handle) int = { + const sc = bufio::newscanner(h); + 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)!; + return safereports; +}; diff --git a/two/in b/two/in new file mode 100644 index 0000000..b49c10d --- /dev/null +++ b/two/in @@ -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 -- cgit v1.2.3