From 8854ab7279f425c31cc7ec229a432b4ae1dfd28e Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Tue, 14 Dec 2021 10:55:30 +0100 Subject: [PATCH] Day 14 --- 14/input.txt | 102 ++++++++++++++++++++++++++++++++++++++++++++++++ 14/main.go | 72 ++++++++++++++++++++++++++++++++++ 14/main_test.go | 17 ++++++++ 14/test.txt | 18 +++++++++ 4 files changed, 209 insertions(+) create mode 100644 14/input.txt create mode 100644 14/main.go create mode 100644 14/main_test.go create mode 100644 14/test.txt diff --git a/14/input.txt b/14/input.txt new file mode 100644 index 0000000..0cc87ed --- /dev/null +++ b/14/input.txt @@ -0,0 +1,102 @@ +FNFPPNKPPHSOKFFHOFOC + +VS -> B +SV -> C +PP -> N +NS -> N +BC -> N +PB -> F +BK -> P +NV -> V +KF -> C +KS -> C +PV -> N +NF -> S +PK -> F +SC -> F +KN -> K +PN -> K +OH -> F +PS -> P +FN -> O +OP -> B +FO -> C +HS -> F +VO -> C +OS -> B +PF -> V +SB -> V +KO -> O +SK -> N +KB -> F +KH -> C +CC -> B +CS -> C +OF -> C +FS -> B +FP -> H +VN -> O +NB -> N +BS -> H +PC -> H +OO -> F +BF -> O +HC -> P +BH -> S +NP -> P +FB -> C +CB -> H +BO -> C +NN -> V +SF -> N +FC -> F +KK -> C +CN -> N +BV -> F +FK -> C +CF -> F +VV -> B +VF -> S +CK -> C +OV -> P +NC -> N +SS -> F +NK -> V +HN -> O +ON -> P +FH -> O +OB -> H +SH -> H +NH -> V +FF -> B +HP -> B +PO -> P +HB -> H +CH -> N +SN -> P +HK -> P +FV -> H +SO -> O +VH -> V +BP -> V +CV -> P +KP -> K +VB -> N +HV -> K +SP -> N +HO -> P +CP -> H +VC -> N +CO -> S +BN -> H +NO -> B +HF -> O +VP -> K +KV -> H +KC -> F +HH -> C +BB -> K +VK -> P +OK -> C +OC -> C +PH -> H \ No newline at end of file diff --git a/14/main.go b/14/main.go new file mode 100644 index 0000000..6574183 --- /dev/null +++ b/14/main.go @@ -0,0 +1,72 @@ +package main + +import ( + "log" + "math" + "os" + "strings" +) + +func main() { + inputBytes, _ := os.ReadFile("input.txt") + input := strings.Split(string(inputBytes), "\n") + + log.Println("Puzzle 1:", puzzle1(input)) + log.Println("Puzzle 2:", puzzle2(input)) +} + +func puzzle1(input []string) int { + return do(input, 10) +} + +func puzzle2(input []string) int { + return do(input, 40) +} + +func parseInput(input []string) (template string, rules map[string]string) { + template = input[0] + rules = map[string]string{} + for _, line := range input[2:] { + parts := strings.Split(line, " -> ") + rules[parts[0]] = parts[1] + } + return +} + +func do(input []string, steps int) int { + // Parse input + template, rules := parseInput(input) + // Polymerize + pairs := map[string]int{} + for i := 0; i < len(template)-1; i++ { + pairs[template[i:i+2]]++ + } + for i := 0; i < steps; i++ { + newPairs := map[string]int{} + for pair, count := range pairs { + rule := rules[pair] + newPairs[pair[0:1]+rule] += count + newPairs[rule+pair[1:2]] += count + } + pairs = newPairs + } + // Sum up counts for each letter + counts := map[byte]int{} + for pair, count := range pairs { + counts[pair[0]] += count + } + // Add count for last letter in template + counts[template[len(template)-1]]++ + // Get minimum and maximum counts + max, min := math.MinInt, math.MaxInt + for _, count := range counts { + if count > max { + max = count + } + if count < min { + min = count + } + } + // Return difference + return max - min +} diff --git a/14/main_test.go b/14/main_test.go new file mode 100644 index 0000000..1eff968 --- /dev/null +++ b/14/main_test.go @@ -0,0 +1,17 @@ +package main + +import ( + "os" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test(t *testing.T) { + inputBytes, _ := os.ReadFile("test.txt") + input := strings.Split(string(inputBytes), "\n") + + assert.Equal(t, 1588, puzzle1(input)) + assert.Equal(t, 2188189693529, puzzle2(input)) +} diff --git a/14/test.txt b/14/test.txt new file mode 100644 index 0000000..6c1c3a1 --- /dev/null +++ b/14/test.txt @@ -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