diff --git a/day20-2.py b/day20-2.py new file mode 100644 index 0000000..ba37210 --- /dev/null +++ b/day20-2.py @@ -0,0 +1,75 @@ +import sys + +class img: + def __init__(self): + self.y = [] + + def addline(self, line): + self.y.append(line) + + def get_num(self,y,x, test): + number='' + if y != 0 and x != 0: number += self.y[y - 1][x - 1] + else: number += test + if y !=0: number += self.y[y - 1][x] + else: number += test + if y != 0 and x != len(self.y[0]) - 1: number += self.y[y - 1][x + 1] + else: number += test + if x != 0: number += self.y[y][x - 1] + else: number += test + number += self.y[y][x] + if x != len(self.y[0]) - 1: number += self.y[y][x + 1] + else: number += test + if y != len(self.y) - 1 and x != 0: number += self.y[y + 1][x - 1] + else: number += test + if y != len(self.y) - 1: number += self.y[y + 1][x] + else: number += test + if y != len(self.y) - 1 and x != len(self.y[0]) - 1: number += self.y[y + 1][x + 1] + else: number += test + number = number.replace('.', '0') + number = number.replace('#', '1') + + return (int(number, 2)) + + def enhance(self, test): + new_image = [] + self.y.insert(0, test*len(self.y[0])) + self.y.append(test*len(self.y[0])) + for i in range(len(self.y)): + self.y[i] = test + self.y[i] + test + for i in range(len(self.y)): + new_line = '' + for t in range(len(self.y[i])): + num = self.get_num(i,t,test) + new_line += algorithm[num] + new_image.append(new_line) + self.y = new_image.copy() + + def count(self): + counter = 0 + for f in self.y: + for g in f: + if g == '#': + counter += 1 + return(counter) + + +image = img() + +with open(sys.argv[1], 'r') as f: + toggle = False + for g in f.readlines(): + g = g.strip() + if g == '': + toggle = True + if not toggle: + algorithm = g + if toggle and g != '': + image.addline(g) + +for i in range(25): + image.enhance('.') + image.enhance('#') + +print(image.count()) + diff --git a/day20.py b/day20.py new file mode 100644 index 0000000..94848b9 --- /dev/null +++ b/day20.py @@ -0,0 +1,74 @@ +import sys + +class img: + def __init__(self): + self.y = [] + + def addline(self, line): + self.y.append(line) + + def get_num(self,y,x): + number='' + if y != 0 and x != 0: number += self.y[y - 1][x - 1] + else: number += '.' + if y !=0: number += self.y[y - 1][x] + else: number += '.' + if y != 0 and x != len(self.y[0]) - 1: number += self.y[y - 1][x + 1] + else: number += '.' + if x != 0: number += self.y[y][x - 1] + else: number += '.' + number += self.y[y][x] + if x != len(self.y[0]) - 1: number += self.y[y][x + 1] + else: number += '.' + if y != len(self.y) - 1 and x != 0: number += self.y[y + 1][x - 1] + else: number += '.' + if y != len(self.y) - 1: number += self.y[y + 1][x] + else: number += '.' + if y != len(self.y) - 1 and x != len(self.y[0]) - 1: number += self.y[y + 1][x + 1] + else: number += '.' + number = number.replace('.', '0') + number = number.replace('#', '1') + + return (int(number, 2)) + + def enhance(self): + new_image = [] + self.y.insert(0, '.'*len(self.y[0])) + self.y.append('.'*len(self.y[0])) + for i in range(len(self.y)): + self.y[i] = '.' + self.y[i] + '.' + for i in range(len(self.y)): + new_line = '' + for t in range(len(self.y[i])): + num = self.get_num(i,t) + new_line += algorithm[num] + new_image.append(new_line) + self.y = new_image.copy() + + def count(self): + counter = 0 + for f in self.y: + for g in f: + if g == '#': + counter += 1 + return(counter) + + +image = img() + +with open(sys.argv[1], 'r') as f: + toggle = False + for g in f.readlines(): + g = g.strip() + if g == '': + toggle = True + if not toggle: + algorithm = g + if toggle and g != '': + image.addline(g) + +image.enhance() +image.enhance() + +print(image.count()) + diff --git a/input20.txt b/input20.txt new file mode 100644 index 0000000..05ddd45 --- /dev/null +++ b/input20.txt @@ -0,0 +1,102 @@ +#.#.....##...##..#.#......#.#...#.#.#...###.##......###.##.##..##.#...#.....###.#.....#.#.#...#.#..###.###..###..#..##..###..##..##.##.#..###########.##....#.#......#...#.###..###...#.####..########.#####.#.#..##.##.##..###.##.####.#..##.##..#...#####..#.#.##.##...##..#..##.....###.#.#....####.##.#...##.########.#.##.#.....###....#..###.####....############.#.##...#.####...#...##.#.#..#..#......#..##...#.########.#.#...#####..#..######.#.#.....#####...##.###.#.#.##.........#.#.##..##.#..#..##..##.###.##.##. + +#.#...##.#.##.##...#......##....##...#.#....##.##.#.##....###.#####.#......###......#.#.##.####..#.# +#.#...###.#.#.#...##...###....#####.#...#.####..########.#.#.#..#.###..##..#.####....#.#..#..##.#### +.....#.######..#.#.....#..#..####.########..#.#.####..#####..##.#..##..##.##.#..###.#.#.#.#...#..### +#.#........#.###.#..######.##..#.##...#.....#.#.......#.##.....#.###.##.#..##..#.##...##...##.####.. +#...#.#.##..###..#..####..#####.##...###.#....#..###..#.#....####..####.####..#..###.####.###.#..### +#..###..##.#....#.##..##.#..#.##....#.#.#.##..#.#.##..#..##.....#####..#..##.##.##..#.#####.###.#... +..####..##...#....##.#...#######..#....##.###...#.#.........##........###.#.##....#.#.###...#...###. +#######..#......##...##..###..##.#..##.#...###..##..#.#.##....##...##.#..##..##....#..###...##...#.. +.###.....#..#..##....#...#.##.#..#.#.#...#..#...#..#.#.###.####..#.###.###.###.#.#..#....#.#.#.#.##. +...##.#.#.#..#..#..#.#.##..#.....#...#####..#####..#.#..#...##..#..#...###.....#...#.#.#....#..##.## +.#####.##.##..#...###.#....##...###..#..#...#...########..#.#...#.#..####........####...###...##.... +.#..#..#.#....##..#.###...#..#.###...##.###.##.#...#####....#.###..#.#..#....#.##..#.......#.....#.# +.#..##..#..####..##.#.#..##..###..#.......#...#...#.####.#.#.#..#.##......#.##.###.###.####.##.#.### +.##..##.#..##..#.##.###...#.#####.#.#.#.##...#..#...##..##...#..###.###.##..##.#######.###....###### +....#.#..##...#..#####...##.#.##......#.###.#.#.###..#...###..##.#..#..####..#.#....#..#.####...#..# +.#..##..##...#.####.#####.#######.######.####.......#.#.###.##.#.#.#.#.##.....#....#....#..###....## +###.##..#.#.#.#.#..#######.#.########.#.#.#..####.#####.#.#..##..#..##.....#...#....#.##.##.#....#.# +#..#.####.####.##.####...#.###.#.##......###..##.####..#..####..##.#.#....##..##.######...#..##..... +#.##...#...#..##...#.#......#..######.#.###.####..#######..#.##...#.#...#.##..###.#####..#...#.####. +##.###.#.######.#...#..##.#...#..##.##.....###..#.##.#..#..####....#.##.##..#.#..#.#....#.....####.. +.#.##.##.###..#...#.#.#.#.#.......#.####.#####..####.#....##.#...##.##.#.#.##.#..#.###.##..###...#.# +#.###.#.##.#.........#..#.#.#.#..##.#..##.#####.###.#.#..#.#.#..##..###..#.##.####.#..#...###.....## +##..#####.#...#.....###.#.#...##.####..#..###....##...#.##.##.##...#.###..#.##...........###.####.## +.###....###.#..#.#..#..#.###..########..##......########.#..#######...#........#.#######..####..#..# +####......#...#..#...#.######..#.##.#.##....#.##.#.#.##.#....##...###..##....#.#.##.##.....###.#.##. +.#.####.##..##.#.#.#.##.###.##....#..#...#.###.#.#...#.....##..##.####....#..######.#.#.#.##...#...# +...#.##...##..##.###..###....#..##.....#####.#.#..#.....##..##.###..##..##.##.....#.#..##.#..###..#. +..##....####...##.....###.#...#.###...##.#.#..##.###....###.##........#.#..#..##..#.######..##...... +###.##.##.#...##.#...###.##.##.######.###.###..###.#....#.#....#.#.#...###..###.#.#...######.#...### +..#.####.########.####.#.#...#..##...##...###..####.###..##..#.##..###..#.####.###..#.#.#.#######..# +###..#.##.##.#..#..#.#..#.###...#...#.##.#####.#.###.#.###.#.##..#...#.#...##...###.....#..###....## +..####.#....####..#.##.....#..#.#.###.###.#.#......#.....####.......##.#.######..####..#######.#..## +.##.#.#..#..###....#....#...##.##.###..##.##...#.######......##....##..#.#.#..##..#####....#.#...#.. +##...##..#...#.#######..#.....#..##.....#####..#...##.###.####.###..###...#..###........###.##.#.### +...###.#....#...##.....##.####..##.##.#.#..##.######.#..###.##.....##.####..#..##.###.##..###.###..# +#.#...##....#....#.##....##...##..#.#...######..###.#.#..#.#.###.#####.#..###....#.##...####..####.. +#....###.###.#...########.##.##.######.##....#...##.#.#.#..#.##.#.....#..#.....#..........#.##..#... +.#.#.#.#.##.##.###...#.#.#...##...#.##.##..#..#...##...##..#.##.#.##....#.###....####..#..###.#..### +...#.#..##.#..##.#.##.####.#.#...###...#.#.###..########.#...###...###..#..#.###..####..##.#..#...#. +##.#...#######.....#.###..#.#..####.##..##.#..##...#.##..##.#####......######.#.#.##.#..####..#.#### +#.......#.#########...#...#####...##.###.#..##..##..#...##..##.#.#.###.#.##..#...#....##..#.#.#...#. +###..###..#...##.#...####.###..##.###..##..##.##.....#.#.#.####.######........#...#####.....#..##### +##.#.#.########..#.#.##.#####..##.###...#...#.#.##...###.#.###..#..##.#......#.#.#.##.#.#.#....###.# +#.##.#..#...##..###..##..#..##..##.###...#.##.##.#.#..........##...##.##..##..#....##.#####..##..#.. +.#...##.##.#..#.##....#..##.##...#...####.###..##...#...#.##...######.#.##.#.#.##.####.##..#.###.### +##.#.#..#.#.###.#..#.##.#....#....####..#.#..##.#######..##..#...#..####.#####....#.####.#..#.#.###. +....##.##..#.#####.###.#.###.#.#.#.#..#.###.#..######....##.##.....#.#.#...###..###..#..###....##.## +#.#######.#..#.#.##.##...#.#..###.##.##..#..###...#.#...#..##.##..##.#.#.##.##.#######.#..#.#..#.#.# +###.....#####..####..#...#..##.#....##.##...##...##..###...#..##..##..####.#.....##.####..#...##.... +####..##..##.#...#.###.##...###.#..#...##.##.#...#.##....####.#.#..#...##..##.##..###.......#..#..#. +#.#.#####..#....#.#...#..##.##..##..#.####..###.####.####.#####.#.#..###.##....#....#.#.###.#.#..##. +...##...#.#.#####.#.#....##.##..#...##...........#.#....#.##.##.....##..###...#.#####..........##.## +.#..#..#.#...#.........####....##........##...#...#######.####.###.###...#....###..#.###.##.#.####.# +..#.#.....#...###.####......###.#..#.#.#.#...####...#####.#..#....#.#.#....#.##.###....#..#....##.## +##.#...##....###.#......####.#.####.##...#..#.#.#.#...#####.#..#..##.#.#...#.####....###.####.#.#.## +.#.#..#....#.####.#..#.###..#.#.#.#.#.###...#.##.##.#.##...####..######....####...##.##.#..##.#.#..# +..##.#...##.####..#.###..##..#.###.#.##..##.###.#.#####..#.##.#.....#.#####.####..#.#.######.#.##.#. +#.....##..###..###.....###.##.###.#..##.#..##.###....#..#.#.##....#.##.##.#...###.###.#......#..#... +.##.#..#...#...#.#..##.....#..#.###....###..#.##...#.##.#.##..##......#.#.#.####.#..#..##...##..##.# +####...##...#......##.##..#######....###.##.#.###.#.#.###...####..#..#.#.##.........#.##.#....#####. +.####.#####..##..###..#.##.##.####.......#.###.#####.#..#...#..#..####.#.....#.####.#.....###...#### +#.##......#.#.#.##....#.#.##..#...#.#.#..#...##..#.##.##..#..#####..###.##.#...##...#..#...##.##..#. +.##.#.#.###..###.##....#...###...####...####..##.###.####.###.#..#.###.#..#.....#.##.#..#.##.#.#.#.# +...#.###.#..#..#.###.#....##..##.#..###.######.#...##..#.#...###.....#.###.#.#....##...##.###.#.#### +.#..#..#....###..##..#....##.##..####.#.#.#..#.######....####.##.##....##.#.#.####.#..#.##.##....#.. +#..##.#...#.###..#.###..###..#.##.#.######.#.#.####....###.##....#.##.#......##.##...##....#.#.###.# +#...##.#.####...#######..#....##.#...##..#.#....#.#.#####.####.##...#.###.##.#.#.##...##.#....##..## +....#.##.#.##.##.#.#..###...##.#.#.#....#...#..........###.#####.##...##..#..#.##.#...#..###..#####. +###..###.##..#.###.#.#.#...##.#.###.##.###...##.###.....####.#...##.#.#.#..#.##...#.....#..#.#.#...# +####...#.####.#.#.#..#.##.##.##.#.#.####...#...#.#..###....##.##..##.###...##.#...#####.###.#..#.... +##..##...###..###.#.###.##.####.#..#.#..####.##.#...#..#..#.....##.#.#####...##.....#.#.#..#..##..#. +.....#......#..##..#...#.#.#.######....###.##.##..#.###...#...#.####.##.##.###.#.######.#..#.#.##... +#.#.#.###........#..#.#.#.#....##..##..###...##.#.##.#..#######.#..###.###.#.###.###.#.#.#.....##..# +#.##....####.......#..##.####.#.##..###.##.###.#.#.###...###.....######...######.#.....###.##....... +#.##..#...#..#..#....#.##...#..#.###...#...#########.#.#...#.....#...#..#.#...#.#.#...#.###.####..## +.#..##........###.#.#....#.......##.#..#.#.#.##.......#.#.#.#..#.#.....#....#.###.#.##...###.....#.. +.#.#...##...#....#.##..##.#...#.##..#..##.#..###.#####..##.#.##.####.####.###...##....##......###..# +#..##....##.###.##.....#.#.....#.#...#..##....##.###.#....###.....######.#.##.###..#..#.#...#.....#. +....#.####.....#..#..#...#.###.#####..##...#.#..###.#...#...##.###.##..#.######..####.#.#....#.#..## +##.#..##.#.#.##..##..#...###..##.##..#.###..#...###..##.#.###.####...#..#.#######.##.###.###.#.#..#. +...##...##.#.#.##.#.##.###..##...##.#..#..##.##.##.##.#.####..##.######....#..#.#.#....###.#.##.#.#. +.#.....#.###...##.#.##.#..###.##..####.###..#.#.....#..##.#.#..##...##.#..#.##..#########..#.#.##.#. +####.#..#.#..##...#..#.##..#..#...#.###.....#..##...#..####....#######.....#.##.##..##...#.##..####. +########...#.#.####..####.#.#.#..##.#.#.##...#...#..#...#..###.#.#..##..#####.#.#.#.##.#.###...#.##. +.#....#..######.#.#.#..##.#.#...#####....##..##.##.#####....#.##..#..#.#.#.###..#.####.###.........# +...####..##......###..#..###..#.#.###.#...#..####.#.#..###....#..##.####.#..#.....#.........##..###. +.####...##...#####.##.##...##...##..##...#.#####...#...##...#.####..####.###.....##.##...##.#######. +##..####....#.#..#.#....##..#####....#.#...#.####..#######..##.#####.#.#.#.##.#.#..#...#.###.##.#.## +#.####....#..##########...###.#..#####.#.#.##....#..#....######.....#...#.##.##.#.##..#.###..###..#. +##.#.##.###..#.###.....##..#.###.#....#####.###...#.#.##.#..#.###.#.#.#####.#.##.##.##.#.#.###.###.. +.###.#.#.##.######.#.#..##.#..####..#....##......#...##.##.#.......#.#.#.####.#.#.##....##.####..#.# +####..#...##....#.#.#....#.#####...#..#....##..#..####...#..###...........####.###.###......#..##..# +..##.####.##....####....#.#.####..#...###.#....#.##.###.#.##..####.#..###..#...#..#####..#...##...#. +...#..#....#.#..##.#.####....#..####...##.#..#####.###..#.##..###.######...#...#...######.##....##.. +..#########.#####.#####.###.##.#.###.###..#.#.#.#.###.#...###........####..#####.#.##.##.#..#..#.##. +#..###.##.###.#...#.#.#.##.###........#.#.#.#..#..##.##.#.....#.#.....##...#..#.##..#..#..#......#.# +...##..#...##.....##...####.....#.##..###.....#..#.###.#..#.#...#..#.#.....###.###.##.#.#.#..#.#.... +#...#...#.#.#...#####.####..#....##...##.##.##.#...#.#.#..#...##....#...###.#.##.##....########..#.. +.#.####....#.#.....########.#....######.#.#######...##..#.###.#.###..###.#..###....#.#.###.....###.# +..#.##.##....#..#...#..#.#.#...###...#..#.#.#.####.#.#####.####....##..#..###.#.#..####....##.####.. diff --git a/testdata20.txt b/testdata20.txt new file mode 100644 index 0000000..8fa4bd4 --- /dev/null +++ b/testdata20.txt @@ -0,0 +1,7 @@ +..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..# + +#..#. +#.... +##..# +..#.. +..###