advent-of-code/2023/day10/day10.bqn
2024-11-12 21:46:18 +01:00

57 lines
2.4 KiB
BQN
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

nmgrid>•FLines"input"
Dirs"|-LJ7F"=/¯10,10,0¯1,01,¯10,01,¯10,0¯1,0¯1,10,01,10˙
Advance{
'S'=(𝕩)grid ? 𝕩 ;
nextpos(𝕩)+(-´2𝕩)¨/Dirs(𝕩)grid
nextpos𝕩
}
sposm(÷˜|)/grid='S'
startspos+01,spos # works for my input
pathAdvance(n×m)start
•Show 2÷˜1-˜path
# First method, using the parity of the number of vertical pipes crossed on each side
# Only takes into account the ones connecting above, because "FJ" is equivalent to a single "|"
pathmask1+(path) nm0
cleangrid'.'˙¨((¬pathmask)(/))grid
inmask(¬pathmask)(m){´2|+´¨"JL|"¨𝕨()𝕩}<˘cleangrid
•Show +´inmask
# Second method for part 2, using Pick's theorem and the shoelace formula
# https://en.wikipedia.org/wiki/Pick%27s_theorem
# https://en.wikipedia.org/wiki/Shoelace_formula#Shoelace_formula
# Area inside the polygon, using the shoelace formula (determinant)
a|2÷˜-´+˝(˘1)×>path
# Number of points in the boundary = length of the path
bpath
# Pick's formula to compute the number of points inside the polygon
•Show a+1-b÷2
## Display the path in the terminal as Unicode box drawing characters
# ReplaceChars←⊑(=⟜"|-FJL7.S"/"┃━┏┛┗┓ █"˙)
# •Out ⥊∾⟜(@+10)˘ReplaceChars¨cleangrid
## Generate an image of the path and its interior
# ⟨PBM,PPM⟩←•Import"../../bqn-graphics/pnm.bqn"
# pixels←3‿3⊸⥊¨⟨
# 9⥊0‿1‿0,
# 0‿0‿0‿1‿1‿1‿0‿0‿0,
# 0‿0‿0‿0‿1‿1‿0‿1‿0,
# 0‿1‿0‿1‿1‿0‿0‿0‿0,
# 0‿1‿0‿0‿1‿1‿0‿0‿0,
# 0‿0‿0‿1‿1‿0‿0‿1‿0,
# 9⥊0,
# 9⥊1,
# 9⥊1,
# ⟩
# ReplaceCharsPixels←⊑(=⟜"|-FJL7.SI"/pixels˙)
# •Out PBM ¬∾˝∾˘´˘ReplaceCharsPixels¨ 'I'˙¨⌾(inmask⊸(/○⥊)) cleangrid
## With color representing the distance from the start
# ⟨Inferno⟩←•Import"../../bqn-graphics/colormaps.bqn"
# pixgrid←ReplaceCharsPixels¨cleangrid
# coloroffset←1500
# colors←⌊255×Inferno¨(coloroffset+2÷˜≠path)÷˜+´path{𝕩⊸+⌾(𝕨⊸⊑)n‿m⥊0}¨coloroffset+⌊˝(⌽≍⊢)↕≠path
# •Out PPM >∾˝∾˘´˘colors{𝕨⊸ר𝕩}¨pixgrid