this post was submitted on 03 Dec 2023
6 points (100.0% liked)

Advent of Code

264 readers
1 users here now

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.

https://adventofcode.com

founded 1 year ago
MODERATORS
top 4 comments
sorted by: hot top controversial new old
[–] [email protected] 2 points 11 months ago (1 children)

Python

This was honestly the “easiest” day so far for me.

I’ve worked a lot with 2d array processing in my graduate days (studying image processing and computer graphics) so I had an idea what to do and I didn’t run in to any weird edge cases.

[–] [email protected] 1 points 11 months ago

@drudoo @hal9001 I agree, the 2nd day's exercice was the most complex of the 4 I think

[–] [email protected] 1 points 11 months ago* (last edited 10 months ago)

In Factor:

Here it is on GitHub with comments and imports.

: symbol-indices ( line -- seq )
  [ ".0123456789" member? not ] find-all [ first ] map
;

: num-spans ( line -- seq )
  >array [ over digit? [ nip ] [ 2drop f ] if ] map-index
  { f } split harvest
  [ [ first ] [ last ] bi 2array ] map
;

: adjacent? ( num-span symbol-indices -- ? )
  swap [ first 1 - ] [ last 1 + ] bi [a,b]
  '[ _ interval-contains? ] any?
;

: part-numbers ( line nearby-symbol-indices -- seq )
  [ dup num-spans ] dip
  '[ _ adjacent? ] filter
  swap '[ first2 1 + _ subseq string>number ] map
;

: part1 ( -- )
  "vocab:aoc-2023/day03/input.txt" utf8 file-lines
  [ [ symbol-indices ] map ] keep
  [
    pick swap [ 1 - ?nth-of ] [ nth-of ] [ 1 + ?nth-of ] 2tri
    3append part-numbers sum
  ] map-index sum nip .
;

: star-indices ( line -- seq )
  [ CHAR: * = ] find-all [ first ] map
;

: gears ( line prev-line next-line -- seq-of-pairs )
  pick star-indices
  [ 1array '[ _ part-numbers ] [ 3dup ] dip tri@ 3append ]
  [ length 2 = ] map-filter [ 3drop ] dip
;

: part2 ( -- )
  "vocab:aoc-2023/day03/input.txt" utf8 file-lines
  dup [
    pick swap [ 1 - ?nth-of ] [ 1 + ?nth-of ] 2bi
    gears [ product ] map-sum
  ] map-index sum nip .
;
[–] [email protected] 1 points 11 months ago

My clojure solution

That was a fun one to parse. Needed to leak some mutability into my code using the raw Java Matcher as there wasn't another way to get the indexes of the matches in the string. Luckily it's contained within a fn so not too bad :D