Upload aoc.csv
Browse files
aoc.csv
CHANGED
@@ -16069,52 +16069,41 @@ Your puzzle answer was 219150360.
|
|
16069 |
|
16070 |
During the bathroom break, someone notices that these robots seem awfully similar to ones built and used at the North Pole. If they're the same type of robots, they should have a hard-coded Easter egg: very rarely, most of the robots should arrange themselves into a picture of a Christmas tree.
|
16071 |
|
16072 |
-
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?",8053,"import
|
16073 |
-
from
|
16074 |
-
from operator import mul
|
16075 |
-
|
16076 |
-
with open('input.txt') as f:
|
16077 |
-
lines = f.read().splitlines()
|
16078 |
-
|
16079 |
-
positions = []
|
16080 |
-
velocities = []
|
16081 |
-
for line in lines:
|
16082 |
-
m = re.match(""p=(\\d+),(\\d+) v=(\\-?\\d+),(\\-?\\d+)"", line)
|
16083 |
-
positions.append(tuple(map(int, m.groups()[0:2])))
|
16084 |
-
velocities.append(tuple(map(int, m.groups()[2:])))
|
16085 |
-
|
16086 |
-
x_len = 101
|
16087 |
-
y_len = 103
|
16088 |
-
|
16089 |
-
initial_positions = positions[:]
|
16090 |
-
|
16091 |
-
for num_seconds in range(10000):
|
16092 |
-
for i in range(len(positions)):
|
16093 |
-
new_x = (initial_positions[i][0] + num_seconds * velocities[i][0]) % x_len
|
16094 |
-
new_y = (initial_positions[i][1] + num_seconds * velocities[i][1]) % y_len
|
16095 |
-
positions[i] = (new_x, new_y)
|
16096 |
-
|
16097 |
-
grid = [[0] * x_len for j in range(y_len)]
|
16098 |
|
16099 |
-
|
16100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16101 |
|
16102 |
-
|
16103 |
-
for y in range(y_len):
|
16104 |
-
for x in range(x_len):
|
16105 |
-
if grid[y][x] > 1:
|
16106 |
-
no_dupes = False
|
16107 |
-
break
|
16108 |
|
16109 |
-
|
16110 |
-
|
16111 |
-
|
16112 |
-
|
16113 |
-
for x in range(x_len):
|
16114 |
-
v = grid[y][x]
|
16115 |
-
row += f'{v}' if v > 0 else '.'
|
16116 |
-
print(row)
|
16117 |
-
break",python:3.9.21-slim
|
16118 |
2024,14,2,"--- Day 14: Restroom Redoubt ---
|
16119 |
|
16120 |
One of The Historians needs to use the bathroom; fortunately, you know there's a bathroom near an unvisited location on their list, and so you're all quickly teleported directly to the lobby of Easter Bunny Headquarters.
|
@@ -16387,48 +16376,25 @@ Your puzzle answer was 219150360.
|
|
16387 |
|
16388 |
During the bathroom break, someone notices that these robots seem awfully similar to ones built and used at the North Pole. If they're the same type of robots, they should have a hard-coded Easter egg: very rarely, most of the robots should arrange themselves into a picture of a Christmas tree.
|
16389 |
|
16390 |
-
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?",8053,"import re
|
16391 |
-
|
16392 |
-
|
16393 |
-
|
16394 |
-
|
16395 |
-
|
16396 |
-
|
16397 |
-
|
16398 |
-
|
16399 |
-
|
16400 |
-
|
16401 |
-
|
16402 |
-
|
16403 |
-
|
16404 |
-
|
16405 |
-
|
16406 |
-
|
16407 |
-
|
16408 |
-
|
16409 |
-
w, h = 101, 103
|
16410 |
-
|
16411 |
-
# w, h = 11, 7
|
16412 |
-
# input = test_data
|
16413 |
-
|
16414 |
-
start_robots = []
|
16415 |
-
for l in input:
|
16416 |
-
a, b, c, d = re.match(r""p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)"", l).groups()
|
16417 |
-
start_robots.append((int(a),int(b),int(c),int(d)))
|
16418 |
-
|
16419 |
-
robots = start_robots
|
16420 |
-
for i in range(100):
|
16421 |
-
tmp = []
|
16422 |
-
for x, y, dx, dy in robots:
|
16423 |
-
tmp.append(((x+dx) % w, (y+dy)%h, dx, dy))
|
16424 |
-
robots = tmp
|
16425 |
-
|
16426 |
-
q1 = [(x,y,dx,dy) for x,y,dx,dy in robots if x<w//2 and y<h//2]
|
16427 |
-
q2 = [(x,y,dx,dy) for x,y,dx,dy in robots if x<w//2 and y>h//2]
|
16428 |
-
q3 = [(x,y,dx,dy) for x,y,dx,dy in robots if x>w//2 and y<h//2]
|
16429 |
-
q4 = [(x,y,dx,dy) for x,y,dx,dy in robots if x>w//2 and y>h//2]
|
16430 |
-
|
16431 |
-
print(len(q1)*len(q2)*len(q3)*len(q4))",python:3.9.21-slim
|
16432 |
2024,14,2,"--- Day 14: Restroom Redoubt ---
|
16433 |
|
16434 |
One of The Historians needs to use the bathroom; fortunately, you know there's a bathroom near an unvisited location on their list, and so you're all quickly teleported directly to the lobby of Easter Bunny Headquarters.
|
@@ -16551,57 +16517,78 @@ Your puzzle answer was 219150360.
|
|
16551 |
|
16552 |
During the bathroom break, someone notices that these robots seem awfully similar to ones built and used at the North Pole. If they're the same type of robots, they should have a hard-coded Easter egg: very rarely, most of the robots should arrange themselves into a picture of a Christmas tree.
|
16553 |
|
16554 |
-
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?",8053,"
|
|
|
16555 |
|
16556 |
-
|
16557 |
-
|
16558 |
|
16559 |
-
|
16560 |
-
lines
|
16561 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16562 |
|
16563 |
-
|
16564 |
-
|
|
|
16565 |
|
16566 |
-
width = 101
|
16567 |
-
height = 103
|
16568 |
-
robots = []
|
16569 |
-
for line in lines:
|
16570 |
-
p, v = line.split()
|
16571 |
-
p = [int(x) for x in re.findall(r""-?\d+"", p)]
|
16572 |
-
v = [int(x) for x in re.findall(r""-?\d+"", v)]
|
16573 |
-
robots.append((p, v))
|
16574 |
-
|
16575 |
-
found_tree = False
|
16576 |
-
for s in range(1, 10000):
|
16577 |
-
if found_tree:
|
16578 |
-
break
|
16579 |
|
16580 |
-
|
16581 |
-
|
16582 |
-
p, v = r
|
16583 |
-
new_p = ((p[0] + v[0]) % width, (p[1] + v[1]) % height)
|
16584 |
-
grid[new_p] = True
|
16585 |
-
robots[i] = (new_p, v)
|
16586 |
-
|
16587 |
-
if s == 100:
|
16588 |
-
quad_1 = quad_2 = quad_3 = quad_4 = 0
|
16589 |
-
for r in robots:
|
16590 |
-
mid_x = width // 2
|
16591 |
-
mid_y = height // 2
|
16592 |
-
|
16593 |
-
p = r[0]
|
16594 |
-
if p[0] < mid_x and p[1] < mid_y:
|
16595 |
-
quad_1 += 1
|
16596 |
-
elif p[0] > mid_x and p[1] < mid_y:
|
16597 |
-
quad_2 += 1
|
16598 |
-
elif p[0] < mid_x and p[1] > mid_y:
|
16599 |
-
quad_3 += 1
|
16600 |
-
elif p[0] > mid_x and p[1] > mid_y:
|
16601 |
-
quad_4 += 1
|
16602 |
-
part_one = quad_1 * quad_2 * quad_3 * quad_4
|
16603 |
|
16604 |
-
|
|
|
|
|
16605 |
2024,14,2,"--- Day 14: Restroom Redoubt ---
|
16606 |
|
16607 |
One of The Historians needs to use the bathroom; fortunately, you know there's a bathroom near an unvisited location on their list, and so you're all quickly teleported directly to the lobby of Easter Bunny Headquarters.
|
@@ -16724,46 +16711,66 @@ Your puzzle answer was 219150360.
|
|
16724 |
|
16725 |
During the bathroom break, someone notices that these robots seem awfully similar to ones built and used at the North Pole. If they're the same type of robots, they should have a hard-coded Easter egg: very rarely, most of the robots should arrange themselves into a picture of a Christmas tree.
|
16726 |
|
16727 |
-
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?",8053,"
|
16728 |
import re
|
|
|
16729 |
|
16730 |
-
|
16731 |
-
pos_x = (100 * curr_vel_x + curr_x) % max_w
|
16732 |
-
pos_y = (100 * curr_vel_y + curr_y) % max_h
|
16733 |
-
|
16734 |
-
return pos_x, pos_y
|
16735 |
-
|
16736 |
-
|
16737 |
-
def move_n_times(n, curr_x, curr_y, curr_vel_x, curr_vel_y, max_w, max_h):
|
16738 |
-
pos_x = (n * curr_vel_x + curr_x) % max_w
|
16739 |
-
pos_y = (n * curr_vel_y + curr_y) % max_h
|
16740 |
-
|
16741 |
-
return pos_x, pos_y
|
16742 |
-
|
16743 |
-
|
16744 |
-
positions = defaultdict(int)
|
16745 |
-
# max_pos = (11, 7)
|
16746 |
-
max_pos = (101, 103)
|
16747 |
-
|
16748 |
-
start_pos = []
|
16749 |
-
|
16750 |
-
for line in open(""input.txt""):
|
16751 |
-
x, y, v_x, v_y = re.findall(r""-?[0-9]+"", line)
|
16752 |
|
16753 |
-
|
16754 |
-
|
16755 |
-
|
16756 |
-
|
16757 |
-
positions[(pos_x, pos_y)] += 1
|
16758 |
|
|
|
|
|
|
|
16759 |
|
16760 |
-
|
16761 |
-
|
16762 |
-
|
16763 |
-
|
16764 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16765 |
|
16766 |
-
|
16767 |
2024,15,1,"--- Day 15: Warehouse Woes ---
|
16768 |
|
16769 |
You appear back inside your own mini submarine! Each Historian drives their mini submarine in a different direction; maybe the Chief has his own submarine down here somewhere as well?
|
@@ -51493,7 +51500,7 @@ During the bathroom break, someone notices that these robots seem awfully simila
|
|
51493 |
|
51494 |
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?",8053,"import { readFileSync } from ""fs"";
|
51495 |
|
51496 |
-
const input = readFileSync(
|
51497 |
encoding: 'utf-8'
|
51498 |
})
|
51499 |
|
@@ -51655,48 +51662,78 @@ Your puzzle answer was 219150360.
|
|
51655 |
|
51656 |
During the bathroom break, someone notices that these robots seem awfully similar to ones built and used at the North Pole. If they're the same type of robots, they should have a hard-coded Easter egg: very rarely, most of the robots should arrange themselves into a picture of a Christmas tree.
|
51657 |
|
51658 |
-
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?",8053,"const fs = require(
|
51659 |
-
const input = fs.readFileSync('input.txt', 'utf8').trim().split('\n');
|
51660 |
|
51661 |
-
|
51662 |
-
|
51663 |
-
const x = parseInt(parts[0].substring(2));
|
51664 |
-
const y = parseInt(parts[1]);
|
51665 |
-
const vx = parseInt(parts[3].substring(2));
|
51666 |
-
const vy = parseInt(parts[4]);
|
51667 |
-
return { x, y, vx, vy };
|
51668 |
-
});
|
51669 |
|
|
|
51670 |
const WIDTH = 101;
|
51671 |
const HEIGHT = 103;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51672 |
|
51673 |
-
function computeArea(t) {
|
51674 |
-
let minX = Infinity, maxX = -Infinity;
|
51675 |
-
let minY = Infinity, maxY = -Infinity;
|
51676 |
for (const { x, y, vx, vy } of robots) {
|
51677 |
-
|
51678 |
-
|
51679 |
-
|
51680 |
-
|
51681 |
-
|
51682 |
-
maxX = Math.max(maxX, posX);
|
51683 |
-
minY = Math.min(minY, posY);
|
51684 |
-
maxY = Math.max(maxY, posY);
|
51685 |
-
}
|
51686 |
-
return (maxX - minX + 1) * (maxY - minY + 1);
|
51687 |
-
}
|
51688 |
|
51689 |
-
|
51690 |
-
|
51691 |
-
|
51692 |
-
|
51693 |
-
|
51694 |
-
|
51695 |
-
|
|
|
|
|
51696 |
}
|
51697 |
-
}
|
51698 |
|
51699 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51700 |
2024,14,2,"--- Day 14: Restroom Redoubt ---
|
51701 |
|
51702 |
One of The Historians needs to use the bathroom; fortunately, you know there's a bathroom near an unvisited location on their list, and so you're all quickly teleported directly to the lobby of Easter Bunny Headquarters.
|
@@ -52235,119 +52272,58 @@ Your puzzle answer was 219150360.
|
|
52235 |
|
52236 |
During the bathroom break, someone notices that these robots seem awfully similar to ones built and used at the North Pole. If they're the same type of robots, they should have a hard-coded Easter egg: very rarely, most of the robots should arrange themselves into a picture of a Christmas tree.
|
52237 |
|
52238 |
-
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?",8053,"""
|
52239 |
-
|
52240 |
-
const input = Deno.readTextFileSync(""day14-input.txt"").trim()
|
52241 |
-
|
52242 |
-
const width = 101
|
52243 |
-
const height = 103
|
52244 |
-
|
52245 |
-
const map = [ ]
|
52246 |
-
|
52247 |
-
const allGuards = [ ]
|
52248 |
-
|
52249 |
-
var time = 0
|
52250 |
|
|
|
|
|
52251 |
|
52252 |
-
|
52253 |
-
|
52254 |
-
|
|
|
52255 |
|
52256 |
-
|
52257 |
-
|
52258 |
-
|
52259 |
-
|
52260 |
-
|
52261 |
-
|
|
|
|
|
52262 |
|
52263 |
-
|
52264 |
-
|
52265 |
-
|
52266 |
-
|
|
|
52267 |
}
|
52268 |
|
52269 |
-
|
52270 |
-
|
52271 |
-
|
52272 |
-
|
52273 |
-
for (const line of lines) {
|
52274 |
-
|
52275 |
-
const parts = line.trim().split("" "")
|
52276 |
-
|
52277 |
-
const tokensP = parts.shift().substr(2).split("","")
|
52278 |
-
const tokensV = parts.shift().substr(2).split("","")
|
52279 |
-
|
52280 |
-
const posX = parseInt(tokensP.shift())
|
52281 |
-
const posY = parseInt(tokensP.shift())
|
52282 |
-
const velX = parseInt(tokensV.shift())
|
52283 |
-
const velY = parseInt(tokensV.shift())
|
52284 |
-
|
52285 |
-
const guard = { ""posX"": posX, ""posY"": posY, ""velX"": velX, ""velY"": velY }
|
52286 |
-
|
52287 |
-
Object.freeze(guard) // will not be edited
|
52288 |
-
|
52289 |
-
allGuards.push(guard)
|
52290 |
-
}
|
52291 |
-
}
|
52292 |
|
52293 |
-
|
|
|
|
|
|
|
52294 |
|
52295 |
-
|
|
|
52296 |
|
52297 |
-
|
52298 |
-
map.push(line)
|
52299 |
|
52300 |
-
|
52301 |
-
|
52302 |
-
}
|
52303 |
-
|
52304 |
-
///////////////////////////////////////////////////////////////////////////////
|
52305 |
-
|
52306 |
-
function moveGuards() {
|
52307 |
|
52308 |
-
|
52309 |
-
|
52310 |
-
const clashes = moveGuard(guard)
|
52311 |
-
|
52312 |
-
if (clashes) { return true }
|
52313 |
}
|
52314 |
-
return false
|
52315 |
-
}
|
52316 |
|
52317 |
-
|
52318 |
-
|
52319 |
-
const bruteX = guard.posX + (time * guard.velX)
|
52320 |
-
const bruteY = guard.posY + (time * guard.velY)
|
52321 |
-
|
52322 |
-
let finalX = bruteX % width
|
52323 |
-
if (finalX < 0) { finalX += width }
|
52324 |
-
|
52325 |
-
let finalY = bruteY % height
|
52326 |
-
if (finalY < 0) { finalY += height }
|
52327 |
-
|
52328 |
-
if (map[finalY][finalX] == time) { return true }
|
52329 |
-
|
52330 |
-
map[finalY][finalX] = time
|
52331 |
-
|
52332 |
-
return false
|
52333 |
-
}
|
52334 |
-
|
52335 |
-
function showMap() {
|
52336 |
-
|
52337 |
-
console.log("""")
|
52338 |
-
|
52339 |
-
for (const line of map) {
|
52340 |
-
|
52341 |
-
let s = """"
|
52342 |
-
for (const number of line) { s += number == time ? ""X"" : ""."" }
|
52343 |
-
console.log(s)
|
52344 |
-
}
|
52345 |
-
console.log("""")
|
52346 |
}
|
52347 |
-
|
52348 |
-
console.time(""execution time"")
|
52349 |
-
main()
|
52350 |
-
console.timeEnd(""execution time"") // 30ms",node:14
|
52351 |
2024,16,1,"--- Day 16: Reindeer Maze ---
|
52352 |
|
52353 |
It's time again for the Reindeer Olympics! This year, the big event is the Reindeer Maze, where the Reindeer compete for the lowest score.
|
|
|
16069 |
|
16070 |
During the bathroom break, someone notices that these robots seem awfully similar to ones built and used at the North Pole. If they're the same type of robots, they should have a hard-coded Easter egg: very rarely, most of the robots should arrange themselves into a picture of a Christmas tree.
|
16071 |
|
16072 |
+
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?",8053,"from collections import defaultdict
|
16073 |
+
from statistics import stdev
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16074 |
|
16075 |
+
def read_input(file_path):
|
16076 |
+
""""""Read input from file and parse positions and velocities.""""""
|
16077 |
+
positions, velocities = [], []
|
16078 |
+
with open(file_path) as f:
|
16079 |
+
for line in f:
|
16080 |
+
p_sec, v_sec = line.split()
|
16081 |
+
positions.append([int(i) for i in p_sec.split(""="")[1].split("","")])
|
16082 |
+
velocities.append([int(i) for i in v_sec.split(""="")[1].split("","")])
|
16083 |
+
return positions, velocities
|
16084 |
+
|
16085 |
+
def simulate_motion(positions, velocities, num_x=101, num_y=103, steps=10000):
|
16086 |
+
""""""Simulate motion and detect significant changes in distribution.""""""
|
16087 |
+
for step in range(1, steps + 1):
|
16088 |
+
block_counts = defaultdict(int)
|
16089 |
+
new_positions = []
|
16090 |
+
|
16091 |
+
for pos, vel in zip(positions, velocities):
|
16092 |
+
new_x = (pos[0] + vel[0]) % num_x
|
16093 |
+
new_y = (pos[1] + vel[1]) % num_y
|
16094 |
+
new_positions.append([new_x, new_y])
|
16095 |
+
block_counts[(new_x // 5, new_y // 5)] += 1
|
16096 |
+
|
16097 |
+
if stdev(block_counts.values()) > 3:
|
16098 |
+
print(step)
|
16099 |
+
break
|
16100 |
|
16101 |
+
positions = new_positions
|
|
|
|
|
|
|
|
|
|
|
16102 |
|
16103 |
+
if __name__ == ""__main__"":
|
16104 |
+
positions, velocities = read_input(""input.txt"")
|
16105 |
+
simulate_motion(positions, velocities)
|
16106 |
+
",python:3.9.21-slim
|
|
|
|
|
|
|
|
|
|
|
16107 |
2024,14,2,"--- Day 14: Restroom Redoubt ---
|
16108 |
|
16109 |
One of The Historians needs to use the bathroom; fortunately, you know there's a bathroom near an unvisited location on their list, and so you're all quickly teleported directly to the lobby of Easter Bunny Headquarters.
|
|
|
16376 |
|
16377 |
During the bathroom break, someone notices that these robots seem awfully similar to ones built and used at the North Pole. If they're the same type of robots, they should have a hard-coded Easter egg: very rarely, most of the robots should arrange themselves into a picture of a Christmas tree.
|
16378 |
|
16379 |
+
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?",8053,"import re; L = len(M:=[*map(int, re.findall('[-\d]+', open(0).read()))])
|
16380 |
+
|
16381 |
+
R, C = 103, 101; B = (1, -1)
|
16382 |
+
for T in range(R*C):
|
16383 |
+
G = [['.']*C for _ in range(R)]
|
16384 |
+
Z = [0]*4
|
16385 |
+
for i in range(0, L, 4):
|
16386 |
+
py, px, vy, vx = M[i:i+4]
|
16387 |
+
x = (px+vx*T)%R; y = (py+vy*T)%C
|
16388 |
+
G[x][y] = '#'
|
16389 |
+
if x == R//2 or y == C//2: continue
|
16390 |
+
Z[(x<R//2)+2*(y<C//2)] += 1
|
16391 |
+
S = '\n'.join(''.join(r) for r in G)
|
16392 |
+
t = B[0]
|
16393 |
+
while '#'*t in S: t += 1
|
16394 |
+
if t > B[0]: B = (t, T)
|
16395 |
+
if T == 100: print('Part 1:', Z[0]*Z[1]*Z[2]*Z[3])
|
16396 |
+
print('Part 2:', B[1])
|
16397 |
+
",python:3.9.21-slim
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16398 |
2024,14,2,"--- Day 14: Restroom Redoubt ---
|
16399 |
|
16400 |
One of The Historians needs to use the bathroom; fortunately, you know there's a bathroom near an unvisited location on their list, and so you're all quickly teleported directly to the lobby of Easter Bunny Headquarters.
|
|
|
16517 |
|
16518 |
During the bathroom break, someone notices that these robots seem awfully similar to ones built and used at the North Pole. If they're the same type of robots, they should have a hard-coded Easter egg: very rarely, most of the robots should arrange themselves into a picture of a Christmas tree.
|
16519 |
|
16520 |
+
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?",8053,"with open(""Day-14-Challenge/input.txt"", ""r"") as file:
|
16521 |
+
lines = file.readlines()
|
16522 |
|
16523 |
+
WIDE = 101
|
16524 |
+
TALL = 103
|
16525 |
|
16526 |
+
robots = []
|
16527 |
+
for robot in lines: # format
|
16528 |
+
sides = robot.split("" "")
|
16529 |
+
left_side = sides[0]
|
16530 |
+
right_side = sides[1]
|
16531 |
+
left_sides = left_side.split("","")
|
16532 |
+
Px = left_sides[0].split(""="")[-1]
|
16533 |
+
Py = left_sides[1]
|
16534 |
+
right_sides = right_side.split("","")
|
16535 |
+
Vx = right_sides[0].split(""="")[-1]
|
16536 |
+
Vy = right_sides[1].strip()
|
16537 |
+
Px = int(Px)
|
16538 |
+
Py = int(Py)
|
16539 |
+
Vx = int(Vx)
|
16540 |
+
Vy = int(Vy)
|
16541 |
+
robots.append([Px,Py,Vx,Vy])
|
16542 |
+
|
16543 |
+
smallest_answer = 999999999999
|
16544 |
+
found_at_second = 0
|
16545 |
+
for second in range(WIDE * TALL): # pattern will repeat every WIDE * TALL times
|
16546 |
+
q1 = 0
|
16547 |
+
q2 = 0
|
16548 |
+
q3 = 0
|
16549 |
+
q4 = 0
|
16550 |
+
final_grid = [[0]*WIDE for _ in range(TALL)]
|
16551 |
+
for i in range(len(robots)):
|
16552 |
+
Px,Py,Vx,Vy = robots[i]
|
16553 |
+
new_Py, new_Px = (Px + Vx * second),(Py + Vy * second) # swap X and Y coords because its swapped in the examples too
|
16554 |
+
# for matplotlib
|
16555 |
+
new_Px, new_Py = (new_Px % TALL), (new_Py % WIDE)
|
16556 |
+
final_grid[new_Px][new_Py] += 1
|
16557 |
+
vertical_middle = WIDE // 2
|
16558 |
+
horizontal_middle = TALL // 2
|
16559 |
+
|
16560 |
+
|
16561 |
+
if new_Px < vertical_middle and new_Py < horizontal_middle:
|
16562 |
+
q1 += 1
|
16563 |
+
if new_Px > vertical_middle and new_Py < horizontal_middle:
|
16564 |
+
q2 += 1
|
16565 |
+
if new_Px < vertical_middle and new_Py > horizontal_middle:
|
16566 |
+
q3 += 1
|
16567 |
+
if new_Px > vertical_middle and new_Py > horizontal_middle:
|
16568 |
+
q4 += 1
|
16569 |
+
|
16570 |
+
answer = q1 * q2 * q3 * q4 # when answer is smallest,
|
16571 |
+
# robots are bunched together to draw the christmas tree, so one corner will have most robots
|
16572 |
+
# so when we multiply with other quadrants answer will be smaller
|
16573 |
+
# If we have 40 robots, answer is smaller if 37 are in Q1 and 1 each in rest = 37
|
16574 |
+
# If they are evenly split its 10 * 10 * 10 * 10 = 10000
|
16575 |
+
# So we assume that tree is drawn when smallest answer.
|
16576 |
+
# Mirrored approach doesnt work
|
16577 |
+
if(answer < smallest_answer): # find smallest answer
|
16578 |
+
smallest_answer = answer
|
16579 |
+
found_at_second = second
|
16580 |
|
16581 |
+
|
16582 |
+
print(""Found smallest safety factor: "", smallest_answer)
|
16583 |
+
print(""at second: "", found_at_second)
|
16584 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16585 |
|
16586 |
+
# Total time complexity
|
16587 |
+
# O(WIDE * TALL * n) so kindof O(n) :)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16588 |
|
16589 |
+
# Total space complexity
|
16590 |
+
# O(WIDE * TALL + n) so kindof O(n) :)
|
16591 |
+
",python:3.9.21-slim
|
16592 |
2024,14,2,"--- Day 14: Restroom Redoubt ---
|
16593 |
|
16594 |
One of The Historians needs to use the bathroom; fortunately, you know there's a bathroom near an unvisited location on their list, and so you're all quickly teleported directly to the lobby of Easter Bunny Headquarters.
|
|
|
16711 |
|
16712 |
During the bathroom break, someone notices that these robots seem awfully similar to ones built and used at the North Pole. If they're the same type of robots, they should have a hard-coded Easter egg: very rarely, most of the robots should arrange themselves into a picture of a Christmas tree.
|
16713 |
|
16714 |
+
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?",8053,"import pathlib
|
16715 |
import re
|
16716 |
+
import math
|
16717 |
|
16718 |
+
lines = pathlib.Path(""data.txt"").read_text().split(""\n"")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16719 |
|
16720 |
+
iterations = 100
|
16721 |
+
width = 101
|
16722 |
+
height = 103
|
16723 |
+
robots = []
|
|
|
16724 |
|
16725 |
+
#example input only
|
16726 |
+
#width = 11
|
16727 |
+
#height = 7
|
16728 |
|
16729 |
+
for line in lines:
|
16730 |
+
matches = re.findall(r""[-\d]+"", line)
|
16731 |
+
x, y, vx, vy = [int(i) for i in matches]
|
16732 |
+
robots.append([x, y, vx, vy])
|
16733 |
+
|
16734 |
+
quadrants = [0, 0, 0, 0]
|
16735 |
+
|
16736 |
+
for x, y, vx, vy in robots:
|
16737 |
+
x = (x + vx * iterations) % width
|
16738 |
+
y = (y + vy * iterations) % height
|
16739 |
+
|
16740 |
+
mid_x = width//2
|
16741 |
+
mid_y = height//2
|
16742 |
+
if x < mid_x and y < mid_y:
|
16743 |
+
quadrants[0] += 1
|
16744 |
+
elif x < mid_x and y > mid_y:
|
16745 |
+
quadrants[1] += 1
|
16746 |
+
elif x > mid_x and y < mid_y:
|
16747 |
+
quadrants[2] += 1
|
16748 |
+
elif x > mid_x and y > mid_y:
|
16749 |
+
quadrants[3] += 1
|
16750 |
+
|
16751 |
+
safety_factor = math.prod(quadrants)
|
16752 |
+
print(safety_factor)
|
16753 |
+
|
16754 |
+
pictures = 0
|
16755 |
+
for i in range(1, 100000):
|
16756 |
+
new_robots = []
|
16757 |
+
board = [[0 for j in range(width)] for j in range(height)]
|
16758 |
+
for x, y, vx, vy in robots:
|
16759 |
+
x = (x + vx * i) % width
|
16760 |
+
y = (y + vy * i) % height
|
16761 |
+
new_robots.append((x, y))
|
16762 |
+
board[y][x] = 1
|
16763 |
+
|
16764 |
+
if len(set(new_robots)) != len(new_robots):
|
16765 |
+
continue
|
16766 |
+
|
16767 |
+
max_sum = max((sum(row) for row in board))
|
16768 |
+
#the tree picture has 31 robots in a single row
|
16769 |
+
if max_sum > 30:
|
16770 |
+
print(i)
|
16771 |
+
break
|
16772 |
|
16773 |
+
",python:3.9.21-slim
|
16774 |
2024,15,1,"--- Day 15: Warehouse Woes ---
|
16775 |
|
16776 |
You appear back inside your own mini submarine! Each Historian drives their mini submarine in a different direction; maybe the Chief has his own submarine down here somewhere as well?
|
|
|
51500 |
|
51501 |
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?",8053,"import { readFileSync } from ""fs"";
|
51502 |
|
51503 |
+
const input = readFileSync('input.txt', {
|
51504 |
encoding: 'utf-8'
|
51505 |
})
|
51506 |
|
|
|
51662 |
|
51663 |
During the bathroom break, someone notices that these robots seem awfully similar to ones built and used at the North Pole. If they're the same type of robots, they should have a hard-coded Easter egg: very rarely, most of the robots should arrange themselves into a picture of a Christmas tree.
|
51664 |
|
51665 |
+
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?",8053,"const fs = require(""fs"");
|
|
|
51666 |
|
51667 |
+
// Read input file and split lines
|
51668 |
+
const lines = fs.readFileSync(""input.txt"", ""utf8"").trim().split(""\n"");
|
|
|
|
|
|
|
|
|
|
|
|
|
51669 |
|
51670 |
+
const ITERATIONS = 100;
|
51671 |
const WIDTH = 101;
|
51672 |
const HEIGHT = 103;
|
51673 |
+
const robots = [];
|
51674 |
+
|
51675 |
+
// Parse input data
|
51676 |
+
lines.forEach(line => {
|
51677 |
+
const [x, y, vx, vy] = line.match(/-?\d+/g).map(Number);
|
51678 |
+
robots.push({ x, y, vx, vy });
|
51679 |
+
});
|
51680 |
+
|
51681 |
+
const quadrants = [0, 0, 0, 0];
|
51682 |
+
|
51683 |
+
// Compute quadrant distribution after 100 iterations
|
51684 |
+
robots.forEach(({ x, y, vx, vy }) => {
|
51685 |
+
const newX = (x + vx * ITERATIONS) % WIDTH;
|
51686 |
+
const newY = (y + vy * ITERATIONS) % HEIGHT;
|
51687 |
+
|
51688 |
+
const midX = Math.floor(WIDTH / 2);
|
51689 |
+
const midY = Math.floor(HEIGHT / 2);
|
51690 |
+
|
51691 |
+
if (newX < midX && newY < midY) quadrants[0]++;
|
51692 |
+
else if (newX < midX && newY > midY) quadrants[1]++;
|
51693 |
+
else if (newX > midX && newY < midY) quadrants[2]++;
|
51694 |
+
else if (newX > midX && newY > midY) quadrants[3]++;
|
51695 |
+
});
|
51696 |
+
|
51697 |
+
// Compute safety factor
|
51698 |
+
const safetyFactor = quadrants.reduce((acc, val) => acc * val, 1);
|
51699 |
+
console.log(""Safety Factor:"", safetyFactor);
|
51700 |
+
|
51701 |
+
// Find the iteration when the tree shape appears
|
51702 |
+
for (let i = 1; i < 100000; i++) {
|
51703 |
+
const seenPositions = new Set();
|
51704 |
+
const board = Array.from({ length: HEIGHT }, () => Array(WIDTH).fill(0));
|
51705 |
+
|
51706 |
+
let collision = false;
|
51707 |
|
|
|
|
|
|
|
51708 |
for (const { x, y, vx, vy } of robots) {
|
51709 |
+
let newX = (x + vx * i) % WIDTH;
|
51710 |
+
let newY = (y + vy * i) % HEIGHT;
|
51711 |
+
|
51712 |
+
if (newX < 0) newX += WIDTH;
|
51713 |
+
if (newY < 0) newY += HEIGHT;
|
|
|
|
|
|
|
|
|
|
|
|
|
51714 |
|
51715 |
+
const posKey = `${newX},${newY}`;
|
51716 |
+
|
51717 |
+
if (seenPositions.has(posKey)) {
|
51718 |
+
collision = true;
|
51719 |
+
break;
|
51720 |
+
}
|
51721 |
+
|
51722 |
+
seenPositions.add(posKey);
|
51723 |
+
board[newY][newX] = 1;
|
51724 |
}
|
|
|
51725 |
|
51726 |
+
if (collision) continue;
|
51727 |
+
|
51728 |
+
const maxRowSum = Math.max(...board.map(row => row.reduce((sum, cell) => sum + cell, 0)));
|
51729 |
+
|
51730 |
+
// The tree picture has 31 robots in a single row
|
51731 |
+
if (maxRowSum > 30) {
|
51732 |
+
console.log(""Tree appears at second:"", i);
|
51733 |
+
break;
|
51734 |
+
}
|
51735 |
+
}
|
51736 |
+
",node:14
|
51737 |
2024,14,2,"--- Day 14: Restroom Redoubt ---
|
51738 |
|
51739 |
One of The Historians needs to use the bathroom; fortunately, you know there's a bathroom near an unvisited location on their list, and so you're all quickly teleported directly to the lobby of Easter Bunny Headquarters.
|
|
|
52272 |
|
52273 |
During the bathroom break, someone notices that these robots seem awfully similar to ones built and used at the North Pole. If they're the same type of robots, they should have a hard-coded Easter egg: very rarely, most of the robots should arrange themselves into a picture of a Christmas tree.
|
52274 |
|
52275 |
+
What is the fewest number of seconds that must elapse for the robots to display the Easter egg?",8053,"const fs = require(""fs"");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52276 |
|
52277 |
+
// Read input file
|
52278 |
+
const inputText = fs.readFileSync(""input.txt"", ""utf8"").trim().split(""\n"");
|
52279 |
|
52280 |
+
const numX = 101;
|
52281 |
+
const numY = 103;
|
52282 |
+
let initialPositions = [];
|
52283 |
+
let velocities = [];
|
52284 |
|
52285 |
+
// Parse input
|
52286 |
+
inputText.forEach(line => {
|
52287 |
+
let [pSec, vSec] = line.split("" "");
|
52288 |
+
let position = pSec.split(""="")[1].split("","").map(Number);
|
52289 |
+
let velocity = vSec.split(""="")[1].split("","").map(Number);
|
52290 |
+
initialPositions.push(position);
|
52291 |
+
velocities.push(velocity);
|
52292 |
+
});
|
52293 |
|
52294 |
+
// Standard deviation function
|
52295 |
+
function standardDeviation(values) {
|
52296 |
+
const mean = values.reduce((a, b) => a + b, 0) / values.length;
|
52297 |
+
const variance = values.reduce((sum, value) => sum + (value - mean) ** 2, 0) / values.length;
|
52298 |
+
return Math.sqrt(variance);
|
52299 |
}
|
52300 |
|
52301 |
+
// Simulation loop
|
52302 |
+
for (let counter = 0; counter < 10000; counter++) {
|
52303 |
+
let blockCounts = new Map();
|
52304 |
+
let newInitialPositions = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52305 |
|
52306 |
+
initialPositions.forEach((initialPos, index) => {
|
52307 |
+
let velocity = velocities[index];
|
52308 |
+
let newX = (initialPos[0] + velocity[0]) % numX;
|
52309 |
+
let newY = (initialPos[1] + velocity[1]) % numY;
|
52310 |
|
52311 |
+
if (newX < 0) newX += numX;
|
52312 |
+
if (newY < 0) newY += numY;
|
52313 |
|
52314 |
+
newInitialPositions.push([newX, newY]);
|
|
|
52315 |
|
52316 |
+
let blockKey = `${Math.floor(newX / 5)},${Math.floor(newY / 5)}`;
|
52317 |
+
blockCounts.set(blockKey, (blockCounts.get(blockKey) || 0) + 1);
|
52318 |
+
});
|
|
|
|
|
|
|
|
|
52319 |
|
52320 |
+
if (blockCounts.size > 0 && standardDeviation([...blockCounts.values()]) > 3) {
|
52321 |
+
console.log(counter + 1);
|
|
|
|
|
|
|
52322 |
}
|
|
|
|
|
52323 |
|
52324 |
+
initialPositions = newInitialPositions;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52325 |
}
|
52326 |
+
",node:14
|
|
|
|
|
|
|
52327 |
2024,16,1,"--- Day 16: Reindeer Maze ---
|
52328 |
|
52329 |
It's time again for the Reindeer Olympics! This year, the big event is the Reindeer Maze, where the Reindeer compete for the lowest score.
|