-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb74486
commit 8707f13
Showing
4 changed files
with
1,535 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
def get_input(): | ||
with open("13.txt", "r") as f: | ||
return [l for l in f.read().splitlines()] | ||
|
||
|
||
def format_reflections(input): | ||
reflections = [] | ||
c = [] | ||
for i in input: | ||
if i == "": | ||
reflections.append(c) | ||
c = [] | ||
else: | ||
c.append(i) | ||
reflections.append(c) | ||
return reflections | ||
|
||
|
||
def find_reflection_point(reflection, axis, p): | ||
p0, p1 = p | ||
if axis == "x": | ||
if p1 > len(reflection) - 1: | ||
return False | ||
to_compare = list(reversed(reflection[: p0 + 1])), reflection[p1:] | ||
for i in range(len(min(to_compare))): | ||
if to_compare[0][i] != to_compare[1][i]: | ||
return False | ||
|
||
return p0 + 1 | ||
|
||
if axis == "y": | ||
if p1 > len(reflection[0]) - 1: | ||
return False | ||
to_compare = [] | ||
i = 0 | ||
columns = [ | ||
[reflection[i][j] for i in range(len(reflection))] | ||
for j in range(len(reflection[0])) | ||
] | ||
to_compare = list(reversed(columns[: p0 + 1])), columns[p1:] | ||
for i in range(len(min(to_compare))): | ||
if to_compare[0][i] != to_compare[1][i]: | ||
return False | ||
return p0 + 1 | ||
|
||
|
||
def main(): | ||
input = get_input() | ||
reflections = format_reflections(input) | ||
sum = 0 | ||
for r in reflections: | ||
reflection_point = False | ||
for i in range(0, len(r)): | ||
reflection_point = find_reflection_point(r, "x", (i, i + 1)) | ||
if reflection_point: | ||
sum += reflection_point * 100 | ||
break | ||
if not reflection_point: | ||
for j in range(0, len(r[0])): | ||
reflection_point = find_reflection_point(r, "y", (j, j + 1)) | ||
if reflection_point: | ||
sum += reflection_point | ||
break | ||
if not reflection_point: | ||
print("no reflection point found") | ||
return False | ||
print(sum) | ||
|
||
|
||
main() | ||
|
||
|
||
# 31877 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
def get_input(): | ||
with open("13.txt", "r") as f: | ||
return [l for l in f.read().splitlines()] | ||
|
||
|
||
def format_reflections(input): | ||
reflections = [] | ||
c = [] | ||
for i in input: | ||
if i == "": | ||
reflections.append(c) | ||
c = [] | ||
else: | ||
c.append(i) | ||
reflections.append(c) | ||
return reflections | ||
|
||
|
||
def find_reflection_point(reflection, axis, p, target_occs=0): | ||
p0, p1 = p | ||
occurrences = 0 | ||
if axis == "x": | ||
if p1 > len(reflection) - 1: | ||
return False | ||
to_compare = list(reversed(reflection[: p0 + 1])), reflection[p1:] | ||
for i in range(min([len(to_compare[0]), len(to_compare[1])])): | ||
if to_compare[0][i] != to_compare[1][i]: | ||
for j, char in enumerate(to_compare[0][i]): | ||
if char != to_compare[1][i][j]: | ||
occurrences += 1 | ||
if occurrences > target_occs: | ||
return False | ||
|
||
return p1 if occurrences == target_occs else False | ||
|
||
if axis == "y": | ||
if p1 > len(reflection[0]) - 1: | ||
return False | ||
to_compare = [] | ||
columns = [ | ||
[reflection[i][j] for i in range(len(reflection))] | ||
for j in range(len(reflection[0])) | ||
] | ||
to_compare = list(reversed(columns[: p0 + 1])), columns[p1:] | ||
for i in range(min([len(to_compare[0]), len(to_compare[1])])): | ||
if to_compare[0][i] != to_compare[1][i]: | ||
for j, char in enumerate(to_compare[0][i]): | ||
if char != to_compare[1][i][j]: | ||
occurrences += 1 | ||
if occurrences > target_occs: | ||
return False | ||
return p1 if occurrences == target_occs else False | ||
|
||
|
||
def summarize_reflection_lines(reflections, target_occurrences): | ||
sum = 0 | ||
for r in reflections: | ||
reflection_point = False | ||
for i in range(0, len(r)): | ||
reflection_point = find_reflection_point( | ||
r, "x", (i, i + 1), target_occs=target_occurrences | ||
) | ||
if reflection_point: | ||
sum += reflection_point * 100 | ||
break | ||
if not reflection_point: | ||
for j in range(0, len(r[0])): | ||
reflection_point = find_reflection_point( | ||
r, "y", (j, j + 1), target_occs=target_occurrences | ||
) | ||
if reflection_point: | ||
sum += reflection_point | ||
break | ||
if not reflection_point: | ||
print("no reflection point found for reflection: ") | ||
print(r) | ||
return False | ||
return sum | ||
|
||
|
||
def main(): | ||
# set to 0 for part 1, 1 for part 2 | ||
target_occurrences = 0 | ||
input = get_input() | ||
reflections = format_reflections(input) | ||
print(summarize_reflection_lines(reflections, target_occurrences)) | ||
|
||
|
||
main() | ||
|
||
# 31877 pt 1 | ||
# 42996 pt 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#.##..##. | ||
..#.##.#. | ||
##......# | ||
##......# | ||
..#.##.#. | ||
..##..##. | ||
#.#.##.#. | ||
|
||
#...##..# | ||
#....#..# | ||
..##..### | ||
#####.##. | ||
#####.##. | ||
..##..### | ||
#....#..# |
Oops, something went wrong.