berkozkutuk
2021-12-10 13:44:47

this looks interesting, but I don’t know what reader is. Is this the right thing I am looking for? https://docs.racket-lang.org/guide/hash-lang_reader.html


massung
2021-12-10 14:32:28

https://lisper.in/reader-macros — This is Common Lisp, but a good example of how powerful it is. You basically have access to the lexer at compile-time and can modify it to do other things at “read time”.

With Racket take a look at https://docs.racket-lang.org/reference/reader.html. Racket uses the #lang to let you completely override the entire reader with one of your own. Think of it as a very powerful lex/yacc that converts whatever your language is back into valid Racket code.


joel
2021-12-10 15:05:33

For Racket I really feel like there ought to be a very easy way to do this with a custom readtable and reading exception messages, but getting it so that < matches only > so far seems to make it more trouble than it’s worth


joel
2021-12-10 15:08:51

I’ve never used readtables before though so I might be missing something.


massung
2021-12-10 15:24:48

Not sure how far you’ve gotten (or finished), but I think w/ part 2 the read table would likely be more trouble than it’s worth.


joel
2021-12-10 15:26:28

yeah, haven’t done part 2 yet. shame though, seems like it’d be extra lispy just to have the reader do it all!



ben.knoble
2021-12-10 17:24:55

I tried the readtable first @joel but I couldn’t get the information I wanted out of exceptions when reading failed because of the imbalance.


ben.knoble
2021-12-10 17:25:04

I still have half-baked code if you want it


badkins
2021-12-10 21:30:59

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


massung
2021-12-10 22:00:50

@badkins wouldn’t https://github.com/lojic/LearningRacket/blob/master/advent-of-code-2021/solutions/day10/day10.rkt#L7 allocate a new hash every single iteration of the loop as opposed to #hash ?


massung
2021-12-10 22:01:37

not sure if racket would realize everything in is constant and work it out. if so, that’s awesome


badkins
2021-12-10 22:02:13

Yes, you’re probably right :-o I usually don’t worry about such things when in puzzle mode, but in this case, there’s no reason to not use a literal - thanks!


badkins
2021-12-10 22:02:41

Of course, there are many things I’d change about it if in production code :)


ben.knoble
2021-12-10 22:02:48

Yeah, depends on the optimizations. Sometimes extracting those myself makes a difference in time, at least, which I assume is correlated with memory usage in these particular cases


massung
2021-12-10 22:03:05

oh for sure. i was asking for myself to just know whether or not that’s what racket would do


badkins
2021-12-10 22:05:12

Hmm… using #hash requires cons pairs which lengthens the line and makes it less clear, so for “puzzle mode”, I think I’ll stick with (hash)


badkins
2021-12-10 22:05:57

I am curious about possible optimizations - my guess would be “no”, but it’s totally a guess.


badkins
2021-12-10 22:06:51

I’m going to make a more readable version, defining those hashes separately will make sense.


massung
2021-12-10 22:07:32

I suppose #hash is immutable vs. mutable hash, so already probably not


ben.knoble
2021-12-10 22:12:06

hash is immutable. make-hash is mutable


badkins
2021-12-10 22:45:58

Here’s a (hopefully) <https://github.com/lojic/LearningRacket/blob/master/advent-of-code–2021/solutions/day10/day10-readable.rkt|more readable version>


massung
2021-12-11 05:15:08

Day 11 spoilers


massung
2021-12-11 05:15:31

Another fairly simple day. A couple potential gotchyas.

https://github.com/massung/advent2021/blob/main/day11/day11.lisp