solve day 1, no time for pretty code
This commit is contained in:
parent
aa505e5bca
commit
40bc8ca23e
4 changed files with 1057 additions and 0 deletions
1000
day_01/input.txt
Normal file
1000
day_01/input.txt
Normal file
File diff suppressed because it is too large
Load diff
38
day_01/program.py
Executable file
38
day_01/program.py
Executable file
|
|
@ -0,0 +1,38 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
# https://adventofcode.com/2024/day/1
|
||||||
|
|
||||||
|
def get_lines(filename: str) -> list:
|
||||||
|
with open(filename, "r") as file:
|
||||||
|
return [line.strip() for line in file.readlines()]
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# lines = get_lines("sample-input.txt")
|
||||||
|
lines = get_lines("input.txt")
|
||||||
|
list_a = []
|
||||||
|
list_b = []
|
||||||
|
for line in lines:
|
||||||
|
(a, b) = line.split(" ")
|
||||||
|
list_a.append(int(a))
|
||||||
|
list_b.append(int(b))
|
||||||
|
sorted_a = sorted(list_a)
|
||||||
|
sorted_b = sorted(list_b)
|
||||||
|
# print(sorted_a)
|
||||||
|
# print(sorted_b)
|
||||||
|
|
||||||
|
total_distance = 0
|
||||||
|
for i in range(len(sorted_a)):
|
||||||
|
total_distance += abs(sorted_b[i] - sorted_a[i])
|
||||||
|
print("Part 1: Total distance is", total_distance)
|
||||||
|
|
||||||
|
similarity_score = 0
|
||||||
|
for i in range(len(list_a)):
|
||||||
|
search_val = list_a[i]
|
||||||
|
appearances = [j for j, x in enumerate(list_b) if x == search_val]
|
||||||
|
similarity_score += search_val * len(appearances)
|
||||||
|
print("Part 2: Similarity score is", similarity_score)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
6
day_01/sample-input.txt
Normal file
6
day_01/sample-input.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
3 4
|
||||||
|
4 3
|
||||||
|
2 5
|
||||||
|
1 3
|
||||||
|
3 9
|
||||||
|
3 3
|
||||||
13
day_01/test_program.py
Executable file
13
day_01/test_program.py
Executable file
|
|
@ -0,0 +1,13 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import program
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
|
class TestThing(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue