type inference for variables

This commit is contained in:
2026-05-13 14:01:44 +02:00
parent 5f2082ef57
commit 027245684d
37 changed files with 304 additions and 302 deletions

View File

@@ -17,13 +17,13 @@ func part2[l1: Array, l2: Array] : void
func main[] : i64
let lines: Array = must(io.read_text_file?("input.txt")) |> str.split("\n")
let l1: Array = []
let l2: Array = []
let l1 = []
let l2 = []
for i in 0..lines->size
let line: str = array.nth(lines, i)
let parts: Array = str.split(line, " ")
let parts = str.split(line, " ")
array.push(l1, str.parse_i64(array.nth(parts, 0)))
array.push(l2, str.parse_i64(array.nth(parts, 1)))

View File

@@ -1,5 +1,5 @@
func check[report: Array] : bool
let increasing: bool = array.nth(report, 0) < array.nth(report, 1)
let increasing = array.nth(report, 0) < array.nth(report, 1)
for i in 0..report->size-1
if array.nth(report, i) > array.nth(report, i + 1) && increasing
@@ -7,7 +7,7 @@ func check[report: Array] : bool
if array.nth(report, i) < array.nth(report, i + 1) && !increasing
return false
let diff: i64 = math.abs(array.nth(report, i) - array.nth(report, i + 1))
let diff = math.abs(array.nth(report, i) - array.nth(report, i + 1))
if diff < 1 || diff > 3
return false
@@ -31,7 +31,7 @@ func part2[data: Array] : void
else
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)))
let sliced = array.concat(array.slice(arr, 0, j), array.slice(arr, j + 1, arr->size - (j + 1)))
if check(sliced)
out = out + 1
@@ -40,13 +40,13 @@ func part2[data: Array] : void
io.println_i64(out)
func main[] : i64
let lines: Array = must(io.read_text_file?("input.txt")) |> str.split("\n")
let lines = must(io.read_text_file?("input.txt")) |> str.split("\n")
let data: Array = []
let data = []
for i in 0..lines->size
let line: str = array.nth(lines, i)
let report: Array = str.split(line, " ")
let report = str.split(line, " ")
for i in 0..report->size
array.set(report, i, str.parse_i64(array.nth(report, i)))
array.push(data, report)

View File

@@ -43,7 +43,7 @@ func part2[lines: Array] : void
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) as str
let s = mem.alloc(5) as str
s[0] = array.nth(lines, x - 1)[y - 1]
s[1] = array.nth(lines, x + 1)[y - 1]
s[2] = array.nth(lines, x + 1)[y + 1]

View File

@@ -12,7 +12,7 @@ func check[update: Array, rules_left: Array, rules_right: Array] : bool
return true
func sort_by_rules[update: Array, rules_left: Array, rules_right: Array] : void
let swapped: bool = true
let swapped = true
while swapped
swapped = false
@@ -20,7 +20,7 @@ func sort_by_rules[update: Array, rules_left: Array, rules_right: Array] : void
let a: i64 = array.nth(update, i)
let b: i64 = array.nth(update, i+1)
if rule_exists(rules_left, rules_right, b, a)
let tmp: i64 = a
let tmp = a
array.set(update, i, b)
array.set(update, i+1, tmp)
swapped = true
@@ -47,23 +47,23 @@ func part2[updates: Array, rules_left: Array, rules_right: Array] : void
io.println_i64(out)
func main[] : i64
let data: Array = must(io.read_text_file?("input.txt")) |> str.split("\n\n")
let data = must(io.read_text_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")
let rules_left = []
let rules_right = []
let rules_lines = 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 = 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")
let updates = []
let updates_lines = 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 = str.split(line, ",")
for i in 0..xs->size
array.set(xs, i, str.parse_i64(array.nth(xs, i)))

View File

@@ -1,8 +1,8 @@
func concat[a: i64, b: i64] : i64
let a_str: str = str.from_i64(a)
let b_str: str = str.from_i64(b)
let ab_str: str = str.concat(a_str, b_str)
let out: i64 = str.parse_i64(ab_str)
let a_str = str.from_i64(a)
let b_str = str.from_i64(b)
let ab_str = str.concat(a_str, b_str)
let out = str.parse_i64(ab_str)
// without freeing the program works but leaks 2GB of memory :p
mem.free(a_str)
mem.free(b_str)
@@ -11,8 +11,8 @@ func concat[a: i64, b: i64] : i64
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 = []
let n = e_1->size - 1
let indices = []
for i in 0..n
array.push(indices, 0)
@@ -30,8 +30,8 @@ func solve[ops: Array, e: Array] : i64
if res == array.nth(e, 0)
return res
let done: bool = true
let i: i64 = n - 1
let done = true
let i = n - 1
while i >= 0
array.set(indices, i, array.nth(indices, i) + 1)
@@ -61,14 +61,14 @@ func part2[equations: Array] : void
io.println_i64(out)
func main[] : i64
let lines: Array = must(io.read_text_file?("input.txt")) |> str.split("\n")
let equations: Array = []
let lines = must(io.read_text_file?("input.txt")) |> str.split("\n")
let equations = []
for i in 0..lines->size
let line: str = array.nth(lines, i)
let parts: Array = str.split(line, ": ")
let parts = str.split(line, ": ")
let xs: Array = str.split(array.nth(parts, 1), " ")
let xs = str.split(array.nth(parts, 1), " ")
for j in 0..xs->size
array.set(xs, j, str.parse_i64(array.nth(xs, j)))

View File

@@ -4,8 +4,8 @@ func part1[lines: Array] : void
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()
let dir = line[0]
let n = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
if dir == 'L'
dial = dial - n
@@ -27,8 +27,8 @@ func part2[lines: Array] : void
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()
let dir = line[0]
let n = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
if dir == 'L'
for i in 0..n
@@ -47,7 +47,7 @@ func part2[lines: Array] : void
io.println_i64(password)
func main[] : i64
let lines: Array = must(io.read_text_file?("input.txt")) |> str.split("\n")
let lines = must(io.read_text_file?("input.txt")) |> str.split("\n")
part1(lines)
part2(lines)

View File

@@ -1,10 +1,10 @@
func part1_is_invalid_id[s: str] : bool
let len: i64 = str.len(s)
let len = str.len(s)
if len % 2 != 0
return false
let first: str = str.substr(s, 0, len / 2)
let second: str = str.substr(s, len / 2, len / 2)
let first = str.substr(s, 0, len / 2)
let second = str.substr(s, len / 2, len / 2)
return str.equal(first, second)
@@ -12,9 +12,9 @@ func part1[ranges: Array] : void
let sum = 0
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()
let parts = array.nth(ranges, i) |> str.split("-")
let start = array.nth(parts, 0) |> str.parse_i64()
let end = array.nth(parts, 1) |> str.parse_i64()
for n in (start)..end+1
if part1_is_invalid_id(str.from_i64(n))
@@ -24,15 +24,15 @@ func part1[ranges: Array] : void
// had to cheat this one
func part2_is_invalid_id[s: str] : bool
let len: i64 = str.len(s)
let len = str.len(s)
if len < 2
return false
for div in 1..(len / 2 + 1)
if len % div == 0
let u: str = str.substr(s, 0, div)
let is_repeat: bool = true
let u = str.substr(s, 0, div)
let is_repeat = true
for k in 1..(len / div)
let segment: str = str.substr(s, k * div, div)
let segment = str.substr(s, k * div, div)
if !str.equal(segment, u)
is_repeat = false
if is_repeat
@@ -43,9 +43,9 @@ func part2[ranges: Array] : void
let sum = 0
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()
let parts = array.nth(ranges, i) |> str.split("-")
let start = array.nth(parts, 0) |> str.parse_i64()
let end = array.nth(parts, 1) |> str.parse_i64()
for n in (start)..end+1
if part2_is_invalid_id(str.from_i64(n))
@@ -54,7 +54,7 @@ func part2[ranges: Array] : void
io.println_i64(sum)
func main[] : i64
let ranges: Array = must(io.read_text_file?("input.txt")) |> str.split(",")
let ranges = must(io.read_text_file?("input.txt")) |> str.split(",")
part1(ranges)
part2(ranges)

View File

@@ -7,11 +7,11 @@ func part1[lines: Array] : void
let largest = 0
for j in 0..str.len(line)
for k in (j+1)..str.len(line)
let s: str = mem.alloc(3) as str
let s = mem.alloc(3) as str
s[0] = line[j]
s[1] = line[k]
s[2] = 0
let n: i64 = str.parse_i64(s)
let n = str.parse_i64(s)
if n > largest
largest = n
@@ -21,11 +21,11 @@ func part1[lines: Array] : void
// had to cheat this one
func part2_rec[bank: str, start: i64, remaining: i64] : i64
let largest = 0
let largest_idx: i64 = start
let len: i64 = str.len(bank)
let largest_idx = start
let len = str.len(bank)
for i in (start)..(len-remaining+1)
let v: i64 = (bank[i] - '0') as i64
let v = (bank[i] - '0') as i64
if v > largest
largest = v
largest_idx = i
@@ -45,7 +45,7 @@ func part2[lines: Array] : void
io.println_i64(sum)
func main[] : i64
let lines: Array = must(io.read_text_file?("input.txt")) |> str.split("\n")
let lines = must(io.read_text_file?("input.txt")) |> str.split("\n")
part1(lines)
part2(lines)

View File

@@ -6,7 +6,7 @@ func main[] : i64
while a < 4000000
if a % 2 == 0
sum = sum + a
let temp: i64 = b
let temp = b
b = a + b
a = temp

View File

@@ -4,8 +4,8 @@ func main[] : i64
for a in 500..1000
for b in 500..1000
if a * b > out
let s: str = str.from_i64(a * b)
let s_rev: str = str.reverse(s)
let s = str.from_i64(a * b)
let s_rev = str.reverse(s)
if str.equal(s, s_rev)
out = a * b
mem.free(s)

View File

@@ -1,8 +1,8 @@
func main[] : i64
let n: str = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
let n = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
let out = 0
let max: i64 = str.len(n) - 13
let max = str.len(n) - 13
for i in 0..max
let s = 1
let j = 0

View File

@@ -1,7 +1,7 @@
func main[] : i64
for a in 1..1000
for b in 1..1000
let c: i64 = 1000 - b - a
let c = 1000 - b - a
if a * a + b * b == c * c
io.println_i64(a * b * c)
return 0

View File

@@ -1,7 +1,7 @@
func main[] : i64
let N = 20
let grid: Array = []
let grid = []
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])

View File

@@ -1,5 +1,5 @@
func num_divisors[n: i64] : i64
let end: i64 = math.isqrt(n)
let end = math.isqrt(n)
let out = 0
for i in 1..end+1

View File

@@ -15,7 +15,7 @@ func main[] : i64
let max_index = 0
for i in 1..1000000
let seq: i64 = collatz_seq(i)
let seq = collatz_seq(i)
if seq > max
max = seq
max_index = i

View File

@@ -1,5 +1,5 @@
func main[] : i64
let n: Array = []
let n = []
array.push(n, 1)
for j in 0..1000

View File

@@ -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 = [0, 3, 3, 5, 4, 4, 3, 5, 5, 4]
let s2 = [3, 6, 6, 8, 8, 7, 7, 9, 8, 8]
let s3 = [0, 0, 6, 6, 5, 5, 5, 7, 6, 6]
let sum = 0

View File

@@ -2,13 +2,13 @@ func findmax[triangle: Array, row: i64, col: i64] : i64
if row == 14
return array.nth(array.nth(triangle, row), col)
let left: i64 = findmax(triangle, row + 1, col)
let right: i64 = findmax(triangle, row + 1, col + 1)
let left = findmax(triangle, row + 1, col)
let right = findmax(triangle, row + 1, col + 1)
return array.nth(array.nth(triangle, row), col) + math.max(left, right)
func main[] : i64
let triangle: Array = []
let triangle = []
array.push(triangle, [75])
array.push(triangle, [95, 64])
array.push(triangle, [17, 47, 82])

View File

@@ -9,7 +9,7 @@ func multiply[n: Array, x: i64] : void
carry = carry / 10
func main[] : i64
let n: Array = [1]
let n = [1]
for i in 2..101
multiply(n, i)

View File

@@ -1,5 +1,5 @@
func divisors_sum[n: i64] : i64
let k: i64 = n
let k = n
let sum = 1
for i in 2..k+1
@@ -14,7 +14,7 @@ func main[] : i64
let sum = 0
for i in 2..10000
let d: i64 = divisors_sum(i)
let d = divisors_sum(i)
if i < d && i == divisors_sum(d)
sum = sum + i + d