Files
zern/examples/puzzles/aoc2024_04.zr
2026-06-06 22:29:25 +02:00

68 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 >= (lines->nth(0) as str)->len()
return false
if lines->nth(x)[y] != 'X'
return false
if lines->nth(x + dx)[y + dy] != 'M'
return false
if lines->nth(x + dx * 2)[y + dy * 2] != 'A'
return false
if lines->nth(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..(lines->nth(x) as str)->len()
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
line_len := (lines->nth(x) as str)->len()
for y in 1..line_len-1
if lines->nth(x)[y] == 'A'
s[0] = lines->nth(x - 1)[y - 1]
s[1] = lines->nth(x + 1)[y - 1]
s[2] = lines->nth(x + 1)[y + 1]
s[3] = lines->nth(x - 1)[y + 1]
s[4] = 0
if s->equal("MSSM") || s->equal("SMMS") || s->equal("MMSS") || s->equal("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 := input->split("\n")
part1(lines)
part2(lines)