Boxcar
; Day 3: Foggy Forest
; Every tree, as `{row col}`, top to bottom and left to right.
[ define trees
[ train input [ lines ] [ enumerate ]
[ map [ fn [{ row line }]
[ train [ indices-of line "#" ] [ map [ fn [ col ] { row col }]]]]]
[ concat ]]]
; Every pair of trees that share a row, as `{row left right}`.
;
; Each one could be the top edge of a square.
[ define spans
[ train input [ lines ] [ enumerate ]
[ map [ fn [{ row line }]
[ train [ indices-of line "#" ] [ pairs ]
[ map [ fn [{ left right }] { row left right }]]]]]
[ concat ]]]
[ part 1
; A span is the top edge of a square exactly when the span directly below
; it, as far down as the span is wide, is also in `spans`: that one is the
; bottom edge.
[ train spans
[ map [ fn [{ row left right }] {[ + row [ - right left ]] left right }]]
[ intersect spans ]
[ count ]]]
; Every pair of trees, as `{from to}` with `from` earlier in `trees` than
; `to`. Each one could be an edge of a square.
[ define edges [ pairs trees ]]
; `edge` moved `dr` rows down and `dc` columns right.
[ define shift
[ fn [ dr dc {{ r1 c1 } { r2 c2 }}]
{{[ + r1 dr ] [ + c1 dc ]} {[ + r2 dr ] [ + c2 dc ]}}]]
; The edge that would be opposite this one in a square: the edge moved by its
; own length, a quarter turn clockwise from the way it points.
;
; For a span (pointing right), that's straight down, as in part 1.
[ define opposite
[ fn [{{ r1 c1 } { r2 c2 }}]
[ shift [ - c2 c1 ] [ - r1 r2 ] {{ r1 c1 } { r2 c2 }}]]]
[ part 2
; An edge is a side of a square exactly when its opposite is an edge too.
;
; That finds each square twice: the two edges of each pair of parallel
; sides point the same way, and the square is clockwise of just one of them.
[ train edges [ map opposite ] [ intersect edges ] [ count ] [ fn [ n ] [ / n 2 ]]]]
[ tests
[ example puzzle [ include "input.txt" ] [ part 1 309 ] [ part 2 1321 ]]
[ example corners "
#...#
.....
.....
.....
#...#
" [ part 1 1 ] [ part 2 1 ]]
[ example offset "
.#..#
.....
.....
.#..#
.....
" [ part 1 1 ]]
[ example tiny "
.....
.....
.##..
.##..
.....
" [ part 1 1 ]]
[ example crowded "
#.###
.##.#
##.##
.#.#.
.##.#
" [ part 1 1 ] [ part 2 5 ]]
[ example nested "
#..#
.##.
.##.
#..#
" [ part 1 2 ]]
[ example shared "
#...#
.....
..#.#
.....
#.#.#
" [ part 1 2 ]]
[ example overlapping "
#...#..
.......
..#...#
.......
#...#..
.......
..#...#
" [ part 1 2 ]]
[ example scattered "
.##..
.##.#
.....
#.##.
..##.
" [ part 1 2 ]]
[ example lattice "
#.#.#
.....
#.#.#
.....
#.#.#
" [ part 1 5 ]]
[ example full "
#####
#####
#####
#####
#####
" [ part 1 30 ]]
[ example diamond "
..#..
.....
#...#
.....
..#..
" [ part 2 1 ]]
[ example tilted "
.#...
....#
.....
#....
...#.
" [ part 2 1 ]]
[ example leaning "
....#..
.......
#......
.......
......#
.......
..#....
" [ part 2 1 ]]
[ example grid "
#..#..#
.......
.......
#..#..#
.......
.......
#..#..#
" [ part 2 6 ]]
[ example woven "
..#....
....#..
.#....#
...#...
#....#.
..#....
....#..
" [ part 2 6 ]]
[ example forest "
#######
#######
#######
#######
#######
#######
#######
" [ part 2 196 ]]]