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
misha 2019-12-22T07:24:57.054800Z

(defn generate-points-from-steps
  "For a vector of steps of the form [\"R\" 10] etc.,
   generates a vector of points starting from origin."
  [steps]
  (reduce
    (fn [path step] 
      (into []
        (concat path (loc-from-loc (last path) step))))
    [origin]
    steps))
can be just
(reduce
  (fn [path step]
    (into path
      (loc-from-loc (peek path) step)))
  [origin]
  steps)
with 2 key improvements: 1)`peek` is constant time lookup of last element in vector (`last` is linear time) 2) into directly into path vector bypasses concat and scans only new lock-from-loc segment. Where concat does not scan for concatting, but then scans entire seq: both old path and new lock-from-lock to put them into empty [] .

misha 2019-12-22T07:31:04.055100Z

so in your code - each next iteration you scan old path segment twice: for last and after concat, instead of nonce

đź‘Ť 1
misha 2019-12-22T07:39:42.055600Z

I think this:

(first (sort
         (fn [p1 p2] (< (dist-to-origin p1) (dist-to-origin p2))
           points)))
can be this:
(->> points
  (sort-by dist-to-origin <)
  (first))

misha 2019-12-22T07:49:25.055800Z

(defn loc-from-loc
  "Loc is a hash with :x and :y, step is a vector with a direction string and an integer. Returns a list of locations that have been stepped over."
  [loc step]
  (case (first step)
    "R" (map (fn [step] (update loc :x + step)) (range 1 (+ (second step) 1)))
    "L" (map (fn [step] (update loc :x - step)) (range 1 (+ (second step) 1)))
    "U" (map (fn [step] (update loc :y + step)) (range 1 (+ (second step) 1)))
    "D" (map (fn [step] (update loc :y - step)) (range 1 (+ (second step) 1)))))
can be shorter with destructuring:
(defn loc-from-loc
  "Loc is a hash with :x and :y, step is a vector with a direction string and an integer.
   Returns a list of locations that have been stepped over."
  [loc [dir len]]
  (let [offsets (range 1 (+ 1 len))]
    (case dir
      "R" (map (fn [step] (update loc :x + step)) offsets)
      "L" (map (fn [step] (update loc :x - step)) offsets)
      "U" (map (fn [step] (update loc :y + step)) offsets)
      "D" (map (fn [step] (update loc :y - step)) offsets))))

2019-12-22T12:47:04.058100Z

Anyone have any tips for day 22 part 2? I think I need to revisit my algebra courses. Put possible spoilers in thread.

2019-12-22T12:49:00.058700Z

In noticed both the number of cards in the deck and the number of times you repeat the shuffle process are prime numbers.

misha 2019-12-22T14:06:55.059Z

https://en.wikipedia.org/wiki/Modular_arithmetic

1
Alper Cugun 2019-12-22T21:15:59.060100Z

I hope nobody minds me posting AoC as far back as this but after the rough start on part 1, assembling the part 2 solution from its components was a breeze.

2019-12-22T21:44:31.062300Z

Phew, finally solved today. Spent so much time going in wrong directions… I put the explanation in a comment https://gitlab.com/dmarjenburgh/adventofcode/blob/c7b250a5e60733fef2c6c01c5099125ef29d9c48/src/adventofcode/year_2019.clj#L732-776

uneman 2019-12-24T01:40:26.065100Z

thanks. i wasn't aware of modular inverse and that's why my division made the different answer..