This commit is contained in:
Tyrel Souza 2024-12-01 08:46:43 -05:00
parent fc5dbe592d
commit 48a0e08f9c
4 changed files with 1062 additions and 0 deletions

2
2024/.mise.toml Normal file
View File

@ -0,0 +1,2 @@
[tools]
python = "3.11"

1000
2024/full/day01.txt Normal file

File diff suppressed because it is too large Load Diff

54
2024/python/day01.py Normal file
View 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
View File

@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3