diff options
author | Julien Dessaux | 2021-12-14 19:36:39 +0100 |
---|---|---|
committer | Julien Dessaux | 2021-12-14 19:36:39 +0100 |
commit | 408f4a856c876ef3f59fc43f424ab8b647cc3e7a (patch) | |
tree | 0620564071a0b48b6b619f026dea1222aa3da7ca /2021/14 | |
parent | Added solutions for 13th day: Transparent Origami (diff) | |
download | advent-of-code-408f4a856c876ef3f59fc43f424ab8b647cc3e7a.tar.gz advent-of-code-408f4a856c876ef3f59fc43f424ab8b647cc3e7a.tar.bz2 advent-of-code-408f4a856c876ef3f59fc43f424ab8b647cc3e7a.zip |
Added solutions for 14th day: Extended Polymerization
Diffstat (limited to '')
-rw-r--r-- | 2021/14/example | 18 | ||||
-rw-r--r-- | 2021/14/first.go | 94 | ||||
-rw-r--r-- | 2021/14/input | 102 | ||||
-rw-r--r-- | 2021/14/second.go | 98 |
4 files changed, 312 insertions, 0 deletions
diff --git a/2021/14/example b/2021/14/example new file mode 100644 index 0000000..b5594dd --- /dev/null +++ b/2021/14/example @@ -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 diff --git a/2021/14/first.go b/2021/14/first.go new file mode 100644 index 0000000..e6256ac --- /dev/null +++ b/2021/14/first.go @@ -0,0 +1,94 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "sort" + "strings" +) + +type transform struct { + first byte + second byte + res byte + count int +} + +type letter struct { + value byte + count int +} + +var ( + pairs []transform + letters []letter +) + +func main() { + scanner := bufio.NewScanner(os.Stdin) + + scanner.Scan() + polymer := scanner.Text() +out: + for i := 0; i < len(polymer); i++ { + for j := range letters { + if letters[j].value == polymer[i] { + letters[j].count++ + continue out + } + } + letters = append(letters, letter{polymer[i], 1}) + } + scanner.Scan() + + for scanner.Scan() { + line := strings.Split(scanner.Text(), " -> ") + pairs = append(pairs, transform{line[0][0], line[0][1], line[1][0], 0}) + } + for i := 0; i < len(polymer)-1; i++ { + for j, v := range pairs { + if v.first == polymer[i] && v.second == polymer[i+1] { + pairs[j].count++ + } + } + } + newPairs := make([]transform, len(pairs)) + copy(newPairs, pairs) + for s := 1; s < 10; s++ { + for j := range newPairs { + newPairs[j].count = 0 + } + for _, a := range pairs { + if a.count > 0 { + for j, b := range pairs { + if b.first == a.first && b.second == a.res { + newPairs[j].count += a.count + break + } + } + for j, b := range pairs { + if b.first == a.res && b.second == a.second { + newPairs[j].count += a.count + break + } + } + } + } + copy(pairs, newPairs) + out2: + for _, v := range pairs { + for j := range letters { + if letters[j].value == v.res { + letters[j].count += v.count + continue out2 + } + } + letters = append(letters, letter{v.res, v.count}) + } + } + sort.Slice(letters, func(i, j int) bool { + return letters[i].count < letters[j].count + }) + fmt.Println(letters[len(letters)-1].count - letters[0].count) +} diff --git a/2021/14/input b/2021/14/input new file mode 100644 index 0000000..331c91b --- /dev/null +++ b/2021/14/input @@ -0,0 +1,102 @@ +ONSVVHNCFVBHKVPCHCPV + +VO -> C +VV -> S +HK -> H +FC -> C +VB -> V +NO -> H +BN -> B +FP -> K +CS -> C +HC -> S +FS -> K +KH -> V +CH -> H +BP -> K +OF -> K +SS -> F +SP -> C +PN -> O +CK -> K +KS -> H +HO -> K +FV -> F +SN -> P +HN -> O +KK -> H +KP -> O +CN -> N +BO -> C +CC -> H +PB -> F +PV -> K +BV -> K +PP -> H +KB -> F +NC -> F +PC -> V +FN -> N +NH -> B +CF -> V +PO -> F +KC -> S +VP -> P +HH -> N +OB -> O +KN -> O +PS -> N +SF -> V +VK -> F +CO -> N +KF -> B +VC -> C +SH -> S +HV -> V +FK -> O +NV -> N +SC -> O +BK -> F +BB -> K +HF -> K +OC -> O +KO -> V +OS -> P +FF -> O +PH -> F +FB -> O +NN -> C +NK -> C +HP -> B +PF -> H +PK -> C +NP -> O +NS -> V +CV -> O +VH -> C +OP -> N +SO -> O +SK -> H +SV -> O +NF -> H +BS -> K +BH -> O +VN -> S +HB -> O +OH -> K +CB -> B +BC -> S +OV -> F +BF -> P +OO -> F +HS -> H +ON -> P +NB -> F +CP -> S +SB -> V +VF -> C +OK -> O +FH -> H +KV -> S +FO -> C +VS -> B diff --git a/2021/14/second.go b/2021/14/second.go new file mode 100644 index 0000000..06c6890 --- /dev/null +++ b/2021/14/second.go @@ -0,0 +1,98 @@ +package main + +// TODO Off by one! I don't know where!!!!! +// I get : 3459174981022 +// I need : 3459174981021 + +import ( + "bufio" + "fmt" + "os" + "sort" + "strings" +) + +type transform struct { + first byte + second byte + res byte + count int +} + +type letter struct { + value byte + count int +} + +var ( + pairs []transform + letters []letter +) + +func main() { + scanner := bufio.NewScanner(os.Stdin) + + scanner.Scan() + polymer := scanner.Text() +out: + for i := 0; i < len(polymer); i++ { + for j := range letters { + if letters[j].value == polymer[i] { + letters[j].count++ + continue out + } + } + letters = append(letters, letter{polymer[i], 1}) + } + scanner.Scan() + + for scanner.Scan() { + line := strings.Split(scanner.Text(), " -> ") + pairs = append(pairs, transform{line[0][0], line[0][1], line[1][0], 0}) + } + for i := 0; i < len(polymer)-1; i++ { + for j, v := range pairs { + if v.first == polymer[i] && v.second == polymer[i+1] { + pairs[j].count++ + } + } + } + newPairs := make([]transform, len(pairs)) + copy(newPairs, pairs) + for s := 1; s < 40; s++ { + for j := range newPairs { + newPairs[j].count = 0 + } + for _, a := range pairs { + if a.count > 0 { + for j, b := range pairs { + if b.first == a.first && b.second == a.res { + newPairs[j].count += a.count + break + } + } + for j, b := range pairs { + if b.first == a.res && b.second == a.second { + newPairs[j].count += a.count + break + } + } + } + } + copy(pairs, newPairs) + out2: + for _, v := range pairs { + for j := range letters { + if letters[j].value == v.res { + letters[j].count += v.count + continue out2 + } + } + letters = append(letters, letter{v.res, v.count}) + } + } + sort.Slice(letters, func(i, j int) bool { + return letters[i].count < letters[j].count + }) + fmt.Println(letters[len(letters)-1].count - letters[0].count) +} |