my advent of code 2021 solutions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.9 KiB

3 years ago
import sys
check = True
inpx = []
inpy = []
inp2 = []
with open(sys.argv[1]) as f:
for i in f.readlines():
i = i.strip()
if i == '':
check = False
continue
if check:
temp1, temp2 = i.split(',', 1)
inpx.append(int(temp1))
inpy.append(int(temp2))
else:
i = i.replace('fold along ', '')
i = i.strip()
temp1, temp2 = i.split('=', 1)
inp2.append((temp1, int(temp2)))
maxx = max(inpx)
maxy = max(inpy)
class grid:
def __init__(self, maxx, maxy):
self._points = []
for i in range(maxy + 1):
self._points.append(['.'] * (maxx + 1))
def mark(self, x, y):
self._points[y][x] = '#'
def fold(self, axis, loca):
if axis == 'y':
for i in range(loca + 1, len(self._points)):
for t in range(len(self._points[i])):
if self._points[i][t] == '#':
temp = i - loca
self._points[loca -temp][t] = '#'
del self._points[loca:len(self._points)]
if axis == 'x':
for i in range(len(self._points)):
for t in range(loca + 1, len(self._points[i])):
if self._points[i][t] == '#':
temp = t - loca
self._points[i][loca - temp] = '#'
for f in self._points:
del f[loca:len(f)]
def count(self):
counter = 0
for f in self._points:
for g in f:
if g == '#':
counter += 1
return counter
gri = grid(maxx, maxy)
folds = 0
for i in range(len(inpx)):
gri.mark(inpx[i], inpy[i])
for i in inp2:
gri.fold(i[0], i[1])
folds += 1
print('Points after ', folds, 'fold(s):', gri.count())
for i in range(len(gri._points)):
print(gri._points[i])