r/adventofcode • • Dec 10 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 10 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

  • 12 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 10: Adapter Array ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:08:42, megathread unlocked!

69 Upvotes

1.1k comments sorted by

View all comments

1

u/bitti1975 Dec 10 '20 edited Dec 10 '20

Clojurescript via Planck

(ns aoc2020.day10
  (:require [planck.core :as core]))
 
(defn read-adapters []
  (->>
    core/*in*
    core/line-seq
    (map #(js/parseInt %))
    sort))
 
(def count-arr
  (memoize
    (fn [input [next & adapters] output]
      (let [max-next (+ 3 input)]
        (cond
          (not next)
          (if (< max-next output) 0 1)
 
          (< max-next next)
          0
 
          (= max-next next)
          (count-arr next adapters output)
 
          (> max-next next)
          (+ (count-arr input adapters output)
             (count-arr next adapters output)))))))
 
(let [adapters (read-adapters)
      device (+ 3 (reduce max adapters))]
 
  (println "Product of number of 1 and 3 jolt differences when using all adapters:"
    (loop [current 0
           [next & adapters] (concat adapters (list device))
           d1 0
           d3 0]
      (if next
        (case (- next current)
          1 (recur next adapters (inc d1) d3)
          3 (recur next adapters d1 (inc d3))
          (recur next adapters d1 d3))
        (* d1 d3))))
 
  (println "Possible adapter arrangements:"
    (count-arr 0 adapters device)))

Execute by providing the input on stdin:

planck day10.cljs <day10-input

1

u/daggerdragon Dec 11 '20

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.

1

u/backtickbot Dec 10 '20

Fixed formatting.

Hello, bitti1975: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.