Advent of Code 2022 day 6
More test code than problem-solving code today. Nothing wrong with that.
I’ll come back to day 5 - coming up with a nice way of parsing the input is taking me a while.
from unittest import TestCase
from utils import get_raw_input_as_str
def distinct_run_index(input, marker_size):
for i in range(len(input) - marker_size - 1):
if len(set(input[i : i + marker_size])) == marker_size:
return i + marker_size
class TestExamples(TestCase):
def setUp(self):
self.example_one_input = "mjqjpqmgbljsphdztnvjfqwrcgsmlb"
self.example_two_input = "bvwbjplbgvbhsrlpgdmjqwftvncz"
self.example_three_input = "nppdvjthqldpwncqszvftbrmjlhg"
self.example_four_input = "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg"
self.example_five_input = "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"
def test_part_one_example_one(self):
self.assertEqual(distinct_run_index(self.example_one_input, 4), 7)
def test_part_one_example_two(self):
self.assertEqual(distinct_run_index(self.example_two_input, 4), 5)
def test_part_one_example_three(self):
self.assertEqual(distinct_run_index(self.example_three_input, 4), 6)
def test_part_one_example_four(self):
self.assertEqual(distinct_run_index(self.example_four_input, 4), 10)
def test_part_one_example_five(self):
self.assertEqual(distinct_run_index(self.example_five_input, 4), 11)
def test_part_two_example_one(self):
self.assertEqual(distinct_run_index(self.example_one_input, 14), 19)
def test_part_two_example_two(self):
self.assertEqual(distinct_run_index(self.example_two_input, 14), 23)
def test_part_two_example_three(self):
self.assertEqual(distinct_run_index(self.example_three_input, 14), 23)
def test_part_two_example_four(self):
self.assertEqual(distinct_run_index(self.example_four_input, 14), 29)
def test_part_two_example_five(self):
self.assertEqual(distinct_run_index(self.example_five_input, 14), 26)
if __name__ == "__main__":
input = get_raw_input_as_str("day_06_input.txt")
print(f"Part one: {distinct_run_index(input, 4)}")
print(f"Part two: {distinct_run_index(input, 14)}")