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
)
@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)])
Good morning
Day 5 probably would have been a lot easier if I had carefully read the problem.. Not too bad though..
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.
;; 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.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.
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
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.
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.
Hi ๐ here's https://github.com/ocisly/advent2020/blob/94b52d8bb63865a8ace75f34226f6acb390c69ef/day-5.clj#L5-L36
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
https://github.com/nbardiuk/adventofcode/blob/master/2020/src/day05.clj spoiler ๐
i prefer (Integer/parseInt s 2)
to read-string
tricks, but that might be personality.
Me too, but the better way to do that is to use Long/parseLong
. Ultimately, all the numbers in Clojure represented as Longs.
@nbardiuk Just realized from your solution, that splitting at row and col and then some math are not needed at all.
I should carefully look at the input data.
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 :)
* 8
was the clue. Why 8?
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))
and then just bit-or
itโs binary addition, so you get the same result just by decoding a whole string.
Thatโs it yes
Missed it ๐
Jonathan did same: https://www.youtube.com/watch?v=wa0VcQugEsI
how brilliant!
row and col are just a single number?
because of adding?
ah
wow
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
I feel dumb. I created a range from 0 to 127 and recursively either drop / take half of it.
Me too 45 minutes of fighting with off-by-one errors and nullpointerexceptions ๐
seat number is just a binary with 0 and 1 replaced with L|F and R|B
another trick is how to parse it โฆ there is a clojure function for that
Very nice
@vincent.cantin cannot find anything suitable, I am very intrigued ๐ฎ
@nbardiuk https://github.com/green-coder/advent-of-code-2020/blob/master/src/aoc/day_5.clj#L20-L21
oh cool! learn something new with every puzzle ๐
https://github.com/lambdaisland/aoc_2020/blob/main/src/lambdaisland/aoc_2020/puzzle05.clj
I guess I'm the only one who actually bothered to use bit-shift/bit-or ๐
edn/read-string is a good parser
https://github.com/jreighley/aoc2020/blob/master/src/day5.clj. Kinda messy โ I always feel clever when I can map gibberish data to functions.
@plexus I like your approach for part2
the (partition coll n 1)
trick has saved me more than once
Today's video: https://youtu.be/eNMJH_GZld0
I didn't know about step argument in partition-all
. Literally yesterday had to implement the pairs
function manually
Nice! @plexus any info about your env setup? Is that vim plus what?
Planning to get that to a shippable state during the christmas/new years holidays
I solved it literally how it says (spoiler) ๐
(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))
full solution https://github.com/zelark/AoC-2020/blob/3cc64ff25278f82d262c4c32c2eec4268c0997a5/src/zelark/aoc_2020/day_05.clj
refactored one https://github.com/zelark/AoC-2020/blob/master/src/zelark/aoc_2020/day_05.clj
And my final decode
(defn decode [code]
(-> (str/escape code {\F 0 \L 0 \B 1 \R 1})
(Long/parseLong 2)))
Cool! escape
another function I've learned today
Ooh that's a good one! Sequestering it for my toolbox :) ๐งฐ
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.