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
2020-12-12T00:13:45.159300Z

just finished part 1 and it took 24 seconds. This does not bode well.

fingertoe 2020-12-12T03:38:44.160200Z

Did anyone have flashbacks of the โ€œAnts demoโ€?

markw 2020-12-12T03:39:29.160700Z

more like game of life, these automata problems show up every year

2020-12-12T03:40:22.161800Z

oh balls, i misread part 2, and have spent faaaar too long calculating how many empty seats i can see in any of the 8 directions until I hit an occupied seat.

pez 2020-12-12T08:14:10.172900Z

Sounds like it could pretty easily be transformed into the right solution, though.

2020-12-12T03:40:51.162200Z

I have no idea why

markw 2020-12-12T03:40:57.162400Z

I did the same thing the first time.. actually I counted all empty seats in any direction

markw 2020-12-12T03:41:07.162700Z

well any of the 8

2020-12-12T03:41:29.163200Z

yeah, i've done the same. Go off on all the 8 directions and count empty seats until i hit an occupied one

2020-12-12T03:41:52.163800Z

Well, its 3:40 am. Fix it tomorow I guess. Dammit

markw 2020-12-12T03:42:01.164Z

My reading comprehension falls off a cliff at 11 P.M. CST apparently

markw 2020-12-12T03:42:17.164300Z

3:40? there goes my excuse

2020-12-12T04:59:38.165500Z

Day 12 answers thread - Post your answers here

2020-12-12T08:02:15.170900Z

I forgot about Math/abs. Thx Dosbol

joppe 2020-12-12T08:16:19.173100Z

Looking at all of your solutions I think I have some work to do regarding not leaning too heavily on loop/recur ๐Ÿ˜… https://github.com/Yopi/advent-of-code/blob/master/src/adventofcode/2020/day12.clj

๐Ÿ‘ 2
nbardiuk 2020-12-12T08:16:25.173400Z

https://github.com/nbardiuk/adventofcode/blob/master/2020/src/day12.clj

๐Ÿ‘ 5
๐Ÿ˜ฎ 2
2020-12-12T08:25:06.174100Z

Nice golf skills

๐Ÿ˜‚ 2
2020-12-12T08:28:28.174500Z

@brann.jesper You may save yourself some time by using case

joppe 2020-12-12T08:29:25.174700Z

That also makes a lot of sense, thank you! I saw cond used somewhere and forgot all about case ๐Ÿ˜„

pez 2020-12-12T10:36:57.179Z

My program (step 1) crashes with an arity excretion at what appears to be just when the final position is reduced. I love everything about Clojure except its error messages and stack traces...

2020-12-12T10:37:58.179200Z

Arity excretion ? XD

pez 2020-12-12T10:38:58.179900Z

Dunno what autocorrect is thinking about today. ๐Ÿ˜Ž

2020-12-12T10:39:04.180100Z

Try completing

erwinrooijakkers 2020-12-12T10:52:19.180600Z

Inspiring @nbardiuk

erwinrooijakkers 2020-12-12T10:52:51.180800Z

You combined part 1 and 2 by having a โ€œshiftโ€ as a concept, how big the steps are when moving forward

erwinrooijakkers 2020-12-12T10:53:18.181Z

How you exactly do rotation so concisely is unclear to me I hope to figure it out

erwinrooijakkers 2020-12-12T10:54:35.181200Z

I have this monstrosity:

\L (update acc :waypoint-location
           (case n
             90 (comp rotate-right rotate-right rotate-right)
             180 (comp rotate-right rotate-right)
             270 rotate-right))
\R (update acc :waypoint-location
           (case n
             270 (comp rotate-right rotate-right rotate-right)
             180 (comp rotate-right rotate-right)
             90 rotate-right))

nbardiuk 2020-12-12T10:58:36.182100Z

I have 2 vectors: position [x y] and direction [dx dy]. Rotation changes direction vector using rotation matrix that I've grabbed from wikipedia https://en.wikipedia.org/wiki/Rotation_matrix#Common_rotations

๐Ÿ‘ 2
1
pez 2020-12-12T11:06:12.182700Z

Haha, I have this:

(defn rotate [dir degrees]
  (case degrees
    90 [({[0 1] [1 0]
          [1 0] [0 -1]
          [0 -1] [-1 0]
          [-1 0] [0 1]} dir)]
    180 [({[0 1] [0 -1]
           [1 0] [-1 0]
           [0 -1] [0 1]
           [-1 0] [1 0]} dir)]
    270 [({[0 1] [-1 0]
           [1 0] [0 1]
           [0 -1] [1 0]
           [-1 0] [ -1]} dir)]
    dir))

2020-12-12T11:07:24.183Z

Not only you, check out the other solutions ๐Ÿ™‚

pez 2020-12-12T11:08:00.183500Z

I canโ€™t yet. Have this crash in step 1 to sort out. Then of course step 2 will probably take me a while too. ๐Ÿ˜ƒ

pez 2020-12-12T11:09:18.184200Z

Not sure how to use completing for the problem, @vincent.cantin. My function for reducing the instructions looks like so:

(defn run [program]
  (reduce (fn [ferry [op arg]]
            (let [{:keys [dir]} ferry]
              (cond
                (dirs op) (update ferry :pos #(move % (dirs op) arg))
                (= "F" op) (update ferry :pos #(move % dir arg))
                (= "R" op) (update ferry :dir #(rotate % arg))
                (= "L" op) (update ferry :dir #(rotate % (get {90 270 270 90} arg arg))))))
          {:pos    [0 0]
           :dir    [1 0]}
          program))
(Edited to fix a confusing update of :dir.)

pez 2020-12-12T11:26:49.185200Z

The function works for the example input of ""F10 N3 F7 R90" then croaks if the next instruction is an F instruction. (All other instruction types workโ€ฆ) Also the updated function doesnโ€™t crash with arity problems, but with

class clojure.lang.PersistentVector cannot be cast to class java.lang.Number 

pez 2020-12-12T11:29:24.185400Z

I think I have a hint at the problem now. Iโ€™ll go back and beg for help here if it doesnโ€™t lead anywhere. ๐Ÿ˜ƒ

pez 2020-12-12T11:54:09.185600Z

It was the gnomes and kittens murder with my rotate that hit me. All good now.

๐ŸŽ‰ 3
benoit 2020-12-12T13:17:34.189700Z

Solution to Day 12: https://github.com/benfle/advent-of-code-2020/blob/main/day12.clj

๐Ÿ‘ 1
benoit 2020-12-12T13:18:00.189900Z

I used a shortcut for Part 1 so I redid it so it can be used as well for Part 2.

๐Ÿ˜ƒ 1
pez 2020-12-12T14:24:42.190800Z

OMG. My head spins now. This was a tough one for me.

(defn parse [input]
  (->> (re-seq #"\w\d+" input)
       (map #(re-seq #"(\w)(\d+)" %))
       (mapv (fn [[[_ op arg]]] [op (Long/parseLong arg)]))))

(def dirs {"N" [0 1]
           "E" [1 0]
           "S" [0 -1]
           "W" [-1 0]})

(defn move [pos dir dist]
  (let [movement (map * dir [dist dist])]
    (->> pos
         (mapv + movement))))

(defn towards [{:keys [me wp]} n]
  (let [[dx' dy'] (map - wp me)
        [dx dy] (map * [dx' dy'] [n n])]
    {:me (map + me [dx dy])
     :wp (map + wp [dx dy])}))

(defn rotate [[x y] degrees]
  (case degrees
    90 [(- (* x 0) (* y -1)) (+ (* x -1) (* y 0))]
    180 [(- (* x -1) (* y 0)) (+ (* x 0) (* y -1))]
    270 [(- (* x 0) (* y 1)) (+ (* x 1) (* y 0))] ))

(defn rotate-around [origin degrees pos]
  (let [[dx' dy'] (map - pos origin)
        [dx dy] (rotate [dx' dy'] degrees)]
    (map + origin [dx dy])))

(defn run [program]
  (reduce (fn [system [op arg]]
            (let [{:keys [me]} system]
              (cond
                (dirs op) (update system :wp #(move % (dirs op) arg))
                (= "F" op) (towards system arg)
                (= "R" op) (update system :wp #(rotate-around me arg %))
                (= "L" op) (update system :wp #(rotate-around me (get {90 270 270 90} arg arg) %)))))
          {:me [0 0]
           :wp [10 1]}
          program))

(defn abs [n]
  (if (neg? n)
    (- n)
    n))

(comment
  (def input (util/fetch-input 12))

  (->> #_ "F10 N3 F7 R90 F11" input
       (parse)
       (run)
       :me
       (map abs)
       (apply +)))

๐Ÿ‘ 1
pez 2020-12-12T14:25:58.191Z

Well, the rotation around the ship was. The rest was pretty straight forward and I was mainly fighting my own sloppyness.

2020-12-12T15:30:50.191300Z

Here's my solution: https://github.com/chrisblom/advent-of-code/blob/master/src/adventofcode/2020/day12.clj

๐Ÿ‘ 1
mchampine 2020-12-12T15:30:59.191600Z

Lots of interesting rotation functions here. I came up with this:

(defn rotate [currdir turndeg op]
  (let [currang ({\E 0 \N 90 \W 180 \S 270} currdir)
        newang (mod (op currang turndeg) 360)]
    ({0 \E 90 \N 180 \W 270 \S} newang)))

;; example: rotate 90 degrees to the left (+) from East
(rotate \E 90 +) ;; \N

๐Ÿ‘ 1
plexus 2020-12-12T15:41:56.192200Z

I feel like my solution is very basic compared to all the clever stuff people are doing. Just a couple nested case statements and vector literals ๐Ÿ™‚ https://github.com/lambdaisland/aoc_2020/blob/main/src/lambdaisland/aoc_2020/puzzle12.clj

๐Ÿ‘ 1
plexus 2020-12-12T15:42:21.192500Z

Pretty fast too, 0.3ms for part 2

pez 2020-12-12T15:43:15.192700Z

0.3! Mine runs in 1.7ms. I thought that was fast.

pez 2020-12-12T15:43:41.192900Z

Now I must run yours on my machine. Haha.

pez 2020-12-12T16:08:54.195800Z

So, on my machine with your solution and your input, @plexus:

Evaluation count : 5046 in 6 samples of 841 calls.
             Execution time mean : 120,907492 ยตs
    Execution time std-deviation : 2,337936 ยตs
   Execution time lower quantile : 118,892646 ยตs ( 2,5%)
   Execution time upper quantile : 124,815435 ยตs (97,5%)
                   Overhead used : 6,737515 ns
My machine, my solution, your input: 1,189428 ms

pez 2020-12-12T16:11:19.196Z

Also funny, while sanity checking my rotate for step 2 I had something extremely like your version in my head. No idea why I didnโ€™t just translate that. Haha.

Average-user 2020-12-12T16:28:30.196300Z

https://github.com/Average-user/aoc2020/blob/main/src/day12.clj

๐Ÿ‘ 1
2020-12-12T18:16:59.202200Z

Using core.matrix for the rotations in part 2: https://github.com/Dexterminator/advent-of-code-2020/blob/main/src/day12/core.clj

๐Ÿค˜ 3
genmeblog 2020-12-12T19:05:36.206Z

Nice mess, part 1: https://raw.githubusercontent.com/genmeblog/advent-of-code/master/images/advent_of_code_2020/day12_rules_1.jpg

โค๏ธ 2
๐Ÿ˜‚ 1
genmeblog 2020-12-12T19:07:58.206600Z

my solution with rendering code: https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2020/day12.clj

Lars Nilsson 2020-12-12T21:34:36.208200Z

Went with multimethods for day 12 https://github.com/chamaeleon/aoc2020-clj/blob/master/src/aoc2020/day12.clj

2020-12-12T23:13:16.209100Z

Day 12 part 2:

(defn parse-line [line]
  (let [[inst v] (rest (re-find #"(\w)(\d+)" line))]
    [(keyword inst) (Integer/parseInt v)]))

(defn parse-input [input]
  (->> input
       (str/split-lines)
       (map parse-line)))

(defn rotate-waypoint [[wx wy] inst ang]
  (cond (or (and (= inst :R) (= ang 90))
            (and (= inst :L) (= ang 270))) [wy (- wx)]
        (or (and (= inst :R) (= ang 270))
            (and (= inst :L) (= ang 90)))  [(- wy) wx]
        (= ang 180) [(- wx) (- wy)]
        (= ang 360) [wx wy]))

(defn forward [[sx sy] [wx wy] distance]
  [(+ sx (* wx distance)) (+ sy (* wy distance))])

(defn move2 [{:keys [ship waypoint] :as acc} [inst d]]
  (let [[wx wy] waypoint]
    (case inst
      :N (assoc acc :waypoint [wx (+ wy d)])
      :S (assoc acc :waypoint [wx (- wy d)])
      :E (assoc acc :waypoint [(+ wx d) wy])
      :W (assoc acc :waypoint [(- wx d) wy])
      :L (assoc acc :waypoint (rotate-waypoint waypoint inst d))
      :R (assoc acc :waypoint (rotate-waypoint waypoint inst d))
      :F (assoc acc :ship (forward ship waypoint d)))))

(->> (reduce move2 {:ship [0 0] :waypoint [10 1]} (parse-input (slurp "resources/2020/day12")))
     (:ship)
     (map #(Math/abs ^int %))
     (reduce +))
Part 2, I just hard coded the rotations.

2020-12-12T05:00:09.166Z

Morning folks ^_^

2020-12-12T05:52:01.166200Z

Good morning

2020-12-12T05:52:55.166300Z

This time it was relatively relax โ€ฆ having a clean code helped not getting confused, I think. https://github.com/green-coder/advent-of-code-2020/blob/master/src/aoc/day_12.clj

โ˜บ๏ธ 2
2020-12-12T06:07:06.167200Z

:troll: mode: my code runs under 1 second ! ๐Ÿ˜‰

2
๐Ÿ˜† 2
2020-12-12T06:10:48.168500Z

I solved the puzzle using clojure.core.protocols/nav (kidding)

rjray 2020-12-12T06:18:04.168600Z

Here's my (rough) code. I think it can be shorter and cleaner after I've slept a bit and can revisit it. https://github.com/rjray/advent-2020-clojure/blob/master/src/advent_of_code/day12.clj

๐Ÿ‘ 3
mchampine 2020-12-12T06:25:10.168900Z

Part 1, quick-n-dirty

(def raw-input (str/split-lines (slurp "resources/day12.data")))

(defn proc1 [i]
  (let [[c nstr] (split-at 1 i)]
    [(first c) (read-string (apply str nstr))]))

(def input (map proc1 raw-input))

(def move {\E [1 0] \W [-1 0] \N [0 1] \S [0 -1] })
(def dir->ang {\E 0 \N 90 \W 180 \S 270})
(def ang->dir {0 \E 90 \N 180 \W 270 \S})

(defn turn [currdir turndeg op]
  (let [currang (dir->ang currdir)
        newang (mod (op currang turndeg) 360)]
    (ang->dir newang)))

(defn go [currpos direc howfar]
  (let [[currx curry] currpos
        [direcx direcy] (move direc)
        newx (+ currx (* direcx howfar))
        newy (+ curry (* direcy howfar))]
    [newx newy]))

(defn step [state inst]
  (let [[i v] inst]
    (case i
      (\N \S \E \W) (update-in state [:pos] go i v) ;; go direction i v steps
      \F (update-in state [:pos] go (:direction state) v) ;; go forward v steps
      \R (update-in state [:direction] turn v -) ;; turn right v degrees
      \L (update-in state [:direction] turn v +)))) ;; turn left v degrees

(defn solve [i]
  (let [abs (fn [v] (if (neg? v) (- v) v))
        init-state {:pos [0 0] :direction \E}
        finstate (reduce step init-state i)
        [px py] (:pos finstate)]
    (+ (abs px) (abs py))))

;; example
(def ex1 [[\F 10] [\N 3] [\F 7] [\R 90] [\F 11]])
(solve ex1)
;; => 25

(solve input)
;; => 441

๐Ÿ‘ 2
2020-12-12T07:12:30.169400Z

The final question (not the puzzle itself) is a bit confusing to me. Thatโ€™s why I spent almost half an hour to figure out what is wrong with my second answer.

2020-12-12T07:16:41.169600Z

Even worse, I solved my first part getting the final calculation wrong. And it gave me the first star.

Dosbol 2020-12-12T07:57:03.170600Z

;; day 12


  (def data (->> (slurp "input12.txt")
                 ((fn [v] (str/split v #"\n")))
                 (map (fn [v]
                        (let [[_ inst n] (re-find #"(\w)(\d+)" v)]
                          [inst (Long/parseLong n)])))))


  (def initial-state {:dir "E"
                      "S" 0
                      "N" 0
                      "E" 0
                      "W" 0})

  (def rotate {"L" {90  {"N" "W"
                         "W" "S"
                         "S" "E"
                         "E" "N"}
                    180 {"N" "S"
                         "S" "N"
                         "W" "E"
                         "E" "W"}
                    270 {"N" "E"
                         "W" "N"
                         "S" "W"
                         "E" "S"}}
               "R" {90  {"N" "E"
                         "E" "S"
                         "S" "W"
                         "W" "N"}
                    180 {"N" "S"
                         "S" "N"
                         "W" "E"
                         "E" "W"}
                    270 {"N" "W"
                         "W" "S"
                         "S" "E"
                         "E" "N"}}})

  (def state (reduce
              (fn [{dir :dir :as state} [instraction n]]
                (case instraction
                  "F"               (update state dir + n)
                  ("L" "R")         (assoc state :dir (get-in rotate [instraction n dir]))
                  ("N" "S" "W" "E") (update state instraction + n)))
              initial-state
              data))

  (+ (Math/abs (- (state "N") (state "S")))
     (Math/abs (- (state "E") (state "W"))))


  ;; part 2
  ;; 


  (def data (->> (slurp "input12.txt")
                 ((fn [v] (str/split v #"\n")))
                 (map (fn [v]
                        (let [[_ inst n] (re-find #"(\w)(\d+)" v)]
                          [inst (Long/parseLong n)])))))

  (defn rotate [{{E "E" W "W" S "S" N "N"} :waypoint :as state}
                dir
                degree]
    (assoc state :waypoint (get-in {"L" {90  {"N" E
                                              "W" N
                                              "S" W
                                              "E" S}
                                         180 {"N" S
                                              "S" N
                                              "W" E
                                              "E" W}
                                         270 {"N" W
                                              "E" N
                                              "S" E
                                              "W" S}}
                                    "R" {90  {"N" W
                                              "E" N
                                              "S" E
                                              "W" S}
                                         180 {"N" S
                                              "S" N
                                              "W" E
                                              "E" W}
                                         270 {"N" E
                                              "W" N
                                              "S" W
                                              "E" S}}}
                                   [dir degree])))
  
  (rotate initial-state "R" 90)

  (def initial-state {:waypoint {"E" 10
                                 "N" 1
                                 "W" 0
                                 "S" 0}
                      "S" 0
                      "N" 0
                      "E" 0
                      "W" 0})

  (defn move-forward [{waypoint :waypoint :as from} times]
    (reduce (fn [state [dir n]] (update state dir + (* n times)))
            from
            waypoint))
  
  (move-forward initial-state 10)

  (def state (reduce
              (fn [state [instruction n]]
                (case instruction
                  "F"               (move-forward state n)
                  ("L" "R")         (rotate state instruction n)
                  ("N" "S" "W" "E") (update-in state [:waypoint instruction] + n)))
              initial-state
              data))



  (+ (Math/abs (- (state "N") (state "S")))
     (Math/abs (- (state "E") (state "W"))))

๐Ÿ‘ 3
misha 2020-12-12T11:13:36.185Z

I wish all the discussion did not happen inside a single overpopulated thread

๐Ÿ’ฏ 3
1
pez 2020-12-12T12:40:16.187500Z

Step 2 movements done. Now to rotate. Big hmmm! Feels like it is my brain rotating and not the waypointโ€ฆ

๐Ÿ˜‚ 1
๐Ÿฆœ 2
๐Ÿ™ˆ 1
3
plexus 2020-12-12T15:38:49.192100Z

Feels like they are saving the easier ones for the weekends

๐Ÿ‘ 1
pez 2020-12-12T15:44:31.193500Z

This Manhattan Distance things brings back Robocode memories for me.

plexus 2020-12-12T15:47:40.195500Z

I recently worked on a turtle-like API for minecraft, so it definitely reminded me of that ๐Ÿ™‚ https://github.com/plexus/witchcraft/blob/main/src/lambdaisland/witchcraft/cursor.clj

๐Ÿ‘ 1
pez 2020-12-12T15:48:37.195700Z

I donโ€™t know about your weekends theory, @plexus. Todayโ€™s was tough for me. Made me realize I am maybe about to drop out soon. Could not afford to spend this amount of time during work days.

2020-12-12T17:57:54.198700Z

Annoying, my day 11 part 2 works with the test data, finishes with the real data but gives wrong answer. Completely lost to what hte problem is. Printing the grid and inspecting it is out the question.

pez 2020-12-12T17:58:58.199700Z

The example data lacks left turns, so maybe start looking there?

pez 2020-12-12T18:08:36.201900Z

If someone can add some left turns to the example data and provide it with the correct answer, then you could validate with that. I can't provide this right now. Busy with bbq. ๐Ÿ˜Ž

plexus 2020-12-12T18:21:50.203400Z

@qmstuart here's some test-input that should hit all discrete cases, plus an overview of what the ship's position and waypoint should be after each step: https://github.com/lambdaisland/aoc_2020/blob/main/src/lambdaisland/aoc_2020/puzzle12.clj#L111-L144

plexus 2020-12-12T18:22:50.204400Z

I had the same thing happen to me first as well, I had an error in my implementation for "W", and the demo input does not contain any west instructions, so the demo input ran fine, but the result on real input was wrong

โค๏ธ 2
plexus 2020-12-12T18:23:48.205300Z

printing out the position and waypoint after each step allowed me to manually go through the steps and mentally compute what the expected output should be, then I noticed that my west was actually going east

โค๏ธ 1
plexus 2020-12-12T18:24:40.205500Z

Video for today: https://youtu.be/WqmswkJX7rM

๐Ÿ‘ 2