Skip to content

Instantly share code, notes, and snippets.

@rhbvkleef
Created December 7, 2021 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhbvkleef/0ec152310954e62999170f70ca8c7489 to your computer and use it in GitHub Desktop.
Save rhbvkleef/0ec152310954e62999170f70ca8c7489 to your computer and use it in GitHub Desktop.
from time import time
import numpy as np
from numpy.linalg import matrix_power
from collections import Counter
start = time()
nums = [0] * 9
for idx, count in Counter(map(int, open("priv/inputs/year2021/day6.txt").readlines()[0].split(","))).items():
nums[idx] = count
nums = np.array(nums)
matrix = np.array([
[0, 0, 0, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0],
], dtype=object)
dot = matrix_power(matrix, 256).transpose().dot(nums)
print(dot.sum())
print(f"Took: {time() - start}s")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment