2023.16
This commit is contained in:
parent
4f7b3f4da6
commit
600f9ed848
7 changed files with 165 additions and 0 deletions
1
challenges/2023/16-theFloorWillBeLava/README.md
Normal file
1
challenges/2023/16-theFloorWillBeLava/README.md
Normal file
|
@ -0,0 +1 @@
|
|||
# [Day 16: The Floor Will Be Lava](https://adventofcode.com/2023/day/16)
|
137
challenges/2023/16-theFloorWillBeLava/main.py
Normal file
137
challenges/2023/16-theFloorWillBeLava/main.py
Normal file
|
@ -0,0 +1,137 @@
|
|||
import gridutil.grid as gu
|
||||
import gridutil.coord as cu
|
||||
from enum import Enum, auto
|
||||
import sys
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
class Direction(Enum):
|
||||
Up = auto()
|
||||
Down = auto()
|
||||
Left = auto()
|
||||
Right = auto()
|
||||
|
||||
|
||||
DIRECTION_DELTAS = {
|
||||
Direction.Up: (0, -1),
|
||||
Direction.Down: (0, 1),
|
||||
Direction.Left: (-1, 0),
|
||||
Direction.Right: (1, 0),
|
||||
}
|
||||
|
||||
|
||||
def parse(instr: str) -> gu.Grid:
|
||||
return gu.parse(instr)
|
||||
|
||||
|
||||
def get_n_energised(
|
||||
box: gu.Grid, start_pos: cu.Coordinate, start_directon: Direction
|
||||
) -> int:
|
||||
energised = defaultdict(lambda: [])
|
||||
beams = [(start_pos, start_directon)]
|
||||
|
||||
while beams:
|
||||
(pos, direction) = beams.pop(0)
|
||||
|
||||
while True:
|
||||
if pos not in box:
|
||||
break
|
||||
|
||||
if pos in energised and direction in energised[pos]:
|
||||
break
|
||||
else:
|
||||
energised[pos].append(direction)
|
||||
|
||||
at_pos = box[pos]
|
||||
|
||||
match at_pos:
|
||||
case "/":
|
||||
match direction:
|
||||
case Direction.Up:
|
||||
direction = Direction.Right
|
||||
case Direction.Down:
|
||||
direction = Direction.Left
|
||||
case Direction.Left:
|
||||
direction = Direction.Down
|
||||
case Direction.Right:
|
||||
direction = Direction.Up
|
||||
|
||||
case "\\":
|
||||
match direction:
|
||||
case Direction.Up:
|
||||
direction = Direction.Left
|
||||
case Direction.Down:
|
||||
direction = Direction.Right
|
||||
case Direction.Left:
|
||||
direction = Direction.Up
|
||||
case Direction.Right:
|
||||
direction = Direction.Down
|
||||
|
||||
case "-":
|
||||
if not (
|
||||
direction == Direction.Left or direction == direction.Right
|
||||
):
|
||||
beams.append(
|
||||
(
|
||||
(cu.add(pos, DIRECTION_DELTAS[Direction.Left])),
|
||||
Direction.Left,
|
||||
)
|
||||
)
|
||||
direction = Direction.Right
|
||||
|
||||
case "|":
|
||||
if not (direction == Direction.Up or direction == direction.Down):
|
||||
beams.append(
|
||||
(
|
||||
(cu.add(pos, DIRECTION_DELTAS[Direction.Up])),
|
||||
Direction.Up,
|
||||
)
|
||||
)
|
||||
direction = Direction.Down
|
||||
|
||||
pos = cu.add(pos, DIRECTION_DELTAS[direction])
|
||||
|
||||
return len(energised)
|
||||
|
||||
|
||||
def one(instr: str):
|
||||
box = parse(instr)
|
||||
return get_n_energised(box, (0, 0), Direction.Right)
|
||||
|
||||
|
||||
def two(instr: str):
|
||||
box = parse(instr)
|
||||
|
||||
max_x = gu.get_max_x(box)
|
||||
max_y = gu.get_max_y(box)
|
||||
|
||||
starts = [(0, y) for y in range(max_y + 1)]
|
||||
starts += [(max_x, y) for y in range(max_y + 1)]
|
||||
starts += [(x, 0) for x in range(1, max_x)]
|
||||
starts += [(x, max_y) for x in range(1, max_x)]
|
||||
|
||||
max_energised = 0
|
||||
|
||||
for start in starts:
|
||||
for direction in Direction:
|
||||
x = get_n_energised(box, start, direction)
|
||||
if x > max_energised:
|
||||
max_energised = x
|
||||
|
||||
return max_energised
|
||||
|
||||
|
||||
def _debug(*args, **kwargs):
|
||||
kwargs["file"] = sys.stderr
|
||||
print(*args, **kwargs)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2 or sys.argv[1] not in ["1", "2"]:
|
||||
print("Missing day argument", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
inp = sys.stdin.read().strip()
|
||||
if sys.argv[1] == "1":
|
||||
print(one(inp))
|
||||
else:
|
||||
print(two(inp))
|
10
challenges/2023/16-theFloorWillBeLava/test.txt
Normal file
10
challenges/2023/16-theFloorWillBeLava/test.txt
Normal file
|
@ -0,0 +1,10 @@
|
|||
.|...\....
|
||||
|.-.\.....
|
||||
.....|-...
|
||||
........|.
|
||||
..........
|
||||
.........\
|
||||
..../.\\..
|
||||
.-.-/..|..
|
||||
.|....-|.\
|
||||
..//.|....
|
14
challenges/2023/16-theFloorWillBeLava/tests.json
Normal file
14
challenges/2023/16-theFloorWillBeLava/tests.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"1": [
|
||||
{
|
||||
"is": "46",
|
||||
"input": ".|...\\....\n|.-.\\.....\n.....|-...\n........|.\n..........\n.........\\\n..../.\\\\..\n.-.-/..|..\n.|....-|.\\\n..//.|....\n"
|
||||
}
|
||||
],
|
||||
"2": [
|
||||
{
|
||||
"is": "51",
|
||||
"input": ".|...\\....\n|.-.\\.....\n.....|-...\n........|.\n..........\n.........\\\n..../.\\\\..\n.-.-/..|..\n.|....-|.\\\n..//.|....\n"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -29,3 +29,4 @@ A day denoted with a star means it has a visualisation.
|
|||
| 13 - Point of Incidence | ★ ★ | Python | This one made me feel slightly intelligent. |
|
||||
| 14* - Parabolic Reflector Dish | ★ ★ | Python | Why do I always overcomplicate cycle detection?! |
|
||||
| 15 - Lens Library | ★ ★ | Go | Still took some brainpower but this time the brainpower was needed to work out what the problem was, *not* to work out how to solve the problem. |
|
||||
| 16 - The Floor Will Be Lava | ★ ★ | Python | Pathfinding, sort of, but also brute forceable?? |
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 43 KiB After Width: | Height: | Size: 44 KiB |
|
@ -27,3 +27,5 @@
|
|||
{"day": 14, "part": 2, "runner": "py", "min": 0.48926782608032227, "max": 0.6927425861358643, "avg": 0.5653452634811401, "n": 50}
|
||||
{"day": 15, "part": 1, "runner": "go", "min": 0.0005846023559570312, "max": 0.0011608600616455078, "avg": 0.0007052874565124512, "n": 100}
|
||||
{"day": 15, "part": 2, "runner": "go", "min": 0.0012998580932617188, "max": 0.0028612613677978516, "avg": 0.0014359498023986817, "n": 100}
|
||||
{"day": 16, "part": 1, "runner": "py", "min": 0.030925512313842773, "max": 0.03607058525085449, "avg": 0.03239912986755371, "n": 10}
|
||||
{"day": 16, "part": 2, "runner": "py", "min": 2.7083919048309326, "max": 5.615460634231567, "avg": 4.0310279607772825, "n": 10}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue