|
| 1 | +from collections import Counter, defaultdict, deque |
| 2 | + |
| 3 | +import aoc_helper |
| 4 | +from aoc_helper import ( |
| 5 | + Grid, |
| 6 | + PrioQueue, |
| 7 | + SparseGrid, |
| 8 | + decode_text, |
| 9 | + extract_ints, |
| 10 | + extract_iranges, |
| 11 | + extract_ranges, |
| 12 | + extract_uints, |
| 13 | + frange, |
| 14 | + irange, |
| 15 | + iter, |
| 16 | + list, |
| 17 | + map, |
| 18 | + multirange, |
| 19 | + range, |
| 20 | + search, |
| 21 | + tail_call, |
| 22 | +) |
| 23 | + |
| 24 | +raw = aoc_helper.fetch(1, 2024) |
| 25 | + |
| 26 | + |
| 27 | +def parse_raw(raw: str): |
| 28 | + return raw |
| 29 | + |
| 30 | + |
| 31 | +data = parse_raw(raw) |
| 32 | + |
| 33 | + |
| 34 | +# providing this default is somewhat of a hack - there isn't any other way to |
| 35 | +# force type inference to happen, AFAIK - but this won't work with standard |
| 36 | +# collections (list, set, dict, tuple) |
| 37 | +def part_one(data=data): |
| 38 | + list_one = [] |
| 39 | + list_two = [] |
| 40 | + for line in data.splitlines(): |
| 41 | + list_one.append(int(line.split()[0])) |
| 42 | + list_two.append(int(line.split()[1])) |
| 43 | + list_one.sort() |
| 44 | + list_two.sort() |
| 45 | + t = 0 |
| 46 | + for i in range(len(list_one)): |
| 47 | + t += abs(list_two[i]-list_one[i]) |
| 48 | + return t |
| 49 | + |
| 50 | +aoc_helper.lazy_test(day=1, year=2024, parse=parse_raw, solution=part_one) |
| 51 | + |
| 52 | + |
| 53 | +# providing this default is somewhat of a hack - there isn't any other way to |
| 54 | +# force type inference to happen, AFAIK - but this won't work with standard |
| 55 | +# collections (list, set, dict, tuple) |
| 56 | +def part_two(data=data): |
| 57 | + list_one = [] |
| 58 | + list_two = [] |
| 59 | + for line in data.splitlines(): |
| 60 | + list_one.append(int(line.split()[0])) |
| 61 | + list_two.append(int(line.split()[1])) |
| 62 | + t = 0 |
| 63 | + for item in list_one: |
| 64 | + c = list_two.count(item) |
| 65 | + t += item * c |
| 66 | + return t |
| 67 | + |
| 68 | +aoc_helper.lazy_test(day=1, year=2024, parse=parse_raw, solution=part_two) |
| 69 | + |
| 70 | +aoc_helper.lazy_submit(day=1, year=2024, solution=part_one, data=data) |
| 71 | +aoc_helper.lazy_submit(day=1, year=2024, solution=part_two, data=data) |
0 commit comments