Last active
January 8, 2021 13:01
-
-
Save sungkmi/11fa0389ec1fd1e325132259a8a49700 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sungkmi.aoc2020.day6 | |
def grouping(input: String): Seq[String] = input.split("\n\n").toSeq | |
def count(reduceRule: (Set[Char], Set[Char]) => Set[Char])(groupString: String): Int = | |
groupString | |
.split("\n") | |
.map(_.toSet) | |
.reduce(reduceRule) | |
.size | |
def sumOfCount(input: String): Int = grouping(input).map(count(_ ++ _)).sum | |
def sumOfCount2(input: String): Int = grouping(input).map(count(_ `intersect` _)).sum | |
@main def part1: Unit = | |
val ans = sumOfCount(input) | |
println(ans) | |
@main def part2: Unit = | |
val ans = sumOfCount2(input) | |
println(ans) | |
//lazy val input = """jmqnkzlsfedaptx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sungkmi.aoc2020.day6 | |
class Day6Test extends munit.FunSuite { | |
val input = """abc | |
a | |
b | |
c | |
ab | |
ac | |
a | |
a | |
a | |
a | |
b""" | |
test("sumOfCount") { | |
assertEquals(sumOfCount(input), 11) | |
} | |
test("sumOfCount2") { | |
assertEquals(sumOfCount2(input), 6) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment