port some aoc solutions to zern
This commit is contained in:
62
examples/puzzles/aoc2024_04.zr
Normal file
62
examples/puzzles/aoc2024_04.zr
Normal file
@@ -0,0 +1,62 @@
|
||||
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))
|
||||
return false
|
||||
|
||||
if array.nth(lines, x)[y] != 'X'
|
||||
return false
|
||||
if array.nth(lines, x + dx)[y + dy] != 'M'
|
||||
return false
|
||||
if array.nth(lines, x + dx * 2)[y + dy * 2] != 'A'
|
||||
return false
|
||||
if array.nth(lines, x + dx * 3)[y + dy * 3] != 'S'
|
||||
return false
|
||||
|
||||
return true
|
||||
|
||||
func part1[lines: Array] : Void
|
||||
let out: I64 = 0
|
||||
|
||||
for x in 0..array.size(lines)
|
||||
for y in 0..str.len(array.nth(lines, x))
|
||||
if check(lines, x, y, 0, 1)
|
||||
out = out + 1
|
||||
if check(lines, x, y, 0, -1)
|
||||
out = out + 1
|
||||
if check(lines, x, y, 1, 0)
|
||||
out = out + 1
|
||||
if check(lines, x, y, -1, 0)
|
||||
out = out + 1
|
||||
if check(lines, x, y, 1, 1)
|
||||
out = out + 1
|
||||
if check(lines, x, y, -1, -1)
|
||||
out = out + 1
|
||||
if check(lines, x, y, 1, -1)
|
||||
out = out + 1
|
||||
if check(lines, x, y, -1, 1)
|
||||
out = out + 1
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func part2[lines: Array] : Void
|
||||
let out: I64 = 0
|
||||
|
||||
for x in 1..array.size(lines)-1
|
||||
for y in 1..str.len(array.nth(lines, x))-1
|
||||
if array.nth(lines, x)[y] == 'A'
|
||||
let s: String = mem.alloc(5)
|
||||
str.set(s, 0, array.nth(lines, x - 1)[y - 1])
|
||||
str.set(s, 1, array.nth(lines, x + 1)[y - 1])
|
||||
str.set(s, 2, array.nth(lines, x + 1)[y + 1])
|
||||
str.set(s, 3, array.nth(lines, x - 1)[y + 1])
|
||||
str.set(s, 4, 0)
|
||||
|
||||
if str.equal(s, "MSSM") | str.equal(s, "SMMS") | str.equal(s, "MMSS") | str.equal(s, "SSMM")
|
||||
out = out + 1
|
||||
|
||||
io.println_i64(out)
|
||||
|
||||
func main[] : I64
|
||||
let lines: Array = io.read_file("input.txt") |> str.split("\n")
|
||||
|
||||
part1(lines)
|
||||
part2(lines)
|
||||
Reference in New Issue
Block a user