this post was submitted on 04 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)

I’m not proud but after 3 hours in part 2 I had to look up help. I was trying to do it using recursion and it was not working at all.

Even after watching a couple of videos and reading code I’m not sure why part 2 works as it does 😭

I don’t normally code so this is like my yearly training 😂

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

Recursion probably made things a little trickier for you (but is totally possible). The special piece to notice in part 2 is that you are only forward creating cards and (most importantly) there are no decisions to optimize. Each card alway produces the same result.

This problem reminded me of the lantern fish problem. The trick with this one was not tracking individual fish, instead you only need the number of each generation you have (and in today's puzzle we just need to track how many of each card you have)

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

In Factor:

Here it is on GitHub with comments and imports.

: line>cards ( line -- winning-nums player-nums )
  ":|" split rest
  [
    [ CHAR: space = ] trim
    split-words harvest [ string>number ] map
  ] map first2
;

: points ( winning-nums player-nums -- n )
  intersect length
  dup 0 > [ 1 - 2^ ] when
;

: part1 ( -- )
  "vocab:aoc-2023/day04/input.txt" utf8 file-lines
  [ line>cards points ] map-sum .
;

: follow-card ( i commons -- n )
  [ 1 ] 2dip
  2dup nth swapd
  over + (a..b]
  [ over follow-card ] map-sum
  nip +
;

: part2 ( -- )
  "vocab:aoc-2023/day04/input.txt" utf8 file-lines
  [ line>cards intersect length ] map
  dup length <iota> swap '[ _ follow-card ]
  map-sum .
;
[–] [email protected] 1 points 11 months ago

My clojure solution

Ran into a couple gottchas today. Didn't notice the tabular data at first. It got me twice, once on the numbers and then again on the card number (which I guess I didn't actually need to use)

The second gottcha was missing that in the second part it is the number of matching numbers not the score that I needed to use. My first attempt was a very very very big number