Compare commits
3 Commits
ad4adc2372
...
befa6403ba
Author | SHA1 | Date | |
---|---|---|---|
befa6403ba | |||
ab60a501f5 | |||
6ab8ef6d26 |
75
day14.py
Normal file
75
day14.py
Normal file
@ -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)
|
BIN
progress.png
BIN
progress.png
Binary file not shown.
Before Width: | Height: | Size: 123 KiB After Width: | Height: | Size: 123 KiB |
@ -23,4 +23,6 @@
|
||||
12:
|
||||
stars: 2
|
||||
13:
|
||||
stars: 2
|
||||
14:
|
||||
stars: 2
|
12
res/examples/day14.txt
Normal file
12
res/examples/day14.txt
Normal file
@ -0,0 +1,12 @@
|
||||
p=0,4 v=3,-3
|
||||
p=6,3 v=-1,-3
|
||||
p=10,3 v=-1,2
|
||||
p=2,0 v=2,-1
|
||||
p=0,0 v=1,3
|
||||
p=3,0 v=-2,-2
|
||||
p=7,6 v=-1,-3
|
||||
p=3,0 v=-1,-2
|
||||
p=9,3 v=2,3
|
||||
p=7,3 v=-1,2
|
||||
p=2,4 v=2,-3
|
||||
p=9,5 v=-3,-3
|
65
src/day14/puzzle1.typ
Normal file
65
src/day14/puzzle1.typ
Normal file
@ -0,0 +1,65 @@
|
||||
#import "/src/utils.typ": *
|
||||
|
||||
#let regexp = regex("^p=(.*?),(.*?) v=(.*?),(.*?)$")
|
||||
|
||||
#let simulate(v0, dv, max: 1, steps: 1) = {
|
||||
return calc.rem-euclid(v0 + dv * steps, max)
|
||||
}
|
||||
|
||||
#let parse-input(input) = {
|
||||
return input.split("\n").map(b => {
|
||||
let m = b.match(regexp)
|
||||
return (
|
||||
pos: (
|
||||
x: int(m.captures.at(0)),
|
||||
y: int(m.captures.at(1)),
|
||||
),
|
||||
vel: (
|
||||
x: int(m.captures.at(2)),
|
||||
y: int(m.captures.at(3))
|
||||
)
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#let solve(w: 0, h: 0, steps: 100, input) = {
|
||||
assert(w != 0, message: "Width must be != 0")
|
||||
assert(h != 0, message: "Height must be != 0")
|
||||
|
||||
let bots = parse-input(input)
|
||||
|
||||
let quadrants = (
|
||||
tl: 0,
|
||||
tr: 0,
|
||||
bl: 0,
|
||||
br: 0
|
||||
)
|
||||
let half-w = calc.div-euclid(w, 2)
|
||||
let half-h = calc.div-euclid(h, 2)
|
||||
|
||||
let sim-x = simulate.with(max: w, steps: steps)
|
||||
let sim-y = simulate.with(max: h, steps: steps)
|
||||
for bot in bots {
|
||||
let x2 = sim-x(bot.pos.x, bot.vel.x)
|
||||
let y2 = sim-y(bot.pos.y, bot.vel.y)
|
||||
|
||||
if x2 == half-w or y2 == half-h {
|
||||
continue
|
||||
}
|
||||
let quadrant = (
|
||||
(if y2 < half-h {"t"} else {"b"}) +
|
||||
(if x2 < half-w {"l"} else {"r"})
|
||||
)
|
||||
quadrants.at(quadrant) += 1
|
||||
}
|
||||
|
||||
return quadrants.values().product()
|
||||
}
|
||||
|
||||
#show-puzzle(
|
||||
14, 1,
|
||||
solve.with(w: 101, h: 103),
|
||||
example: (
|
||||
(result: 12, args: (w: 11, h: 7)),
|
||||
)
|
||||
)
|
33
src/day14/puzzle2.typ
Normal file
33
src/day14/puzzle2.typ
Normal file
@ -0,0 +1,33 @@
|
||||
#import "/src/utils.typ": *
|
||||
#import "@preview/cetz:0.3.1": canvas, draw
|
||||
#import "puzzle1.typ": parse-input, simulate
|
||||
|
||||
#let input = get-input(14)
|
||||
#let bots = parse-input(input)
|
||||
#let res = 8270
|
||||
#let (width, height) = (101, 103)
|
||||
#let sim-x = simulate.with(max: width, steps: res)
|
||||
#let sim-y = simulate.with(max: height, steps: res)
|
||||
#let size = 0.1
|
||||
|
||||
#figure(
|
||||
canvas({
|
||||
draw.rect(
|
||||
(0, 0),
|
||||
(width * size, -height * size)
|
||||
)
|
||||
for bot in bots {
|
||||
let x = sim-x(bot.pos.x, bot.vel.x)
|
||||
let y = sim-y(bot.pos.y, bot.vel.y)
|
||||
draw.rect(
|
||||
(x * size, -y * size),
|
||||
((x + 1) * size, -(y + 1) * size),
|
||||
stroke: none,
|
||||
fill: black
|
||||
)
|
||||
}
|
||||
}),
|
||||
caption: "Christmas tree easter egg"
|
||||
)
|
||||
|
||||
#show-result(res)
|
BIN
src/main.pdf
BIN
src/main.pdf
Binary file not shown.
@ -81,6 +81,12 @@
|
||||
for (suffix, result) in example.pairs() {
|
||||
check-example(day, func, result, suffix: suffix)
|
||||
}
|
||||
} else if type(example) == array {
|
||||
for ex in example {
|
||||
let suffix = ex.at("suffix", default: none)
|
||||
let args = ex.at("args", default: (:))
|
||||
check-example(day, func.with(..args), ex.result, suffix: suffix)
|
||||
}
|
||||
} else {
|
||||
check-example(day, func, example)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user