Initial commit (day 1 and day 2)

This commit is contained in:
Isaac Shoebottom 2024-12-02 18:34:33 -04:00
commit 4af7ced4b5
5 changed files with 2138 additions and 0 deletions

1000
2024/01/input.txt Normal file

File diff suppressed because it is too large Load Diff

35
2024/01/solution.py Normal file
View File

@ -0,0 +1,35 @@
list1 = []
list2 = []
file = open("input.txt", "r").read()
lines = file.split("\n")
for line in lines:
nums = line.split(" ")
num1 = int(nums[0])
num2 = int(nums[1])
list1.append(num1)
list2.append(num2)
list1.sort()
list2.sort()
# for i in range(len(list1)):
# print(list1[i], list2[i])
total_dist = 0
for i in range(len(list1)):
dist = list2[i] - list1[i]
dist = abs(dist)
total_dist += dist
print("Total distance: ", total_dist)
total_similarity = 0
for i in range(len(list1)):
num = list1[i]
occurs = 0
for j in range(len(list2)):
if num == list2[j]:
occurs += 1
similarity = num * occurs
total_similarity += similarity
print("Total similarity: ", total_similarity)

1000
2024/02/input.txt Normal file

File diff suppressed because it is too large Load Diff

81
2024/02/solution.py Normal file
View File

@ -0,0 +1,81 @@
file = open("input.txt", "r").read()
lines = file.split("\n")
reports = []
for line in lines:
nums = line.split(" ")
nums = [int(num) for num in nums]
reports.append(nums)
total_safe = 0
safe_nums = [1, 2, 3]
for report in reports:
dists = [report[i+1] - report[i] for i in range(len(report)) if i != len(report) - 1]
# dists is either all positive or all negative (checks all increasing or all decreasing)
if all([dist > 0 for dist in dists]) or all([dist < 0 for dist in dists]):
dists = [abs(dist) for dist in dists]
if all([dist in safe_nums for dist in dists]):
total_safe += 1
print("Total safe reports: ", total_safe)
total_safe_tolerance = 0
for report in reports:
print("Checking report: ", report)
dists = [report[i+1] - report[i] for i in range(len(report)) if i != len(report) - 1]
for i in range(len(dists)):
if abs(dists[i]) not in safe_nums:
report_copy = report.copy()
report_copy.pop(i+1)
dists_copy = [report_copy[i+1] - report_copy[i] for i in range(len(report_copy)) if i != len(report_copy) - 1]
if all([dist > 0 for dist in dists_copy]) or all([dist < 0 for dist in dists_copy]):
dists_copy = [abs(dist) for dist in dists_copy]
if all([dist in safe_nums for dist in dists_copy]):
print("Popping (dist): ", report[i+1])
report.pop(i+1)
else:
print("Popping (dist): ", report[i])
report.pop(i)
break
else:
# make a list comprehension that makes the list a 0 if its negative and a 1 if its positive and then if the sum is 1 or len(dists) - 1 then its safe
lst = [0 if dist < 0 else 1 for dist in dists]
if sum(lst) == 1: # Only one positive
for i in range(len(report)):
if dists[i] > 0:
report_copy = report.copy()
report_copy.pop(i+1)
dists_copy = [report_copy[i+1] - report_copy[i] for i in range(len(report_copy)) if i != len(report_copy) - 1]
if all([dist > 0 for dist in dists_copy]) or all([dist < 0 for dist in dists_copy]):
dists_copy = [abs(dist) for dist in dists_copy]
if all([dist in safe_nums for dist in dists_copy]):
print("Popping (asce): ", report[i+1])
report.pop(i+1)
else:
print("Popping (asce): ", report[i])
report.pop(i)
break
elif sum(lst) == len(dists) - 1: # Only one negative
for i in range(len(report)):
if dists[i] < 0:
report_copy = report.copy()
report_copy.pop(i+1)
dists_copy = [report_copy[i+1] - report_copy[i] for i in range(len(report_copy)) if i != len(report_copy) - 1]
if all([dist > 0 for dist in dists_copy]) or all([dist < 0 for dist in dists_copy]):
dists_copy = [abs(dist) for dist in dists_copy]
if all([dist in safe_nums for dist in dists_copy]):
print("Popping (desc): ", report[i+1])
report.pop(i+1)
else:
print("Popping (desc): ", report[i])
report.pop(i)
break
dists = [report[i+1] - report[i] for i in range(len(report)) if i != len(report) - 1]
# dists is either all positive or all negative (checks all increasing or all decreasing)
if all([dist > 0 for dist in dists]) or all([dist < 0 for dist in dists]):
dists = [abs(dist) for dist in dists]
if all([dist in safe_nums for dist in dists]):
print("Safe report: ", report)
total_safe_tolerance += 1
print("Total safe reports with tolerance: ", total_safe_tolerance)

22
2024/02/test.txt Normal file
View File

@ -0,0 +1,22 @@
48 46 47 49 51 54 56
1 1 2 3 4 5
1 2 3 4 5 5
5 1 2 3 4 5
1 4 3 2 1
1 6 7 8 9
1 2 3 4 3
9 8 7 6 7
7 10 8 10 11
29 28 27 25 26 25 22 20
90 89 86 84 83 79
97 96 93 91 85
29 26 24 25 21
36 37 40 43 47
43 44 47 48 49 54
35 33 31 29 27 25 22 18
77 76 73 70 64
68 65 69 72 74 77 80 83
37 40 42 43 44 47 51
70 73 76 79 86
7 10 8 10 11
29 28 27 25 26 25 22 20