keith047
2020-12-10 17:56:37

or you could something simple.


me1890
2020-12-10 17:57:18

yeah, I just did dynamic programming with a vector


keith047
2020-12-10 17:58:09

keith047
2020-12-10 17:59:23

Memoization is the cheater’s DP :stuck_out_tongue:


me1890
2020-12-10 17:59:40

it is cheating :stuck_out_tongue:


me1890
2020-12-10 17:59:48

can you do it in O(1) space now?


me1890
2020-12-10 17:59:50

i did


keith047
2020-12-10 18:01:27

I did something like that in JS, but not constant space.


keith047
2020-12-10 18:01:40

Just no need. n ~ 100.


me1890
2020-12-10 18:02:50

I saw on reddit, someone made an improved puzzle input that was much larger so people couldn’t cheat with memoization


keith047
2020-12-10 18:03:20

I don’t know what you mean - that it’s so big that the memo would OOM?


me1890
2020-12-10 18:03:20

i think the result was something like 40k digits long, and you just needed to find the last 10 digits


me1890
2020-12-10 18:03:54

not necesarily that it would oom, but it would be very inefficient to use all that extra space


keith047
2020-12-10 18:04:36

meh. Memoization’s not cheating in real life. It’s equivalent to DP.


me1890
2020-12-10 18:04:47

true


me1890
2020-12-10 18:05:21

but sometimes it’s just fun to compete for a more optimal solution


me1890
2020-12-10 18:05:26

that’s all


keith047
2020-12-10 18:05:31

sure


keith047
2020-12-10 18:06:09

which is why my part 1 is overdone :slightly_smiling_face: There’s just no need for more than one pass, but the simple way is more concise.


me1890
2020-12-10 18:06:23

more than one pass?


keith047
2020-12-10 18:06:33

through the list


me1890
2020-12-10 18:06:45

don’t you just sort the list then take the differences then count the ones and threes?


keith047
2020-12-10 18:06:59

well, sort is one pass.


me1890
2020-12-10 18:07:14

i suppose


keith047
2020-12-10 18:07:19

but if you take the difference, that’s another pass through the list


keith047
2020-12-10 18:07:29

and each count is another pass through the list.


keith047
2020-12-10 18:07:52

They’re all linear (except I guess sorting). And it just doesn’t matter.


me1890
2020-12-10 18:08:00

if you use count it is, but you can also count it while you do the subtractions


me1890
2020-12-10 18:08:15

also, the sorting can be linear because you can use counting sort algo


keith047
2020-12-10 18:55:55

actually, if you have a memoization macro, part 2 is basically a one-liner: (define/memo part2 (define (reachable? x) (< n x (+ n 4))) (max 1 (apply + (map part2 (filter reachable? input)))))


racket192
2020-12-10 20:40:42

OMG I felt like an idiot. It wasn’t until I saw Peter Norvig’s solution that the lightbulb went off.


badkins
2020-12-10 23:09:06

FYI - for everyone doing (map string->number (file->lines "input")) as I was, you can do (file->list "input") instead. Thanks to @samth for the idea.


badkins
2020-12-10 23:11:39

I did not like this one :) I could sense there was an elegant solution that I wasn’t coming up with, so I just did memoization and called it a day :) I’ll have to take a look at the better solutions.


badkins
2020-12-10 23:14:36

It seems like there is a mathematical solution that doesn’t require dynamic programming or memoization.


badkins
2020-12-10 23:16:52

@racket192 is a Racket fan? That’s awesome :)


badkins
2020-12-10 23:31:42

This <https://github.com/tckmn/polyaoc–2020/blob/master/10/jl/10.jl|Julia version> is pretty nice.


badkins
2020-12-10 23:45:44

And a similarly elegant <https://github.com/tckmn/polyaoc–2020/blob/master/10/rb/10.rb|Ruby version> - I feel like a hack after viewing those!


winny
2020-12-11 00:41:13

I like (port-&gt;list read) but it’s not line delimited


samth
2020-12-11 03:23:08

port->list and read are what file->list is doing