diff options
| author | 2023-12-05 10:06:58 +0100 | |
|---|---|---|
| committer | 2023-12-05 10:07:18 +0100 | |
| commit | 3b8a0f9990cbd10170fb563490f28a6bd84eea1b (patch) | |
| tree | 19278220c81199ea69b201b6d84918ddca96655d /day5 | |
| parent | e688c2b674fc7ad6a964a48df379e5abd01843a7 (diff) | |
| download | 2023-3b8a0f9990cbd10170fb563490f28a6bd84eea1b.tar.gz 2023-3b8a0f9990cbd10170fb563490f28a6bd84eea1b.tar.bz2 2023-3b8a0f9990cbd10170fb563490f28a6bd84eea1b.zip | |
Day5 part 1main
Diffstat (limited to 'day5')
| -rw-r--r-- | day5/__init__.py | 128 | ||||
| -rw-r--r-- | day5/example.txt | 33 | ||||
| -rw-r--r-- | day5/input.txt | 205 |
3 files changed, 366 insertions, 0 deletions
diff --git a/day5/__init__.py b/day5/__init__.py new file mode 100644 index 0000000..3189a07 --- /dev/null +++ b/day5/__init__.py | |||
| @@ -0,0 +1,128 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | from abc import ABC | ||
| 3 | from dataclasses import dataclass | ||
| 4 | from multiprocessing import Pool | ||
| 5 | from typing import Iterator | ||
| 6 | |||
| 7 | from aoc import BaseAssignment | ||
| 8 | |||
| 9 | |||
| 10 | @dataclass | ||
| 11 | class InnerMapping: | ||
| 12 | source: int | ||
| 13 | destination: int | ||
| 14 | length: int | ||
| 15 | |||
| 16 | def __getitem__(self, item): | ||
| 17 | if self.source <= item < self.source + self.length: | ||
| 18 | return self.destination + item - self.source | ||
| 19 | raise KeyError() | ||
| 20 | |||
| 21 | |||
| 22 | class Mapping: | ||
| 23 | def __init__(self): | ||
| 24 | self.mappings = [] | ||
| 25 | |||
| 26 | def __getitem__(self, item): | ||
| 27 | for mapping in self.mappings: | ||
| 28 | try: | ||
| 29 | return mapping[item] | ||
| 30 | except KeyError: | ||
| 31 | continue | ||
| 32 | |||
| 33 | return item | ||
| 34 | |||
| 35 | def update(self, source: int, destination: int, length: int): | ||
| 36 | self.mappings.append(InnerMapping(source, destination, length)) | ||
| 37 | |||
| 38 | |||
| 39 | I = tuple[list[int], list[Mapping]] | ||
| 40 | T = int | ||
| 41 | |||
| 42 | |||
| 43 | class Assignment(BaseAssignment, ABC): | ||
| 44 | @staticmethod | ||
| 45 | def map_seed_position(seed: int, mappings: list[Mapping]) -> int: | ||
| 46 | value = seed | ||
| 47 | for item in mappings: | ||
| 48 | value = item[value] | ||
| 49 | |||
| 50 | return value | ||
| 51 | |||
| 52 | def read_input(self, example=False) -> I: | ||
| 53 | seeds, *almanac = super().read_input(example) | ||
| 54 | _, seeds = seeds.split(": ") | ||
| 55 | seeds = [int(i) for i in seeds.split(" ")] | ||
| 56 | |||
| 57 | maps: list[Mapping] = [] | ||
| 58 | map = None | ||
| 59 | |||
| 60 | index = 0 | ||
| 61 | while True: | ||
| 62 | try: | ||
| 63 | line = almanac[index] | ||
| 64 | except IndexError: | ||
| 65 | break | ||
| 66 | |||
| 67 | if line == "": | ||
| 68 | index += 2 | ||
| 69 | |||
| 70 | if map is not None: | ||
| 71 | maps.append(map) | ||
| 72 | map = None | ||
| 73 | |||
| 74 | continue | ||
| 75 | else: | ||
| 76 | index += 1 | ||
| 77 | |||
| 78 | if map is None: | ||
| 79 | map = Mapping() | ||
| 80 | |||
| 81 | destination, source, length = line.split(" ") | ||
| 82 | map.update(int(source), int(destination), int(length)) | ||
| 83 | |||
| 84 | return seeds, [*maps, map] | ||
| 85 | |||
| 86 | |||
| 87 | class AssignmentOne(Assignment): | ||
| 88 | example_result = 35 | ||
| 89 | |||
| 90 | def run(self, input: I) -> T: | ||
| 91 | seeds, almanac = input | ||
| 92 | |||
| 93 | values = [] | ||
| 94 | for seed in seeds: | ||
| 95 | values.append(self.map_seed_position(int(seed), almanac)) | ||
| 96 | |||
| 97 | return min(values) | ||
| 98 | |||
| 99 | |||
| 100 | def run_per_seed_range(index: int, seeds: list[int], almanac: list[Mapping]): | ||
| 101 | seed, length = int(seeds[index]), int(seeds[index + 1]) | ||
| 102 | |||
| 103 | value = None | ||
| 104 | for seed in range(seed, seed + length): | ||
| 105 | location = Assignment.map_seed_position(seed, almanac) | ||
| 106 | if value is None or value > location: | ||
| 107 | value = location | ||
| 108 | |||
| 109 | return value | ||
| 110 | |||
| 111 | |||
| 112 | class AssignmentTwo(Assignment): | ||
| 113 | example_result = 46 | ||
| 114 | |||
| 115 | def run(self, input: I) -> T: | ||
| 116 | seeds, almanac = input | ||
| 117 | |||
| 118 | value = None | ||
| 119 | |||
| 120 | with Pool(processes=len(seeds) // 2) as pool: | ||
| 121 | for v in pool.imap_unordered( | ||
| 122 | lambda index: run_per_seed_range(index, seeds, almanac), | ||
| 123 | range(0, len(seeds), 2), | ||
| 124 | ): | ||
| 125 | if value is None or value > v: | ||
| 126 | value = v | ||
| 127 | |||
| 128 | return value | ||
diff --git a/day5/example.txt b/day5/example.txt new file mode 100644 index 0000000..f756727 --- /dev/null +++ b/day5/example.txt | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | seeds: 79 14 55 13 | ||
| 2 | |||
| 3 | seed-to-soil map: | ||
| 4 | 50 98 2 | ||
| 5 | 52 50 48 | ||
| 6 | |||
| 7 | soil-to-fertilizer map: | ||
| 8 | 0 15 37 | ||
| 9 | 37 52 2 | ||
| 10 | 39 0 15 | ||
| 11 | |||
| 12 | fertilizer-to-water map: | ||
| 13 | 49 53 8 | ||
| 14 | 0 11 42 | ||
| 15 | 42 0 7 | ||
| 16 | 57 7 4 | ||
| 17 | |||
| 18 | water-to-light map: | ||
| 19 | 88 18 7 | ||
| 20 | 18 25 70 | ||
| 21 | |||
| 22 | light-to-temperature map: | ||
| 23 | 45 77 23 | ||
| 24 | 81 45 19 | ||
| 25 | 68 64 13 | ||
| 26 | |||
| 27 | temperature-to-humidity map: | ||
| 28 | 0 69 1 | ||
| 29 | 1 0 69 | ||
| 30 | |||
| 31 | humidity-to-location map: | ||
| 32 | 60 56 37 | ||
| 33 | 56 93 4 | ||
diff --git a/day5/input.txt b/day5/input.txt new file mode 100644 index 0000000..78aebd2 --- /dev/null +++ b/day5/input.txt | |||
| @@ -0,0 +1,205 @@ | |||
| 1 | seeds: 304740406 53203352 1080760686 52608146 1670978447 367043978 1445830299 58442414 4012995194 104364808 4123691336 167638723 2284615844 178205532 3164519436 564398605 90744016 147784453 577905361 122056749 | ||
| 2 | |||
| 3 | seed-to-soil map: | ||
| 4 | 0 699677807 922644641 | ||
| 5 | 4174180469 3833727510 120786827 | ||
| 6 | 1525682201 2566557266 229511566 | ||
| 7 | 3280624601 3954514337 340452959 | ||
| 8 | 2228029508 2796068832 310221139 | ||
| 9 | 3621077560 3280624601 553102909 | ||
| 10 | 2120836342 592484641 107193166 | ||
| 11 | 1982514669 227320902 138321673 | ||
| 12 | 1755193767 0 227320902 | ||
| 13 | 922644641 1622322448 603037560 | ||
| 14 | 2538250647 365642575 226842066 | ||
| 15 | 2765092713 2225360008 341197258 | ||
| 16 | |||
| 17 | soil-to-fertilizer map: | ||
| 18 | 1916776044 145070025 3464138 | ||
| 19 | 1920240182 0 145070025 | ||
| 20 | 706160141 2208005933 115191764 | ||
| 21 | 2898492924 830275742 87027483 | ||
| 22 | 3489083348 3344594558 103871907 | ||
| 23 | 2985520407 148534163 415139950 | ||
| 24 | 821351905 917303225 327392865 | ||
| 25 | 1148744770 1517236949 182706102 | ||
| 26 | 295069722 3448466465 411090419 | ||
| 27 | 1816984891 3244803405 99791153 | ||
| 28 | 4282585972 4292886644 2080652 | ||
| 29 | 3592955255 563674113 266601629 | ||
| 30 | 4266462972 4158154511 16123000 | ||
| 31 | 1331450872 1244696090 272540859 | ||
| 32 | 2715943131 3062253612 182549793 | ||
| 33 | 4284666624 4174277511 10300672 | ||
| 34 | 4158154511 4184578183 108308461 | ||
| 35 | 1603991731 1995012773 212993160 | ||
| 36 | 2065310207 2411620688 650632924 | ||
| 37 | 0 1699943051 295069722 | ||
| 38 | 3400660357 2323197697 88422991 | ||
| 39 | |||
| 40 | fertilizer-to-water map: | ||
| 41 | 3585244197 3493316345 482900943 | ||
| 42 | 2871272496 878061687 456215665 | ||
| 43 | 3477664135 4187387234 107580062 | ||
| 44 | 845559238 15587711 56716031 | ||
| 45 | 121711204 2918313406 409174755 | ||
| 46 | 1639718746 0 15587711 | ||
| 47 | 530885959 2603640127 314673279 | ||
| 48 | 902275269 2435903232 167736895 | ||
| 49 | 2635221133 72303742 236051363 | ||
| 50 | 1070012164 308355105 569706582 | ||
| 51 | 1699846244 1334277352 935374889 | ||
| 52 | 4279315086 3477664135 15652210 | ||
| 53 | 1655306457 2269652241 44539787 | ||
| 54 | 109056711 2423248739 12654493 | ||
| 55 | 0 2314192028 109056711 | ||
| 56 | 4068145140 3976217288 211169946 | ||
| 57 | |||
| 58 | water-to-light map: | ||
| 59 | 3841742547 3016842841 17384315 | ||
| 60 | 2875021919 2637593760 185450069 | ||
| 61 | 3413635232 3588265685 87508205 | ||
| 62 | 1311241677 236307150 54007684 | ||
| 63 | 3349161906 4276682782 18284514 | ||
| 64 | 896790030 1355845673 34430118 | ||
| 65 | 3060471988 3835573209 145836645 | ||
| 66 | 2741184131 3675773890 133837788 | ||
| 67 | 1387754847 947687177 15489861 | ||
| 68 | 3785944618 2057196631 55797929 | ||
| 69 | 2006585491 2931426646 85416195 | ||
| 70 | 3873217816 3809611678 25961531 | ||
| 71 | 1667765627 643929130 34884144 | ||
| 72 | 2092001686 2434956599 202637161 | ||
| 73 | 1001898651 158618769 77688381 | ||
| 74 | 3899179347 2253048950 181907649 | ||
| 75 | 1786416461 377140410 101956748 | ||
| 76 | 0 833901414 113785763 | ||
| 77 | 1403244708 479097158 5 | ||
