aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

commit 35efed10bdc5ea2ccdfbdb5a114d67b1e1e4af82 (patch)
parent 3d1d1df937b997715071049f4597afcaf5067a1e
Author: Alex Karle <alex@alexkarle.com>
Date:   Sun, 15 Dec 2024 03:01:38 +0100

2024: Add day 14

Diffstat:
A2024/14/1.py | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2024/14/2.py | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 142 insertions(+), 0 deletions(-)

diff --git a/2024/14/1.py b/2024/14/1.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 +import sys +import re + +class Bot: + def __init__(self, x, y, dx, dy): + self.x = x + self.y = y + self.dx = dx + self.dy = dy + def __str__(self): + return f"Bot({self.x}, {self.y}, {self.dx}, {self.dy})" + +lre = re.compile("p=(\d+),(\d+) v=(-?\d+),(-?\d+)") + +def viewmap(bots, maxxy, maxy): + locs = {} + for b in bots: + locs[(b.x, b.y)] = locs.get((b.x, b.y), 0) + 1 + + for y in range(maxy): + for x in range(maxx): + if (x, y) in locs: + print(locs[x, y], end="") + else: + print(".", end="") + print("") + +def cycle(bots, maxx, maxy): + for b in bots: + b.x = (b.x + b.dx) % maxx + b.y = (b.y + b.dy) % maxy + +def score(bots, maxx, maxy): + quads = [0, 0, 0, 0] # top-left, top-right, bot-left, bot-right + for b in bots: + if b.x == maxx // 2 or b.y == maxy // 2: + continue + else: + quad = 0 + if b.x > maxx // 2: + quad += 1 + if b.y > maxy // 2: + quad += 2 + quads[quad] += 1 + return quads[0] * quads[1] * quads[2] * quads[3] + +def parse(): + bots = [] + maxx = 0 + maxy = 0 + for l in sys.stdin: + m = lre.match(l) + nums = [int(x) for x in m.groups()] + b = Bot(*nums) + bots.append(b) + if b.x > maxx: + maxx = b.x + if b.y > maxy: + maxy = b.y + return bots, maxx + 1, maxy + 1 + +if __name__ == '__main__': + bots, maxx, maxy = parse() + # viewmap(bots, maxx, maxy) + for i in range(100): + cycle(bots, maxx, maxy) + # print("---") + # viewmap(bots, maxx, maxy) + + print(score(bots, maxx, maxy)) diff --git a/2024/14/2.py b/2024/14/2.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 +import sys +import re + +class Bot: + def __init__(self, x, y, dx, dy): + self.x = x + self.y = y + self.dx = dx + self.dy = dy + def __str__(self): + return f"Bot({self.x}, {self.y}, {self.dx}, {self.dy})" + +lre = re.compile("p=(\d+),(\d+) v=(-?\d+),(-?\d+)") + +def viewmap(bots, maxxy, maxy): + locs = {} + for b in bots: + locs[(b.x, b.y)] = locs.get((b.x, b.y), 0) + 1 + + for y in range(maxy): + for x in range(maxx): + if (x, y) in locs: + print(locs[x, y], end="") + else: + print(" ", end="") + print("") + +def cycle(bots, maxx, maxy): + for b in bots: + b.x = (b.x + b.dx) % maxx + b.y = (b.y + b.dy) % maxy + +def score(bots, maxx, maxy): + quads = [0, 0, 0, 0] # top-left, top-right, bot-left, bot-right + for b in bots: + if b.x == maxx // 2 or b.y == maxy // 2: + continue + else: + quad = 0 + if b.x > maxx // 2: + quad += 1 + if b.y > maxy // 2: + quad += 2 + quads[quad] += 1 + return quads[0] * quads[1] * quads[2] * quads[3] + +def parse(): + bots = [] + maxx = 0 + maxy = 0 + for l in sys.stdin: + m = lre.match(l) + nums = [int(x) for x in m.groups()] + b = Bot(*nums) + bots.append(b) + if b.x > maxx: + maxx = b.x + if b.y > maxy: + maxy = b.y + return bots, maxx + 1, maxy + 1 + +if __name__ == '__main__': + bots, maxx, maxy = parse() + for i in range(10000): + if i % maxx == 39 or i % maxy == 99: + print(f"{i} ---------------") + viewmap(bots, maxx, maxy) + cycle(bots, maxx, maxy) + + print(score(bots, maxx, maxy))