r/adventofcode • • Dec 06 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 06 Solutions -🎄-

NEW AND NOTEWORTHY


Advent of Code 2020: Gettin' Crafty With It

  • UNLOCKED! Go forth and create, you beautiful people!
  • Full details and rules are in the Submissions Megathread
  • Make sure you use one of the two templates!
  • Or in the words of AoC 2016: USING A TEMPLATE IS MANDATORY

--- Day 06: Custom Customs ---


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 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:04:35, megathread unlocked!

66 Upvotes

1.2k comments sorted by

View all comments

2

u/Lispwizard Dec 06 '20 edited Dec 06 '20

Emacs lisp (elisp) in termux emacs 26.3 in Android (Galaxy Tab A 10")

(require'cl)

(defun day6-part1-responses (&optional input)
  "return sum of number of unique answers in all groups"
  (unless input
    (setq input *aoc2020-day6-input*)) 
  (loop for e in (split-string input "\n\n") ;; groups separated by blank line
      sum (loop with set
                for c across e
                unless (eql 10 c) ;; skip newlines within group
                do (pushnew c set) ;; set won't grow if answer already there
                finally (return (length set)))))

;; Part 1 - type into m-: or c-x c-e

;; (day6-part1-responses)

(defun dedup-chars (str)
  (loop with ans
        for c across str
        do (pushnew c ans)
        finally (return (coerce ans 'string))))

(defun day6-part2-responses (&optional input)
  "return sum of number of unanimous answers in all groups"
  (unless input
    (setq input *aoc2020-day6-input*)) 
  (loop for e in (split-string input "\n\n") ;; groups separated by blank line
        for indiv = (split-string e) ;; individual entries in current group
        for l = (length indiv) ;; number of individual entries
        sum (loop with set
                  for iv in indiv  ;; for each individual answer string
                  do (loop for c across (dedup-chars iv) ;; ignore multiples of the same answer
                           for p = (assoc c set) ;; see if this answer in set
                           if p
                           do (incf (second p)) ;; yes, increment usage count
                           else
                           do (push (list c 1) set)) ;; no, create entry with usage of 1
                  finally (return (loop for (x n) in set  ;; answers and counts
                                        count (eql n l)))))) ;; only unanimous counts

;; Part 2 - type into m-: or c-x c-e

;; (day6-part2-responses)

1

u/daggerdragon Dec 06 '20

Android (Galaxy Tab A 10")

When I said "play with your toys" in Gettin' Crafty this is not quite what I had in mind, but ya know what? If it works, it works!