new var declaration syntax

This commit is contained in:
2026-05-27 20:25:58 +02:00
parent 284bc61f24
commit 4fda79f0bc
48 changed files with 450 additions and 449 deletions

View File

@@ -1,15 +1,15 @@
func main[] : i64
// https://brainfuck.org/sierpinski.b
let src = "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
let src_len = str.len(src)
let i = 0
src := "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
src_len := str.len(src)
i := 0
let memory = mem.alloc(30000)
memory := mem.alloc(30000)
mem.zero(memory, 30000)
let p = 0
p := 0
while i < src_len
let op = src[i]
op := src[i]
if op == '>'
p = p + 1
@@ -26,7 +26,7 @@ func main[] : i64
else if op == '['
if !memory[p]
i = i + 1
let opened = 0
opened := 0
while i < src_len && !(src[i] == ']' && !opened)
if src[i] == '['
opened = opened + 1
@@ -36,7 +36,7 @@ func main[] : i64
else if op == ']'
if memory[p]
i = i - 1
let closed = 0
closed := 0
while i >= 0 && !(src[i] == '[' && !closed)
if src[i] == ']'
closed = closed + 1

View File

@@ -22,7 +22,7 @@ struct CHIP8
keyboard_map: Array
func chip8_create[] : CHIP8
let c = new CHIP8
c := new CHIP8
c->memory = mem.alloc(4096)
mem.zero(c->memory, 4096)
c->display = mem.alloc(64*32)
@@ -33,7 +33,7 @@ func chip8_create[] : CHIP8
c->stack = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
c->keyboard = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
let fonts = [0xf0, 0x90, 0x90, 0x90, 0xf0, 0x20, 0x60, 0x20, 0x20, 0x70, 0xf0, 0x10, 0xf0, 0x80, 0xf0, 0xf0, 0x10, 0xf0, 0x10, 0xf0, 0x90, 0x90, 0xf0, 0x10, 0x10, 0xf0, 0x80, 0xf0, 0x10, 0xf0, 0xf0, 0x80, 0xf0, 0x90, 0xf0, 0xf0, 0x10, 0x20, 0x40, 0x40, 0xf0, 0x90, 0xf0, 0x90, 0xf0, 0xf0, 0x90, 0xf0, 0x10, 0xf0, 0xf0, 0x90, 0xf0, 0x90, 0x90, 0xe0, 0x90, 0xe0, 0x90, 0xe0, 0xf0, 0x80, 0x80, 0x80, 0xf0, 0xe0, 0x90, 0x90, 0x90, 0xe0, 0xf0, 0x80, 0xf0, 0x80, 0xf0, 0xf0, 0x80, 0xf0, 0x80, 0x80]
fonts := [0xf0, 0x90, 0x90, 0x90, 0xf0, 0x20, 0x60, 0x20, 0x20, 0x70, 0xf0, 0x10, 0xf0, 0x80, 0xf0, 0xf0, 0x10, 0xf0, 0x10, 0xf0, 0x90, 0x90, 0xf0, 0x10, 0x10, 0xf0, 0x80, 0xf0, 0x10, 0xf0, 0xf0, 0x80, 0xf0, 0x90, 0xf0, 0xf0, 0x10, 0x20, 0x40, 0x40, 0xf0, 0x90, 0xf0, 0x90, 0xf0, 0xf0, 0x90, 0xf0, 0x10, 0xf0, 0xf0, 0x90, 0xf0, 0x90, 0x90, 0xe0, 0x90, 0xe0, 0x90, 0xe0, 0xf0, 0x80, 0x80, 0x80, 0xf0, 0xe0, 0x90, 0x90, 0x90, 0xe0, 0xf0, 0x80, 0xf0, 0x80, 0xf0, 0xf0, 0x80, 0xf0, 0x80, 0x80]
for i in 0..80
c->memory[i] = array.nth(fonts, i)
mem.free(fonts)
@@ -58,18 +58,18 @@ func chip8_disassemble[c: CHIP8, ins_count: i64] : void
for i in 0..ins_count
io.printf("0x%x: ", c->pc)
let high = c->memory[c->pc] as i64
let low = c->memory[c->pc + 1] as i64
high := c->memory[c->pc] as i64
low := c->memory[c->pc + 1] as i64
c->pc = c->pc + 2
let ins = (high << 8) | low
let nnn = ins & 0x0fff
let kk = ins & 0x00ff
let n = ins & 0x000f
let x = (ins >> 8) & 0x0f
let y = (ins >> 4) & 0x0f
ins := (high << 8) | low
nnn := ins & 0x0fff
kk := ins & 0x00ff
n := ins & 0x000f
x := (ins >> 8) & 0x0f
y := (ins >> 4) & 0x0f
let op = (ins >> 12) & 0x0f
op := (ins >> 12) & 0x0f
if op == 0x0
if nnn == 0x0e0
io.println("CLS")
@@ -154,18 +154,18 @@ func chip8_disassemble[c: CHIP8, ins_count: i64] : void
io.printf("??? (%x)\n", ins)
func chip8_step[c: CHIP8] : void
let high = c->memory[c->pc] as i64
let low = c->memory[c->pc + 1] as i64
high := c->memory[c->pc] as i64
low := c->memory[c->pc + 1] as i64
c->pc = c->pc + 2
let ins = (high << 8) | low
let nnn = ins & 0x0fff
let kk = ins & 0x00ff
let n = ins & 0x000f
let x = (ins >> 8) & 0x0f
let y = (ins >> 4) & 0x0f
ins := (high << 8) | low
nnn := ins & 0x0fff
kk := ins & 0x00ff
n := ins & 0x000f
x := (ins >> 8) & 0x0f
y := (ins >> 4) & 0x0f
let op = (ins >> 12) & 0x0f
op := (ins >> 12) & 0x0f
if op == 0x0
if nnn == 0x0e0
mem.zero(c->display, 64 * 32)
@@ -201,7 +201,7 @@ func chip8_step[c: CHIP8] : void
else if n == 0x3
c->reg[x] = c->reg[x] ^ c->reg[y]
else if n == 0x4
let res: u8 = c->reg[x] + c->reg[y]
res : u8 = c->reg[x] + c->reg[y]
c->reg[0xf] = (res > 0xff) as u8
c->reg[x] = res
else if n == 0x5
@@ -230,9 +230,9 @@ func chip8_step[c: CHIP8] : void
for row in 0..n
for col in 0..8
if (c->memory[c->I + row] & (0x80 >> col)) != 0
let pixel_x: u8 = (c->reg[x] + col) % 64
let pixel_y: u8 = (c->reg[y] + row) % 32
let offset = pixel_x as i64 + (pixel_y * 64)
pixel_x : u8 = (c->reg[x] + col) % 64
pixel_y : u8 = (c->reg[y] + row) % 32
offset := pixel_x as i64 + (pixel_y * 64)
if c->display[offset] == 1
c->reg[0xf] = 1
@@ -248,7 +248,7 @@ func chip8_step[c: CHIP8] : void
if kk == 0x07
c->reg[x] = c->delay_timer
else if kk == 0x0A
let key_pressed = false
key_pressed := false
while !key_pressed && !WindowShouldClose()
for i in 0..16
if IsKeyDown(array.nth(c->keyboard_map, i))
@@ -274,11 +274,11 @@ func chip8_step[c: CHIP8] : void
c->reg[i] = c->memory[c->I + i]
func main[argc: i64, argv: ptr] : i64
let path = 0 as str
let disassemble = false
path := 0 as str
disassemble := false
for i in 1..argc
let arg = mem.read64(argv + i * 8) as str
arg := mem.read64(argv + i * 8) as str
if str.equal(arg, "-d")
disassemble = true
else
@@ -288,9 +288,9 @@ func main[argc: i64, argv: ptr] : i64
io.println("Usage: chip8 -d <path>")
return 1
let c = chip8_create()
c := chip8_create()
let buffer: io.Buffer = must(io.read_binary_file?(path))
buffer : io.Buffer = must(io.read_binary_file?(path))
for i in 0..buffer->size
c->memory[0x200 + i] = buffer->data[i]

View File

@@ -3,7 +3,7 @@ func main[argc: i64, argv: ptr] : i64
io.println("ERROR: url is missing")
return 1
let url = mem.read64(argv + 8) as str
url := mem.read64(argv + 8) as str
if str.len(url) <= 7
io.println("ERROR: invalid url (Did you forget \"http://\"?)")
@@ -13,25 +13,25 @@ func main[argc: i64, argv: ptr] : i64
io.println("ERROR: only http scheme is supported")
return 1
let url_len = str.len(url)
let host_start = 7
let i = host_start
url_len := str.len(url)
host_start := 7
i := host_start
while i < url_len
if url[i] == '/'
break
i = i + 1
let host = str.substr(url, host_start, i - host_start)
let path = "/"
host := str.substr(url, host_start, i - host_start)
path := "/"
if i < url_len
path = str.substr(url, i, url_len - i)
let addr: i64 = must(net.resolve?(host))
addr : i64 = must(net.resolve?(host))
let s: i64 = must(net.connect?(addr, 80))
s : i64 = must(net.connect?(addr, 80))
// TODO: add string builder to std
let req = "GET "
req := "GET "
req = str.concat(req, path)
req = str.concat(req, " HTTP/1.0\r\nHost: ")
req = str.concat(req, host)
@@ -39,16 +39,16 @@ func main[argc: i64, argv: ptr] : i64
net.send(s, req as ptr, str.len(req))
mem.free(req)
let header_buf = mem.alloc(8192) as str
let header_size = 0
let found = false
let end_index = -1
header_buf := mem.alloc(8192) as str
header_size := 0
found := false
end_index := -1
while !found && header_size < 8192
let n = net.read(s, header_buf as ptr + header_size, 8192 - header_size)
n := net.read(s, header_buf as ptr + header_size, 8192 - header_size)
if n <= 0
break
let current_size = header_size + n
current_size := header_size + n
i = 0
while i <= current_size - 4
if header_buf[i] == '\r' && header_buf[i + 1] == '\n' && header_buf[i + 2] == '\r' && header_buf[i + 3] == '\n'
@@ -62,9 +62,9 @@ func main[argc: i64, argv: ptr] : i64
io.print_sized(header_buf as ptr + end_index, header_size as i64 - end_index)
mem.free(header_buf)
let buffer = mem.alloc(4096)
buffer := mem.alloc(4096)
while true
let n = net.read(s, buffer, 4096)
n := net.read(s, buffer, 4096)
if n <= 0
break
io.print_sized(buffer, n)

View File

@@ -1,9 +1,9 @@
func main[] : i64
let a = 0
let b = 1
a := 0
b := 1
while a < 100000
io.println_i64(a)
let temp = b
temp := b
b = a + b
a = temp

View File

@@ -1,9 +1,9 @@
func main[] : i64
let answer = math.abs(os.urandom_i64()) % 100
answer := math.abs(os.urandom_i64()) % 100
while true
io.println("Guess a number: ")
let guess = io.read_line() |> str.trim() |> str.parse_i64()
guess := io.read_line() |> str.trim() |> str.parse_i64()
if guess == answer
io.println("You win!")

View File

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

@@ -14,7 +14,7 @@ func check[lines: Array, x: i64, y: i64, dx: i64, dy: i64] : bool
return true
func part1[lines: Array] : void
let out = 0
out := 0
for x in 0..lines->size
for y in 0..str.len(array.nth(lines, x))
@@ -38,12 +38,12 @@ func part1[lines: Array] : void
io.println_i64(out)
func part2[lines: Array] : void
let out = 0
out := 0
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 = mem.alloc(5) as str
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]
@@ -56,7 +56,7 @@ func part2[lines: Array] : void
io.println_i64(out)
func main[] : i64
let lines: Array = must(io.read_text_file?("input.txt")) |> str.split("\n")
lines : Array = must(io.read_text_file?("input.txt")) |> str.split("\n")
part1(lines)
part2(lines)

View File

@@ -12,34 +12,34 @@ 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 = true
swapped := true
while swapped
swapped = false
for i in 0..update->size-1
let a: i64 = array.nth(update, i)
let b: i64 = array.nth(update, i+1)
a : i64 = array.nth(update, i)
b : i64 = array.nth(update, i+1)
if rule_exists(rules_left, rules_right, b, a)
let tmp = a
tmp := a
array.set(update, i, b)
array.set(update, i+1, tmp)
swapped = true
func part1[updates: Array, rules_left: Array, rules_right: Array] : void
let out = 0
out := 0
for i in 0..updates->size
let update: Array = array.nth(updates, i)
update : Array = array.nth(updates, i)
if check(update, rules_left, rules_right)
out = out + array.nth(update, update->size / 2)
io.println_i64(out)
func part2[updates: Array, rules_left: Array, rules_right: Array] : void
let out = 0
out := 0
for i in 0..updates->size
let update: Array = array.nth(updates, i)
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, update->size / 2)
@@ -47,23 +47,23 @@ func part2[updates: Array, rules_left: Array, rules_right: Array] : void
io.println_i64(out)
func main[] : i64
let data = must(io.read_text_file?("input.txt")) |> str.split("\n\n")
data := must(io.read_text_file?("input.txt")) |> str.split("\n\n")
let rules_left = []
let rules_right = []
let rules_lines = str.split(array.nth(data, 0), "\n")
rules_left := []
rules_right := []
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 = str.split(line, "|")
line : str = array.nth(rules_lines, i)
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 = []
let updates_lines = str.split(array.nth(data, 1), "\n")
updates := []
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 = str.split(line, ",")
line : str = array.nth(updates_lines, i)
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.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)
a_str := str.from_i64(a)
b_str := str.from_i64(b)
ab_str := str.concat(a_str, b_str)
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)
@@ -10,16 +10,16 @@ func concat[a: i64, b: i64] : i64
return out
func solve[ops: Array, e: Array] : i64
let e_1: Array = array.nth(e, 1)
let n = e_1->size - 1
let indices = []
e_1 : Array = array.nth(e, 1)
n := e_1->size - 1
indices := []
for i in 0..n
array.push(indices, 0)
while true
let res: i64 = array.nth(e_1, 0)
res : i64 = array.nth(e_1, 0)
for i in 0..n
let op: str = array.nth(ops, array.nth(indices, i))
op : str = array.nth(ops, array.nth(indices, i))
if str.equal(op, "add")
res = res + array.nth(e_1, i + 1)
@@ -30,8 +30,8 @@ func solve[ops: Array, e: Array] : i64
if res == array.nth(e, 0)
return res
let done = true
let i = n - 1
done := true
i := n - 1
while i >= 0
array.set(indices, i, array.nth(indices, i) + 1)
@@ -45,7 +45,7 @@ func solve[ops: Array, e: Array] : i64
return 0
func part1[equations: Array] : void
let out = 0
out := 0
for i in 0..equations->size
out = out + solve(["add", "mul"], array.nth(equations, i))
@@ -53,7 +53,7 @@ func part1[equations: Array] : void
io.println_i64(out)
func part2[equations: Array] : void
let out = 0
out := 0
for i in 0..equations->size
out = out + solve(["add", "mul", "concat"], array.nth(equations, i))
@@ -61,14 +61,14 @@ func part2[equations: Array] : void
io.println_i64(out)
func main[] : i64
let lines = must(io.read_text_file?("input.txt")) |> str.split("\n")
let equations = []
lines := must(io.read_text_file?("input.txt")) |> str.split("\n")
equations := []
for i in 0..lines->size
let line: str = array.nth(lines, i)
let parts = str.split(line, ": ")
line : str = array.nth(lines, i)
parts := str.split(line, ": ")
let xs = str.split(array.nth(parts, 1), " ")
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

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

View File

@@ -1,20 +1,20 @@
func part1_is_invalid_id[s: str] : bool
let len = str.len(s)
len := str.len(s)
if len % 2 != 0
return false
let first = str.substr(s, 0, len / 2)
let second = str.substr(s, len / 2, len / 2)
first := str.substr(s, 0, len / 2)
second := str.substr(s, len / 2, len / 2)
return str.equal(first, second)
func part1[ranges: Array] : void
let sum = 0
sum := 0
for i in 0..ranges->size
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()
parts := array.nth(ranges, i) |> str.split("-")
start := array.nth(parts, 0) |> str.parse_i64()
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 = str.len(s)
len := str.len(s)
if len < 2
return false
for div in 1..(len / 2 + 1)
if len % div == 0
let u = str.substr(s, 0, div)
let is_repeat = true
u := str.substr(s, 0, div)
is_repeat := true
for k in 1..(len / div)
let segment = str.substr(s, k * div, div)
segment := str.substr(s, k * div, div)
if !str.equal(segment, u)
is_repeat = false
if is_repeat
@@ -40,12 +40,12 @@ func part2_is_invalid_id[s: str] : bool
return false
func part2[ranges: Array] : void
let sum = 0
sum := 0
for i in 0..ranges->size
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()
parts := array.nth(ranges, i) |> str.split("-")
start := array.nth(parts, 0) |> str.parse_i64()
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 = must(io.read_text_file?("input.txt")) |> str.split(",")
ranges := must(io.read_text_file?("input.txt")) |> str.split(",")
part1(ranges)
part2(ranges)

View File

@@ -1,17 +1,17 @@
func part1[lines: Array] : void
let sum = 0
sum := 0
for i in 0..lines->size
let line: str = array.nth(lines, i)
line : str = array.nth(lines, i)
let largest = 0
largest := 0
for j in 0..str.len(line)
for k in (j+1)..str.len(line)
let s = mem.alloc(3) as str
s := mem.alloc(3) as str
s[0] = line[j]
s[1] = line[k]
s[2] = 0
let n = str.parse_i64(s)
n := str.parse_i64(s)
if n > largest
largest = n
@@ -20,12 +20,12 @@ 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 = start
let len = str.len(bank)
largest := 0
largest_idx := start
len := str.len(bank)
for i in (start)..(len-remaining+1)
let v = (bank[i] - '0') as i64
v := (bank[i] - '0') as i64
if v > largest
largest = v
largest_idx = i
@@ -36,16 +36,16 @@ func part2_rec[bank: str, start: i64, remaining: i64] : i64
return largest
func part2[lines: Array] : void
let sum = 0
sum := 0
for i in 0..lines->size
let line: str = array.nth(lines, i)
line : str = array.nth(lines, i)
sum = sum + part2_rec(line, 0, 12)
io.println_i64(sum)
func main[] : i64
let lines = must(io.read_text_file?("input.txt")) |> str.split("\n")
lines := must(io.read_text_file?("input.txt")) |> str.split("\n")
part1(lines)
part2(lines)

View File

@@ -1,5 +1,5 @@
func main[] : i64
let sum = 0
sum := 0
for i in 0..266000
if i % 2 != 0

View File

@@ -1,5 +1,5 @@
func main[] : i64
let sum = 0
sum := 0
for i in 0..1000
if i % 5 == 0 || i % 3 == 0

View File

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

View File

@@ -1,6 +1,6 @@
func main[] : i64
let n = 600851475143
let f = 2
n := 600851475143
f := 2
while f * f <= n
if n % f == 0

View File

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

View File

@@ -1,5 +1,5 @@
func main[] : i64
let out = 1
out := 1
for i in 1..21
out = math.lcm(out, i)

View File

@@ -1,9 +1,9 @@
func main[] : i64
let sum_of_squares = 0
sum_of_squares := 0
for i in 1..101
sum_of_squares = sum_of_squares + i * i
let square_of_sum = 0
square_of_sum := 0
for i in 1..101
square_of_sum = square_of_sum + i
square_of_sum = square_of_sum * square_of_sum

View File

@@ -1,7 +1,7 @@
func main[] : i64
let found = 0
found := 0
let i = 1
i := 1
while true
if math.is_prime(i)
found = found + 1

View File

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

View File

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

View File

@@ -1,5 +1,5 @@
func main[] : i64
let sum = 0
sum := 0
for i in 0..2000000
if math.is_prime(i)

View File

@@ -1,7 +1,7 @@
func main[] : i64
let N = 20
N := 20
let grid = []
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])
@@ -23,14 +23,14 @@ func main[] : i64
array.push(grid, [20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54])
array.push(grid, [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48])
let out = 0
out := 0
for i in 0..N-3
for j in 0..N-3
let h: i64 = array.nth(array.nth(grid, i), j) * array.nth(array.nth(grid, i), j+1) * array.nth(array.nth(grid, i), j+2) * array.nth(array.nth(grid, i), j+3)
let v: i64 = array.nth(array.nth(grid, j), i) * array.nth(array.nth(grid, j+1), i) * array.nth(array.nth(grid, j+2), i) * array.nth(array.nth(grid, j+3), i)
let d1: i64 = array.nth(array.nth(grid, i), j) * array.nth(array.nth(grid, i+1), j+1) * array.nth(array.nth(grid, i+2), j+2) * array.nth(array.nth(grid, i+3), j+3)
let d2: i64 = array.nth(array.nth(grid, i), N-j-1) * array.nth(array.nth(grid, i+1), N-j-2) * array.nth(array.nth(grid, i+2), N-j-3) * array.nth(array.nth(grid, i+3), N-j-4)
h : i64 = array.nth(array.nth(grid, i), j) * array.nth(array.nth(grid, i), j+1) * array.nth(array.nth(grid, i), j+2) * array.nth(array.nth(grid, i), j+3)
v : i64 = array.nth(array.nth(grid, j), i) * array.nth(array.nth(grid, j+1), i) * array.nth(array.nth(grid, j+2), i) * array.nth(array.nth(grid, j+3), i)
d1 : i64 = array.nth(array.nth(grid, i), j) * array.nth(array.nth(grid, i+1), j+1) * array.nth(array.nth(grid, i+2), j+2) * array.nth(array.nth(grid, i+3), j+3)
d2 : i64 = array.nth(array.nth(grid, i), N-j-1) * array.nth(array.nth(grid, i+1), N-j-2) * array.nth(array.nth(grid, i+2), N-j-3) * array.nth(array.nth(grid, i+3), N-j-4)
out = math.max(out, math.max(h, math.max(v, math.max(d1, d2))))
io.println_i64(out)

View File

@@ -1,7 +1,7 @@
func num_divisors[n: i64] : i64
let end = math.isqrt(n)
end := math.isqrt(n)
let out = 0
out := 0
for i in 1..end+1
if n % i == 0
out = out + 2
@@ -11,8 +11,8 @@ func num_divisors[n: i64] : i64
return out
func main[] : i64
let n = 0
let i = 1
n := 0
i := 1
while true
n = n + i
if num_divisors(n) > 500

View File

@@ -4,18 +4,18 @@ func collatz_num[n: i64] : i64
return n * 3 + 1
func collatz_seq[n: i64]: i64
let i = 1
i := 1
while n != 1
n = collatz_num(n)
i = i + 1
return i
func main[] : i64
let max = 0
let max_index = 0
max := 0
max_index := 0
for i in 1..1000000
let seq = collatz_seq(i)
seq := collatz_seq(i)
if seq > max
max = seq
max_index = i

View File

@@ -1,7 +1,7 @@
func main[] : i64
let n = 40
let r = 20
let out = 1
n := 40
r := 20
out := 1
for i in 1..r+1
out = out * (n - (r - i)) / i

View File

@@ -1,18 +1,18 @@
func main[] : i64
let n = []
n := []
array.push(n, 1)
for j in 0..1000
let carry = 0
carry := 0
for i in 0..n->size
let tmp: i64 = array.nth(n, i) * 2 + carry
tmp : i64 = array.nth(n, i) * 2 + carry
array.set(n, i, tmp % 10)
carry = tmp / 10
while carry > 0
array.push(n, carry % 10)
carry = carry / 10
let sum = 0
sum := 0
for i in 0..n->size
sum = sum + array.nth(n, i)

View File

@@ -1,9 +1,9 @@
func main[] : i64
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]
s1 := [0, 3, 3, 5, 4, 4, 3, 5, 5, 4]
s2 := [3, 6, 6, 8, 8, 7, 7, 9, 8, 8]
s3 := [0, 0, 6, 6, 5, 5, 5, 7, 6, 6]
let sum = 0
sum := 0
for i in 1..10
sum = sum + array.nth(s1, i)

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 = findmax(triangle, row + 1, col)
let right = findmax(triangle, row + 1, col + 1)
left := findmax(triangle, row + 1, col)
right := findmax(triangle, row + 1, col + 1)
return array.nth(array.nth(triangle, row), col) + math.max(left, right)
func main[] : i64
let triangle = []
triangle := []
array.push(triangle, [75])
array.push(triangle, [95, 64])
array.push(triangle, [17, 47, 82])

View File

@@ -10,8 +10,8 @@ func days[y: i64, m: i64] : i64
return 31
func main[] : i64
let wday = 0
let sun = 0
wday := 0
sun := 0
for year in 1901..2001
for mon in 1..13

View File

@@ -1,7 +1,7 @@
func multiply[n: Array, x: i64] : void
let carry = 0
carry := 0
for i in 0..n->size
let prod: i64 = array.nth(n, i) * x + carry
prod : i64 = array.nth(n, i) * x + carry
array.set(n, i, prod % 10)
carry = prod / 10
while carry > 0
@@ -9,12 +9,12 @@ func multiply[n: Array, x: i64] : void
carry = carry / 10
func main[] : i64
let n = [1]
n := [1]
for i in 2..101
multiply(n, i)
let sum = 0
sum := 0
for i in 0..n->size
sum = sum + array.nth(n, i)

View File

@@ -1,9 +1,9 @@
func divisors_sum[n: i64] : i64
let k = n
let sum = 1
k := n
sum := 1
for i in 2..k+1
let p = 1
p := 1
while k % i == 0
p = p * i
k = k / i
@@ -11,10 +11,10 @@ func divisors_sum[n: i64] : i64
return sum - n
func main[] : i64
let sum = 0
sum := 0
for i in 2..10000
let d = divisors_sum(i)
d := divisors_sum(i)
if i < d && i == divisors_sum(d)
sum = sum + i + d

View File

@@ -1,5 +1,5 @@
func main[] : i64
let arr = []
arr := []
for i in 0..10
array.push(arr, math.abs(os.urandom_i64()) % 1000)

View File

@@ -18,8 +18,8 @@ const WHITE = 0xffffffff
const RED = 0xff0000ff
func main[] : i64
let x = 200
let y = 200
x := 200
y := 200
InitWindow(800, 600, "Hello, World!")
SetTargetFPS(60)

View File

@@ -1,12 +1,12 @@
func rule110_step[state: Array] : Array
let new_state = []
new_state := []
for i in 0..state->size
let left = false
left := false
if i - 1 >= 0
left = array.nth(state, i - 1)
let center: bool = array.nth(state, i)
let right = false
center : bool = array.nth(state, i)
right := false
if i + 1 < state->size
right = array.nth(state, i + 1)
@@ -23,9 +23,9 @@ func print_state[state: Array]: void
io.println("")
func main[] : i64
let SIZE = 60
SIZE := 60
let state = []
state := []
for i in 0..SIZE
array.push(state, false)
array.push(state, true)

View File

@@ -11,9 +11,9 @@ extern sqlite3_column_text
extern sqlite3_finalize
func main[] : i64
let rc = 0
let db = 0
let stmt = 0
rc := 0
db := 0
stmt := 0
rc = sqlite3_open("todo.db", ^db)
if rc
@@ -30,7 +30,7 @@ func main[] : i64
io.println("0. Quit")
io.print("\n> ")
let choice = io.read_line() |> str.parse_i64()
choice := io.read_line() |> str.parse_i64()
if choice == 0
break
@@ -39,8 +39,8 @@ func main[] : i64
sqlite3_prepare_v2(db, "SELECT * FROM todo", -1, ^stmt, 0)
while sqlite3_step(stmt) == 100
let id = sqlite3_column_int(stmt, 0)
let task = sqlite3_column_text(stmt, 1)
id := sqlite3_column_int(stmt, 0)
task := sqlite3_column_text(stmt, 1)
io.print_i64(id)
io.print(" - ")
@@ -49,7 +49,7 @@ func main[] : i64
io.println("============")
else if choice == 2
io.print("\nEnter new task: ")
let task = io.read_line() |> str.trim()
task := io.read_line() |> str.trim()
sqlite3_prepare_v2(db, "INSERT INTO todo(task) VALUES (?);", -1, ^stmt, 0)
sqlite3_bind_text(stmt, 1, task, -1, 0)
@@ -59,7 +59,7 @@ func main[] : i64
io.println("\nTask added\n")
else if choice == 3
io.print("\nEnter task id: ")
let id = io.read_line() |> str.parse_i64()
id := io.read_line() |> str.parse_i64()
sqlite3_prepare_v2(db, "DELETE FROM todo WHERE id = ?;", -1, ^stmt, 0)
sqlite3_bind_int(stmt, 1, id, -1, 0)

View File

@@ -1,13 +1,13 @@
func main[] : i64
let s: i64 = must(net.listen?(net.pack_addr(127, 0, 0, 1), 8000))
s : i64 = must(net.listen?(net.pack_addr(127, 0, 0, 1), 8000))
io.println("Listening on port 8000...")
let resp = mem.alloc(60000)
resp := mem.alloc(60000)
while true
let conn = net.accept(s)
conn := net.accept(s)
if conn < 0
continue
let n = net.read(conn, resp, 60000)
n := net.read(conn, resp, 60000)
net.send(conn, resp, n)
net.close(conn)

View File

@@ -1,9 +1,9 @@
func main[] : i64
let s: net.UDPSocket = must(net.create_udp_server?(net.pack_addr(127, 0, 0, 1), 8000))
s : net.UDPSocket = must(net.create_udp_server?(net.pack_addr(127, 0, 0, 1), 8000))
io.println("Listening on port 8000...")
while true
let pkt = net.udp_receive(s, 60000)
pkt := net.udp_receive(s, 60000)
if pkt->size <= 0
net.UDPPacket.free(pkt)
continue