From e532f86d983251c7f4c3d77752d7c75d75933762 Mon Sep 17 00:00:00 2001 From: Tom Larsen Date: Wed, 30 Nov 2022 21:21:54 -0600 Subject: [PATCH] Incomplete attempt at solving Day 14, Part 2 Advent of Code 2021. --- 2021/day14-2/src/main/kotlin/Main.kt | 103 ++++++++++++++++++++ 2021/day14-2/src/main/resources/small_input | 18 ++++ 2 files changed, 121 insertions(+) create mode 100644 2021/day14-2/src/main/kotlin/Main.kt create mode 100644 2021/day14-2/src/main/resources/small_input diff --git a/2021/day14-2/src/main/kotlin/Main.kt b/2021/day14-2/src/main/kotlin/Main.kt new file mode 100644 index 0000000..637b1d8 --- /dev/null +++ b/2021/day14-2/src/main/kotlin/Main.kt @@ -0,0 +1,103 @@ +import java.io.File + +fun main(args: Array) { + println("AOC 2021, Day 14.2 starting!!!!") + + val polymerBuilder = PolymerBuilder() + // args[0] should read /Users/tlarsen/src/adventofcode/2021/DAY-PART/src/main/resources + File(args[0]).forEachLine { + if (polymerBuilder.polymerPairOfChar.isEmpty()) { + polymerBuilder.addTemplate(it) + } else if (it.isNotEmpty()) { + polymerBuilder.addRule(it) + } + } + + println("Original polymer: ${polymerBuilder.polymerPairOfChar}") + // build 10 times + var polymer = "" + for(i in 1..10) { + polymer = polymerBuilder.build() + //println("After step $i, the polymer is: $polymer") + println("After step $i, the polymer is X long.") + } + println("Stats on the last polymer: ${polymer.length}") + val chemCounter = HashMap() + for(ch in polymer) { + if(!chemCounter.containsKey(ch)) { + chemCounter[ch] = 0 + } + chemCounter[ch] = chemCounter[ch]!! + 1L + } + + println("Counts: $chemCounter") + + var mostCommonCount = 0L + var leastCommonCount = 0L + for(count in chemCounter.values) { + if(mostCommonCount == 0L) { + mostCommonCount = count + } else { + mostCommonCount = Math.max(count, mostCommonCount) + } + if(leastCommonCount == 0L) { + leastCommonCount = count + } else { + leastCommonCount = Math.min(count, leastCommonCount) + } + } + + println("Most Common Count : $mostCommonCount") + println("Least Common Count: $leastCommonCount") + println("Difference : ${mostCommonCount - leastCommonCount}") + + println("AOC 2021, Day 14.2 complete!!!!") +} + +data class MyPair constructor(val left:L, val right:R) + +data class PairOfChar constructor(val left: Char, val right: Char) + +class PolymerBuilder { + var polymerPairOfChar = ArrayList>() + private set + var rules = HashMap() + private set + + fun build() : String { + val newPolymer = ArrayList() + + for(i in 0 until polymerPairOfChar.size - 1) { + var copy = true + for(rule in rules.keys) { + if((polymer[i] == rule[0]) && (polymer[i + 1] == rule[1])) { + newPolymer += polymer[i] + newPolymer += rules[rule]!! + + copy = false + } + } + if(copy) { + newPolymer += polymer[i] + } + } + // the loop is always - 1 so copy forward the very last character + newPolymer += polymer.last() + + polymer = newPolymer + + return String(newPolymer.toCharArray()) + } + + + fun addTemplate(templ: String) { + for(i in 0 until templ.length - 1) { + polymerPairOfChar.add(PairOfChar(templ[i], templ[i + 1])) + } + } + + fun addRule(rule:String) { + val splitPointer = rule.split("->") // split line left -> right + rules[splitPointer[0].trim()] = splitPointer[1].trim()[0] + } +} \ No newline at end of file diff --git a/2021/day14-2/src/main/resources/small_input b/2021/day14-2/src/main/resources/small_input new file mode 100644 index 0000000..6c1c3a1 --- /dev/null +++ b/2021/day14-2/src/main/resources/small_input @@ -0,0 +1,18 @@ +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 \ No newline at end of file