Paste: AOC day 3

Author: chunes
Mode: factor
Date: Thu, 3 Dec 2020 13:23:33
Plain Text |
USING: arrays io.encodings.ascii io.files kernel literals math
math.matrices math.vectors prettyprint sequences sequences.extras ;
IN: aoc.2020.03

<< CONSTANT: input
    $[ "resource:work/aoc/2020/03/input.txt" ascii file-lines ] >>

CONSTANT: width $[ input first length ]

: trees ( fall run -- n )
    input length pick /i -rot 2array <repetition>
    { 0 0 } [ v+ first2 width mod 2array ] accumulate nip
    input matrix-nths [ CHAR: # = ] count ;

1 3 trees .   ! part1
1 1 trees 1 3 trees 1 5 trees 1 7 trees 2 1 trees * * * * .   ! part2

Annotation: Explanation

Author: chunes
Mode: factor
Date: Thu, 3 Dec 2020 13:25:55
Plain Text |
! A brief explanation of each line of trees:


! 1. Calculate how many spots we need to check for trees. 
! Build a virtual sequence of this many slopes as pairs. For 
! example, if our slope is { 1 3 } and we need to check 4 
! spots, the virtual sequence looks like
! { { 1 3 } { 1 3 } { 1 3 } { 1 3 } }.

! 2. Take the cumulative sum of the virtual sequence, but mod 
! the column by the width of the input matrix. This gives us 
! a sequence of indices we need to check for trees in the 
! input matrix.

! 3. Get the elements from the input matrix using the list of 
! indices we built. Count how many of them are octothorps.

New Annotation

Summary:
Author:
Mode:
Body: