massung
2021-12-9 14:40:39

Day 9 spoilers


massung
2021-12-9 14:41:36

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


thechairman
2021-12-9 15:50:55

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


ben.knoble
2021-12-9 15:57:27

badkins
2021-12-9 22:06:28

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


ben.knoble
2021-12-9 22:10:59

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.


badkins
2021-12-9 22:22:38

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


badkins
2021-12-9 22:24:22

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.


berkozkutuk
2021-12-9 22:48:33

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?


soegaard2
2021-12-9 23:00:32

Yes.


berkozkutuk
2021-12-9 23:12:25

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:


badkins
2021-12-9 23:30:23

@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>


ben.knoble
2021-12-9 23:38:53

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


massung
2021-12-10 00:43:58

@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) ; =&gt; 10


joel
2021-12-10 01:03:07

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


sorawee
2021-12-10 01:05:42

#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) ; =&gt; 10


sorawee
2021-12-10 01:06:37

Obviously, please don’t actually do this


massung
2021-12-10 01:10:45

puzzle != production :slightly_smiling_face:


massung
2021-12-10 01:15:19

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


samth
2021-12-10 01:58:24

You can do this more sensibly with a namespace anchor.


ben.knoble
2021-12-10 03:05:42

file-&gt;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.


massung
2021-12-10 05:55:34

Day 10 spoilers


massung
2021-12-10 05:56:09

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