Add 2021-11 in Python
Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
parent
6e0c944e03
commit
4c09f7fd3a
6 changed files with 129 additions and 0 deletions
2
challenges/2021/11-dumboOctopus/README.md
Normal file
2
challenges/2021/11-dumboOctopus/README.md
Normal file
|
@ -0,0 +1,2 @@
|
|||
# [Day 11: Dumbo Octopus](https://adventofcode.com/2021/day/11)
|
||||
|
15
challenges/2021/11-dumboOctopus/benchmark.json
Normal file
15
challenges/2021/11-dumboOctopus/benchmark.json
Normal file
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"day": 11,
|
||||
"dir": "challenges/2021/11-dumboOctopus",
|
||||
"implementations": {
|
||||
"Python": {
|
||||
"part.1.avg": 0.007964067935943604,
|
||||
"part.1.max": 0.022200822830200195,
|
||||
"part.1.min": 0.0058345794677734375,
|
||||
"part.2.avg": 0.03082272505760193,
|
||||
"part.2.max": 0.07726001739501953,
|
||||
"part.2.min": 0.023880720138549805
|
||||
}
|
||||
},
|
||||
"numRuns": 1000
|
||||
}
|
21
challenges/2021/11-dumboOctopus/info.json
Normal file
21
challenges/2021/11-dumboOctopus/info.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"inputFile": "input.txt",
|
||||
"testCases": {
|
||||
"one": [
|
||||
{
|
||||
"input": "11111\n19991\n19191\n19991\n11111\n",
|
||||
"expected": "-1"
|
||||
},
|
||||
{
|
||||
"input": "5483143223\n2745854711\n5264556173\n6141336146\n6357385478\n4167524645\n2176841721\n6882881134\n4846848554\n5283751526\n",
|
||||
"expected": "1656"
|
||||
}
|
||||
],
|
||||
"two": [
|
||||
{
|
||||
"input": "5483143223\n2745854711\n5264556173\n6141336146\n6357385478\n4167524645\n2176841721\n6882881134\n4846848554\n5283751526\n",
|
||||
"expected": "195"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
90
challenges/2021/11-dumboOctopus/py/__init__.py
Normal file
90
challenges/2021/11-dumboOctopus/py/__init__.py
Normal file
|
@ -0,0 +1,90 @@
|
|||
from typing import Dict, Tuple, List
|
||||
from aocpy import BaseChallenge
|
||||
|
||||
Point = Tuple[int, int]
|
||||
Cave = Dict[Point, int]
|
||||
|
||||
def parse(instr: str) -> Cave:
|
||||
# I would be lying if I said I didn't copy this from day 9
|
||||
o = {}
|
||||
for y, line in enumerate(instr.strip().splitlines()):
|
||||
for x, digit in enumerate(line):
|
||||
o[(x, y)] = int(digit)
|
||||
return o
|
||||
|
||||
def print_cave(cave: Cave):
|
||||
max_x = max(x for x, y in cave)
|
||||
max_y = max(y for x, y in cave)
|
||||
for y in range(max_y+1):
|
||||
for x in range(max_x+1):
|
||||
print(cave[(x, y)], end="")
|
||||
print(flush=True)
|
||||
print()
|
||||
|
||||
|
||||
def get_adjacent_points(point: Point) -> List[Point]:
|
||||
x, y = point
|
||||
return [(x, y - 1), (x - 1, y), (x + 1, y), (x, y + 1), (x-1, y-1), (x+1, y+1), (x-1, y+1), (x+1, y-1)]
|
||||
|
||||
|
||||
def iterate(cave: Cave) -> Tuple[Cave, int, bool]:
|
||||
|
||||
for point in cave:
|
||||
cave[point] = cave[point] + 1
|
||||
|
||||
updates = {}
|
||||
has_flashed = []
|
||||
|
||||
def handle_nine(point: Point):
|
||||
updates[point] = 0
|
||||
has_flashed.append(point)
|
||||
for adjacent in get_adjacent_points(point):
|
||||
if adjacent not in cave:
|
||||
continue
|
||||
|
||||
previous_value = cave[adjacent]
|
||||
if adjacent in updates:
|
||||
previous_value = updates[adjacent]
|
||||
|
||||
updates[adjacent] = previous_value + 1
|
||||
|
||||
if previous_value + 1 > 9 and adjacent not in has_flashed:
|
||||
handle_nine(adjacent)
|
||||
|
||||
for point in cave:
|
||||
if cave[point] > 9:
|
||||
if point not in has_flashed:
|
||||
handle_nine(point)
|
||||
|
||||
for point in updates:
|
||||
cave[point] = updates[point]
|
||||
|
||||
for point in has_flashed:
|
||||
cave[point] = 0
|
||||
|
||||
return cave, len(has_flashed), len(has_flashed) == len(cave)
|
||||
|
||||
|
||||
class Challenge(BaseChallenge):
|
||||
|
||||
@staticmethod
|
||||
def one(instr: str) -> int:
|
||||
cave = parse(instr)
|
||||
sigma = 0
|
||||
for _ in range(100):
|
||||
c, n, _ = iterate(cave)
|
||||
cave = c
|
||||
sigma += n
|
||||
return sigma
|
||||
|
||||
@staticmethod
|
||||
def two(instr: str) -> int:
|
||||
cave = parse(instr)
|
||||
i = 0
|
||||
while True:
|
||||
i += 1
|
||||
c, _, all_flashed = iterate(cave)
|
||||
if all_flashed:
|
||||
break
|
||||
cave = c
|
||||
return i
|
|
@ -20,6 +20,7 @@ Solutions to the [2021 Advent of Code](https://adventofcode.com/2021).
|
|||
| 08 - Seven Segment Search | Complete | [Python](08-sevenSegmentSearch/py), [Go](08-sevenSegmentSearch) | I may have taken the easy way out for part two, but it does work! No-one ever said the smart solution is the best solution, anyway. |
|
||||
| 09 - Smoke Basin \* | Complete | [Python](09-smokeBasin/py) | Schmokey! Also, as it turns out, I struggle to implement basic logic. Fun. |
|
||||
| 10 - Syntax Scoring | Complete | [Python](10-syntaxScoring/py) | I can't say I've ever broken something so thoroughly that it has a syntax error on *every* line... |
|
||||
| 11 - Dumbo Octopus | Complete | [Python](11-dumboOctopus/py) | Cellular automata my beloved <3 |
|
||||
|
||||
<!-- PARSE END -->
|
||||
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 46 KiB After Width: | Height: | Size: 48 KiB |
Loading…
Add table
Add a link
Reference in a new issue