day 4 puzzle 1
This commit is contained in:
parent
66f04ed889
commit
65beed1c51
BIN
progress.png
BIN
progress.png
Binary file not shown.
Before Width: | Height: | Size: 110 KiB After Width: | Height: | Size: 110 KiB |
@ -3,4 +3,6 @@
|
||||
2:
|
||||
stars: 2
|
||||
3:
|
||||
stars: 2
|
||||
stars: 2
|
||||
4:
|
||||
stars: 1
|
10
res/examples/day4.txt
Normal file
10
res/examples/day4.txt
Normal file
@ -0,0 +1,10 @@
|
||||
MMMSXXMASM
|
||||
MSAMXMSMSA
|
||||
AMXSXMAAMM
|
||||
MSAMASMSMX
|
||||
XMASAMXAMM
|
||||
XXAMMXXAMA
|
||||
SMSMSASXSS
|
||||
SAXAMASAAA
|
||||
MAMMMXMMMM
|
||||
MXMXAXMASX
|
57
src/day4/puzzle1.typ
Normal file
57
src/day4/puzzle1.typ
Normal file
@ -0,0 +1,57 @@
|
||||
#import "/src/utils.typ": *
|
||||
|
||||
#let check-xmas(lines, ox, oy) = {
|
||||
let w = lines.first().len()
|
||||
let h = lines.len()
|
||||
|
||||
let total = 0
|
||||
for dy in (-1, 0, 1) {
|
||||
for dx in (-1, 0, 1) {
|
||||
if dx == 0 and dy == 0 {
|
||||
continue
|
||||
}
|
||||
let buffer = ""
|
||||
let x = ox
|
||||
let y = oy
|
||||
for i in range(4) {
|
||||
buffer += lines.at(y).at(x)
|
||||
x += dx
|
||||
y += dy
|
||||
if (
|
||||
not "XMAS".starts-with(buffer) or
|
||||
x < 0 or x >= w or
|
||||
y < 0 or y >= h
|
||||
) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if buffer == "XMAS" {
|
||||
total += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
#let solve(input) = {
|
||||
let lines = input.split("\n")
|
||||
let w = lines.first().len()
|
||||
let h = lines.len()
|
||||
|
||||
let total = 0
|
||||
for y in range(h) {
|
||||
for x in range(h) {
|
||||
if lines.at(y).at(x) == "X" {
|
||||
total += check-xmas(lines, x, y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
#show-puzzle(
|
||||
4, 1,
|
||||
solve,
|
||||
example: 18
|
||||
)
|
0
src/day4/puzzle2.typ
Normal file
0
src/day4/puzzle2.typ
Normal file
BIN
src/main.pdf
BIN
src/main.pdf
Binary file not shown.
Loading…
Reference in New Issue
Block a user