Created
December 3, 2024 17:33
-
-
Save matthewbauer/3709607d6d6205632f6e541b3bc5e512 to your computer and use it in GitHub Desktop.
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
import collections | |
# import http.client | |
# | |
# conn = http.client.HTTPSConnection('adventofcode.com') | |
# conn.request('GET', '/2024/day/1/input') | |
# response = conn.getresponse() | |
# | |
# print(response.readlines()) | |
file = open("day1.csv", "r") | |
list1 = [] | |
list2 = [] | |
for line in file.read().splitlines(): | |
[s1, s2] = line.split(" ") | |
[n1, n2] = [int(s1), int(s2)] | |
list1.append(n1) | |
list2.append(n2) | |
list1.sort() | |
list2.sort() | |
distance = 0 | |
for i in range(0, len(list1)): | |
distance += abs(list1[i] - list2[i]) | |
print(distance) | |
list2_count = collections.defaultdict(int) | |
for n in list2: | |
if n not in list2_count: | |
list2_count[n] = 0 | |
list2_count[n] += 1 | |
similarity = 0 | |
for n in list1: | |
similarity += n * list2_count[n] | |
print(similarity) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment