2022-13
Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
parent
6f282e5761
commit
5f4ec35ccf
6 changed files with 116 additions and 1 deletions
1
challenges/2022/13-distressSignal/README.md
Normal file
1
challenges/2022/13-distressSignal/README.md
Normal file
|
@ -0,0 +1 @@
|
|||
# [Day 13: Distress Signal](https://adventofcode.com/2022/day/13)
|
15
challenges/2022/13-distressSignal/benchmark.json
Normal file
15
challenges/2022/13-distressSignal/benchmark.json
Normal file
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"day": 13,
|
||||
"dir": "challenges/2022/13-distressSignal",
|
||||
"implementations": {
|
||||
"Python": {
|
||||
"part.1.avg": 0.002429138422012329,
|
||||
"part.1.max": 0.013918876647949219,
|
||||
"part.1.min": 0.0013120174407958984,
|
||||
"part.2.avg": 0.008559255361557007,
|
||||
"part.2.max": 0.03811001777648926,
|
||||
"part.2.min": 0.004506111145019531
|
||||
}
|
||||
},
|
||||
"numRuns": 1000
|
||||
}
|
17
challenges/2022/13-distressSignal/info.json
Normal file
17
challenges/2022/13-distressSignal/info.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"inputFile": "input.txt",
|
||||
"testCases": {
|
||||
"one": [
|
||||
{
|
||||
"input": "[1,1,3,1,1]\n[1,1,5,1,1]\n\n[[1],[2,3,4]]\n[[1],4]\n\n[9]\n[[8,7,6]]\n\n[[4,4],4,4]\n[[4,4],4,4,4]\n\n[7,7,7,7]\n[7,7,7]\n\n[]\n[3]\n\n[[[]]]\n[[]]\n\n[1,[2,[3,[4,[5,6,7]]]],8,9]\n[1,[2,[3,[4,[5,6,0]]]],8,9]\n",
|
||||
"expected": "13"
|
||||
}
|
||||
],
|
||||
"two": [
|
||||
{
|
||||
"input": "[1,1,3,1,1]\n[1,1,5,1,1]\n\n[[1],[2,3,4]]\n[[1],4]\n\n[9]\n[[8,7,6]]\n\n[[4,4],4,4]\n[[4,4],4,4,4]\n\n[7,7,7,7]\n[7,7,7]\n\n[]\n[3]\n\n[[[]]]\n[[]]\n\n[1,[2,[3,[4,[5,6,7]]]],8,9]\n[1,[2,[3,[4,[5,6,0]]]],8,9]\n",
|
||||
"expected": "140"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
82
challenges/2022/13-distressSignal/py/__init__.py
Normal file
82
challenges/2022/13-distressSignal/py/__init__.py
Normal file
|
@ -0,0 +1,82 @@
|
|||
from typing import *
|
||||
from aocpy import BaseChallenge
|
||||
import json
|
||||
from functools import cmp_to_key
|
||||
|
||||
|
||||
_Pair = Union[int, List['_Pair']]
|
||||
Pair = Tuple[_Pair, _Pair]
|
||||
|
||||
def parse(instr: str) -> List[Pair]:
|
||||
res = []
|
||||
|
||||
for raw_pair in instr.strip().split("\n\n"):
|
||||
a, b = raw_pair.splitlines()
|
||||
res.append((
|
||||
json.loads(a),
|
||||
json.loads(b),
|
||||
))
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def is_pair_well_ordered(pair: Pair) -> Optional[bool]:
|
||||
(a, b) = pair
|
||||
type_a, type_b = type(a), type(b)
|
||||
|
||||
if type_a == int and type_b == list:
|
||||
return is_pair_well_ordered(([a], b))
|
||||
|
||||
elif type_a == list and type_b == int:
|
||||
return is_pair_well_ordered((a, [b]))
|
||||
|
||||
elif type_a == int and type_b == int:
|
||||
if a == b:
|
||||
return None
|
||||
return a < b
|
||||
|
||||
elif type_a == list and type_b == list:
|
||||
|
||||
for x in zip(a, b):
|
||||
y = is_pair_well_ordered(x)
|
||||
if y is not None:
|
||||
return y
|
||||
|
||||
la, lb = len(a), len(b)
|
||||
|
||||
if la == lb:
|
||||
return None
|
||||
return la < lb
|
||||
|
||||
raise ValueError(f"impossible combiation of types ({type_a} and {type_b})")
|
||||
|
||||
|
||||
@cmp_to_key
|
||||
def is_well_ordered(a, b: Any) -> int:
|
||||
x = is_pair_well_ordered((a, b))
|
||||
if x is None:
|
||||
return 0
|
||||
return 1 if x else -1
|
||||
|
||||
|
||||
class Challenge(BaseChallenge):
|
||||
|
||||
@staticmethod
|
||||
def one(instr: str) -> int:
|
||||
inp = parse(instr)
|
||||
sigma = 0
|
||||
for i, pair in enumerate(inp):
|
||||
if is_pair_well_ordered(pair):
|
||||
sigma += i + 1
|
||||
return sigma
|
||||
|
||||
@staticmethod
|
||||
def two(instr: str) -> int:
|
||||
inp = [[[2]], [[6]]]
|
||||
for (a, b) in parse(instr):
|
||||
inp.append(a)
|
||||
inp.append(b)
|
||||
|
||||
inp.sort(key=is_well_ordered, reverse=True)
|
||||
|
||||
return (inp.index([[2]]) + 1) * (inp.index([[6]]) + 1)
|
|
@ -28,7 +28,7 @@ The red dotted line denotes 15 seconds.
|
|||
| 10 - Cathode-Ray Tube | ★ ★ | [Python](10-cathodeRayTube/py/__init__.py) | A nasty problem with a nasty solution and nasty outputs that mess with my framework. |
|
||||
| 11 - Monkey in the Middle | ★ ★ | [Python](11-monkeyInTheMiddle/py/__init__.py) | Return of Advent of Maths! |
|
||||
| 12 - Hill Climbing Algorithm | ★ ★ | [Python](12-hillClimbingAlgorithm/py/__init__.py) | Iiiiiiiiiiiiiiiit's Djikstra's! |
|
||||
| 13 - Distress Signal | ☆ ☆ | | |
|
||||
| 13 - Distress Signal | ★ ★ | [Python](13-distressSignal/py/__init__.py) | Sorting some weird pairs of values with weird rules |
|
||||
| 14 - Regolith Reservoir | ★ ★ | [Python](14-regolithReservoir/py/__init__.py) | Simulating falling sand |
|
||||
| 15 - Beacon Exclusion Zone | ★ ★ | [Python](15-beaconExclusionZone/py/__init__.py) | Searching through a 4000000^2 size grid for a literal single empty spot |
|
||||
| 16 - Proboscidea Volcanium | ★ ★ | [Python](16-proboscideaVolcanium/py/__init__.py) | Nasty combinatorics |
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 63 KiB After Width: | Height: | Size: 69 KiB |
Loading…
Add table
Add a link
Reference in a new issue