1
1
"""
2
+ You're given a series of patterns like this -
3
+
4
+ #.##..##.
5
+ ..#.##.#.
6
+ ##......#
7
+ ##......#
8
+ ..#.##.#.
9
+ ..##..##.
10
+ #.#.##.#.
11
+
12
+ Task - find the line of reflection (either row or col)
13
+ Here, it's the vertical line between cols 5 and 6.
14
+
15
+ A vertical line of reflection means that the reflected columns
16
+ going left and right from the LOR are identical.
17
+
18
+ A horizontal line of reflection is the same, but for rows going up/down.
19
+ In this case:
20
+
21
+ #...##..#
22
+ #....#..#
23
+ ..##..###
24
+ #####.##.
25
+ #####.##.
26
+ ..##..###
27
+ #....#..#
28
+
29
+ The line of reflection is horizontal, after row 4.
30
+
31
+ TASK: iterate through each pattern in input.
32
+ Then, create a summary by:
33
+ - For every VLOR - Sum- the # of cols to the left of each VERTICAL LOR (where applicable)
34
+ - For every HLOR - Sum- (100 * # of rows above each HORIZONTAL LOR)
35
+ - Sum those 2 results.
36
+
37
+ The answer to small is 405.
38
+ """
39
+
40
+ def pp (L ):
41
+ for l in L :
42
+ print (l )
43
+ print ("----------------------------------" )
44
+
45
+ inp = []
46
+ with open ("input.txt" ) as f :
47
+ lines = f .readlines ()
48
+
49
+ i = 0
50
+ inp = []
51
+ t = []
52
+ while i < len (lines ):
53
+ L = lines [i ].strip ()
54
+ # if L is a blank line
55
+ if "#" not in L and "." not in L :
56
+ inp .append (t )
57
+ t = []
58
+ else :
59
+ t .append (L )
60
+ i += 1
61
+ inp .append (t )
62
+
63
+
64
+ # Run computation
65
+ # after column c - 0 indexed
66
+ vlors = []
67
+
68
+ # after row r - 0 indexed
69
+ hlors = []
70
+
71
+
72
+ # [h, -1]
73
+ part1_answers = {}
74
+ for i in range (0 , len (inp )):
75
+ part1_answers [i ] = ["z" , - 1 ]
76
+
77
+ # COMPARE ROWS - HORIZONTAL LINE OF REFLECTION?
78
+ def check_hlor (s1 , s2 ):
79
+ i = len (s1 ) - 1
80
+ j = 0
81
+ # i => s1, walk backwards
82
+ while i >= 0 and j < len (s2 ):
83
+ print ("i={}, j={}, s1[i]={}, s2[j]={}" .format (i , j , s1 [i ], s2 [j ]))
84
+ if s1 [i ] != s2 [j ]:
85
+ return False
86
+ i -= 1
87
+ j += 1
88
+ return True
89
+
90
+ for idx , i in enumerate (inp ):
91
+ # print("\n\n 🏁 STARTING INPUT {}".format(i))
92
+ hlor = False
93
+ # check rows for HLOR
94
+ r = 1
95
+ while r < len (i ):
96
+ s1 = i [:r ]
97
+ s2 = i [r :]
98
+ # print("\n↔️ r={}, checking horiz line of reflection for \ns1={}\ns2={}".format(r, s1, s2))
99
+ if check_hlor (s1 , s2 ):
100
+ hlors .append (r )
101
+ hlor = True
102
+ part1_answers [idx ] = ["h" , r ]
103
+ # print("✅ input {} has hlor after row {}".format(i, r))
104
+ break
105
+ r += 1
106
+ if hlor :
107
+ continue
108
+
109
+ # create a copy of i, rotated 90 degrees to the right
110
+ # this will be used to check for VLOR
111
+ i90 = list (zip (* i [::- 1 ]))
112
+ # convert list of tuples to list of strings
113
+ i90 = ["" .join (t ) for t in i90 ]
114
+
115
+
116
+ print ("ROTATED 90 DEGREES" )
117
+ pp (i90 )
118
+ r = 1
119
+ vlor = False
120
+ while r < len (i90 ):
121
+ s1 = i90 [:r ]
122
+ s2 = i90 [r :]
123
+ # print("\nr={}, checking vert line of reflection for \ns1={}\ns2={}".format(r, s1, s2))
124
+ if check_hlor (s1 , s2 ):
125
+ vlors .append (r )
126
+ vlor = True
127
+ part1_answers [idx ] = ["v" , r ]
128
+ # print("✅ input {} has hlor after row {}".format(i, r))
129
+ break
130
+ r += 1
131
+
132
+ if not vlor and not hlor :
133
+ print ("\n ⚠️ input at index {} has no VLOR or HLOR:" .format (idx ))
134
+ pp (i )
135
+ # calculate summary
136
+ """
137
+ TASK: iterate through each pattern in input.
138
+ Then, create a summary by:
139
+ - For every VLOR - Sum- the # of cols to the left of each VERTICAL LOR (where applicable)
140
+ - For every HLOR - Sum- (100 * # of rows above each HORIZONTAL LOR)
141
+ - Sum those 2 results.
142
+ """
143
+
144
+ # after row, after col
145
+ res = 0
146
+ for h in hlors :
147
+ res += 100 * h
148
+ for v in vlors :
149
+ res += v
150
+ print ("⭐️ PART 1 res={}" .format (res ))
151
+ print (part1_answers )
152
+ """
2
153
PART TWO
3
154
Every mirror has exactly one smudge - one # or . is incorrect and should
4
155
be the opposite type.
16
167
..##..##.
17
168
#.#.##.#.
18
169
19
- If hte top left "#" were "." instead, we'd have a different line of reflection.
170
+ If the top left "#" were "." instead, we'd have a different line of reflection.
20
171
21
172
..##..##.
22
173
..#.##.#.
@@ -35,7 +186,7 @@ def pp(L):
35
186
36
187
37
188
inp = []
38
- with open ("small .txt" ) as f :
189
+ with open ("input .txt" ) as f :
39
190
lines = f .readlines ()
40
191
41
192
i = 0
@@ -95,13 +246,16 @@ def create_perms(i):
95
246
perms = create_perms (i )
96
247
found_lor = False
97
248
p_index = 0
98
- min_lor = 1000
99
- hor_lor = False
100
249
while not found_lor :
101
250
hlor = False
102
251
r = 1
103
- cur = perms [p_index ]
252
+ cur = perms [p_index ]
253
+ print ("\n \n i=" )
254
+ pp (i )
255
+ print ("cur=" )
256
+ pp (cur )
104
257
while r < len (i ):
258
+ print ("r={}" .format (r ))
105
259
s1 = cur [:r ]
106
260
s2 = cur [r :]
107
261
# print("\nevaluating cur where r={}".format(r))
@@ -111,17 +265,17 @@ def create_perms(i):
111
265
# print("and s2=")
112
266
# pp(s2)
113
267
if check_hlor (s1 , s2 ):
114
- # hlors.append(r)
115
- hlor = True
116
- found_lor = True
117
- print ("✅ input {} has hlor after row {}" .format (i , r ))
118
- if r < min_lor :
119
- min_lor = r
120
- hor_lor = True
121
- break
268
+ if part1_answers [idx ][0 ] == "h" and part1_answers [idx ][1 ] == r :
269
+ print ("hlor can't be the same as part 1" )
270
+ else :
271
+ hlor = True
272
+ found_lor = True
273
+ hlors .append (r )
274
+ print ("✅ input has hlor after row {}" .format (r ))
275
+ pp (s1 )
276
+ pp (s2 )
277
+ break
122
278
r += 1
123
- if hlor :
124
- continue
125
279
126
280
# create a copy of i, rotated 90 degrees to the right
127
281
# this will be used to check for VLOR
@@ -138,22 +292,21 @@ def create_perms(i):
138
292
s2 = i90 [r :]
139
293
# print("\nr={}, checking vert line of reflection for \ns1={}\ns2={}".format(r, s1, s2))
140
294
if check_hlor (s1 , s2 ):
141
- # vlors.append(r)
142
- vlor = True
143
- found_lor = True
144
- print ( "✅ input {} has vlor after row {}" . format ( i , r ))
145
- if r < min_lor :
146
- min_lor = r
147
- hor_lor = False
148
- break
295
+ if part1_answers [ idx ][ 0 ] == "v" and part1_answers [ idx ][ 1 ] == r :
296
+ print ( " vlor can't be the same as part 1" )
297
+ else :
298
+ vlor = True
299
+ found_lor = True
300
+ print ( "✅ input {} has vlor after row {}" . format ( cur , r ))
301
+ vlors . append ( r )
302
+ break
149
303
r += 1
150
- if hor_lor :
151
- hlors .append (min_lor )
152
- else :
153
- vlors .append (min_lor )
154
- p_index += 1
304
+ p_index += 1
155
305
156
306
# after row, after col
307
+ print ("done iterating" )
308
+ print ("hlors={}" .format (hlors ))
309
+ print ("vlors={}" .format (vlors ))
157
310
res = 0
158
311
for h in hlors :
159
312
res += 100 * h
0 commit comments