replace array with an actual struct now that we have them
This commit is contained in:
@@ -1,29 +1,29 @@
|
||||
func part1[l1: array, l2: array] : void
|
||||
func part1[l1: Array, l2: Array] : void
|
||||
let out = 0
|
||||
|
||||
for i in 0..array.size(l1)
|
||||
for i in 0..l1->size
|
||||
out = out + math.abs(array.nth(l1, i) - array.nth(l2, i))
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[l1: array, l2: array] : void
|
||||
func part2[l1: Array, l2: Array] : void
|
||||
let out = 0
|
||||
|
||||
for i in 0..array.size(l1)
|
||||
for i in 0..l1->size
|
||||
out = out + array.nth(l1, i) * alg.count(l2, array.nth(l1, i))
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : i64
|
||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||
|
||||
let l1: array = []
|
||||
let l2: array = []
|
||||
let l1: Array = []
|
||||
let l2: 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, " ")
|
||||
|
||||
array.push(l1, str.parse_i64(array.nth(parts, 0)))
|
||||
array.push(l2, str.parse_i64(array.nth(parts, 1)))
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
func check[report: array] : bool
|
||||
func check[report: Array] : bool
|
||||
let increasing: bool = array.nth(report, 0) < array.nth(report, 1)
|
||||
|
||||
for i in 0..array.size(report)-1
|
||||
for i in 0..report->size-1
|
||||
if array.nth(report, i) > array.nth(report, i + 1) && increasing
|
||||
return false
|
||||
if array.nth(report, i) < array.nth(report, i + 1) && !increasing
|
||||
@@ -13,24 +13,25 @@ func check[report: array] : bool
|
||||
|
||||
return true
|
||||
|
||||
func part1[data: array] : void
|
||||
func part1[data: Array] : void
|
||||
let out = 0
|
||||
|
||||
for i in 0..array.size(data)
|
||||
for i in 0..data->size
|
||||
if check(array.nth(data, i))
|
||||
out = out + 1
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[data: array] : void
|
||||
func part2[data: Array] : void
|
||||
let out = 0
|
||||
|
||||
for i in 0..array.size(data)
|
||||
for i in 0..data->size
|
||||
if check(array.nth(data, i))
|
||||
out = out + 1
|
||||
else
|
||||
for j in 0..array.size(array.nth(data, i))
|
||||
let sliced: array = array.concat(array.slice(array.nth(data, i), 0, j), array.slice(array.nth(data, i), j + 1, array.size(array.nth(data, i)) - (j + 1)))
|
||||
let arr: Array = array.nth(data, i)
|
||||
for j in 0..arr->size
|
||||
let sliced: Array = array.concat(array.slice(arr, 0, j), array.slice(arr, j + 1, arr->size - (j + 1)))
|
||||
|
||||
if check(sliced)
|
||||
out = out + 1
|
||||
@@ -39,14 +40,14 @@ func part2[data: array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : i64
|
||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||
|
||||
let data: array = []
|
||||
for i in 0..array.size(lines)
|
||||
let data: Array = []
|
||||
for i in 0..lines->size
|
||||
let line: str = array.nth(lines, i)
|
||||
|
||||
let report: array = str.split(line, " ")
|
||||
for i in 0..array.size(report)
|
||||
let report: Array = str.split(line, " ")
|
||||
for i in 0..report->size
|
||||
array.set(report, i, str.parse_i64(array.nth(report, i)))
|
||||
array.push(data, report)
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
func check[lines: array, x: i64, y: i64, dx: i64, dy: i64] : bool
|
||||
if x + dx * 3 < 0 || x + dx * 3 >= array.size(lines) || y + dy * 3 < 0 || y + dy * 3 >= str.len(array.nth(lines, 0))
|
||||
func check[lines: Array, x: i64, y: i64, dx: i64, dy: i64] : bool
|
||||
if x + dx * 3 < 0 || x + dx * 3 >= lines->size || y + dy * 3 < 0 || y + dy * 3 >= str.len(array.nth(lines, 0))
|
||||
return false
|
||||
|
||||
if array.nth(lines, x)[y] != 'X'
|
||||
@@ -13,10 +13,10 @@ func check[lines: array, x: i64, y: i64, dx: i64, dy: i64] : bool
|
||||
|
||||
return true
|
||||
|
||||
func part1[lines: array] : void
|
||||
func part1[lines: Array] : void
|
||||
let out = 0
|
||||
|
||||
for x in 0..array.size(lines)
|
||||
for x in 0..lines->size
|
||||
for y in 0..str.len(array.nth(lines, x))
|
||||
if check(lines, x, y, 0, 1)
|
||||
out = out + 1
|
||||
@@ -37,10 +37,10 @@ func part1[lines: array] : void
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[lines: array] : void
|
||||
func part2[lines: Array] : void
|
||||
let out = 0
|
||||
|
||||
for x in 1..array.size(lines)-1
|
||||
for x in 1..lines->size-1
|
||||
for y in 1..str.len(array.nth(lines, x))-1
|
||||
if array.nth(lines, x)[y] == 'A'
|
||||
let s: str = mem.alloc(5)
|
||||
@@ -56,7 +56,7 @@ func part2[lines: array] : void
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : i64
|
||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||
|
||||
part1(lines)
|
||||
part2(lines)
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
func rule_exists[rules_left: array, rules_right: array, a: i64, b: i64] : bool
|
||||
for k in 0..array.size(rules_left)
|
||||
func rule_exists[rules_left: Array, rules_right: Array, a: i64, b: i64] : bool
|
||||
for k in 0..rules_left->size
|
||||
if array.nth(rules_left, k) == a && array.nth(rules_right, k) == b
|
||||
return true
|
||||
return false
|
||||
|
||||
func check[update: array, rules_left: array, rules_right: array] : bool
|
||||
for i in 0..array.size(update)
|
||||
func check[update: Array, rules_left: Array, rules_right: Array] : bool
|
||||
for i in 0..update->size
|
||||
for j in 0..i
|
||||
if rule_exists(rules_left, rules_right, array.nth(update, i), array.nth(update, j))
|
||||
return false
|
||||
return true
|
||||
|
||||
func sort_by_rules[update: array, rules_left: array, rules_right: array] : void
|
||||
func sort_by_rules[update: Array, rules_left: Array, rules_right: Array] : void
|
||||
let swapped: bool = true
|
||||
|
||||
while swapped
|
||||
swapped = false
|
||||
for i in 0..array.size(update)-1
|
||||
for i in 0..update->size-1
|
||||
let a: i64 = array.nth(update, i)
|
||||
let b: i64 = array.nth(update, i+1)
|
||||
if rule_exists(rules_left, rules_right, b, a)
|
||||
@@ -25,47 +25,47 @@ func sort_by_rules[update: array, rules_left: array, rules_right: array] : void
|
||||
array.set(update, i+1, tmp)
|
||||
swapped = true
|
||||
|
||||
func part1[updates: array, rules_left: array, rules_right: array] : void
|
||||
func part1[updates: Array, rules_left: Array, rules_right: Array] : void
|
||||
let out = 0
|
||||
|
||||
for i in 0..array.size(updates)
|
||||
let update: array = array.nth(updates, i)
|
||||
for i in 0..updates->size
|
||||
let update: Array = array.nth(updates, i)
|
||||
if check(update, rules_left, rules_right)
|
||||
out = out + array.nth(update, array.size(update) / 2)
|
||||
out = out + array.nth(update, update->size / 2)
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[updates: array, rules_left: array, rules_right: array] : void
|
||||
func part2[updates: Array, rules_left: Array, rules_right: Array] : void
|
||||
let out = 0
|
||||
|
||||
for i in 0..array.size(updates)
|
||||
let update: array = array.nth(updates, i)
|
||||
for i in 0..updates->size
|
||||
let update: Array = array.nth(updates, i)
|
||||
if !check(update, rules_left, rules_right)
|
||||
sort_by_rules(update, rules_left, rules_right)
|
||||
out = out + array.nth(update, array.size(update) / 2)
|
||||
out = out + array.nth(update, update->size / 2)
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : i64
|
||||
let data: array = io.read_file("input.txt") |> str.split("\n\n")
|
||||
let data: Array = io.read_file("input.txt") |> str.split("\n\n")
|
||||
|
||||
let rules_left: array = []
|
||||
let rules_right: array = []
|
||||
let rules_lines: array = str.split(array.nth(data, 0), "\n")
|
||||
for i in 0..array.size(rules_lines)
|
||||
let rules_left: Array = []
|
||||
let rules_right: Array = []
|
||||
let rules_lines: Array = str.split(array.nth(data, 0), "\n")
|
||||
for i in 0..rules_lines->size
|
||||
let line: str = array.nth(rules_lines, i)
|
||||
let parts: array = str.split(line, "|")
|
||||
let parts: Array = str.split(line, "|")
|
||||
|
||||
array.push(rules_left, str.parse_i64(array.nth(parts, 0)))
|
||||
array.push(rules_right, str.parse_i64(array.nth(parts, 1)))
|
||||
|
||||
let updates: array = []
|
||||
let updates_lines: array = str.split(array.nth(data, 1), "\n")
|
||||
for i in 0..array.size(updates_lines)
|
||||
let updates: Array = []
|
||||
let updates_lines: Array = str.split(array.nth(data, 1), "\n")
|
||||
for i in 0..updates_lines->size
|
||||
let line: str = array.nth(updates_lines, i)
|
||||
let xs: array = str.split(line, ",")
|
||||
let xs: Array = str.split(line, ",")
|
||||
|
||||
for i in 0..array.size(xs)
|
||||
for i in 0..xs->size
|
||||
array.set(xs, i, str.parse_i64(array.nth(xs, i)))
|
||||
array.push(updates, xs)
|
||||
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
func part1[lines: array] : void
|
||||
func part1[lines: Array] : void
|
||||
let password = 0
|
||||
let dial = 50
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
for i in 0..lines->size
|
||||
let line: str = array.nth(lines, i)
|
||||
let dir: u8 = line[0]
|
||||
let n: i64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||
@@ -21,11 +21,11 @@ func part1[lines: array] : void
|
||||
|
||||
io.println_i64(password)
|
||||
|
||||
func part2[lines: array] : void
|
||||
func part2[lines: Array] : void
|
||||
let password = 0
|
||||
let dial = 50
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
for i in 0..lines->size
|
||||
let line: str = array.nth(lines, i)
|
||||
let dir: u8 = line[0]
|
||||
let n: i64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||
@@ -47,7 +47,7 @@ func part2[lines: array] : void
|
||||
io.println_i64(password)
|
||||
|
||||
func main[] : i64
|
||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||
|
||||
part1(lines)
|
||||
part2(lines)
|
||||
|
||||
@@ -8,11 +8,11 @@ func part1_is_invalid_id[s: str] : bool
|
||||
|
||||
return str.equal(first, second)
|
||||
|
||||
func part1[ranges: array] : void
|
||||
func part1[ranges: Array] : void
|
||||
let sum = 0
|
||||
|
||||
for i in 0..array.size(ranges)
|
||||
let parts: array = array.nth(ranges, i) |> str.split("-")
|
||||
for i in 0..ranges->size
|
||||
let parts: Array = array.nth(ranges, i) |> str.split("-")
|
||||
let start: i64 = array.nth(parts, 0) |> str.parse_i64()
|
||||
let end: i64 = array.nth(parts, 1) |> str.parse_i64()
|
||||
|
||||
@@ -39,11 +39,11 @@ func part2_is_invalid_id[s: str] : bool
|
||||
return true
|
||||
return false
|
||||
|
||||
func part2[ranges: array] : void
|
||||
func part2[ranges: Array] : void
|
||||
let sum = 0
|
||||
|
||||
for i in 0..array.size(ranges)
|
||||
let parts: array = array.nth(ranges, i) |> str.split("-")
|
||||
for i in 0..ranges->size
|
||||
let parts: Array = array.nth(ranges, i) |> str.split("-")
|
||||
let start: i64 = array.nth(parts, 0) |> str.parse_i64()
|
||||
let end: i64 = array.nth(parts, 1) |> str.parse_i64()
|
||||
|
||||
@@ -54,7 +54,7 @@ func part2[ranges: array] : void
|
||||
io.println_i64(sum)
|
||||
|
||||
func main[] : i64
|
||||
let ranges: array = io.read_file("input.txt") |> str.split(",")
|
||||
let ranges: Array = io.read_file("input.txt") |> str.split(",")
|
||||
|
||||
part1(ranges)
|
||||
part2(ranges)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
func part1[lines: array] : void
|
||||
func part1[lines: Array] : void
|
||||
let sum = 0
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
for i in 0..lines->size
|
||||
let line: str = array.nth(lines, i)
|
||||
|
||||
let largest = 0
|
||||
@@ -35,17 +35,17 @@ func part2_rec[bank: str, start: i64, remaining: i64] : i64
|
||||
else
|
||||
return largest
|
||||
|
||||
func part2[lines: array] : void
|
||||
func part2[lines: Array] : void
|
||||
let sum = 0
|
||||
|
||||
for i in 0..array.size(lines)
|
||||
for i in 0..lines->size
|
||||
let line: str = array.nth(lines, i)
|
||||
|
||||
sum = sum + part2_rec(line, 0, 12)
|
||||
io.println_i64(sum)
|
||||
|
||||
func main[] : i64
|
||||
let lines: array = io.read_file("input.txt") |> str.split("\n")
|
||||
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||
|
||||
part1(lines)
|
||||
part2(lines)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
func main[] : i64
|
||||
let N = 20
|
||||
|
||||
let grid: array = []
|
||||
let grid: Array = []
|
||||
array.push(grid, [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8])
|
||||
array.push(grid, [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0])
|
||||
array.push(grid, [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65])
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
func main[] : i64
|
||||
let n: array = []
|
||||
let n: Array = []
|
||||
array.push(n, 1)
|
||||
|
||||
for j in 0..1000
|
||||
let carry = 0
|
||||
for i in 0..array.size(n)
|
||||
for i in 0..n->size
|
||||
let tmp: i64 = array.nth(n, i) * 2 + carry
|
||||
array.set(n, i, tmp % 10)
|
||||
carry = tmp / 10
|
||||
@@ -13,7 +13,7 @@ func main[] : i64
|
||||
carry = carry / 10
|
||||
|
||||
let sum = 0
|
||||
for i in 0..array.size(n)
|
||||
for i in 0..n->size
|
||||
sum = sum + array.nth(n, i)
|
||||
|
||||
io.println_i64(sum)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
func main[] : i64
|
||||
let s1: array = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4]
|
||||
let s2: array = [3, 6, 6, 8, 8, 7, 7, 9, 8, 8]
|
||||
let s3: array = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6]
|
||||
let s1: Array = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4]
|
||||
let s2: Array = [3, 6, 6, 8, 8, 7, 7, 9, 8, 8]
|
||||
let s3: Array = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6]
|
||||
|
||||
let sum = 0
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
func findmax[triangle: array, row: i64, col: i64] : i64
|
||||
func findmax[triangle: Array, row: i64, col: i64] : i64
|
||||
if row == 14
|
||||
return array.nth(array.nth(triangle, row), col)
|
||||
|
||||
@@ -8,7 +8,7 @@ func findmax[triangle: array, row: i64, col: i64] : i64
|
||||
return array.nth(array.nth(triangle, row), col) + math.max(left, right)
|
||||
|
||||
func main[] : i64
|
||||
let triangle: array = []
|
||||
let triangle: Array = []
|
||||
array.push(triangle, [75])
|
||||
array.push(triangle, [95, 64])
|
||||
array.push(triangle, [17, 47, 82])
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
func multiply[n: array, x: i64] : void
|
||||
func multiply[n: Array, x: i64] : void
|
||||
let carry = 0
|
||||
for i in 0..array.size(n)
|
||||
for i in 0..n->size
|
||||
let prod: i64 = array.nth(n, i) * x + carry
|
||||
array.set(n, i, prod % 10)
|
||||
carry = prod / 10
|
||||
@@ -9,13 +9,13 @@ func multiply[n: array, x: i64] : void
|
||||
carry = carry / 10
|
||||
|
||||
func main[] : i64
|
||||
let n: array = [1]
|
||||
let n: Array = [1]
|
||||
|
||||
for i in 2..101
|
||||
multiply(n, i)
|
||||
|
||||
let sum = 0
|
||||
for i in 0..array.size(n)
|
||||
for i in 0..n->size
|
||||
sum = sum + array.nth(n, i)
|
||||
|
||||
io.println_i64(sum)
|
||||
|
||||
Reference in New Issue
Block a user