Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Created January 21, 2022 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sungkmi/2ef39a7c13f185cab54a175cc3fceb2b to your computer and use it in GitHub Desktop.
Save sungkmi/2ef39a7c13f185cab54a175cc3fceb2b to your computer and use it in GitHub Desktop.
package lascala.aoc2021.day1_10.day8
opaque type Digit = Set[Char]
opaque type SignalPattern = Set[Digit]
def parse(s: String): (SignalPattern, Seq[Digit]) =
val Array(pattern, digits) = s.split(" \\| ")
def parsePart(s: String): Seq[Digit] =
s.split(" ").map(_.toSet).toSeq
(parsePart(pattern).toSet, parsePart(digits))
def recognize(pattern: SignalPattern): Map[Digit, Int] =
val group = pattern.groupBy(_.size)
val one = group(2).head
val seven = group(3).head
val four = group(4).head
val eight = group(7).head
val three = group(5).filter(one forall _.contains).head
val five = group(5).filter(_ forall six.contains).head
val two = (group(5) - three - five).head
val six = group(6).filterNot(one forall _.contains).head
val nine = group(6).filter(four forall _.contains).head
val zero = (group(6) - six - nine).head
Map(
zero -> 0,
one -> 1,
two -> 2,
three -> 3,
four -> 4,
five -> 5,
six -> 6,
seven -> 7,
eight -> 8,
nine -> 9,
)
def decode(s: String): Int =
val (pattern, digits) = parse(s)
val map = recognize(pattern)
digits.map(map).mkString.toInt
def solve1(s: String): BigInt =
val target = Set(2, 3, 4, 7)
val lines = s.split("\n")
lines.map(parse).map{
case (_, digits) =>
digits.count{ (d: Digit) =>
target contains d.size
}
}.sum
end solve1
def solve2(s: String): BigInt =
val lines = s.split("\n")
lines.map(decode).sum
end solve2
@main def part1: Unit =
val ans = solve1(input)
println(ans)
@main def part2: Unit =
val ans = solve2(input)
println(ans)
//val input =
package lascala.aoc2021.day1_10.day8
import minitest.SimpleTestSuite
import hedgehog.minitest.HedgehogSupport
import hedgehog.*
object Day8Test extends SimpleTestSuite with HedgehogSupport:
val testInput = """be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce"""
example("day8 - solve1") {
solve1(testInput) ==== 26
}
example("day8 - decode") {
val line = "acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | cdfeb fcadb cdfeb cdbaf"
decode(line) ==== 5353
}
example("day8 - solve2") {
solve2(testInput) ==== 61229
}
end Day8Test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment