advent of code day 1
This commit is contained in:
@@ -17,7 +17,7 @@ func main[] : I64
|
|||||||
|
|
||||||
while true
|
while true
|
||||||
io.println("Guess a number: ")
|
io.println("Guess a number: ")
|
||||||
let guess: I64 = io.read_stdin() |> str.trim() |> str.parse_i64()
|
let guess: I64 = io.read_line() |> str.trim() |> str.parse_i64()
|
||||||
|
|
||||||
if guess == answer
|
if guess == answer
|
||||||
io.println("You win!")
|
io.println("You win!")
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ func main[] : I64
|
|||||||
|
|
||||||
while true
|
while true
|
||||||
io.println("Guess a number: ")
|
io.println("Guess a number: ")
|
||||||
let guess: I64 = io.read_stdin() |> str.trim() |> str.parse_i64()
|
let guess: I64 = io.read_line() |> str.trim() |> str.parse_i64()
|
||||||
|
|
||||||
if guess == answer
|
if guess == answer
|
||||||
io.println("You win!")
|
io.println("You win!")
|
||||||
|
|||||||
53
examples/puzzles/aoc2025_01.zr
Normal file
53
examples/puzzles/aoc2025_01.zr
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
func part1[lines: Array] : Void
|
||||||
|
let password: I64 = 0
|
||||||
|
let dial: I64 = 50
|
||||||
|
|
||||||
|
for i in 0..array.size(lines)
|
||||||
|
let line: String = array.nth(lines, i)
|
||||||
|
let dir: U8 = line[0]
|
||||||
|
let n: I64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||||
|
|
||||||
|
if dir == 'L'
|
||||||
|
dial = dial - n
|
||||||
|
while dial < 0
|
||||||
|
dial = 100 + dial
|
||||||
|
else
|
||||||
|
dial = dial + n
|
||||||
|
while dial >= 100
|
||||||
|
dial = dial - 100
|
||||||
|
|
||||||
|
if dial == 0
|
||||||
|
password = password + 1
|
||||||
|
|
||||||
|
io.println_i64(password)
|
||||||
|
|
||||||
|
func part2[lines: Array] : Void
|
||||||
|
let password: I64 = 0
|
||||||
|
let dial: I64 = 50
|
||||||
|
|
||||||
|
for i in 0..array.size(lines)
|
||||||
|
let line: String = array.nth(lines, i)
|
||||||
|
let dir: U8 = line[0]
|
||||||
|
let n: I64 = str.substr(line, 1, str.len(line) - 1) |> str.parse_i64()
|
||||||
|
|
||||||
|
if dir == 'L'
|
||||||
|
for i in 0..n
|
||||||
|
dial = dial - 1
|
||||||
|
if dial == 0
|
||||||
|
password = password + 1
|
||||||
|
if dial == -1
|
||||||
|
dial = 99
|
||||||
|
else
|
||||||
|
for i in 0..n
|
||||||
|
dial = dial + 1
|
||||||
|
if dial == 100
|
||||||
|
dial = 0
|
||||||
|
password = password + 1
|
||||||
|
|
||||||
|
io.println_i64(password)
|
||||||
|
|
||||||
|
func main[] : I64
|
||||||
|
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||||
|
|
||||||
|
part1(lines)
|
||||||
|
part2(lines)
|
||||||
@@ -2,9 +2,12 @@
|
|||||||
extern sqlite3_open
|
extern sqlite3_open
|
||||||
extern sqlite3_exec
|
extern sqlite3_exec
|
||||||
extern sqlite3_prepare_v2
|
extern sqlite3_prepare_v2
|
||||||
|
extern sqlite3_bind_int
|
||||||
extern sqlite3_bind_text
|
extern sqlite3_bind_text
|
||||||
extern sqlite3_step
|
extern sqlite3_step
|
||||||
extern sqlite3_errmsg
|
extern sqlite3_errmsg
|
||||||
|
extern sqlite3_column_int
|
||||||
|
extern sqlite3_column_text
|
||||||
extern sqlite3_finalize
|
extern sqlite3_finalize
|
||||||
|
|
||||||
func main[] : I64
|
func main[] : I64
|
||||||
@@ -20,13 +23,49 @@ func main[] : I64
|
|||||||
if rc
|
if rc
|
||||||
dbg.panic(sqlite3_errmsg(mem.read64(db)))
|
dbg.panic(sqlite3_errmsg(mem.read64(db)))
|
||||||
|
|
||||||
|
while true
|
||||||
|
io.println("1. List tasks")
|
||||||
|
io.println("2. Add task")
|
||||||
|
io.println("3. Delete task")
|
||||||
|
io.println("0. Quit")
|
||||||
|
io.print("\n> ")
|
||||||
|
|
||||||
|
let choice: I64 = io.read_line() |> str.parse_i64()
|
||||||
|
|
||||||
|
if choice == 0
|
||||||
|
break
|
||||||
|
else if choice == 1
|
||||||
|
io.println("============")
|
||||||
|
sqlite3_prepare_v2(mem.read64(db), "SELECT * FROM todo", -1, stmt, 0)
|
||||||
|
|
||||||
|
while sqlite3_step(mem.read64(stmt)) == 100
|
||||||
|
let id: I64 = sqlite3_column_int(mem.read64(stmt), 0)
|
||||||
|
let task: String = sqlite3_column_text(mem.read64(stmt), 1)
|
||||||
|
|
||||||
|
io.print_i64(id)
|
||||||
|
io.print(" - ")
|
||||||
|
io.println(task)
|
||||||
|
|
||||||
|
io.println("============")
|
||||||
|
else if choice == 2
|
||||||
|
io.print("\nEnter new task: ")
|
||||||
|
let task: String = io.read_line() |> str.trim()
|
||||||
|
|
||||||
sqlite3_prepare_v2(mem.read64(db), "INSERT INTO todo(task) VALUES (?);", -1, stmt, 0)
|
sqlite3_prepare_v2(mem.read64(db), "INSERT INTO todo(task) VALUES (?);", -1, stmt, 0)
|
||||||
|
sqlite3_bind_text(mem.read64(stmt), 1, task, -1, 0)
|
||||||
sqlite3_bind_text(mem.read64(stmt), 1, "World domination", -1, 0)
|
|
||||||
|
|
||||||
if sqlite3_step(mem.read64(stmt)) != 101
|
if sqlite3_step(mem.read64(stmt)) != 101
|
||||||
dbg.panic(sqlite3_errmsg(mem.read64(db)))
|
dbg.panic(sqlite3_errmsg(mem.read64(db)))
|
||||||
|
|
||||||
io.println("Task added!")
|
io.println("\nTask added\n")
|
||||||
|
else if choice == 3
|
||||||
|
io.print("\nEnter task id: ")
|
||||||
|
let id: I64 = io.read_line() |> str.parse_i64()
|
||||||
|
|
||||||
|
sqlite3_prepare_v2(mem.read64(db), "DELETE FROM todo WHERE id = ?;", -1, stmt, 0)
|
||||||
|
sqlite3_bind_int(mem.read64(stmt), 1, id, -1, 0)
|
||||||
|
if sqlite3_step(mem.read64(stmt)) != 101
|
||||||
|
dbg.panic(sqlite3_errmsg(mem.read64(db)))
|
||||||
|
|
||||||
|
io.println("\nTask deleted\n")
|
||||||
|
|
||||||
sqlite3_finalize(mem.read64(stmt))
|
sqlite3_finalize(mem.read64(stmt))
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ impl Analyzer {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// TODO: cant error here since we dont analyze externs/builtins
|
// TODO: cant error here since we dont analyze externs/builtins YET
|
||||||
}
|
}
|
||||||
|
|
||||||
for arg in args {
|
for arg in args {
|
||||||
|
|||||||
@@ -254,12 +254,15 @@ _builtin_environ:
|
|||||||
|
|
||||||
self.compile_stmt(env, *body)?;
|
self.compile_stmt(env, *body)?;
|
||||||
|
|
||||||
// fallback to returning null
|
// fallback to null
|
||||||
|
// very hacky but works
|
||||||
|
if !self.output.trim_end().ends_with(" ret") {
|
||||||
emit!(&mut self.output, " mov rax, 0");
|
emit!(&mut self.output, " mov rax, 0");
|
||||||
emit!(&mut self.output, " mov rsp, rbp");
|
emit!(&mut self.output, " mov rsp, rbp");
|
||||||
emit!(&mut self.output, " pop rbp");
|
emit!(&mut self.output, " pop rbp");
|
||||||
emit!(&mut self.output, " ret");
|
emit!(&mut self.output, " ret");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Stmt::Return(expr) => {
|
Stmt::Return(expr) => {
|
||||||
self.compile_expr(env, expr)?;
|
self.compile_expr(env, expr)?;
|
||||||
emit!(&mut self.output, " mov rsp, rbp");
|
emit!(&mut self.output, " mov rsp, rbp");
|
||||||
|
|||||||
10
src/std.zr
10
src/std.zr
@@ -65,7 +65,7 @@ func io.read_char[] : U8
|
|||||||
mem.free(s)
|
mem.free(s)
|
||||||
return c
|
return c
|
||||||
|
|
||||||
func io.read_stdin[]: String
|
func io.read_line[]: String
|
||||||
let buffer: String = mem.alloc(1025)
|
let buffer: String = mem.alloc(1025)
|
||||||
let n: I64 = _builtin_syscall(0, 0, buffer, 1024)
|
let n: I64 = _builtin_syscall(0, 0, buffer, 1024)
|
||||||
if n < 0
|
if n < 0
|
||||||
@@ -74,7 +74,7 @@ func io.read_stdin[]: String
|
|||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
func io.read_file[path: String]: String
|
func io.read_file[path: String]: String
|
||||||
let fd: I64 = _builtin_syscall(2, path, 0, 0) // open
|
let fd: I64 = _builtin_syscall(257, -100, path, 0, 0) // openat
|
||||||
if fd <= 0
|
if fd <= 0
|
||||||
dbg.panic("failed to open file")
|
dbg.panic("failed to open file")
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@ func io.read_file[path: String]: String
|
|||||||
return buffer
|
return buffer
|
||||||
|
|
||||||
func io.write_file[path: String, content: String] : Void
|
func io.write_file[path: String, content: String] : Void
|
||||||
let fd: Ptr = _builtin_syscall(2, path, 0x241, 0o644) // open
|
let fd: Ptr = _builtin_syscall(257, -100, path, 0x241, 0o644) // openat
|
||||||
if fd < 0
|
if fd < 0
|
||||||
dbg.panic("failed to open file")
|
dbg.panic("failed to open file")
|
||||||
|
|
||||||
@@ -415,7 +415,7 @@ func os.exit[code: I64] : Void
|
|||||||
|
|
||||||
func os.urandom[]: I64
|
func os.urandom[]: I64
|
||||||
let buffer: Ptr = mem.alloc(8)
|
let buffer: Ptr = mem.alloc(8)
|
||||||
let fd: I64 = _builtin_syscall(2, "/dev/urandom", 0, 0) // open
|
let fd: I64 = _builtin_syscall(257, -100, "/dev/urandom", 0, 0) // openat
|
||||||
_builtin_syscall(0, fd, buffer, 8) // read
|
_builtin_syscall(0, fd, buffer, 8) // read
|
||||||
_builtin_syscall(3, fd) // close
|
_builtin_syscall(3, fd) // close
|
||||||
let n: I64 = mem.read64(buffer)
|
let n: I64 = mem.read64(buffer)
|
||||||
@@ -452,7 +452,7 @@ func os.shell[command: String] : I64
|
|||||||
return -(st & 0x7f)
|
return -(st & 0x7f)
|
||||||
|
|
||||||
func os.listdir[path: String] : Array
|
func os.listdir[path: String] : Array
|
||||||
let fd: I64 = _builtin_syscall(2, path, 0, 0) // open
|
let fd: I64 = _builtin_syscall(257, -100, path, 0, 0) // openat
|
||||||
if fd < 0
|
if fd < 0
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user