Created
February 25, 2022 13:51
-
-
Save eugene70/7fd8f2b93e45f1b240af65c59456c5d9 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 aoc2021 | |
import java.util | |
import scala.annotation.tailrec | |
import scala.collection.mutable | |
object Day14: | |
type InputType = (Seq[Char], Instruction) | |
type Instruction = Map[(Char, Char), Char] | |
@main def runDay14: Unit = | |
val testData = time("testReady", () => ready(testInput)) | |
time("testPart1", () => part1(testData)) // should be 1588 | |
time("testPart2", () => part2(testData)) // should be 2188189693529 | |
val data = time("ready", () => ready(input)) | |
time("part1", () => part1(data)) | |
//time("part2", () => part2(data)) | |
def ready(input: String): InputType = | |
val data = input | |
.split("\n\n") | |
( | |
data(0) | |
.toIndexedSeq, | |
data(1) | |
.linesIterator | |
.map(_ match | |
case s"$p -> $i" => ((p.head, p.last), i.head) | |
) | |
.toMap | |
) | |
def step(polymer: Seq[Char], instruction: Instruction): Seq[Char] = | |
polymer.sliding(2) | |
.flatMap(w => Seq(w.head, instruction.getOrElse((w.head, w.last), ' '))) | |
.toSeq | |
:+ polymer.last | |
def part1(data: InputType): BigInt = | |
val map = Range.apply(0, 10) | |
.foldLeft(data._1)((p, _) => step(p, data._2)) | |
.groupBy(c => c) | |
.map((k, v) => (k, v.size)) | |
.map((k, v) => v) | |
map.max - map.min | |
val cMap: mutable.HashMap[Char, Long] = mutable.HashMap.empty | |
def inc(char: Char) = | |
val count = cMap.getOrElse(char, 0L) | |
cMap.put(char, count + 1) | |
def step2(first: Char, second: Char, step: Int, maxStep: Int, inst: Instruction): Unit = | |
if (step < maxStep) { | |
val insertion = inst.getOrElse((first, second), ' ') | |
inc(insertion) | |
val nextStep = step + 1 | |
step2(first, insertion, nextStep, maxStep, inst) | |
step2(insertion, second, nextStep, maxStep, inst) | |
} | |
def part2(data: InputType): BigInt = | |
step2('N', 'N', 0, 10, data._2) | |
println(cMap) | |
0 | |
val testInput = | |
"""NNCB | |
| | |
|CH -> B | |
|HH -> N | |
|CB -> H | |
|NH -> C | |
|HB -> C | |
|HC -> B | |
|HN -> C | |
|NN -> C | |
|BH -> H | |
|NC -> B | |
|NB -> B | |
|BN -> B | |
|BB -> N | |
|BC -> B | |
|CC -> N | |
|CN -> C""".stripMargin | |
val input = | |
"""PBFNVFFPCPCPFPHKBONB | |
| | |
|KK -> S | |
|FO -> B | |
|PP -> O | |
|HN -> S | |
|CN -> H | |
|VH -> P | |
|BK -> B | |
|VC -> N | |
|CB -> H | |
|OC -> K | |
|BF -> P | |
|FV -> K | |
|SP -> F | |
|OP -> K | |
|SS -> B | |
|NN -> O | |
|CS -> K | |
|CF -> K | |
|FF -> S | |
|SV -> P | |
|OK -> S | |
|CO -> F | |
|OB -> K | |
|BH -> B | |
|HH -> S | |
|VB -> V | |
|KV -> H | |
|CK -> V | |
|NV -> N | |
|SF -> V | |
|PK -> H | |
|PV -> N | |
|FB -> O | |
|BO -> K | |
|FP -> N | |
|OF -> N | |
|FK -> O | |
|VK -> V | |
|NO -> V | |
|NS -> C | |
|KC -> S | |
|VF -> V | |
|BV -> N | |
|CP -> K | |
|PB -> V | |
|CC -> S | |
|NH -> B | |
|CV -> P | |
|SO -> V | |
|NC -> O | |
|HK -> K | |
|SB -> H | |
|OO -> V | |
|HO -> P | |
|PS -> B | |
|BC -> P | |
|KO -> C | |
|KB -> C | |
|VV -> F | |
|BS -> F | |
|HB -> B | |
|KN -> S | |
|FC -> C | |
|SN -> S | |
|HC -> O | |
|HP -> F | |
|BP -> V | |
|ON -> K | |
|BB -> K | |
|KH -> O | |
|NP -> H | |
|KS -> N | |
|SH -> K | |
|VP -> O | |
|PF -> O | |
|HF -> S | |
|BN -> S | |
|NK -> C | |
|FH -> O | |
|CH -> B | |
|KP -> B | |
|FN -> K | |
|OV -> P | |
|VS -> K | |
|OH -> V | |
|PC -> F | |
|VO -> H | |
|SK -> S | |
|PO -> O | |
|KF -> N | |
|NF -> V | |
|NB -> C | |
|PN -> O | |
|FS -> C | |
|PH -> F | |
|VN -> S | |
|OS -> V | |
|HV -> H | |
|HS -> B | |
|SC -> C""".stripMargin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment