advent_of_code/2024/day1/main.py
2024-12-01 15:03:28 -05:00

42 lines
780 B
Python

import sys
import os
sys.path.insert(1, os.path.abspath('../../'))
from python_tools.aoc_utils import *
import collections as C
dict2 = C.defaultdict(lambda: 0)
list1 = []
list2 = []
def part_one(input):
sum = 0
for i in input:
x = i.split()
list1.append(x[0])
list2.append(x[1])
dict2[x[1]] += 1
list1.sort()
list2.sort()
for i in range(len(list1)):
sum += abs(int(list1[i])-int(list2[i]))
print("Part_One:", sum)
def part_two(input):
p2_ans = 0
for i in range(len(list1)):
p2_ans += abs(int(list1[i])*int(dict2[list1[i]]))
print("Part_Two:", p2_ans)
def main():
contents = file2list("input.txt")
part_one(contents)
part_two(contents)
if __name__ == "__main__":
main()