diff --git a/14/input.txt b/14/input.txt new file mode 100644 index 0000000..70c7685 --- /dev/null +++ b/14/input.txt @@ -0,0 +1,102 @@ +CKKOHNSBPCPCHVNKHFFK + +KO -> C +SO -> S +BF -> V +VN -> B +OV -> K +VH -> O +KV -> N +KB -> F +NB -> C +HS -> K +PF -> B +HB -> N +OC -> H +FS -> F +VV -> S +KF -> C +FN -> F +KP -> S +HO -> N +NH -> K +OO -> S +FB -> C +BP -> F +CH -> N +SN -> O +KN -> B +CV -> O +CC -> B +VB -> C +PH -> V +CO -> K +KS -> K +BK -> N +FH -> S +PV -> H +CB -> P +FO -> F +BB -> K +OB -> C +HH -> F +ON -> O +FK -> B +NF -> F +SV -> F +CP -> H +SS -> B +OP -> H +NS -> O +HK -> N +BC -> P +NV -> V +VS -> F +PC -> V +CS -> F +NP -> V +PS -> F +VC -> F +KK -> S +PO -> P +HF -> H +KC -> P +SF -> N +BV -> N +FF -> V +FV -> V +BO -> N +OS -> C +OF -> H +CN -> S +NO -> O +NC -> B +VK -> C +HN -> B +PK -> N +SK -> S +HV -> F +BH -> B +OK -> S +VO -> B +BS -> H +PP -> N +SC -> K +BN -> P +FC -> S +SB -> B +SH -> H +NN -> V +NK -> N +VF -> H +CF -> F +PB -> C +SP -> P +KH -> C +VP -> N +CK -> H +HP -> P +FP -> B +HC -> O +PN -> F +OH -> H \ No newline at end of file diff --git a/14/main.go b/14/main.go new file mode 100644 index 0000000..6d06c6b --- /dev/null +++ b/14/main.go @@ -0,0 +1,79 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" +) + +func main() { + file, err := os.Open("input.txt") + if err != nil { + log.Fatal(err) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + scanner.Split(bufio.ScanLines) + + insertions := make(map[string]string) + pairs := make(map[string]uint64) + + var i int + var template string + for scanner.Scan() { + line := scanner.Text() + if line == "" { + continue + } + if i == 0 { + template = line + for j := 0; j < len(template)-1; j++ { + pairs[string(template[j])+string(template[j+1])]++ + } + } else { + var one, two, three rune + fmt.Sscanf(line, "%c%c -> %c", &one, &two, &three) + insertions[string(one)+string(two)] = string(three) + } + i++ + } + + var newPairs map[string]uint64 + + for j := 0; j < 40; j++ { + newPairs = make(map[string]uint64) + for pair, count := range pairs { + firstNewPair := string(pair[0]) + insertions[pair] + secondNewPair := insertions[pair] + string(pair[1]) + newPairs[firstNewPair] += uint64(count) + newPairs[secondNewPair] += uint64(count) + } + pairs = newPairs + if j == 9 { + fmt.Println(count(&pairs)) + } + } + fmt.Println(count(&pairs)) +} + +func count(pairs *map[string]uint64) uint64 { + counts := make(map[string]uint64) + for pair, count := range *pairs { + counts[string(pair[1])] += uint64(count) + } + + var maxCount, minCount uint64 = 0, 0xffffffffffffffff + + for _, total := range counts { + if total > maxCount { + maxCount = total + } + if total < minCount { + minCount = total + } + } + + return maxCount - minCount +}