adventofcode

Happy Advent 2020! Please put answers in the pinned threads or create one if it does not exist yet. | https://github.com/adventofcode-clojurians/adventofcode-clojurians | Join the private leaderboard with code 217019-4a55b8eb
neeasade 2020-12-05T04:20:27.267500Z

thanks for mentioning re-seq, I remember when I first encountered it and then suddenly I use it literally everywhere. Another tip: with destructuring, you get really smooth group matches to bound names: EG from my day2 of Advent:

(let [[_ lower upper letter pass] (first (re-seq #"([0-9]+)-([0-9]+) ([a-zA-Z]): (.+)" line))]
  ;; do stuff with lower, upper, letter, pass
  )

kenj 2020-12-05T05:06:41.267900Z

@neeasade If you use re-matches you donโ€™t even need the first:

(let [[_ pmin pmax pc password]
      (re-matches #"(\d+)-(\d+) ([a-z]): ([a-z]+)" s)])

2020-12-05T05:27:46.268200Z

Good morning

๐Ÿ‘‹ 5
fingertoe 2020-12-05T05:51:50.269900Z

Day 5 probably would have been a lot easier if I had carefully read the problem.. Not too bad though..

rjray 2020-12-05T05:52:00.270100Z

Day 5 done. I found the wording of part 2 to be very confusing. Lost time trying to figure out WTF was being asked for. Once I understood, the solution was pretty simple.

mchampine 2020-12-05T05:54:09.270200Z

;; part 1
(def input (str/split-lines (slurp "resources/day05.data")))
(def fblrbits {\F 0 \B 1 \L 0 \R 1})

(defn seatcode [v]
  (-> (apply str (map fblrbits v)) (Integer/parseInt 2)))

(def codes (map seatcode input))
(apply max codes)

;; part 2
(let [dc (partition 2 1 (sort codes))]
  (inc (ffirst (filter (fn [[a b]] (= 2 (- b a))) dc))))
Explanation: Part 1: FBLR code is just binary. Convert to 1's and 0's and then to base 10. Part 2, Youโ€™re looking for a gap. Sort the codes and make pairs of (code, nextcode). Where that difference is 2, your seat number is in-between them, so add one to the first value.

๐Ÿ‘ 2
๐Ÿ‘ 2
๐Ÿคฏ 3
mchampine 2020-12-05T06:02:48.274600Z

Agreed, the wording of part 2 took a while to interpret. I thought the description in part one was also a very long winded way to describe binary that could easily lead someone down a very long unproductive path. I suspect that was intentional and that youโ€™re supposed to be rewarded (with time savings) for insights that lead to dramatically simpler solutions. Iโ€™m pretty happy with my 6 line solution to part 1. Part 2 took another 2 lines but is slightly hacky. Looking forward to seeing other approaches.

rjray 2020-12-05T06:16:24.274800Z

Definitely more verbose than the previous one ๐Ÿ™‚. This is my post-submission cleaned-up version: https://github.com/rjray/advent-2020-clojure/blob/master/src/advent_of_code/day05bis.clj

2020-12-05T06:16:40.275100Z

My cat started screaming right at that time, making it very difficult to focus on the problem. ๐Ÿ“ memo: take the cat away for tomorrowโ€™s puzzle.

2020-12-05T06:18:41.276300Z

I solved part2 before I had a correct expression to solve it, just by doing some data exploration โ€ฆ the solution appeared clearly in the middle of false positives.

erwinrooijakkers 2020-12-05T06:42:01.281300Z

Good morning. Probably can be done nicer using an index instead of a vector representation but itโ€™s Saturday. I hope to see an example with index later today ๐Ÿ™‚ https://github.com/transducer/adventofcode/blob/master/src/adventofcode/2020/day5.clj

nbardiuk 2020-12-05T06:53:24.282300Z

https://github.com/nbardiuk/adventofcode/blob/master/2020/src/day05.clj spoiler ๐Ÿ‘‡

๐Ÿ‘ 5
tws 2020-12-05T10:21:43.290900Z

i prefer (Integer/parseInt s 2) to read-string tricks, but that might be personality.

โž• 1
2020-12-05T10:32:07.292Z

Me too, but the better way to do that is to use Long/parseLong . Ultimately, all the numbers in Clojure represented as Longs.

2020-12-05T10:57:39.293600Z

@nbardiuk Just realized from your solution, that splitting at row and col and then some math are not needed at all.

๐Ÿ‘ 1
2020-12-05T10:58:19.293900Z

I should carefully look at the input data.

plexus 2020-12-05T12:20:01.295400Z

Yeah I thought I was pretty smart by spotting it was just binary, but now I feel silly for not realizing that row+col are just a single number, no need to split them up :)

๐Ÿ˜ฒ 1
โž• 1
tws 2020-12-05T12:25:27.295800Z

* 8 was the clue. Why 8?

2020-12-05T12:30:41.296Z

Itโ€™s a power of 2. 2 2 2 = 8. Multiplying by 2 means the same thing you do with shift-left. For example

(= (bit-shift-left 1 3)
   (* 1 8))

2020-12-05T12:32:32.296200Z

and then just bit-or itโ€™s binary addition, so you get the same result just by decoding a whole string.

erwinrooijakkers 2020-12-05T13:07:40.296500Z

Thatโ€™s it yes

erwinrooijakkers 2020-12-05T13:07:51.296700Z

Missed it ๐Ÿ™‚

erwinrooijakkers 2020-12-05T13:08:04.296900Z

Jonathan did same: https://www.youtube.com/watch?v=wa0VcQugEsI

woonki.moon 2020-12-05T13:27:02.297900Z

how brilliant!

erwinrooijakkers 2020-12-05T14:25:55.298500Z

row and col are just a single number?

erwinrooijakkers 2020-12-05T14:26:15.298700Z

because of adding?

erwinrooijakkers 2020-12-05T14:26:16.298900Z

ah

erwinrooijakkers 2020-12-05T14:26:35.299100Z

wow

erwinrooijakkers 2020-12-05T14:43:25.299300Z

So ๐Ÿ™‚ For example โ€œBFFFBBFRRRโ€ is in binary 1000110 111, so:

(+ (* 8 (Integer/parseInt "1000110" 2))
   (Integer/parseInt "111" 2))
;; => 567
which is equal to:
(Integer/parseInt "1000110111" 2) ;; => 567 

๐Ÿคฏ 1
2020-12-05T19:08:28.306700Z

I feel dumb. I created a range from 0 to 127 and recursively either drop / take half of it.

erwinrooijakkers 2020-12-07T11:31:15.361300Z

Me too 45 minutes of fighting with off-by-one errors and nullpointerexceptions ๐Ÿ™‚

nbardiuk 2020-12-05T06:53:54.282600Z

seat number is just a binary with 0 and 1 replaced with L|F and R|B

2020-12-05T06:58:07.282900Z

another trick is how to parse it โ€ฆ there is a clojure function for that

1
dpkp 2020-12-05T07:04:47.283300Z

Very nice

nbardiuk 2020-12-05T07:07:35.283600Z

@vincent.cantin cannot find anything suitable, I am very intrigued ๐Ÿ˜ฎ

nbardiuk 2020-12-05T07:12:59.285300Z

oh cool! learn something new with every puzzle ๐Ÿ˜†

plexus 2020-12-05T07:17:37.285800Z

I guess I'm the only one who actually bothered to use bit-shift/bit-or ๐Ÿ™‚

๐Ÿ™‚ 1
2020-12-05T07:18:28.286Z

edn/read-string is a good parser

fingertoe 2020-12-05T07:19:31.286300Z

https://github.com/jreighley/aoc2020/blob/master/src/day5.clj. Kinda messy โ€” I always feel clever when I can map gibberish data to functions.

2020-12-05T07:20:24.286600Z

@plexus I like your approach for part2

plexus 2020-12-05T07:21:16.286900Z

the (partition coll n 1) trick has saved me more than once

plexus 2020-12-05T07:46:33.287600Z

Today's video: https://youtu.be/eNMJH_GZld0

๐Ÿ‘ 5
nbardiuk 2020-12-05T09:06:34.288Z

I didn't know about step argument in partition-all . Literally yesterday had to implement the pairs function manually

andrea 2020-12-05T20:59:30.307300Z

Nice! @plexus any info about your env setup? Is that vim plus what?

plexus 2020-12-06T05:31:51.310100Z

https://github.com/plexus/corgi

plexus 2020-12-06T05:33:51.310800Z

Planning to get that to a shippable state during the christmas/new years holidays

๐Ÿ‘ 1
2020-12-05T09:47:50.289300Z

I solved it literally how it says (spoiler) ๐Ÿ˜ž

โ˜๏ธ 1
2020-12-05T09:48:15.289400Z

(defn decode [lo hi s]
  (if-let [c (first s)]
    (case c
      (\F \L) (recur lo (+ lo (quot (- hi lo) 2)) (next s))
      (\B \R) (recur (+ lo (inc (quot (- hi lo) 2))) hi (next s)))
    lo))

๐Ÿ‘ 1
2020-12-05T11:09:51.294300Z

And my final decode

(defn decode [code]
  (-> (str/escape code {\F 0 \L 0 \B 1 \R 1})
      (Long/parseLong 2)))

๐Ÿ’ฏ 3
nbardiuk 2020-12-05T12:04:16.294700Z

Cool! escape another function I've learned today

๐Ÿ‘ 2
plexus 2020-12-05T12:21:56.295600Z

Ooh that's a good one! Sequestering it for my toolbox :) ๐Ÿงฐ

๐Ÿ˜ 1
โ˜๏ธ 1
pez 2020-12-05T14:22:34.298300Z

I did it even more literally. ๐Ÿ˜ƒ I figured while doing it that there must be another way, but the thing with gold stars is that they give me fever and make me indifferent to what happens to gnomes and kittens.

1
โ˜๏ธ 2
1