
Day 9 spoilers

A nice, easier day after Day 8. Code is a bit longer, but easier. Anyone who’s has to write a floodfill
before got this one pretty quick.
https://github.com/massung/advent2021/blob/main/day9/day9.lisp

I fussed around a little on paper with trying to figure out how to identify local minima before realizing that it’s just a flood fill, yeah

https://github.com/benknoble/advent2021/blob/main/day9/solution.rkt Doing the fill with Qi was fun

<https://github.com/lojic/LearningRacket/blob/master/advent-of-code–2021/solutions/day09/day09.rkt|Day 9 Solution>

Nice use of remove duplicates; I was trying to come up with a better way to handle that then the typical “set of seen coordinates” approach.

Thanks. I’m relatively pleased with the code except for the repeated use of dealing with north/east/south/west coordinates.

I may create a macro for each of those, and then: (north get x y)
and (north flood x y h)
- not sure about that though.

Hey! This year I am trying to solve AoC problems with Racket and I have just solved day 2. Would this be the right place to ask for a code review?

Yes.

I have pushed my solution here: https://github.com/ozkutuk/aoc21/blob/master/day2.rkt
I use Haskell in my day-to-day programming and I feel like I try to use Racket pretty much the same way. I would love to learn if there are more “idiomatic Racket” alternatives to parts of my solution :smile:

@berkozkutuk since it’s late for Day 2, asking in the main channel is fine. If you start getting caught up, we have a thread for each day where people can put spoilers, so that people who haven’t coded a solution yet can avoid them :) It might help if you have specific questions re: your code review. As one comparison for day 2, here’s <https://github.com/lojic/LearningRacket/blob/master/advent-of-code–2021/solutions/day02/day02-min.rkt|my solution>

If you like haskell, you might like the qi package. It has some resemblances to point-free programming, though not entirely.

@berkozkutuk - one thing awesome about Lisp is the ability to use the reader instead of parsing and comparing. For example (Common Lisp, someone here can easily post the Racket equiv)…
(setq x 0)
(defun forward (n)
(incf x n))
(with-input-from-string (p "forward 10")
(let ((f (read p))
(n (read p)))
(funcall f n)))
(print x) ; => 10

Does Racket have anything like funcall
to call a function at run time by using a symbol?

#lang racket/load
(define x 0)
(define (forward n)
(set! x (+ x n)))
(with-input-from-string "forward 10"
(λ ()
(let ([f (read)]
[n (read)])
(eval (list f n)))))
(print x) ; => 10

Obviously, please don’t actually do this

puzzle != production :slightly_smiling_face:

anyway, often times for these puzzles, instead of read-line, split by whitespace, etc. you can just read line then (read)
all the values much easier

You can do this more sensibly with a namespace anchor.

file->list
uses read to produce a list of read
values, but that proc can be overwritten (so for example I have several times used a little helper called read-ignore-comma that reads the file 1,2
as the list '(1 2)
rather than '(1 ,2)
. It does that by simply matching the result of (read)
and, when it’s ,x
, returning ',x
, passing everything else through.). Very handy.

Day 10 spoilers

Another pretty simple day… and kinda feels like a shout-out to us lispniks, too! :wink:
https://github.com/massung/advent2021/blob/main/day10/day10.lisp