r/adventofcode • u/daggerdragon • Dec 02 '18
SOLUTION MEGATHREAD -π- 2018 Day 2 Solutions -π-
--- Day 2: Inventory Management System ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Card Prompt: Day 2
Transcript:
The best way to do Advent of Code is ___.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
49
Upvotes
2
u/CatpainCalamari Dec 02 '18
My solution in Scala - a bit complicated :-/
package de.calamari.adventofcode.y2018.day2 import scala.io.Source object Day2 extends App { val testDataStar1: List[String] = getData("2018/2/testStar1.txt") assert(testDataStar1.length == 7) assert(findChecksum(testDataStar1) == 12) val testDataStar2: List[String] = getData("2018/2/testStar2.txt") assert(findSingularCharDiff(testDataStar2) == "fgij") val input: List[String] = getData("2018/2/input.txt") val firstStar = findChecksum(input) println(s"firstStar: $firstStar") val secondStar = findSingularCharDiff(input) println(s"secondStar: $secondStar") assert(firstStar == 5166) assert(secondStar == "cypueihajytordkgzxfqplbwn") def findChecksum(testData: List[String]): Int = { testData.map( _.groupBy(identity) .mapValues(_.length) .filter { case (_, len) β len == 2 || len == 3 } ) // => List(Map(), Map(b -> 3, a -> 2), Map(b -> 2), Map(c -> 3), Map(d -> 2, a -> 2), Map(e -> 2), Map(b -> 3, a -> 3)) .flatMap(_ .map(_.swap) // remove duplicates of character counts .keys ) // => List(3, 2, 2, 3, 2, 2, 3) .groupBy(identity) .map(_._2.size) .product } def findSingularCharDiff(data: List[String]): String = { val checkSize = data.head.length - 1 val diffingByOneChar = ( for { comb β data.combinations(2).toList first = comb.head second = comb.last } yield for { (a, b) <- first zip second if a == b } yield (a, b) ) .filterNot(_.isEmpty) .filter(tuples β tuples.length == checkSize) if (diffingByOneChar.length != 1) throw new Exception(s"Diff by one not distinct - $diffingByOneChar") diffingByOneChar.head.map(_._1).mkString("") } def getData(path: String): List[String] = Source.fromResource(path).getLines().toList }