-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# The EEHQ Christmas Tree | ||
|
||
![christmas tree](christmastree.gif) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env python3 | ||
# [Day 14: Restroom Redoubt](https://adventofcode.com/2024/day/14) | ||
|
||
import itertools | ||
import re | ||
import sys | ||
from collections import defaultdict | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from random import randint, seed | ||
|
||
from imgcat import imgcat | ||
from PIL import Image | ||
|
||
filename = sys.argv[1] if len(sys.argv) > 1 else "input.txt" | ||
data = Path(filename).read_text().strip() | ||
lines = data.splitlines() | ||
|
||
# to always make the same picture with a given input | ||
seed(sum(map(ord, filename))) | ||
|
||
|
||
@dataclass | ||
class Robot: | ||
px: int | ||
py: int | ||
vx: int | ||
vy: int | ||
|
||
|
||
robots = [] | ||
for line in lines: | ||
m = re.match(r"p=(\d+),(\d+) v=(-?\d+),(-?\d+)", line) | ||
px, py, vx, vy = map(int, m.groups()) | ||
robots.append(Robot(px, py, vx, vy)) | ||
|
||
width = 101 | ||
height = 103 | ||
|
||
for n in itertools.count(5000): | ||
|
||
grid = defaultdict(int) | ||
for robot in robots: | ||
px = (robot.px + robot.vx * n) % width | ||
py = (robot.py + robot.vy * n) % height | ||
|
||
if (px, py) in grid: | ||
break | ||
|
||
grid[px, py] = 1 | ||
else: | ||
break | ||
|
||
picture = Image.new("RGB", (width, height)) | ||
for y in range(height): | ||
for x in range(width): | ||
if grid[x, y] != 0: | ||
h = sum(grid[x + i, y] for i in range(-4, 5)) | ||
v = sum(grid[x, y + i] for i in range(-4, 5)) | ||
|
||
if h >= 5 or v >= 5: | ||
picture.putpixel((x, y), (0, 255, 0)) | ||
else: | ||
picture.putpixel((x, y), (255, randint(0, 255), 0)) | ||
|
||
picture.save("christmastree.png") | ||
imgcat(picture) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
i=0 | ||
declare -a frames=() | ||
for input in ../../data/*/2024/14.in ; do | ||
i=$((i+1)) | ||
|
||
./easteregg.py $input | ||
|
||
mv christmastree.png frame$i.png | ||
frames+=(frame$i.png) | ||
done | ||
|
||
magick -delay 50 -loop 1 ${frames[*]} christmastree.gif | ||
rm ${frames[*]} |