Skip to content

Instantly share code, notes, and snippets.

@galleon
Created December 15, 2021 12:08
Show Gist options
  • Save galleon/09037de5fd96dfd8e8583bf435612a57 to your computer and use it in GitHub Desktop.
Save galleon/09037de5fd96dfd8e8583bf435612a57 to your computer and use it in GitHub Desktop.
Part of day 15 for 2021
class AdventDay15(AdventDay):
def __init__(self, test: bool = False):
super().__init__(test=test)
def load_input(self):
print("Loading input...")
outputs = []
with open(self.filename, "r") as f:
lines = f.readlines()
for line in lines:
outputs.append(line.strip())
return outputs
def process(self, values, part):
board = Board.from_text(values)
n = 1
if part == 2:
n = 5
tb = Board(n * board._height, n * board._width)
for ib in range(n):
for jb in range(n):
for xy, value in board._kv.items():
value = (int(value) + ib + jb - 1) % 9 + 1
tb[xy[0] + ib * board._width, xy[1] + jb * board._height] = value
exit = (tb._width - 1, tb._height - 1)
print(tb._height, tb._width, exit)
hiphop = []
heappush(hiphop, (0, (0, 0)))
seen = {(0, 0)}
while hiphop:
distance, pos = heappop(hiphop)
if pos == exit:
break
x, y = pos
for neighbour in tb.neighbors(x, y, diagonals=False, valid_only=True):
if neighbour not in seen:
seen.add(neighbour)
heappush(hiphop, (distance + int(tb[neighbour]), neighbour))
return distance
def part1(self):
lines = self.load_input()
print(self.process(lines, 1))
def part2(self):
lines = self.load_input()
print(self.process(lines, 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment