uniform function call syntax

This commit is contained in:
2026-06-06 22:29:25 +02:00
parent 3a53cd4f32
commit 3098d0abf9
34 changed files with 488 additions and 398 deletions

View File

@@ -1,38 +1,38 @@
func concat[a: i64, b: i64] : i64
ab_str := _stackalloc(50) as str
io.snprintf(ab_str, 50, "%d%d", a, b)
return str.parse_i64(ab_str)
ab_str->format_into(50, "%d%d", a, b)
return ab_str->parse_i64()
func solve[ops: Array, e: Array] : i64
e_1 : Array = array.nth(e, 1)
e_1 : Array = e->nth(1)
n := e_1->size - 1
indices := []
for i in 0..n
array.push(indices, 0)
indices->push(0)
while true
res : i64 = array.nth(e_1, 0)
res : i64 = e_1->nth(0)
for i in 0..n
op : str = array.nth(ops, array.nth(indices, i))
op : str = ops->nth(indices->nth(i))
if str.equal(op, "add")
res += array.nth(e_1, i + 1)
else if str.equal(op, "mul")
res = res * array.nth(e_1, i + 1)
else if str.equal(op, "concat")
res = concat(res, array.nth(e_1, i + 1))
if res == array.nth(e, 0)
if op->equal("add")
res += e_1->nth(i + 1)
else if op->equal("mul")
res = res * e_1->nth(i + 1)
else if op->equal("concat")
res = concat(res, e_1->nth(i + 1))
if res == e->nth(0)
return res
done := true
i := n - 1
while i >= 0
array.set(indices, i, array.nth(indices, i) + 1)
if array.nth(indices, i) < ops->size
indices->set(i, indices->nth(i) + 1)
if indices->nth(i) < ops->size
done = false
break
array.set(indices, i, 0)
indices->set(i, 0)
i -= 1
if done
@@ -42,7 +42,7 @@ func part1[equations: Array] : void
out := 0
for i in 0..equations->size
out += solve(["add", "mul"], array.nth(equations, i))
out += solve(["add", "mul"], equations->nth(i))
io.println_i64(out)
@@ -50,7 +50,7 @@ func part2[equations: Array] : void
out := 0
for i in 0..equations->size
out += solve(["add", "mul", "concat"], array.nth(equations, i))
out += solve(["add", "mul", "concat"], equations->nth(i))
io.println_i64(out)
@@ -59,18 +59,18 @@ func main[] : i64
if !ok
panic("failed to open input.txt")
lines := str.split(input, "\n")
lines := input->split("\n")
equations := []
for i in 0..lines->size
line : str = array.nth(lines, i)
parts := str.split(line, ": ")
line : str = lines->nth(i)
parts := line->split(": ")
xs := str.split(array.nth(parts, 1), " ")
xs := (parts->nth(1) as str)->split(" ")
for j in 0..xs->size
array.set(xs, j, str.parse_i64(array.nth(xs, j)))
xs->set(j, (xs->nth(j) as str)->parse_i64())
array.push(equations, [str.parse_i64(array.nth(parts, 0)), xs])
equations->push([(parts->nth(0) as str)->parse_i64(), xs])
part1(equations)
part2(equations)