Skip to content

Commit 3009f84

Browse files
committed
Day 14 Part 2
1 parent 03e3040 commit 3009f84

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ __pycache__
44
.pytest_cache
55
.vscode
66

7+
# day 14
8+
*.json
9+
*.html
10+

Day_14/task_14b.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import re
2+
import copy
3+
import json
4+
import tqdm
5+
6+
7+
def load_data(filename):
8+
data_dict = {}
9+
with open(filename, "r") as f:
10+
for ind, line in enumerate(f.readlines()):
11+
pos_y, pos_x, vel_y, vel_x = map(int, re.findall(r"[+-]?\d+", line))
12+
data_dict[ind] = {"pos": (pos_x, pos_y), "vel": (vel_x, vel_y)}
13+
return data_dict
14+
15+
16+
def predit_motion(robots_dict, time, space_wide, space_tall):
17+
robots_dict = copy.deepcopy(robots_dict)
18+
reduced_time_for_wide = time % space_wide
19+
reduced_time_for_tall = time % space_tall
20+
21+
for robot in robots_dict:
22+
pos_x, pos_y = robots_dict[robot]["pos"]
23+
vel_x, vel_y = robots_dict[robot]["vel"]
24+
25+
for _ in range(reduced_time_for_wide):
26+
pos_x = pos_x + vel_x
27+
if pos_x > space_wide - 1:
28+
pos_x = pos_x - space_wide
29+
if pos_x < 0:
30+
pos_x = space_wide + pos_x
31+
32+
for _ in range(reduced_time_for_tall):
33+
pos_y = pos_y + vel_y
34+
if pos_y > space_tall - 1:
35+
pos_y = pos_y - space_tall
36+
if pos_y < 0:
37+
pos_y = space_tall + pos_y
38+
39+
robots_dict[robot]["pos"] = (pos_x, pos_y)
40+
41+
return robots_dict
42+
43+
44+
def create_robot_pos_map(robot_dict, length, width):
45+
map_data = [["." for _ in range(width)] for _ in range(length)]
46+
47+
for robot in robot_dict.values():
48+
x, y = robot["pos"]
49+
map_data[x][y] = "#"
50+
51+
map_data = ["".join(row) for row in map_data]
52+
map_data = "\n".join(map_data)
53+
# print(map_data)
54+
return map_data
55+
56+
57+
def save_json_as_html(file_path):
58+
with open(file_path, "r") as file:
59+
map_data = json.load(file)
60+
61+
html_content = """
62+
<!DOCTYPE html>
63+
<html lang="en">
64+
<head>
65+
<meta charset="UTF-8">
66+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
67+
<title>Easter egg</title>
68+
<style>
69+
body {
70+
font-family: monospace;
71+
background-color: #f4f4f4;
72+
color: #333;
73+
padding: 20px;
74+
}
75+
.container {
76+
display: flex;
77+
flex-wrap: wrap;
78+
}
79+
.map {
80+
margin-bottom: 10px;
81+
padding: 10px;
82+
background: #fff;
83+
border: 1px solid #ddd;
84+
border-radius: 5px;
85+
overflow-x: auto;
86+
white-space: pre;
87+
}
88+
.map pre {
89+
font-size: 3px;
90+
}
91+
</style>
92+
</head>
93+
<body>
94+
<h1>Data Map</h1>
95+
<div class="container">
96+
"""
97+
98+
# Add each block to the HTML
99+
for t, easter_egg in tqdm.tqdm(map_data.items()):
100+
html_content += f"""
101+
<div class="map">
102+
<h3>Time: {t}</h3>
103+
<pre>{easter_egg}</pre>
104+
</div>
105+
"""
106+
107+
# Close HTML tags
108+
html_content += """
109+
</div>
110+
</body>
111+
</html>
112+
"""
113+
114+
# Save the HTML to a file
115+
output_file = "Day_14/day_14.html"
116+
with open(output_file, "w") as file:
117+
file.write(html_content)
118+
119+
print(f"HTML file generated: {output_file}")
120+
121+
122+
if "__main__" == __name__:
123+
WIDTH = 101
124+
LENGTH = 103
125+
126+
time_limit = WIDTH * LENGTH
127+
robots_dict = load_data("Day_14/puzzle_input.txt")
128+
map_data = {}
129+
130+
for t in tqdm.tqdm(range(1, time_limit)):
131+
prediction = predit_motion(robots_dict, t, LENGTH, WIDTH)
132+
map_data[t] = create_robot_pos_map(prediction, LENGTH, WIDTH)
133+
134+
# save map data to json
135+
file_path = "Day_14/easter_egg.json"
136+
with open(file_path, "w") as file:
137+
json.dump(map_data, file, indent=4)
138+
print(f"Map data saved to {file_path}")
139+
140+
save_json_as_html(file_path)

0 commit comments

Comments
 (0)