replace array with an actual struct now that we have them

This commit is contained in:
2026-03-11 20:44:55 +01:00
parent d78a77b075
commit 3fcd82cc04
22 changed files with 220 additions and 224 deletions

View File

@@ -9,23 +9,24 @@ func concat[a: i64, b: i64] : i64
mem.free(ab_str)
return out
func solve[ops: array, e: array] : i64
let n: i64 = array.size(array.nth(e, 1)) - 1
let indices: array = []
func solve[ops: Array, e: Array] : i64
let e_1: Array = array.nth(e, 1)
let n: i64 = e_1->size - 1
let indices: Array = []
for i in 0..n
array.push(indices, 0)
while true
let res: i64 = array.nth(array.nth(e, 1), 0)
let res: i64 = array.nth(e_1, 0)
for i in 0..n
let op: str = array.nth(ops, array.nth(indices, i))
if str.equal(op, "add")
res = res + array.nth(array.nth(e, 1), i + 1)
res = res + array.nth(e_1, i + 1)
else if str.equal(op, "mul")
res = res * array.nth(array.nth(e, 1), i + 1)
res = res * array.nth(e_1, i + 1)
else if str.equal(op, "concat")
res = concat(res, array.nth(array.nth(e, 1), i + 1))
res = concat(res, array.nth(e_1, i + 1))
if res == array.nth(e, 0)
return res
@@ -34,7 +35,7 @@ func solve[ops: array, e: array] : i64
while i >= 0
array.set(indices, i, array.nth(indices, i) + 1)
if array.nth(indices, i) < array.size(ops)
if array.nth(indices, i) < ops->size
done = false
break
array.set(indices, i, 0)
@@ -43,32 +44,32 @@ func solve[ops: array, e: array] : i64
if done
return 0
func part1[equations: array] : void
func part1[equations: Array] : void
let out = 0
for i in 0..array.size(equations)
for i in 0..equations->size
out = out + solve(["add", "mul"], array.nth(equations, i))
io.println_i64(out)
func part2[equations: array] : void
func part2[equations: Array] : void
let out = 0
for i in 0..array.size(equations)
for i in 0..equations->size
out = out + solve(["add", "mul", "concat"], array.nth(equations, i))
io.println_i64(out)
func main[] : i64
let lines: array = io.read_file("input.txt") |> str.split("\n")
let equations: array = []
let lines: Array = io.read_file("input.txt") |> str.split("\n")
let equations: Array = []
for i in 0..array.size(lines)
for i in 0..lines->size
let line: str = array.nth(lines, i)
let parts: array = str.split(line, ": ")
let parts: Array = str.split(line, ": ")
let xs: array = str.split(array.nth(parts, 1), " ")
for j in 0..array.size(xs)
let xs: Array = str.split(array.nth(parts, 1), " ")
for j in 0..xs->size
array.set(xs, j, str.parse_i64(array.nth(xs, j)))
array.push(equations, [str.parse_i64(array.nth(parts, 0)), xs])