Skip to content

Commit 05b07b1

Browse files
author
Joe Webster
committed
Day 13 part 1 and 2
1 parent 368d568 commit 05b07b1

File tree

4 files changed

+4868
-0
lines changed

4 files changed

+4868
-0
lines changed

Diff for: 13/__init__.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
class TransparentOrigami(object):
2+
def __init__(self):
3+
self.dots = {}
4+
self.folds = []
5+
self.width = 0
6+
self.height = 0
7+
8+
def add_dot(self, x, y):
9+
self.dots.setdefault(y, {})
10+
self.dots[y][x] = 1
11+
12+
self.width = max(self.width, x)
13+
self.height = max(self.height, y)
14+
15+
def has_dot(self, x, y):
16+
try:
17+
return self.dots[y][x]
18+
except KeyError:
19+
return False
20+
21+
def fold(self, direction, index):
22+
fn = getattr(self, 'fold_' + direction)
23+
fn(index)
24+
25+
def fold_y(self, index):
26+
print('before fold_y %s' % index)
27+
self.status()
28+
29+
for y in range(index+1, self.height+1):
30+
for x in range(0, self.width+1):
31+
if self.has_dot(x, y):
32+
new_y = index - (y - index)
33+
self.add_dot(x, new_y)
34+
35+
self.dots = {k: v for k, v in self.dots.items() if k < index}
36+
self.height = index-1
37+
38+
print('after fold_y %s' % index)
39+
self.status()
40+
41+
def fold_x(self, index):
42+
print('before fold_x %s' % index)
43+
self.status()
44+
45+
new_dots = {}
46+
for y in range(0, self.height+1):
47+
new_dots.setdefault(y, {})
48+
for x in range(0, self.width+1):
49+
if x < index and self.has_dot(x, y):
50+
new_dots[y][x] = 1
51+
elif x > index and self.has_dot(x, y):
52+
new_x = index - (x - index)
53+
new_dots[y][new_x] = 1
54+
55+
self.dots = new_dots
56+
self.width = index-1
57+
58+
print('after fold_x %s' % index)
59+
self.status()
60+
61+
def load_data(self, data):
62+
loading_dots = True
63+
64+
for line in data:
65+
if line.startswith('fold'):
66+
direction, index = line.split()[-1].split('=')
67+
self.folds.append([direction, int(index)])
68+
elif ',' in line:
69+
print('dot line', line)
70+
x, y = line.split(',')
71+
self.add_dot(int(x), int(y))
72+
elif line == '':
73+
pass
74+
else:
75+
print("Ignoring load_data line:", line)
76+
77+
def status(self):
78+
print(self.dots)
79+
80+
for y in range(0, self.height+1):
81+
line = str(y).rjust(5) + ' '
82+
for x in range(0, self.width+1):
83+
line += '#' if self.has_dot(x, y) else '.'
84+
print(line)
85+
86+
print('dot count:', sum(len(l) for i,l in self.dots.items()))
87+
88+
def get_data():
89+
return [l.rstrip() for l in open('data.txt', 'r').readlines()]
90+
91+
def get_test_data():
92+
return [l.rstrip() for l in open('test_data.txt', 'r').readlines()]
93+
94+
def solution_part_1(data):
95+
to = TransparentOrigami()
96+
to.load_data(data)
97+
to.status()
98+
99+
fold = to.folds[0]
100+
to.fold(fold[0], fold[1])
101+
102+
def solution_part_2(data):
103+
to = TransparentOrigami()
104+
to.load_data(data)
105+
to.status()
106+
107+
for fold in to.folds:
108+
to.fold(fold[0], fold[1])
109+
110+
if __name__ == '__main__':
111+
solution_part_1(get_test_data())
112+
solution_part_1(get_data())
113+
114+
solution_part_2(get_test_data())
115+
solution_part_2(get_data())

0 commit comments

Comments
 (0)