From befa6403ba618d88bc68cd22d318d486406f9509 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Sun, 15 Dec 2024 14:12:08 +0100 Subject: [PATCH] day 14 puzzle 2 (python) --- day14.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 day14.py diff --git a/day14.py b/day14.py new file mode 100644 index 0000000..e21ef90 --- /dev/null +++ b/day14.py @@ -0,0 +1,75 @@ +import re +import pygame + +with open("res/inputs/day14.txt", "r") as f: + lines = f.read().split("\n") + +bots = [] + +for line in lines: + m = re.match("^p=(.*?),(.*?) v=(.*?),(.*?)$", line) + bot = { + "pos": { + "x": int(m.group(1)), + "y": int(m.group(2)), + }, + "vel": { + "x": int(m.group(3)), + "y": int(m.group(4)), + } + } + bots.append(bot) + +W = 101 +H = 103 +pygame.init() +win = pygame.display.set_mode([W, H]) + +def display(): + win.fill(0) + for bot in bots: + win.set_at((bot["pos"]["x"], bot["pos"]["y"]), (255, 255, 255)) + + pygame.display.flip() + +def move(f): + for bot in bots: + bot["pos"]["x"] = (bot["pos"]["x"] + bot["vel"]["x"] * f) % W + bot["pos"]["y"] = (bot["pos"]["y"] + bot["vel"]["y"] * f) % H + +clock = pygame.time.Clock() +running = True +step = 0 +update = True +auto = True + +while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: + running = False + elif event.key == pygame.K_RIGHT: + step += 1 + move(1) + update = True + elif event.key == pygame.K_LEFT: + step -= 1 + move(-1) + update = True + elif event.key == pygame.K_SPACE: + auto = not auto + + if auto: + move(1) + step += 1 + update = True + + if update: + print(step) + pygame.display.set_caption(f"Day 14 - step {step} - {clock.get_fps():.2f}fps") + display() + update = False + + clock.tick(20)