Loading 2021/day6/README +7 Changes for 2021/day6/README: 7 added lines, 0 removed lines. Original line number Diff line number Diff line I fell in the trap of making a class per fish. I advise you to run code1 with the number of days of part 2 as a parameter before looking at code2 to understand the problem of this approach. I am pretty sure there is a nice modula algebra formula to get it cleanly but I fell for the quick and dirty approach of computing it explicitely. 2021/day6/code1 +x+60 Changes for 2021/day6/code1: 60 added lines, 0 removed lines. Original line number Diff line number Diff line #!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' Your docstring here ''' import numpy as np from scipy.optimize import curve_fit rank = 0 class Fish: def __init__(self, counter, rank=0): self.counter = counter self.rank = rank def check(self, fish): if self.counter == 0: self.counter = 6 fish.append(Fish(9)) else: self.counter -= 1 def __repr__(self): return str(self.counter) def read_input(filename): with open(filename, 'r') as f: line = f.readline().strip().split(',') data = list(map(int, line)) return data def sol1(filename, ndays): data = read_input(filename) fish = [] print("Sol 1:") for d in data: fish.append(Fish(d)) for i in range(ndays): for f in fish: f.check(fish) print(i) print("Day: {}, Number of fish: {}".format(i+1, len(fish))) def main(): filename = 'input' ndays = 80 sol1(filename, ndays) return if __name__ == "__main__": try: main() except KeyboardInterrupt: raise SystemExit("User interruption.") 2021/day6/code2 +x+54 Changes for 2021/day6/code2: 54 added lines, 0 removed lines. Original line number Diff line number Diff line #!/usr/bin/env python3 # -*- coding: utf-8 -*- class Shoal: def __init__(self, fish): self.pop = {} for i in range(9): self.pop[i] = 0 for f in fish: self.pop[f] += 1 def sum(self): s = 0 for i in range(9): s += self.pop[i] return s def step(self): newpop = {8:0} for i in range(7, -1, -1): newpop[i] = self.pop[i+1] newpop[6] += self.pop[0] newpop[8] += self.pop[0] self.pop = newpop def read_input(filename): with open(filename, 'r') as f: line = f.readline().strip().split(',') data = list(map(int, line)) return data def sol2(filename, days): data = read_input(filename) my_shoal = Shoal(data) for _ in range(days): my_shoal.step() print(my_shoal.sum()) def main(): filename = 'input' days = 256 sol2(filename, days) return if __name__ == "__main__": try: main() except KeyboardInterrupt: raise SystemExit("User interruption.") 2021/day6/input +1 Changes for 2021/day6/input: 1 added line, 0 removed lines. Original line number Diff line number Diff line 4,1,1,1,5,1,3,1,5,3,4,3,3,1,3,3,1,5,3,2,4,4,3,4,1,4,2,2,1,3,5,1,1,3,2,5,1,1,4,2,5,4,3,2,5,3,3,4,5,4,3,5,4,2,5,5,2,2,2,3,5,5,4,2,1,1,5,1,4,3,2,2,1,2,1,5,3,3,3,5,1,5,4,2,2,2,1,4,2,5,2,3,3,2,3,4,4,1,4,4,3,1,1,1,1,1,4,4,5,4,2,5,1,5,4,4,5,2,3,5,4,1,4,5,2,1,1,2,5,4,5,5,1,1,1,1,1,4,5,3,1,3,4,3,3,1,5,4,2,1,4,4,4,1,1,3,1,3,5,3,1,4,5,3,5,1,1,2,2,4,4,1,4,1,3,1,1,3,1,3,3,5,4,2,1,1,2,1,2,3,3,5,4,1,1,2,1,2,5,3,1,5,4,3,1,5,2,3,4,4,3,1,1,1,2,1,1,2,1,5,4,2,2,1,4,3,1,1,1,1,3,1,5,2,4,1,3,2,3,4,3,4,2,1,2,1,2,4,2,1,5,2,2,5,5,1,1,2,3,1,1,1,3,5,1,3,5,1,3,3,2,4,5,5,3,1,4,1,5,2,4,5,5,5,2,4,2,2,5,2,4,1,3,2,1,1,4,4,1,5 2021/day6/output +1 Changes for 2021/day6/output: 1 added line, 0 removed lines. Original line number Diff line number Diff line 193 Loading
2021/day6/README +7 Changes for 2021/day6/README: 7 added lines, 0 removed lines. Original line number Diff line number Diff line I fell in the trap of making a class per fish. I advise you to run code1 with the number of days of part 2 as a parameter before looking at code2 to understand the problem of this approach. I am pretty sure there is a nice modula algebra formula to get it cleanly but I fell for the quick and dirty approach of computing it explicitely.
2021/day6/code1 +x+60 Changes for 2021/day6/code1: 60 added lines, 0 removed lines. Original line number Diff line number Diff line #!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' Your docstring here ''' import numpy as np from scipy.optimize import curve_fit rank = 0 class Fish: def __init__(self, counter, rank=0): self.counter = counter self.rank = rank def check(self, fish): if self.counter == 0: self.counter = 6 fish.append(Fish(9)) else: self.counter -= 1 def __repr__(self): return str(self.counter) def read_input(filename): with open(filename, 'r') as f: line = f.readline().strip().split(',') data = list(map(int, line)) return data def sol1(filename, ndays): data = read_input(filename) fish = [] print("Sol 1:") for d in data: fish.append(Fish(d)) for i in range(ndays): for f in fish: f.check(fish) print(i) print("Day: {}, Number of fish: {}".format(i+1, len(fish))) def main(): filename = 'input' ndays = 80 sol1(filename, ndays) return if __name__ == "__main__": try: main() except KeyboardInterrupt: raise SystemExit("User interruption.")
2021/day6/code2 +x+54 Changes for 2021/day6/code2: 54 added lines, 0 removed lines. Original line number Diff line number Diff line #!/usr/bin/env python3 # -*- coding: utf-8 -*- class Shoal: def __init__(self, fish): self.pop = {} for i in range(9): self.pop[i] = 0 for f in fish: self.pop[f] += 1 def sum(self): s = 0 for i in range(9): s += self.pop[i] return s def step(self): newpop = {8:0} for i in range(7, -1, -1): newpop[i] = self.pop[i+1] newpop[6] += self.pop[0] newpop[8] += self.pop[0] self.pop = newpop def read_input(filename): with open(filename, 'r') as f: line = f.readline().strip().split(',') data = list(map(int, line)) return data def sol2(filename, days): data = read_input(filename) my_shoal = Shoal(data) for _ in range(days): my_shoal.step() print(my_shoal.sum()) def main(): filename = 'input' days = 256 sol2(filename, days) return if __name__ == "__main__": try: main() except KeyboardInterrupt: raise SystemExit("User interruption.")
2021/day6/input +1 Changes for 2021/day6/input: 1 added line, 0 removed lines. Original line number Diff line number Diff line 4,1,1,1,5,1,3,1,5,3,4,3,3,1,3,3,1,5,3,2,4,4,3,4,1,4,2,2,1,3,5,1,1,3,2,5,1,1,4,2,5,4,3,2,5,3,3,4,5,4,3,5,4,2,5,5,2,2,2,3,5,5,4,2,1,1,5,1,4,3,2,2,1,2,1,5,3,3,3,5,1,5,4,2,2,2,1,4,2,5,2,3,3,2,3,4,4,1,4,4,3,1,1,1,1,1,4,4,5,4,2,5,1,5,4,4,5,2,3,5,4,1,4,5,2,1,1,2,5,4,5,5,1,1,1,1,1,4,5,3,1,3,4,3,3,1,5,4,2,1,4,4,4,1,1,3,1,3,5,3,1,4,5,3,5,1,1,2,2,4,4,1,4,1,3,1,1,3,1,3,3,5,4,2,1,1,2,1,2,3,3,5,4,1,1,2,1,2,5,3,1,5,4,3,1,5,2,3,4,4,3,1,1,1,2,1,1,2,1,5,4,2,2,1,4,3,1,1,1,1,3,1,5,2,4,1,3,2,3,4,3,4,2,1,2,1,2,4,2,1,5,2,2,5,5,1,1,2,3,1,1,1,3,5,1,3,5,1,3,3,2,4,5,5,3,1,4,1,5,2,4,5,5,5,2,4,2,2,5,2,4,1,3,2,1,1,4,4,1,5
2021/day6/output +1 Changes for 2021/day6/output: 1 added line, 0 removed lines. Original line number Diff line number Diff line 193