77 lines
1.8 KiB
Plaintext
77 lines
1.8 KiB
Plaintext
func concat[a: i64, b: i64] : i64
|
|
ab_str := _stackalloc(50) as 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 = e->nth(1)
|
|
n := e_1->size - 1
|
|
indices := []
|
|
for i in 0..n
|
|
indices->push(0)
|
|
|
|
while true
|
|
res : i64 = e_1->nth(0)
|
|
for i in 0..n
|
|
op : str = ops->nth(indices->nth(i))
|
|
|
|
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
|
|
indices->set(i, indices->nth(i) + 1)
|
|
if indices->nth(i) < ops->size
|
|
done = false
|
|
break
|
|
indices->set(i, 0)
|
|
i -= 1
|
|
|
|
if done
|
|
return 0
|
|
|
|
func part1[equations: Array] : void
|
|
out := 0
|
|
|
|
for i in 0..equations->size
|
|
out += solve(["add", "mul"], equations->nth(i))
|
|
|
|
io.println_i64(out)
|
|
|
|
func part2[equations: Array] : void
|
|
out := 0
|
|
|
|
for i in 0..equations->size
|
|
out += solve(["add", "mul", "concat"], equations->nth(i))
|
|
|
|
io.println_i64(out)
|
|
|
|
func main[] : i64
|
|
~input, ok := io.read_text_file("input.txt")
|
|
if !ok
|
|
panic("failed to open input.txt")
|
|
|
|
lines := input->split("\n")
|
|
equations := []
|
|
|
|
for i in 0..lines->size
|
|
line : str = lines->nth(i)
|
|
parts := line->split(": ")
|
|
|
|
xs := (parts->nth(1) as str)->split(" ")
|
|
for j in 0..xs->size
|
|
xs->set(j, (xs->nth(j) as str)->parse_i64())
|
|
|
|
equations->push([(parts->nth(0) as str)->parse_i64(), xs])
|
|
|
|
part1(equations)
|
|
part2(equations)
|