diff --git a/examples/puzzles/aoc2024_07.zr b/examples/puzzles/aoc2024_07.zr new file mode 100644 index 0000000..8ae27c8 --- /dev/null +++ b/examples/puzzles/aoc2024_07.zr @@ -0,0 +1,77 @@ +func concat[a: I64, b: I64] : I64 + let a_str: String = str.from_i64(a) + let b_str: String = str.from_i64(b) + let ab_str: String = str.concat(a_str, b_str) + let out: I64 = str.parse_i64(ab_str) + // without freeing the program works but leaks 2GB of memory :p + mem.free(a_str) + mem.free(b_str) + 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 = [] + for i in 0..n + array.push(indices, 0) + + while true + let res: I64 = array.nth(array.nth(e, 1), 0) + for i in 0..n + let op: String = array.nth(ops, array.nth(indices, i)) + + if str.equal(op, "add") + res = res + array.nth(array.nth(e, 1), i + 1) + else if str.equal(op, "mul") + res = res * array.nth(array.nth(e, 1), i + 1) + else if str.equal(op, "concat") + res = concat(res, array.nth(array.nth(e, 1), i + 1)) + if res == array.nth(e, 0) + return res + + let done: Bool = true + let i: I64 = n - 1 + + while i >= 0 + array.set(indices, i, array.nth(indices, i) + 1) + if array.nth(indices, i) < array.size(ops) + done = false + break + array.set(indices, i, 0) + i = i - 1 + + if done + return 0 + +func part1[equations: Array] : Void + let out: I64 = 0 + + for i in 0..array.size(equations) + out = out + solve(["add", "mul"], array.nth(equations, i)) + + io.println_i64(out) + +func part2[equations: Array] : Void + let out: I64 = 0 + + for i in 0..array.size(equations) + 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 = [] + + for i in 0..array.size(lines) + let line: String = array.nth(lines, i) + let parts: Array = str.split(line, ": ") + + let xs: Array = str.split(array.nth(parts, 1), " ") + for j in 0..array.size(xs) + array.set(xs, j, str.parse_i64(array.nth(xs, j))) + + array.push(equations, [str.parse_i64(array.nth(parts, 0)), xs]) + + part1(equations) + part2(equations) \ No newline at end of file diff --git a/examples/puzzles/euler10.zr b/examples/puzzles/euler_01.zr similarity index 100% rename from examples/puzzles/euler10.zr rename to examples/puzzles/euler_01.zr diff --git a/examples/puzzles/euler12.zr b/examples/puzzles/euler_02.zr similarity index 100% rename from examples/puzzles/euler12.zr rename to examples/puzzles/euler_02.zr diff --git a/examples/puzzles/euler13.zr b/examples/puzzles/euler_03.zr similarity index 100% rename from examples/puzzles/euler13.zr rename to examples/puzzles/euler_03.zr diff --git a/examples/puzzles/euler14.zr b/examples/puzzles/euler_04.zr similarity index 100% rename from examples/puzzles/euler14.zr rename to examples/puzzles/euler_04.zr diff --git a/examples/puzzles/euler15.zr b/examples/puzzles/euler_05.zr similarity index 100% rename from examples/puzzles/euler15.zr rename to examples/puzzles/euler_05.zr diff --git a/examples/puzzles/euler1.zr b/examples/puzzles/euler_06.zr similarity index 100% rename from examples/puzzles/euler1.zr rename to examples/puzzles/euler_06.zr diff --git a/examples/puzzles/euler2.zr b/examples/puzzles/euler_07.zr similarity index 100% rename from examples/puzzles/euler2.zr rename to examples/puzzles/euler_07.zr diff --git a/examples/puzzles/euler3.zr b/examples/puzzles/euler_08.zr similarity index 100% rename from examples/puzzles/euler3.zr rename to examples/puzzles/euler_08.zr diff --git a/examples/puzzles/euler4.zr b/examples/puzzles/euler_09.zr similarity index 100% rename from examples/puzzles/euler4.zr rename to examples/puzzles/euler_09.zr diff --git a/examples/puzzles/euler5.zr b/examples/puzzles/euler_10.zr similarity index 100% rename from examples/puzzles/euler5.zr rename to examples/puzzles/euler_10.zr diff --git a/examples/puzzles/euler6.zr b/examples/puzzles/euler_11.zr similarity index 100% rename from examples/puzzles/euler6.zr rename to examples/puzzles/euler_11.zr diff --git a/examples/puzzles/euler7.zr b/examples/puzzles/euler_12.zr similarity index 100% rename from examples/puzzles/euler7.zr rename to examples/puzzles/euler_12.zr diff --git a/examples/puzzles/euler8.zr b/examples/puzzles/euler_13.zr similarity index 100% rename from examples/puzzles/euler8.zr rename to examples/puzzles/euler_13.zr diff --git a/examples/puzzles/euler9.zr b/examples/puzzles/euler_14.zr similarity index 100% rename from examples/puzzles/euler9.zr rename to examples/puzzles/euler_14.zr