From 508c8cdc42358ba4bdfdfdb034c288e7cb5d4d9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Tue, 5 Dec 2023 07:22:56 +0100 Subject: [PATCH] 2023 day 5 part 1. --- 2023/day5.py | 93 +++++++++++++++- 2023/inputs/day5.txt | 250 +++++++++++++++++++++++++++++++++++++++++++ 2023/tests/day5.txt | 33 ++++++ 3 files changed, 375 insertions(+), 1 deletion(-) create mode 100644 2023/inputs/day5.txt create mode 100644 2023/tests/day5.txt diff --git a/2023/day5.py b/2023/day5.py index 992bf35..147120b 100644 --- a/2023/day5.py +++ b/2023/day5.py @@ -1,13 +1,104 @@ import sys from collections import defaultdict from dataclasses import dataclass +from typing import Literal, TypeAlias + +MAP_ORDER = [ + "seed", + "soil", + "fertilizer", + "water", + "light", + "temperature", + "humidity", + "location", +] lines = sys.stdin.read().splitlines() +seeds: list[int] = [] +maps: dict[tuple[str, str], list[tuple[int, int, int]]] = {} + +# parsing +index = 2 +while index < len(lines): + l0 = lines[index] + p1, _, p2 = l0.split("-") + p2 = p2.split()[0].strip() + + index += 1 + maps[p1, p2] = [] + while index < len(lines) and lines[index]: + n1, n2, n3 = lines[index].split() + maps[p1, p2].append((int(n1), int(n2), int(n3))) + index += 1 + + index += 1 + + +def find_location(seed: int) -> int: + value = seed + for map1, map2 in zip(MAP_ORDER[:-1], MAP_ORDER[1:]): + for target, start, length in maps[map1, map2]: + if value >= start and value < start + length: + value = target + (value - start) + break + return value + + +def find_range( + values: tuple[int, int], map: list[tuple[int, int, int]] +) -> list[tuple[int, int]]: + r_start, r_length = values + ranges: list[tuple[int, int]] = [] + print(r_start, r_length) + for target, start, length in map: + # start is in the range + if start <= r_start and r_start < start + length: + if r_start + r_length < start + length: + ranges.append( + (target + r_start - start, r_length) + ) + else: + ranges.append( + (target + r_start - start, length - (r_start - start)) + ) + elif start < r_start: + if r_start + r_length < start + length: + ranges.append( + (target + r_start - start, target + r_start - start + r_length) + ) + elif start >= r_start and r_start >= start + + if r_start <= start and start < start + length: + print(start, length, target) + if r_start + r_length < start + length: + ranges.append( + (target + (start - r_start), target + (start - r_start) + length) + ) + else: + ranges.append((target + (start - r_start), target + length)) + + return ranges + + # part 1 -answer_1 = ... +seeds = [int(s) for s in lines[0].split(":")[1].strip().split()] +answer_1 = min(find_location(seed) for seed in seeds) print(f"answer 1 is {answer_1}") # part 2 +parts = lines[0].split(":")[1].strip().split() +seeds_p2 = [(int(s), int(e)) for s, e in zip(parts[::2], parts[1::2])] + +for seed in range(seeds_p2[0][0], seeds_p2[0][0] + seeds_p2[0][1]): + print(seed, find_location(seed)) +print("---") + +seeds_p2 = [seeds_p2[0]] +for map1, map2 in zip(MAP_ORDER[:-1], MAP_ORDER[1:]): + seeds_p2 = [s2 for s1 in seeds_p2 for s2 in find_range(s1, maps[map1, map2])] +print(seeds_p2) + answer_2 = ... print(f"answer 2 is {answer_2}") diff --git a/2023/inputs/day5.txt b/2023/inputs/day5.txt new file mode 100644 index 0000000..1caea9f --- /dev/null +++ b/2023/inputs/day5.txt @@ -0,0 +1,250 @@ +seeds: 3640772818 104094365 1236480411 161072229 376099792 370219099 1590268366 273715765 3224333694 68979978 2070154278 189826014 3855332650 230434913 3033760782 82305885 837883389 177854788 2442602612 571881366 + +seed-to-soil map: +496269031 1203272644 52136246 +548405277 496269031 457095898 +1005501175 953364929 249907715 + +soil-to-fertilizer map: +217408321 2086205436 25053699 +2604208456 1670861921 31003781 +1631572552 0 258383552 +129225554 3768288787 36192668 +2421205388 2905533654 126666762 +242462020 3399542287 357404885 +866152503 3032200416 253960559 +2039921781 2262442546 381283607 +2635212237 2714844607 190689047 +3613008578 1753855801 23976114 +3636984692 1503365158 167496763 +1340671861 2111259135 274956 +1889956104 806620565 149965677 +0 3286160975 113381312 +2987089260 956586242 546778916 +2547872150 3756947172 11341615 +3846919647 3807789063 43277850 +3533868176 258383552 79140402 +165418222 1701865702 51990099 +1120113062 586061766 220558799 +2559213765 2217447855 44994691 +3807789063 4255836712 39130584 +3890197497 3851066913 404769799 +1340946817 1777831915 42087923 +2881175496 2111534091 105913764 +113381312 2643726153 15844242 +1383034740 337523954 248537812 +2825901284 2659570395 55274212 +599866905 1819919838 266285598 + +fertilizer-to-water map: +3950520280 1751042330 139651634 +936578795 3912173308 42397072 +3553681000 1722281506 28760824 +697953317 651809140 90189394 +3582441824 876081661 368078456 +978975867 2358439651 252255693 +1495879532 2678320518 199775133 +1910380638 3308279888 122339216 +3355092099 1561901004 91630618 +3187667509 2033753243 70292073 +2752202873 3816184128 41568037 +648600286 479585511 49353031 +3149600631 2356473769 1965882 +1695654665 2629130810 49189708 +201901143 385568770 94016741 +536376004 741998534 3182157 +2472303091 3954570380 279899782 +539558161 257732262 15304877 +3131165165 2610695344 18435466 +3446722717 1890693964 106958283 +2793770910 1653531622 40823934 +9849113 65680232 192052030 +2878736712 2104045316 252428453 +1792836692 2878095651 117543946 +2172585320 1244160117 273599019 +2060645804 3884012463 28160845 +3257959582 3719051611 97132517 +408449515 745180691 127926489 +876081661 4234470162 60497134 +1231231560 3043631916 264647972 +3151566513 1997652247 36100996 +2446184339 3430619104 26118752 +803816626 582518586 69290554 +554863038 9849113 40157204 +2146325022 3857752165 26260298 +788142711 50006317 15673915 +295917884 273037139 112531631 +2834594844 1517759136 44141868 +4090171914 3456737856 204795382 +2088806649 3661533238 57518373 +1744844373 2995639597 47992319 +2032719854 1694355556 27925950 +595020242 528938542 53580044 + +water-to-light map: +556810106 840812947 14926117 +2598413684 2184905392 114045192 +2130064037 1600958027 248227533 +1271028210 1253957270 39538107 +3521286912 4262821917 32145379 +1930562940 1944404618 64932992 +4252373354 3302720391 42593942 +1109026743 279496091 162001467 +725777554 2565853410 3969864 +2712458876 1330352326 185715465 +4039742261 3173278185 129442206 +0 2064417497 120487895 +299311037 855739064 257499069 +729747418 1849185560 95219058 +1373001379 767226476 66231296 +571736223 688212171 79014305 +1878676528 2890412515 51886412 +2898174341 682278717 5933454 +1781236499 441497558 97440029 +3090089298 3673052565 50139248 +1365646204 833457772 7355175 +1738475707 2412172480 42760792 +1995495932 2569823274 134568105 +3193548680 3476912261 196140304 +824966476 538937587 143341130 +3553432291 3723191813 486309970 +3140228546 4209501783 53320134 +968307606 1113238133 140719137 +4169184467 3090089298 83188887 +650750528 204469065 75027026 +1439232675 2704391379 186021136 +2511835025 1516067791 84890236 +1625253811 2298950584 113221896 +3389688984 3345314333 131597928 +2378291570 0 22623317 +2596725261 2942298927 1688423 +1310566317 2009337610 55079887 +2940964744 201446459 3022606 +120487895 22623317 178823142 +2904107795 1293495377 36856949 +2400914887 2454933272 110920138 + +light-to-temperature map: +1244459013 624435822 80444775 +2608592263 3309263777 172991510 +3165402867 2278806547 335097905 +292819381 1643978777 105413752 +704475267 462426854 15399493 +3696584161 2678497330 345840247 +2891254573 3613542439 34162874 +1894523870 281665589 180761265 +1706897891 1456352798 187625979 +398233133 1877125477 198159658 +4176101046 3482255287 104541624 +2278806547 3662029939 329785716 +3500500772 4098883907 196083389 +4280642670 3647705313 14324626 +4149492660 3024337577 11203522 +85784517 0 207034864 +2925417447 3231118601 78145176 +0 1749392529 85784517 +4160696182 3215713737 15404864 +4042424408 3991815655 107068252 +1129076520 207034864 74630725 +596392791 1348270322 108082476 +3100809989 2613904452 64592878 +1326100451 1114082357 234187965 +2864509045 3586796911 26745528 +2781583773 3132788465 82925272 +1560288416 477826347 146609475 +3003562623 3035541099 97247366 +719874760 704880597 409201760 +1324903788 1875928814 1196663 +1203707245 1835177046 40751768 + +temperature-to-humidity map: +2622049454 736812858 79169969 +3979548277 2854489162 116161222 +0 2175018874 84480806 +567798788 2400631546 48501534 +3627076350 2837901836 16587326 +1459224370 1289368272 21397154 +3018037189 3252694507 93925363 +4095709499 4063232797 21404553 +1675574530 2560175285 695067 +650203851 328659590 327259736 +3143763337 3475937023 49576 +1591893253 2091337597 83681277 +3655324442 3516768137 39649206 +1125784995 2259499680 94582805 +1082589333 280432563 43195662 +1676269597 1011614859 3965286 +3955996260 4039680780 23552017 +2701219423 684725863 52086995 +977463587 2387091631 13539915 +3643663676 3346619870 11660766 +3143812913 3556417343 483263437 +2907111949 3358280636 110925240 +2293944099 2615629615 132579811 +991003502 920029028 91585831 +1513630670 1015580145 78262583 +196718861 1310765426 316320664 +1680234883 1638774240 331394507 +2011629390 815982827 1882146 +2874635247 4084637350 32476702 +3701704795 2998403042 254291465 +2426523910 1093842728 195525544 +2837901836 3475986599 8980753 +3694973648 3469205876 6731147 +3111962552 3484967352 31800785 +616300322 662956301 21769562 +1220367800 1627086090 10492300 +1480621524 2354082485 33009146 +2846882589 2970650384 27752658 +1352028950 323628225 5031365 +2753306418 655919326 7036975 +513039525 2560870352 54759263 +84480806 1637578390 1195850 +2013511536 0 280432563 +85676656 2449133080 111042205 +1357060315 817864973 102164055 +638069884 2748209426 12133967 +1230860100 1970168747 121168850 + +humidity-to-location map: +3071447765 3790677895 35519893 +501148922 1470714761 60946444 +949413779 3960084356 1114317 +2276139972 547813284 42132370 +261623667 0 220957931 +0 220957931 207965683 +2629055810 2988733812 367963097 +936813255 2507216386 12600524 +3106967658 2402339659 33240399 +909841910 3356696909 11084951 +1030029700 920191219 341387512 +2503236334 3572469232 125819476 +1371417212 1666455982 273052538 +2997018907 3367781860 25674024 +3311094548 2519816910 468916902 +2021404744 1531661205 83389724 +3140208057 1939508520 59879232 +259387264 428923614 2236403 +950528096 1334878052 79501604 +2446901229 1414379656 56335105 +1644469750 3393455884 140294896 +3022692931 3698288708 48754834 +4042169428 1999387752 252797868 +1805974329 3981490698 215430415 +1784764646 501148922 21209683 +889549885 3961198673 20292025 +3780011450 522358605 10399946 +2104794468 1261578731 73299321 +3790411396 3842084182 118000174 +2318272342 2252185620 113574154 +4003450976 3533750780 38718452 +2178093789 4196921113 98046183 +3908411570 3747043542 43634353 +562095366 2435580058 71636328 +2431846496 532758551 15054733 +920926861 3826197788 15886394 +3952045923 1615050929 51405053 +207965683 431160017 51421581 +633731694 2365759774 36579885 +670311579 700952913 219238306 +3200087289 589945654 111007259 diff --git a/2023/tests/day5.txt b/2023/tests/day5.txt new file mode 100644 index 0000000..f756727 --- /dev/null +++ b/2023/tests/day5.txt @@ -0,0 +1,33 @@ +seeds: 79 14 55 13 + +seed-to-soil map: +50 98 2 +52 50 48 + +soil-to-fertilizer map: +0 15 37 +37 52 2 +39 0 15 + +fertilizer-to-water map: +49 53 8 +0 11 42 +42 0 7 +57 7 4 + +water-to-light map: +88 18 7 +18 25 70 + +light-to-temperature map: +45 77 23 +81 45 19 +68 64 13 + +temperature-to-humidity map: +0 69 1 +1 0 69 + +humidity-to-location map: +60 56 37 +56 93 4