1
0
Fork 0

Added first two days for 2024

This commit is contained in:
Nicola Zangrandi 2024-12-03 08:43:55 +01:00 committed by Nicola Zangrandi
parent 438edfdb90
commit 2bbe5fbac8
Signed by: wasp
GPG key ID: 43C1470D890F23ED
4 changed files with 2072 additions and 0 deletions

31
2024/day01.py Normal file
View file

@ -0,0 +1,31 @@
#!/usr/bin/env python3
lines = []
with open("./inputs/day01.txt") as f:
lines = f.readlines()
left_column = []
right_column = []
for line in lines:
l, r = line.strip().split(" ")
left_column.append(int(l))
right_column.append(int(r))
left_column = sorted(left_column)
right_column = sorted(right_column)
diffs = [abs(left_column[x] - right_column[x]) for x in range(0, len(left_column))]
print(sum(diffs))
unique_nums = {}
for num in right_column:
if num in unique_nums.keys():
unique_nums[num] += 1
else:
unique_nums[num] = 1
sim_score = 0
for num in left_column:
if num in unique_nums.keys():
sim_score += num * unique_nums[num]
print(sim_score)

41
2024/day02.py Normal file
View file

@ -0,0 +1,41 @@
#!/usr/bin/env python3
def validate_report(levels):
inc = True
dec = True
for i in range(0, len(levels) - 1):
curr = levels[i]
nxt = levels[i + 1]
if inc and (curr == nxt or curr < nxt or curr > nxt + 3):
inc = False
if dec and (curr == nxt or curr > nxt or curr < nxt - 3):
dec = False
if inc or dec:
return True
return False
def dampened_reports(levels):
if validate_report(levels):
return True
valid_if_dampened = False
for i in range(0, len(levels)):
if validate_report(levels[:i] + levels[i + 1:]):
valid_if_dampened = True
break
return valid_if_dampened
reports = []
with open("./2024/inputs/day02.txt") as f:
reports = f.readlines()
levels = [[int(n) for n in x.strip().split(" ")] for x in reports]
safe_reports = [x for x in levels if validate_report(x)]
print(len(safe_reports))
dampened_reports = [x for x in levels if dampened_reports(x)]
print(len(dampened_reports))

1000
2024/inputs/day01.txt Normal file

File diff suppressed because it is too large Load diff

1000
2024/inputs/day02.txt Normal file

File diff suppressed because it is too large Load diff