day06 solution

This commit is contained in:
RussellChamp committed 2022-12-07 09:35:03 -05:00
1 parent dfe8eaf2ab
commit 90aad38e16
2 files changed
+105

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
# Day 06: Tuning Trouble
import os
def get_path():
return os.path.join(os.path.dirname(__file__), 'data.txt')
def all_same(arr):
if len(arr) is 0:
return False
elif len(arr) is 1:
return True
else:
for value in arr:
if value is not arr[0]:
return False
return True
def all_different(arr):
if len(arr) <= 1:
return True
else:
for idx in range(len(arr)):
for idz in range(idx+1, len(arr)):
# print('%s is %s ? %s ' % (arr[idx], arr[idz], arr[idx][0] is arr[idz][0]))
if arr[idx] is arr[idz]:
return False
return True
def get_unique_straight(size):
total = size
with open(get_path()) as fp:
# technically this has a bug if the first set of n characters match it will not detect it
# but i'm done working on this puzzle...
set = list(fp.read(size))
while True:
char = fp.read(1)
if not char:
print('Read entire input file :(')
break
total += 1
# ok i don't understand the black magic happening here but when you call read(1) it will read a single character from input
# however, if you compare these characters you'll find that equality is borked
# eg comparing `'a' is 'a'` will return False. Why? I don't know! But calling 'a'[0] will return a non-compare borked char instead
set = set[1:] + [char[0]]
# print('#%d: %s' % (total, set))
if total > size -1 and all_different(set):
break
return total
def solve_part1():
total = get_unique_straight(4)
print ('GOAL: Find the the first start-of-packet marker')
print('ANSWER: How many characters need to be processed before the first start-of-packet marker is detected?')
print('Total: %d' % (total))
def solve_part2():
total = get_unique_straight(14)
print('GOAL: Find the start-of-message marker')
print('ANSWER: How many characters need to be processed before the first start-of-message marker is detected?')
print('Total: %d' % (total))
if __name__ == '__main__':
solve_part1()
solve_part2()
+36
View File
@@ -0,0 +1,36 @@
import unittest
import solve
class Day06TestMethods(unittest.TestCase):
def setUp(self):
pass
def test_all_same(self):
test_cases = [
[False, []],
[True, [111]],
[True, [1,1,1,1]],
[False, [1,2,3,4]],
[True, ['a','a','a','a']],
[False, ['z', 'd', 'z', 'z']],
]
for [expected, arr] in test_cases:
self.assertEqual(solve.all_same(arr), expected, 'expected %s to be %s' % (arr, expected))
def test_all_different(self):
test_cases = [
[True, []],
[True, [111]],
[False, [1,2,3,1]],
[False, [1,1,1,1]],
[True, [1,2,3,4]],
[False, ['a','a','a','a']],
[False, ['p', 'q', 'f', 'f']],
[False, ['z', 'd', 'z', 'z']],
[True, ['a', 'z', 'd', 'f']]
]
for [expected, arr] in test_cases:
self.assertEqual(solve.all_different(arr), expected, 'expected %s to be %s' % (arr, expected))
if __name__ == '__main__':
unittest.main()