Skip to content

Commit 35abe28

Browse files
committed
Completed day 1 of year 2024
1 parent df18c17 commit 35abe28

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

2024/1.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
def parse(data):
5+
data = data.replace(' ', '\n').split('\n')
6+
list1 = data[::2]
7+
list2 = data[1::2]
8+
9+
return list1, list2
10+
11+
split_data = parse
12+
completed = True
13+
raw_data = None # Not To be touched
14+
15+
def part1(data):
16+
list1, list2 = data
17+
cum = 0
18+
for l1, l2 in zip(sorted(list1), sorted(list2)):
19+
cum += abs(int(l1) - int(l2))
20+
21+
return cum
22+
23+
24+
def part2(data):
25+
list1, list2 = data
26+
27+
hashmap = {} # Quick hashmap to store the frequency for later use
28+
for elem in list2:
29+
hashmap[elem] = hashmap.get(elem, 0) + 1
30+
31+
simScore = 0
32+
for elem in list1:
33+
simScore += int(elem) * hashmap.get(elem, 0)
34+
35+
return simScore

0 commit comments

Comments
 (0)