Files
zern/examples/puzzles/aoc2024_04.zr
2026-06-06 19:36:28 +02:00

67 lines
1.9 KiB
Plaintext

func check[lines: Array, x: i64, y: i64, dx: i64, dy: i64] : bool
if x + dx * 3 < 0 || x + dx * 3 >= lines->size || 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
out := 0
for x in 0..lines->size
for y in 0..str.len(array.nth(lines, x))
if check(lines, x, y, 0, 1)
out += 1
if check(lines, x, y, 0, -1)
out += 1
if check(lines, x, y, 1, 0)
out += 1
if check(lines, x, y, -1, 0)
out += 1
if check(lines, x, y, 1, 1)
out += 1
if check(lines, x, y, -1, -1)
out += 1
if check(lines, x, y, 1, -1)
out += 1
if check(lines, x, y, -1, 1)
out += 1
io.println_i64(out)
func part2[lines: Array] : void
out := 0
s := _stackalloc(5) as str
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'
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]
s[3] = array.nth(lines, x - 1)[y + 1]
s[4] = 0
if str.equal(s, "MSSM") || str.equal(s, "SMMS") || str.equal(s, "MMSS") || str.equal(s, "SSMM")
out += 1
io.println_i64(out)
func main[] : i64
~input, ok := io.read_text_file("input.txt")
if !ok
panic("failed to open input.txt")
lines := str.split(input, "\n")
part1(lines)
part2(lines)