Add 2021-14 in Python
Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
parent
4bdcd58799
commit
7b86b36048
6 changed files with 106 additions and 0 deletions
2
challenges/2021/14-extendedPolymerization/README.md
Normal file
2
challenges/2021/14-extendedPolymerization/README.md
Normal file
|
@ -0,0 +1,2 @@
|
|||
# [Day 14: Extended Polymerization](https://adventofcode.com/2021/day/14)
|
||||
|
15
challenges/2021/14-extendedPolymerization/benchmark.json
Normal file
15
challenges/2021/14-extendedPolymerization/benchmark.json
Normal file
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"day": 14,
|
||||
"dir": "challenges/2021/14-extendedPolymerization",
|
||||
"implementations": {
|
||||
"Python": {
|
||||
"part.1.avg": 0.0009306585788726807,
|
||||
"part.1.max": 0.002932310104370117,
|
||||
"part.1.min": 0.0006134510040283203,
|
||||
"part.2.avg": 0.0037261221408843993,
|
||||
"part.2.max": 0.008280754089355469,
|
||||
"part.2.min": 0.0021991729736328125
|
||||
}
|
||||
},
|
||||
"numRuns": 1000
|
||||
}
|
17
challenges/2021/14-extendedPolymerization/info.json
Normal file
17
challenges/2021/14-extendedPolymerization/info.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"inputFile": "input.txt",
|
||||
"testCases": {
|
||||
"one": [
|
||||
{
|
||||
"input": "NNCB\n\nCH -> B\nHH -> N\nCB -> H\nNH -> C\nHB -> C\nHC -> B\nHN -> C\nNN -> C\nBH -> H\nNC -> B\nNB -> B\nBN -> B\nBB -> N\nBC -> B\nCC -> N\nCN -> C",
|
||||
"expected": "1588"
|
||||
}
|
||||
],
|
||||
"two": [
|
||||
{
|
||||
"input": "NNCB\n\nCH -> B\nHH -> N\nCB -> H\nNH -> C\nHB -> C\nHC -> B\nHN -> C\nNN -> C\nBH -> H\nNC -> B\nNB -> B\nBN -> B\nBB -> N\nBC -> B\nCC -> N\nCN -> C",
|
||||
"expected": "2188189693529"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
71
challenges/2021/14-extendedPolymerization/py/__init__.py
Normal file
71
challenges/2021/14-extendedPolymerization/py/__init__.py
Normal file
|
@ -0,0 +1,71 @@
|
|||
import math
|
||||
from typing import Tuple, List, Dict
|
||||
from aocpy import BaseChallenge
|
||||
|
||||
Rule = Tuple[str, str]
|
||||
Pairs = Dict[str, int]
|
||||
|
||||
|
||||
def parse(instr: str) -> Tuple[str, List[Rule]]:
|
||||
seq, rawRules = instr.strip().split("\n\n")
|
||||
return seq, [tuple(x.split(" -> ")) for x in rawRules.splitlines()]
|
||||
|
||||
|
||||
def make_pair_dict(seq: str) -> Pairs:
|
||||
o = {}
|
||||
for i in range(len(seq) - 1):
|
||||
s = seq[i] + seq[i + 1]
|
||||
o[s] = o.get(s, 0) + 1
|
||||
return o
|
||||
|
||||
|
||||
def apply_rules(pairs: Pairs, rules: List[Rule]) -> Pairs:
|
||||
deltas = {}
|
||||
|
||||
for rule in rules:
|
||||
rule_part_a = rule[0][0] + rule[1]
|
||||
rule_part_b = rule[1] + rule[0][1]
|
||||
|
||||
if rule[0] in pairs:
|
||||
count = pairs[rule[0]]
|
||||
deltas[rule[0]] = deltas.get(rule[0], 0) - count
|
||||
deltas[rule_part_a] = deltas.get(rule_part_a, 0) + count
|
||||
deltas[rule_part_b] = deltas.get(rule_part_b, 0) + count
|
||||
|
||||
for key in deltas:
|
||||
new = pairs.get(key, 0) + deltas[key]
|
||||
pairs[key] = 0 if new < 0 else new
|
||||
|
||||
return pairs
|
||||
|
||||
|
||||
def count_letters(pairs: Pairs) -> Dict[str, int]:
|
||||
o = {}
|
||||
for pair in pairs:
|
||||
n = pairs[pair]
|
||||
o[pair[0]] = o.get(pair[0], 0) + n
|
||||
o[pair[1]] = o.get(pair[1], 0) + n
|
||||
|
||||
for key in o:
|
||||
o[key] = math.ceil(o[key] / 2)
|
||||
|
||||
return o
|
||||
|
||||
|
||||
class Challenge(BaseChallenge):
|
||||
@staticmethod
|
||||
def core(instr: str, n: int) -> int:
|
||||
sequence, rules = parse(instr)
|
||||
sequence_pairs = make_pair_dict(sequence)
|
||||
for _ in range(n):
|
||||
sequence_pairs = apply_rules(sequence_pairs, rules)
|
||||
vals = count_letters(sequence_pairs).values()
|
||||
return max(vals) - min(vals)
|
||||
|
||||
@staticmethod
|
||||
def one(instr: str) -> int:
|
||||
return Challenge.core(instr, 10)
|
||||
|
||||
@staticmethod
|
||||
def two(instr: str) -> int:
|
||||
return Challenge.core(instr, 40)
|
|
@ -25,6 +25,7 @@ Solutions to the [2021 Advent of Code](https://adventofcode.com/2021).
|
|||
| 11 - Dumbo Octopus | ★ ★ | [Python](11-dumboOctopus/py), [Nim](11-dumboOctopus/nim) | Cellular automata my beloved <3 |
|
||||
| 12 - Passage Pathing | ★ ★ | [Go](12-passagePathing/go) | I couldn't tell you how it works, but it does kinda work and I think I have a vague idea (external help was used). |
|
||||
| 13 - Transparent Origami | ★ ★ | [Python](13-transparentOrigami/py), [Nim](13-transparentOrigami/nim) | I got stuck for hours on an intermittent off-by-one error. :( |
|
||||
| 14 - Extended Polymerization | ★ ★ | [Python](14-extendedPolymerization/py) | Another off-by-one error, but this time it was because of dodgy division. Wonderful. |
|
||||
|
||||
<!-- PARSE END -->
|
||||
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 50 KiB After Width: | Height: | Size: 51 KiB |
Loading…
Add table
Add a link
Reference in a new issue