day 1
This commit is contained in:
parent
fc5dbe592d
commit
48a0e08f9c
2
2024/.mise.toml
Normal file
2
2024/.mise.toml
Normal file
@ -0,0 +1,2 @@
|
||||
[tools]
|
||||
python = "3.11"
|
1000
2024/full/day01.txt
Normal file
1000
2024/full/day01.txt
Normal file
File diff suppressed because it is too large
Load Diff
54
2024/python/day01.py
Normal file
54
2024/python/day01.py
Normal file
@ -0,0 +1,54 @@
|
||||
import shared
|
||||
import itertools
|
||||
import functools
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
# @shared.profile
|
||||
def part1(rows):
|
||||
left = []
|
||||
right = []
|
||||
for row in rows:
|
||||
r = row.split()
|
||||
left.append(int(r[0]))
|
||||
right.append(int(r[1]))
|
||||
left = list(sorted(left))
|
||||
right = list(sorted(right))
|
||||
pairs = list(zip(left,right))
|
||||
diffs = []
|
||||
for l, r in pairs:
|
||||
diffs.append(abs(r - l))
|
||||
print(sum(diffs))
|
||||
|
||||
|
||||
# @shared.profile
|
||||
def part2(rows):
|
||||
left = []
|
||||
right = []
|
||||
for row in rows:
|
||||
r = row.split()
|
||||
left.append(int(r[0]))
|
||||
right.append(int(r[1]))
|
||||
right_hist = defaultdict(int)
|
||||
for r in right:
|
||||
right_hist[r] += 1
|
||||
tot = 0
|
||||
for l in left:
|
||||
tot += l * right_hist[l]
|
||||
print(tot)
|
||||
|
||||
|
||||
def main():
|
||||
rows = [row for row in shared.load_rows(1)]
|
||||
with shared.elapsed_timer() as elapsed:
|
||||
part1(rows)
|
||||
print("🕒", elapsed())
|
||||
|
||||
rows = [row for row in shared.load_rows(1, True)]
|
||||
with shared.elapsed_timer() as elapsed:
|
||||
part2(rows)
|
||||
print("🕒", elapsed())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
6
2024/samples/day01.txt
Normal file
6
2024/samples/day01.txt
Normal file
@ -0,0 +1,6 @@
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
Loading…
Reference in New Issue
Block a user