mirror of
https://github.com/cupcakearmy/advent-of-code.git
synced 2024-12-22 08:06:25 +00:00
day 13
This commit is contained in:
parent
bf5adf42c8
commit
607651d3ba
13
2021/13/README.md
Normal file
13
2021/13/README.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# 13
|
||||||
|
|
||||||
|
Ahh, RTFM! I did not get what was wrong. You are supposed to only input the first fold for the first question.
|
||||||
|
|
||||||
|
Second part is a joy :) I already had the "screen" working, so that was an easy fix win.
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Solutions</summary>
|
||||||
|
<ol>
|
||||||
|
<li>807</li>
|
||||||
|
<li>2</li>
|
||||||
|
</ol>
|
||||||
|
</details>
|
101
2021/13/python/main.py
Normal file
101
2021/13/python/main.py
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
from os.path import dirname, join
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
|
# Day 13
|
||||||
|
|
||||||
|
# Common
|
||||||
|
|
||||||
|
|
||||||
|
def read_input(filename):
|
||||||
|
data = join(dirname(__file__), '..', filename)
|
||||||
|
with open(data) as f:
|
||||||
|
return f.read().strip()
|
||||||
|
|
||||||
|
|
||||||
|
test = read_input('test.txt')
|
||||||
|
data = read_input('input.txt')
|
||||||
|
|
||||||
|
|
||||||
|
class Paper:
|
||||||
|
def __init__(self, points: set[tuple[int, int]], folds=list[tuple[bool, int]]):
|
||||||
|
self.points = points
|
||||||
|
self.folds = folds
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
max_x = 0
|
||||||
|
max_y = 0
|
||||||
|
for x, y in self.points:
|
||||||
|
max_x = max(max_x, x)
|
||||||
|
max_y = max(max_y, y)
|
||||||
|
|
||||||
|
# Init grid
|
||||||
|
grid = [
|
||||||
|
['.' for _ in range(max_x+1)]
|
||||||
|
for _ in range(max_y+1)
|
||||||
|
]
|
||||||
|
|
||||||
|
# Fill grid
|
||||||
|
for x, y in self.points:
|
||||||
|
grid[y][x] = '#'
|
||||||
|
|
||||||
|
return '\n'.join([
|
||||||
|
''.join(row)
|
||||||
|
for row in grid
|
||||||
|
])
|
||||||
|
|
||||||
|
def fold_all(self):
|
||||||
|
for fold in self.folds:
|
||||||
|
self.fold(fold)
|
||||||
|
|
||||||
|
def fold(self, fold):
|
||||||
|
x, threshold = fold
|
||||||
|
i = 0 if x else 1
|
||||||
|
for point in list(self.points):
|
||||||
|
value = point[i]
|
||||||
|
if value < threshold:
|
||||||
|
continue
|
||||||
|
new_value = 2*threshold - value
|
||||||
|
self.points.remove(point)
|
||||||
|
p = list(point)
|
||||||
|
p[i] = new_value
|
||||||
|
self.points.add(tuple(p))
|
||||||
|
|
||||||
|
def flag_1(self):
|
||||||
|
self.fold(self.folds[0])
|
||||||
|
print(len(self.points))
|
||||||
|
|
||||||
|
def flag_2(self):
|
||||||
|
self.fold_all()
|
||||||
|
print(self)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def parse(data: str) -> 'Paper':
|
||||||
|
header, footer = data.split('\n\n')
|
||||||
|
points = {
|
||||||
|
tuple(map(int, point.split(',')))
|
||||||
|
for point
|
||||||
|
in header.split('\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
folds = [
|
||||||
|
(fold[11] == 'x', int(fold[13:]))
|
||||||
|
for fold in footer.split('\n')
|
||||||
|
]
|
||||||
|
return Paper(points, folds)
|
||||||
|
|
||||||
|
# 1
|
||||||
|
print('1.')
|
||||||
|
paper = Paper.parse(test)
|
||||||
|
paper.flag_1()
|
||||||
|
paper = Paper.parse(data)
|
||||||
|
paper.flag_1()
|
||||||
|
|
||||||
|
|
||||||
|
# 2
|
||||||
|
print('\n2.')
|
||||||
|
paper = Paper.parse(test)
|
||||||
|
paper.flag_2()
|
||||||
|
paper = Paper.parse(data)
|
||||||
|
paper.flag_2()
|
Loading…
Reference in New Issue
Block a user