summaryrefslogtreecommitdiffstats
path: root/day5
diff options
context:
space:
mode:
Diffstat (limited to 'day5')
-rw-r--r--day5/__init__.py128
-rw-r--r--day5/example.txt33
-rw-r--r--day5/input.txt205
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 -*-
2from abc import ABC
3from dataclasses import dataclass
4from multiprocessing import Pool
5from typing import Iterator
6
7from aoc import BaseAssignment
8
9
10@dataclass
11class 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
22class 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
39I = tuple[list[int], list[Mapping]]
40T = int
41
42
43class 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
87class 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
100def 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
112class 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 @@
1seeds: 79 14 55 13
2
3seed-to-soil map:
450 98 2
552 50 48
6
7soil-to-fertilizer map:
80 15 37
937 52 2
1039 0 15
11
12fertilizer-to-water map:
1349 53 8
140 11 42
1542 0 7
1657 7 4
17
18water-to-light map:
1988 18 7
2018 25 70
21
22light-to-temperature map:
2345 77 23
2481 45 19
2568 64 13
26
27temperature-to-humidity map:
280 69 1
291 0 69
30
31humidity-to-location map:
3260 56 37
3356 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 @@
1seeds: 304740406 53203352 1080760686 52608146 1670978447 367043978 1445830299 58442414 4012995194 104364808 4123691336 167638723 2284615844 178205532 3164519436 564398605 90744016 147784453 577905361 122056749
2
3seed-to-soil map:
40 699677807 922644641
54174180469 3833727510 120786827
61525682201 2566557266 229511566
73280624601 3954514337 340452959
82228029508 2796068832 310221139
93621077560 3280624601 553102909
102120836342 592484641 107193166
111982514669 227320902 138321673
121755193767 0 227320902
13922644641 1622322448 603037560
142538250647 365642575 226842066
152765092713 2225360008 341197258
16
17soil-to-fertilizer map:
181916776044 145070025 3464138
191920240182 0 145070025
20706160141 2208005933 115191764
212898492924 830275742 87027483
223489083348 3344594558 103871907
232985520407 148534163 415139950
24821351905 917303225 327392865
251148744770 1517236949 182706102
26295069722 3448466465 411090419
271816984891 3244803405 99791153
284282585972 4292886644 2080652
293592955255 563674113 266601629
304266462972 4158154511 16123000
311331450872 1244696090 272540859
322715943131 3062253612 182549793
334284666624 4174277511 10300672
344158154511 4184578183 108308461
351603991731 1995012773 212993160
362065310207 2411620688 650632924
370 1699943051 295069722
383400660357 2323197697 88422991
39
40fertilizer-to-water map:
413585244197 3493316345 482900943
422871272496 878061687 456215665
433477664135 4187387234 107580062
44845559238 15587711 56716031
45121711204 2918313406 409174755
461639718746 0 15587711
47530885959 2603640127 314673279
48902275269 2435903232 167736895
492635221133 72303742 236051363
501070012164 308355105 569706582
511699846244 1334277352 935374889
524279315086 3477664135 15652210
531655306457 2269652241 44539787
54109056711 2423248739 12654493
550 2314192028 109056711
564068145140 3976217288 211169946
57
58water-to-light map:
593841742547 3016842841 17384315
602875021919 2637593760 185450069
613413635232 3588265685 87508205
621311241677 236307150 54007684
633349161906 4276682782 18284514
64896790030 1355845673 34430118
653060471988 3835573209 145836645
662741184131 3675773890 133837788
671387754847 947687177 15489861
683785944618 2057196631 55797929
692006585491 2931426646 85416195
703873217816 3809611678 25961531
711667765627 643929130 34884144
722092001686 2434956599 202637161
731001898651 158618769 77688381
743899179347 2253048950 181907649
751786416461 377140410 101956748
760 833901414 113785763
771403244708 479097158 56815029
783859126862 3034227156 14090954
79747996464 678813274 31450438
80869173795 963177038 27616235
813268502638 2006585491 50611140
82113785763 0 148879571
83262665334 1511505797 386606610
841187603975 710263712 123637702
853319113778 3987361499 30048128
863367446420 2885237834 46188812
87931220148 990793273 15913032
881460059737 1006706305 120880314
891079587032 535912187 108016943