Add 2021-25 in Python

Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
akp 2021-12-25 18:19:09 +00:00
parent 2b91880512
commit a23e78200a
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
6 changed files with 130 additions and 0 deletions

View file

@ -0,0 +1,2 @@
# [Day 25: Sea Cucumber](https://adventofcode.com/2021/day/25)

View file

@ -0,0 +1,15 @@
{
"day": 25,
"dir": "challenges/2021/25-seaCucumber",
"implementations": {
"Python": {
"part.1.avg": 1.2896365237236023,
"part.1.max": 1.4006781578063965,
"part.1.min": 1.2583773136138916,
"part.2.avg": 0.000003247261047363281,
"part.2.max": 0.000013828277587890625,
"part.2.min": 0.0000016689300537109375
}
},
"numRuns": 100
}

View file

@ -0,0 +1,15 @@
{
"inputFile": "input.txt",
"testCases": {
"one": [
{
"input": "v...>>.vv>\n.vv>>.vv..\n>>.>v>...v\n>>v>>.>.v.\nv>v.vv.v..\n>.>>..v...\n.vv..>.>v.\nv.v..>>v.v\n....v..v.>",
"expected": "58"
}
],
"two": []
},
"incorrect": {
"one": ["406", "312"]
}
}

View file

@ -0,0 +1,94 @@
from typing import Any, List, Tuple, Dict
from aocpy import BaseChallenge
CUCUMBER_EAST = ">"
CUCUMBER_SOUTH = "v"
EMPTY = "."
Point = Tuple[int, int]
SeaBed = Dict[Point, str]
def parse(instr: str) -> SeaBed:
lines = instr.strip().splitlines()
o: SeaBed = {}
for y, line in enumerate(lines):
for x, char in enumerate(line):
o[(x, y)] = char
return o
def iterate_once(sea_bed: SeaBed) -> bool:
# returns true if moves were made
moves_made = False
changes: SeaBed = {}
def get_point_state(p: Point) -> str:
# if p in changes:
# return changes[p]
return
# eastbound
for point in sea_bed:
point_content = sea_bed[point]
if point_content != CUCUMBER_EAST:
continue
x, y = point
next_point = (x + 1, y)
if next_point not in sea_bed:
next_point = (0, y)
if sea_bed[next_point] == EMPTY:
moves_made = True
changes[next_point] = point_content
changes[point] = EMPTY
for point in changes:
sea_bed[point] = changes[point]
changes = {}
# southbound
for point in sea_bed:
point_content = sea_bed[point]
if point_content != CUCUMBER_SOUTH:
continue
x, y = point
next_point = (x, y + 1)
if next_point not in sea_bed:
next_point = (x, 0)
if sea_bed[next_point] == EMPTY:
moves_made = True
changes[next_point] = point_content
changes[point] = EMPTY
for point in changes:
sea_bed[point] = changes[point]
return moves_made
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> int:
sea_bed = parse(instr)
i = 1
while iterate_once(sea_bed):
i += 1
return i
@staticmethod
def two(instr: str) -> int:
return -1

View file

@ -34,6 +34,10 @@ Solutions to the [2021 Advent of Code](https://adventofcode.com/2021).
| 20 - Trench Map \* | ★ ★ | [Python](20-trenchMap/py), [Nim](20-trenchMap/nim) | Took a moment to realise that the infinite grid alternates between lit and unlit, and even then I had to look at someone else's solution to realise it. |
| 21 - Dirac Dice | ★ ☆ | [Python](21-diracDice/py) | Couldn't be bothered with part two - I've got too much going on at the moment to spend a lot of time thinking about it. |
| 22 - Reactor Reboot | ★ ★ | [Python, OpenSCAD and Blender](22-reactorReboot/) | I did not expect to end up using 3D CAD software to complete AoC... |
| 23 - Amphipod | Unattempted | | |
| 24 - Arithmetic Logic Unit | Unattempted | | |
| 25 - Sea Cucumber | ★ ☆ | [Python](25-seaCucumber/py) | Missing some stars :( |
<!-- PARSE END -->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

After

Width:  |  Height:  |  Size: 38 KiB

Before After
Before After