Day 14: Parabolic Reflector Dish
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
🔒 Thread is locked until there's at least 100 2 star entries on the global leaderboard
Edit: 🔓 Unlocked
Python
Also on Github
import numpy as np from .solver import Solver def _tilt(row: list[int], reverse: bool = False) -> list[int]: res = row[::-1] if reverse else row[:] rock_x = 0 for x, item in enumerate(res): if item == 1: rock_x = x + 1 if item == 2: if rock_x < x: res[rock_x] = 2 res[x] = 0 rock_x += 1 return res[::-1] if reverse else res class Day14(Solver): data: np.ndarray def __init__(self): super().__init__(14) def presolve(self, input: str): lines = input.splitlines() self.data = np.zeros((len(lines), len(lines[0])), dtype=np.int8) for x, line in enumerate(lines): for y, char in enumerate(line): if char == '#': self.data[x, y] = 1 elif char == 'O': self.data[x, y] = 2 def solve_first_star(self) -> int: for y in range(self.data.shape[1]): self.data[:, y] = _tilt(self.data[:, y].tolist()) return sum((self.data.shape[0] - x) * (self.data[x] == 2).sum() for x in range(self.data.shape[0])) def solve_second_star(self) -> int: seen = {} order = [] for i in range(1_000_000_000): order += [self.data.copy()] s = self.data.tobytes() if s in seen: loop_size = i - seen[s] remainder = (1_000_000_000 - i) % loop_size self.data = order[seen[s] + remainder] break seen[s] = i for y in range(self.data.shape[1]): self.data[:, y] = _tilt(self.data[:, y].tolist()) for x in range(self.data.shape[0]): self.data[x, :] = _tilt(self.data[x, :].tolist()) for y in range(self.data.shape[1]): self.data[:, y] = _tilt(self.data[:, y].tolist(), reverse=True) for x in range(self.data.shape[0]): self.data[x, :] = _tilt(self.data[x, :].tolist(), reverse=True) return sum((self.data.shape[0] - x) * (self.data[x] == 2).sum() for x in range(self.data.shape[0]))
33.938 line-seconds (ranks 3rd hardest after days 8 and 12 so far).
If you use numpy you could just take advantage of np.rot90 function to do the tilting for you:)
Oh yeah, great idea, thanks!