this post was submitted on 04 Dec 2024
18 points (95.0% liked)

Advent Of Code

996 readers
2 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 4: Ceres Search

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] Andy 3 points 1 month ago* (last edited 1 month ago)

Factor

spoiler

: get-input ( -- rows )
  "vocab:aoc-2024/04/input.txt" utf8 file-lines ;

: verticals ( rows -- lines )
  [ dimension last [0..b) ] keep cols ;

: slash-origins ( rows -- coords )
  dimension
  [ first [0..b) [ 0 2array ] map ] [
    first2 [ 1 - ] [ 1 (a..b] ] bi*
    [ 2array ] with map
  ] bi append ;

: backslash-origins ( rows -- coords )
  dimension first2
  [ [0..b) [ 0 2array ] map ]
  [ 1 (a..b] [ 0 swap 2array ] map ] bi* append ;

: slash ( rows origin -- line )
  first2
  [ 0 [a..b] ]
  [ pick dimension last [a..b) ] bi* zip
  swap matrix-nths ;

: backslash ( rows origin -- line )
  [ dup dimension ] dip first2
  [ over first [a..b) ]
  [ pick last [a..b) ] bi* zip nip
  swap matrix-nths ;

: slashes ( rows -- lines )
  dup slash-origins
  [ slash ] with map ;

: backslashes ( rows -- lines )
  dup backslash-origins
  [ backslash ] with map ;

: word-count ( line word -- n )
  dupd [ reverse ] dip
  '[ _ subseq-indices length ] bi@ + ;

: part1 ( -- n )
  get-input
  { [ ] [ verticals ] [ slashes ] [ backslashes ] } cleave-array concat
  [ "XMAS" word-count ] map-sum ;

: origin-adistances ( rows origins line-quot: ( rows origin -- line ) -- origin-adistances-assoc )
  with zip-with
  "MAS" "SAM" [ '[ [ _ subseq-indices ] map-values ] ] bi@ bi append
  harvest-values
  [ [ 1 + ] map ] map-values ; inline

: a-coords ( origin-adistances coord-quot: ( adistance -- row-delta col-delta ) -- coords )
  '[ first2 [ @ 2array v+ ] with map ] map-concat ; inline

: slash-a-coords ( rows -- coords )
  dup slash-origins [ slash ] origin-adistances
  [ [ 0 swap - ] keep ] a-coords ;

: backslash-a-coords ( rows -- coords )
  dup backslash-origins [ backslash ] origin-adistances
  [ dup ] a-coords ;

: part2 ( -- n )
  get-input [ slash-a-coords ] [ backslash-a-coords ] bi
  intersect length ;

Better viewed on GitHub.