mirror of
https://github.com/cupcakearmy/advent-of-code.git
synced 2025-09-05 23:00:38 +00:00
2024
This commit is contained in:
20
2024/06/findings.md
Normal file
20
2024/06/findings.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# Day 5
|
||||
|
||||
# Part 1
|
||||
|
||||
What a joy! First day where a bit of Computer Science might actually help.
|
||||
So to me this looks like a [DAG](https://en.wikipedia.org/wiki/Directed_acyclic_graph), since if there has to be an order, cycles cannot be allowed. Otherwise the problem could not be solved.
|
||||
|
||||
For not I just hope that it's one cohesive DAG, and not 2 or more. Technically there is a possibility that more than one, disjoined DAGs are defined by the rules. If this occurs, we'll see.
|
||||
|
||||

|
||||
|
||||
We can see that there in this case there is one "start" and one "end". In this case `97` is the start and `13` the end. Again, there might be more than one "start" or "end".
|
||||
|
||||
My idea is to precompile an order so that `97=0` and `13=7` so that we then just map the numbers to their ascending number and check if the array is already sorted. Why? My assumption is that you do the work upfront and then it's efficient to check for each input.
|
||||
|
||||
The start and ends are easy, what about the _middle_ nodes? We can do a so called transitive reduction, simplifying the graph. Basically, if we know that `97` needs to be before `61` and `13` and at the same time `61` needs to be before `13`, we can delete the `97` before `13` link, as it's already specified going over `61`. Basically we eliminate all the "shortcuts". This gives us a nice line in this case.
|
||||
|
||||

|
||||
|
||||
_no code has been written until now, this might go terribly wrong 💩_
|
90
2024/06/main.py
Normal file
90
2024/06/main.py
Normal file
@@ -0,0 +1,90 @@
|
||||
from typing import List, Union, Tuple, Set, Self
|
||||
from dataclasses import dataclass
|
||||
|
||||
type Point = Tuple[int, int]
|
||||
|
||||
TURN = {
|
||||
# (-1,0) > (0, 1) -> (1, 0) -> (0, -1)
|
||||
(-1, 0): (0, 1),
|
||||
(0, 1): (1, 0),
|
||||
(1, 0): (0, -1),
|
||||
(0, -1): (-1, 0),
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class Map:
|
||||
fields: List[List[bool]]
|
||||
position: Point
|
||||
direction: Point
|
||||
max_y: int = 0
|
||||
max_x: int = 0
|
||||
|
||||
def __post_init__(self):
|
||||
self.max_y = len(self.fields)
|
||||
self.max_x = len(self.fields[0])
|
||||
|
||||
def next_step(self):
|
||||
return (
|
||||
self.position[0] + self.direction[0],
|
||||
self.position[1] + self.direction[1],
|
||||
)
|
||||
|
||||
def get(self, point: Point) -> Union[bool, None]:
|
||||
y, x = point
|
||||
try:
|
||||
return self.fields[y][x]
|
||||
except IndexError:
|
||||
return None
|
||||
|
||||
def walk(self):
|
||||
visited: Set[Point] = set()
|
||||
while True:
|
||||
visited.add(self.position)
|
||||
next_step = self.next_step()
|
||||
content = self.get(next_step)
|
||||
if content is None:
|
||||
# Exited map
|
||||
break
|
||||
if content:
|
||||
# Turn
|
||||
self.direction = TURN[self.direction]
|
||||
next_step = self.next_step()
|
||||
self.position = next_step
|
||||
|
||||
return len(visited)
|
||||
|
||||
@staticmethod
|
||||
def parse(raw: str) -> Self:
|
||||
fields = [[f == "#" for f in line] for line in raw.splitlines()]
|
||||
# Find init
|
||||
position = (0, 0)
|
||||
direction = (-1, 0) # Up
|
||||
for y, line in enumerate(raw.splitlines()):
|
||||
for x, f in enumerate(line):
|
||||
if f == "^":
|
||||
print(f"FOUND! {x} {y}")
|
||||
position = (y, x)
|
||||
return Map(fields, position, direction)
|
||||
|
||||
|
||||
def solve(raw: str) -> int:
|
||||
# Part 1
|
||||
part1 = 0
|
||||
part2 = 0
|
||||
|
||||
m = Map.parse(raw)
|
||||
part1 = m.walk()
|
||||
|
||||
return (part1, part2)
|
||||
|
||||
|
||||
# Test
|
||||
with open("./2024/06/test.txt", "r") as f:
|
||||
result = solve(f.read().strip())
|
||||
print(result)
|
||||
|
||||
# Input
|
||||
with open("./2024/06/input.txt", "r") as f:
|
||||
result = solve(f.read().strip())
|
||||
print(result)
|
Reference in New Issue
Block a user