Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions day01/binTree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
class BinTree:
def __init__(self):
self.data = []

def _print_entries(self):
for date in self.data:
print(f"{date}")

def add(self, number):
if len(self.data) == 0:
self.data.insert(0, number)
return
if len(self.data) == 1:
if self.data[0] > number:
self.data.insert(0, number)
elif self.data[0] < number:
self.data.append(number)
return

#at least two elements are in the array
ret_val = self._binary_search_(number, len(self.data) - 1, 0)
if ret_val[0] == True:
return #entry is already in set
elif ret_val[0] == False:
self.data.insert(ret_val[1], number)


def __contains__(self, number):
if len(self.data) > 1:
return self._binary_search_(number, len(self.data) - 1, 0)[0]
elif len(self.data) == 1:
return self.data[0] == number
else:
return False

def _binary_search_(self, search_value, upper_bound, lower_bound):
"""returns a tuple for the searched object. First entry is if the object is in the set,
second entry is the point where it was found or where it should be inserted"""
#cornercase for adjacent objects
if lower_bound == upper_bound - 1:
#is in list
if self.data[lower_bound] == search_value:
return True, lower_bound
elif self.data[upper_bound] == search_value:
return True, upper_bound
else: #not found, search where it should be inserted
if search_value > self.data[upper_bound]:
return False, upper_bound + 1
elif search_value < self.data[lower_bound]:
return False, lower_bound - 1
else:
return False, upper_bound

#general binary search function
middle = (int)((upper_bound + lower_bound) / 2)
middle_data = self.data[middle]
if middle_data == search_value:
return True, middle
elif middle_data < search_value:
return self._binary_search_(search_value=search_value, upper_bound=upper_bound, lower_bound=middle)
elif middle_data > search_value:
return self._binary_search_(search_value=search_value, upper_bound=middle, lower_bound=lower_bound)


if __name__ == "__main__":

bt = BinTree(print_debug=True)

for i in range(1,35,3):
bt.add(i)

bt.add(12)
bt.add(13)
bt.add(14)

bt._print_entries()

print(f"31? should be true: {bt.__contains__(31)}")
print(f"30? should be false: {bt.__contains__(30)}")








57 changes: 51 additions & 6 deletions day01/day01.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@

from typing import List;
import os;
import time;
import matplotlib.pyplot as plt
import numpy as np
from binTree import BinTree


def read_input(file_name):
current_path = os.path.dirname(__file__)
Expand All @@ -17,12 +20,54 @@ def read_input(file_name):

class Frequency:
def __init__(self):
self.currentFrequency = 0
self.currentFrequency_1 = 0
self.currentFrequency_2 = 0
self.uniqueFrequencies = list()

def calibrate(self, num: int):
pass
def calibrate_task1(self, num):
self.currentFrequency_1 += num

def calibrate_task2(self, num):
self.currentFrequency_2 += num
if self.currentFrequency_2 in self.uniqueFrequencies:
return True
self.uniqueFrequencies.append(self.currentFrequency_2)
return None

# put your inputs file next to this file!
lines = read_input('input.txt');
# solve the problem here!
lines = read_input('input1.txt')
# solve the problem here!

f = Frequency()

for line in lines:
f.calibrate_task1(line)
print(f"Task 1: frq {f.currentFrequency_1}")


retVal = None
times = []
start_time = time.time()
last_time = None

while retVal is None:
for line in lines:
retVal = f.calibrate_task2(line)
if retVal is True:
break
if last_time is not None:
times.append(last_time - start_time)
start_time = last_time
last_time = time.time()
else:
last_time = time.time()

x = range(len(times))
plt.plot(x, times)
plt.title('Runtime complexity')
plt.xlabel('X axis')
plt.ylabel('Elapsed time')

plt.savefig("plot1.png")

#print(f"Task 2: Current is {f.currentFrequency_2}, uniqueFrqs: {len(f.uniqueFrequencies)}")
Loading