Files
zern/examples/puzzles/aoc2024_04.zr

62 lines
2.0 KiB
Plaintext

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 = 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 = 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: str = 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)