-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renaming of folder and file (underscore added)
- Loading branch information
1 parent
f3ab5f5
commit 48433db
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# AoC - Day 1 | ||
# Federico Pevere - 2024 | ||
|
||
# ---- Part One ---- | ||
print('----- Day 1 Part One -----') | ||
|
||
# Open the puzzle input file for reading | ||
with open("input.txt", "r") as file: | ||
# Initialize input_lists, it will contain | ||
# the two lists of location IDs | ||
input_lists = [[],[]] | ||
for line in file: | ||
# Convert each column value to a integer number, | ||
# add it to the corresponding list | ||
for idx, id in enumerate(line.split()): | ||
input_lists[idx].append(int(id)) | ||
|
||
# DEBUG | ||
print('Input lists: ', input_lists) | ||
|
||
# sort the lists | ||
ordered_lists = input_lists | ||
for ordered_list in ordered_lists: | ||
ordered_list.sort() | ||
|
||
# DEBUG | ||
print('Ordered lists: ', ordered_lists) | ||
|
||
# calculate the distance between corresponding elements | ||
# note that zip(*ordered_lists) transposes the list containing | ||
# the two lists | ||
distances = [abs(a-b) for a,b in zip(*ordered_lists)] | ||
|
||
# DEBUG | ||
#print('Distances: ', distances) | ||
|
||
# find the total distance | ||
total_distance = sum(distances) | ||
print('Total distance = ', total_distance) | ||
|
||
# ---- Part Two ---- | ||
print('----- Day 1 Part Two -----') | ||
|
||
import numpy as np | ||
|
||
ordered_lists = np.array(ordered_lists) | ||
similarity_score = 0 | ||
for value in ordered_lists[0]: | ||
condition = ordered_lists[1] == value | ||
# DEBUG | ||
#print('Condition:', condition) | ||
count = np.count_nonzero(np.extract(condition, ordered_lists[1])) | ||
similarity_score += value*count | ||
# DEBUG | ||
#print(f'Value {value} from the 1st list is present {count} times in the 2nd list.') | ||
#print(f'Similarity score increases by {value*count}') | ||
|
||
# DEBUG | ||
print('Similarity score = ', similarity_score) |