Skip to content

Commit

Permalink
feat(2019): day 16 - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rpidanny committed Jan 3, 2024
1 parent b1013a2 commit 21d1bcc
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 2019/Day16/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# --- Day 0: Template ---

To be used as template for the other days.
48 changes: 48 additions & 0 deletions 2019/Day16/fft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from functools import cache


class FlawedFrequencyTransmission:
def __init__(self, signal):
self.original_signal = list(map(int, list(signal)))
self.base_pattern = [0, 1, 0, -1]

@cache
def _get_pattern(self, i):
pattern = []
for element in self.base_pattern:
pattern.extend([element] * (i + 1))
return pattern

@cache
def _get_multiplier(self, i: int, j: int) -> int:
pattern = self._get_pattern(i)
idx = (j + 1) % len(pattern)
return pattern[idx]

def _get_digit(self, i: int, signal: list[int]):
num = 0
for j, digit in enumerate(signal):
num += digit * self._get_multiplier(i, j)
return abs(num) % 10

def apply(self, times=1, offset=0) -> str:
signal = self.original_signal

start_idx = 0
if offset > 0:
start_idx = int("".join(map(str, self.original_signal[:offset])))

seen = set()
seen.add(tuple(signal))

for _ in range(times):
signal = [
self._get_digit(i, signal) for i in range(len(self.original_signal))
]
if tuple(signal) in seen:
print("Pattern")
seen.add(tuple(signal))

# return "".join(map(str, signal))[start_idx:8]
print(f"Full response: {''.join(map(str, signal))}")
return "".join(map(str, signal))
1 change: 1 addition & 0 deletions 2019/Day16/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
59727310424796235189476878806940387435291429226818921130171187957262146115559932358924341808253400617220924411865224341744614706346865536561788244183609411225788501102400269978290670307147139438239865673058478091682748114942700860895620690690625512670966265975462089087644554004423208369517716075591723905075838513598360188150158989179151879406086757964381549720210763972463291801513250953430219653258827586382953297392567981587028568433943223260723561880121205475323894070000380258122357270847092900809245133752093782889315244091880516672127950518799757198383131025701009960944008679555864631340867924665650332161673274408001712152664733237178121872
1 change: 1 addition & 0 deletions 2019/Day16/input_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12345678
1 change: 1 addition & 0 deletions 2019/Day16/input_test_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
80871224585914546619083218645595
1 change: 1 addition & 0 deletions 2019/Day16/input_test_3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
69317163492948606335995924319873
1 change: 1 addition & 0 deletions 2019/Day16/input_test_4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
03036732577212944063491565474664
16 changes: 16 additions & 0 deletions 2019/Day16/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os
import sys

sys.path.append(".")

from solutions import part1, part2

from utils.inputs import get_inputs
from utils.timings import profile_run

if __name__ == "__main__":
input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt"
inputs = get_inputs(input_path)

profile_run("Part 1", lambda: part1(inputs))
profile_run("Part 2", lambda: part2(inputs))
11 changes: 11 additions & 0 deletions 2019/Day16/solutions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from fft import FlawedFrequencyTransmission


def part1(inputs: list[str], times=100) -> int:
fft = FlawedFrequencyTransmission(inputs[0])
return fft.apply(times)


def part2(inputs: list[str], times=100) -> int:
fft = FlawedFrequencyTransmission(inputs[0])
return fft.apply(times, 7)
34 changes: 34 additions & 0 deletions 2019/Day16/test_solutions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os

import pytest
from solutions import part1, part2

from utils.inputs import get_inputs

current_dir = os.path.dirname(os.path.realpath(__file__))

input = get_inputs(f"{current_dir}/input.txt")
input_test = get_inputs(f"{current_dir}/input_test.txt")
input_test_2 = get_inputs(f"{current_dir}/input_test_2.txt")
input_test_3 = get_inputs(f"{current_dir}/input_test_3.txt")
input_test_4 = get_inputs(f"{current_dir}/input_test_4.txt")


@pytest.mark.skip(reason="not implemented")
class TestPart1:
def test_with_test_data(self):
assert part1(input_test, 4) == "01029498"
assert part1(input_test_2, 100) == "24176176"
assert part1(input_test_3, 100) == "52432133"

def test_with_real_data(self):
assert part1(input) == "68317988"


class TestPart2:
def test_with_test_data(self):
assert part2(input_test_4, 100) == "84462026"

@pytest.mark.skip(reason="not implemented")
def test_with_real_data(self):
assert part2(input) == 2

0 comments on commit 21d1bcc

Please sign in to comment.