Advertisement
Guest User

AOC 2021 Day 6

a guest
Dec 9th, 2021
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. # aoc202106.py
  2.  
  3. import pathlib
  4. import sys
  5.  
  6.  
  7. def parse(puzzle_input: str) -> list[int]:
  8.     """ Parse input """
  9.     return [int(char) for char in puzzle_input.split(',')]
  10.  
  11.  
  12. def grow_shoal(initial_state: list[int], cycle_length: int,
  13.                num_days: int) -> list[int]:
  14.     """ Increase shoal of fish by num of days """
  15.     stages = [0] * cycle_length
  16.     for fish in initial_state:
  17.         stages[fish] += 1
  18.     for _ in range(num_days):
  19.         zero_day = stages[0]
  20.         stages = stages[1:] + stages[:1]
  21.         stages[6] += zero_day
  22.     return stages
  23.  
  24.  
  25. def part1(data: list[int]) -> int:
  26.     """ Solve part 1 """
  27.     days = 80
  28.     generation = 9
  29.     return sum(grow_shoal(data, generation, days))
  30.  
  31.  
  32. def part2(data: list[int]) -> int:
  33.     """ Solve part 2 """
  34.     days = 256
  35.     generation = 9
  36.     return sum(grow_shoal(data, generation, days))
  37.  
  38.  
  39. def solve(puzzle_input: str) -> tuple[int, int]:
  40.     """ Solve the puzzle for the given input """
  41.     data = parse(puzzle_input)
  42.     solution1 = part1(data)  # Correct answer was 391888 (with my data)
  43.     solution2 = part2(data)  # Correct answer was 1754597645339 (with my data)
  44.  
  45.     return solution1, solution2
  46.  
  47.  
  48. if __name__ == "__main__":
  49.     for path in sys.argv[1:]:
  50.         print(f"{path}:")
  51.         puzzle_input = pathlib.Path(path).read_text().strip()
  52.         solutions = solve(puzzle_input)
  53.         print('\n'.join(str(solution) for solution in solutions))
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement