Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita64ad6e

Browse files
committed
Added day 2021-20
1 parent619c852 commita64ad6e

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed

‎2021/20-Trench Map.py

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
importos,grid,graph,dot,assembly,re,itertools,copy,functools
3+
fromcollectionsimportCounter,deque,defaultdict
4+
5+
fromcompassimport*
6+
7+
8+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
9+
deflmap(func,*iterables):
10+
returnlist(map(func,*iterables))
11+
12+
13+
defints(s:str):
14+
returnlmap(int,re.findall(r"-?\d+",s))# thanks mserrano!
15+
16+
17+
defpositive_ints(s:str):
18+
returnlmap(int,re.findall(r"\d+",s))# thanks mserrano!
19+
20+
21+
deffloats(s:str):
22+
returnlmap(float,re.findall(r"-?\d+(?:\.\d+)?",s))
23+
24+
25+
defpositive_floats(s:str):
26+
returnlmap(float,re.findall(r"\d+(?:\.\d+)?",s))
27+
28+
29+
defwords(s:str):
30+
returnre.findall(r"[a-zA-Z]+",s)
31+
32+
33+
test_data= {}
34+
35+
test=1
36+
test_data[test]= {
37+
"input":"""..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#
38+
39+
#..#.
40+
#....
41+
##..#
42+
..#..
43+
..###""",
44+
"expected": ["35","Unknown"],
45+
}
46+
47+
test="real"
48+
input_file=os.path.join(
49+
os.path.dirname(__file__),
50+
"Inputs",
51+
os.path.basename(__file__).replace(".py",".txt"),
52+
)
53+
test_data[test]= {
54+
"input":open(input_file,"r+").read(),
55+
"expected": ["5044","18074"],
56+
}
57+
58+
59+
# -------------------------------- Control program execution ------------------------- #
60+
61+
case_to_test="real"
62+
part_to_test=2
63+
64+
# -------------------------------- Initialize some variables ------------------------- #
65+
66+
puzzle_input=test_data[case_to_test]["input"]
67+
puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1]
68+
puzzle_actual_result="Unknown"
69+
70+
71+
# -------------------------------- Actual code execution ----------------------------- #
72+
73+
dot.Dot.all_directions=directions_diagonals
74+
all_directions=directions_diagonals
75+
dot.Dot.allowed_direction_map= {
76+
".": {dir:all_directionsfordirinall_directions},
77+
"#": {dir:all_directionsfordirinall_directions},
78+
}
79+
dot.Dot.terrain_map= {
80+
".": [True,False],
81+
"#": [True,False],
82+
"X": [True,False],
83+
}
84+
85+
86+
defget_neighbors(self):
87+
ifself.neighbors_obsolete:
88+
self.neighbors= {}
89+
fordirectioninself.allowed_directions:
90+
if (self+direction)and (self+direction).is_walkable:
91+
self.neighbors[self+direction]=1
92+
else:
93+
new_dot=self.__class__(self.grid,self.position+direction,".")
94+
self.grid.dots[self.position+direction]=new_dot
95+
self.neighbors[self+direction]=1
96+
97+
self.neighbors_obsolete=False
98+
returnself.neighbors
99+
100+
101+
dot.Dot.get_neighbors=get_neighbors
102+
103+
grid.Grid.all_directions=directions_diagonals
104+
105+
dot.Dot.sort_value=dot.Dot.sorting_map["reading"]
106+
107+
ifpart_to_test==1:
108+
generations=2
109+
else:
110+
generations=50
111+
112+
113+
algorithm=puzzle_input.split("\n")[0]
114+
115+
image=grid.Grid()
116+
image.all_directions=directions_diagonals
117+
image.text_to_dots("\n".join(puzzle_input.split("\n")[2:]))
118+
119+
# print (image.dots_to_text())
120+
121+
foriinrange(generations+5):
122+
dots=image.dots.copy()
123+
[image.dots[x].get_neighbors()forxindots]
124+
125+
126+
foriinrange(generations):
127+
# print ('Generation', i)
128+
new_image=grid.Grid()
129+
new_image.dots= {
130+
x:dot.Dot(new_image,image.dots[x].position,image.dots[x].terrain)
131+
forxinimage.dots
132+
}
133+
new_image.all_directions=directions_diagonals
134+
135+
forxinimage.dots.copy():
136+
neighbors= [neighborforneighborinimage.dots[x].get_neighbors()]+ [
137+
image.dots[x]
138+
]
139+
text="".join([neighbor.terrainforneighborinsorted(neighbors)])
140+
binary=int(text.replace(".","0").replace("#","1"),2)
141+
new_image.dots[x].set_terrain(algorithm[binary])
142+
# print (new_image.dots_to_text())
143+
144+
# Empty borders so they're not counted later
145+
# They use surrounding data (out of image) that default to . and this messes up the rest
146+
# This is done only for odd generations because that's enough (all non-borders get blanked out due to the "." at the end of the algorithm)
147+
ifi%2==1:
148+
borders,_=new_image.get_borders()
149+
borders=functools.reduce(lambdaa,b:a+b,borders)
150+
[dot.set_terrain(".")fordotinborders]
151+
152+
image.dots= {
153+
x:dot.Dot(image,new_image.dots[x].position,new_image.dots[x].terrain)
154+
forxinnew_image.dots
155+
}
156+
157+
# print ('Lit dots', sum([1 for dot in image.dots if image.dots[dot].terrain == '#']))
158+
159+
# Remove the borders that were added (they shouldn't count because they take into account elements outside the image)
160+
borders,_=image.get_borders()
161+
borders=functools.reduce(lambdaa,b:a+b,borders)
162+
image.dots= {
163+
dot:image.dots[dot]fordotinimage.dotsifimage.dots[dot]notinborders
164+
}
165+
166+
puzzle_actual_result=sum([1fordotinimage.dotsifimage.dots[dot].terrain=="#"])
167+
168+
169+
# -------------------------------- Outputs / results --------------------------------- #
170+
171+
print("Case :",case_to_test,"- Part",part_to_test)
172+
print("Expected result : "+str(puzzle_expected_result))
173+
print("Actual result : "+str(puzzle_actual_result))
174+
# Date created: 2021-12-20 08:30:35.363096
175+
# Part 1: 2021-12-20 10:19:36
176+
# Part 2: 2021-12-20 10:35:25

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp