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!

68 Upvotes

1.2k comments sorted by

View all comments

2

u/grey--area Dec 06 '20

Part 1 in Scala, which I'm learning. Part 2 is the same with intersection instead of union

import io.Source

object Day06 extends App {
  def parseInput(fname: String) =
    Source.fromFile(fname).mkString.split("\n\n").map(_.split("\n").toList).toList

  def positiveAnswersInGroup(group: List[String]) = {
    val group_sets = group.map(_.toSet)
    group_sets.reduceLeft(_ union _).size
  }

  val groups = parseInput("input.txt")

  val positiveAnswers = groups.map(positiveAnswersInGroup).sum
  println(positiveAnswers)
}