Paste: AoC 2022 day 8

Author: zip
Mode: factor
Date: Thu, 8 Dec 2022 16:27:04
Plain Text |
USING: arrays io.encodings.utf8 io.files kernel math math.order
math.parser prettyprint sequences math.matrices ;
IN: dec-8

: string>digits ( string -- array ) [ 1array dec> ] f 1array map-as ;

: biggest-so-far ( array -- array ) { -1 f } [ [ first ] dip [ max ] [ < ] 2bi 2array ] accumulate
  [ 1array ] dip prepend rest [ second ] map ;

: check-row ( array -- array ) [ biggest-so-far ] [ reverse biggest-so-far reverse ] bi [ or ] 2map ;

: check-rows ( array -- array ) [ check-row ] map ;

: check-columns ( array -- array ) flip check-rows flip ;

: check-all ( array -- array ) [ check-rows ] [ check-columns ] bi [ [ or ] 2map ] 2map ;

: solve1 ( array -- number ) [ string>digits ] map check-all concat sift length ;

: input ( -- array ) "/Users/zip/Desktop/aoc2022/dec-8/input.txt" utf8 file-lines ;

: part1 ( -- number ) input solve1 ;

: last-tree-index ( n seq -- n ) [ nth ] 2keep [ 1 + ] dip rot '[ drop _ >= ] find-index-from drop ;

: last-tree-indices ( seq -- seq ) dup length <iota> [ over last-tree-index ] map dup length 1 - '[ _ or ] map nip ;

: visible-tree-distances-row ( seq -- seq ) last-tree-indices dup length <iota> [ - ] 2map ;

: tree-scores ( seq -- seq ) { [ [ visible-tree-distances-row ] map ]
                                          [ [ reverse visible-tree-distances-row reverse ] map ]
                                          [ flip [ visible-tree-distances-row ] map flip ]
                                          [ flip [ reverse visible-tree-distances-row reverse ] map flip ] } cleave m* m* m* ;

: solve2 ( array -- number ) tree-scores concat supremum ;

: part2 ( -- number ) input [ string>digits ] map solve2 ;

MAIN: [ part1 . part2 . ]

New Annotation

Summary:
Author:
Mode:
Body: