aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

a-if-it-was-serious.py (1126B)


      1 import fileinput
      2 
      3 class Present:
      4 	def __init__(self, lines5):
      5 		self.baseshape = [line[:-1] for line in lines5[1:4]]
      6 		self.area = sum(l.count('#') for l in self.baseshape)
      7 
      8 	@staticmethod
      9 	def pretty(s):
     10 		return '\n'.join(s)
     11 
     12 	def __str__(self):
     13 		return Present.pretty(self.baseshape)
     14 
     15 	@staticmethod
     16 	def singlerot(s):
     17 		return [''.join(s[j][2-i] for j in range(3)) for i in range(3)]
     18 
     19 	@staticmethod
     20 	def singleflip(s):
     21 		return [''.join(s[i][2-j] for j in range(3)) for i in range(3)]
     22 
     23 	def shape(self, rot, flip):
     24 		s = list(self.baseshape)
     25 		for _ in range(rot):
     26 			s = Present.singlerot(s)
     27 		if flip:
     28 			s = Present.singleflip(s)
     29 		return s
     30 
     31 class Region:
     32 	def __init__(self, line):
     33 		wh, n = line[:-1].split(': ')
     34 		self.w, self.h = tuple(int(i) for i in wh.split('x'))
     35 		self.p = [int(i) for i in n.split(' ')]
     36 
     37 	def __str__(self):
     38 		return f"({self.w}x{self.h}) {self.p}"
     39 
     40 with fileinput.input() as lines:
     41 	lines = list(lines)
     42 	presents = [Present(lines[5*i:5*(i+1)]) for i in range(6)]
     43 	regions = [Region(line) for line in lines[30:]]
     44 
     45 # Then you'd have to find some algorithm to solve this, but it is crazy hard