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

1

u/raevnos Dec 06 '20 edited Dec 06 '20

Chicken scheme. Using SRFI-14 character sets made this really easy. The input parsing took more lines of code than the part actually calculating the answers.

#!/usr/local/bin/csi -s
(import
 (chicken format)
 (chicken io)
 (srfi 1)
 (srfi 14))

(define (read-group #!optional (port (current-input-port)))
  (let loop ((answers '())
             (line (read-line port)))
    (if (or (eof-object? line) (string=? line ""))
        answers
        (loop (cons (string->char-set line) answers) (read-line port)))))

(define (read-groups #!optional (port (current-input-port)))
  (let loop ((groups '()))
    (let ((new-group (read-group port)))
      (if (null? new-group)
          groups
          (loop (cons new-group groups))))))

(define (solve reduce-op groups)
  (fold (lambda (group total)
          (+ total (char-set-size (reduce reduce-op (char-set) group))))
        0 groups))

(define (solve1 groups) (solve char-set-union groups))
(define (solve2 groups) (solve char-set-intersection groups))

(define input (read-groups))
(printf "Part 1: ~A~%" (solve1 input))
(printf "Part 2: ~A~%" (solve2 input))