From 23a6dcf9d848bfecfbb15f299913de9b4faeb852 Mon Sep 17 00:00:00 2001 From: Emily Hunt Date: Tue, 5 Dec 2023 08:01:29 +0100 Subject: [PATCH] 2023 Day 5 part-solved --- src/routes/2023/day/05/_data/data.txt | 208 ++++++++++++++++++- src/routes/2023/day/05/_data/exampleData.txt | 35 +++- src/routes/2023/day/05/_info.json | 2 +- src/routes/2023/day/05/main.js | 100 ++++++++- 4 files changed, 337 insertions(+), 8 deletions(-) diff --git a/src/routes/2023/day/05/_data/data.txt b/src/routes/2023/day/05/_data/data.txt index ed81d07..f2e77d9 100644 --- a/src/routes/2023/day/05/_data/data.txt +++ b/src/routes/2023/day/05/_data/data.txt @@ -1,2 +1,206 @@ -Hello -World \ No newline at end of file +seeds: 919339981 562444630 3366006921 67827214 1496677366 101156779 4140591657 5858311 2566406753 71724353 2721360939 35899538 383860877 424668759 3649554897 442182562 2846055542 49953829 2988140126 256306471 + +seed-to-soil map: +627617777 1691901751 235673208 +2425244517 2483951770 157286279 +1339042890 1549225044 142676707 +481294110 381503165 89539853 +863290985 1007717708 39103521 +570833963 324719351 56783814 +3953140805 3714151881 155523737 +902394506 1941176275 61481385 +963875891 675869083 331848625 +1922840702 1046821229 502403815 +1481719597 1927574959 13601316 +1820040264 573068645 102800438 +1295724516 2641238049 43318374 +0 2002657660 481294110 +1495320913 0 324719351 +2582530796 471043018 102025627 +3714151881 3869675618 238988924 + +soil-to-fertilizer map: +1288462652 3191328122 309853381 +3216116191 1774097401 151922673 +1739360920 2276789875 44162492 +220941763 1080382821 325206789 +2416141229 3949354107 345613189 +1783523412 765967805 117439234 +2876214366 1010215130 70167691 +2761754418 3142514478 48813644 +3840090062 3541735844 44042641 +2946382057 1417076798 106871618 +3750331405 3501181503 29948437 +1105413398 1405589610 11487188 +2867709608 3585778485 8504758 +3053253675 3594283243 162862516 +15940804 0 90040993 +2192246779 537548320 223894450 +210335859 3531129940 10605904 +546148552 2351877346 559264846 +1598316033 883407039 110119908 +2851021425 993526947 16688183 +0 90040993 15940804 +4134281688 3757145759 160685608 +3718808665 3917831367 31522740 +1116900586 2970952412 171562066 +2810568062 210335859 40453363 +3780279842 2911142192 59810220 +1900962646 761442770 4525035 +1905487681 250789222 286759098 +3368038864 1926020074 350769801 +1708435941 2320952367 30924979 +3884132703 1523948416 250148985 + +fertilizer-to-water map: +2450598719 3993777626 178688420 +990387307 2284751995 36035279 +1479519873 1430606124 25388869 +2928263979 2233553333 51198662 +1825810145 1093816339 30855096 +3131362216 1499369679 38622365 +2629287139 1980229176 243592551 +2979462641 1827354243 146328983 +937194740 3188384434 9817881 +2069360741 1973683226 975358 +1856665241 3781082126 212695500 +642830371 945238436 148577903 +4114154511 1124671435 95554104 +3777484412 483834992 305990165 +3264059981 2427531551 296117669 +272170557 2723649220 370659814 +262438951 2223821727 9731606 +1615429560 1220225539 210380585 +1028349507 226445100 83554339 +4209708615 3198202315 85258681 +3169984581 3094309034 94075400 +4083474577 309999439 30679934 +2872879690 115495593 55384289 +2138362191 340679373 4164957 +947012621 1455994993 43374686 +791408274 1606018136 145786466 +1026422586 1825427322 1926921 +1578531462 789825157 8469921 +3670740135 2320787274 106744277 +2272532251 170879882 55565218 +3125791624 1974658584 5570592 +1111903846 3283460996 367616027 +2070336099 1537992044 68026092 +1504908742 1751804602 73622720 +2142527148 3651077023 130005103 +2328097469 4172466046 122501250 +1587001383 344844330 28428177 +3560177650 373272507 110562485 +115495593 798295078 146943358 + +water-to-light map: +220561404 643114856 123713527 +1087373312 4123526389 171440907 +0 766828383 220561404 +2907464644 3955968868 167557521 +376259478 31984547 611130309 +344274931 0 31984547 +4209967986 1266450159 84999310 +3234177104 1246528251 19921908 +4075482989 2729348487 134484997 +1258814219 1870560966 858787521 +3075022165 1087373312 159154939 +2439602258 3906331676 49637192 +2297013316 3685217461 142588942 +2567764723 1530861045 339699921 +3254099012 2863833484 821383977 +2489239450 3827806403 78525273 +2117601740 1351449469 179411576 + +light-to-temperature map: +83647742 560398800 311449275 +4201716419 3337900402 93250877 +1071565427 3024001249 4392221 +3424420254 3520028211 161472091 +1227070419 2828368402 116428819 +0 2744720660 83647742 +4199359339 3431151279 2357080 +1807547949 2359072262 384093366 +3336003529 3486377667 33650544 +1075957648 871848075 149557739 +3585892345 3681500302 613466994 +955495817 2243002652 116069610 +2191641315 1406250497 836752155 +3369654073 3433508359 52869308 +1343499238 2944797221 79204028 +3422523381 3336003529 1896873 +395097017 0 560398800 +1225515387 2743165628 1555032 +1422703266 1021405814 384844683 + +temperature-to-humidity map: +3340701300 3627322367 174281926 +3514983226 4012096602 282870694 +403969772 2344934648 125307793 +356249525 1299750763 47720247 +529277565 0 19150241 +2425473363 606907612 15105367 +1622667408 1693081570 188915118 +1858084350 1347471010 47053630 +4223873328 2752952198 71093968 +2749048741 3035669808 188955792 +0 2134178305 190659232 +1112200615 96440819 510466793 +4148180263 2749048741 3903457 +1811582526 1113827741 11131074 +548427806 2324837537 20097111 +1905137980 2051821050 82357255 +3116507691 3224625600 224193609 +1822713600 1657710820 35370750 +568524917 622012979 491814762 +2440578730 66777108 29663711 +2250681415 1124958815 174791948 +4009477562 3873393901 138702701 +190659232 1881996688 165590293 +1987495235 1394524640 263186180 +1107966546 2047586981 4234069 +1060339679 19150241 47626867 +4210822392 3801604293 13050936 +2938004533 3448819209 178503158 +3797853920 2824046166 211623642 +4152083720 3814655229 58738672 + +humidity-to-location map: +2102802203 2756269781 87468599 +2877112183 2931341663 22549647 +3494879649 1477788233 182041959 +2436026725 2953891310 81993792 +2899661830 373999642 244663414 +2190270802 850802545 169763790 +3866824018 90439080 13573105 +320139638 1659830192 113214403 +1131501111 1773044595 246249128 +2417984929 828638896 16505804 +3839766923 845144700 5657845 +4161134012 2843738380 87603283 +3731370785 3801964390 108396138 +1003659815 3442085055 127841296 +918426975 3986208955 85232840 +433354041 618663056 209975840 +2811371438 0 65740745 +2518020517 1329719948 148068285 +651651860 3035885102 266775115 +0 2256642811 320139638 +2666088802 228717006 145282636 +4136435677 65740745 24698335 +3144325244 1046598285 188874270 +1657153777 2576782449 179487332 +1844732214 3569926351 232038039 +643329881 2077244060 8321979 +1471997632 4071441795 175759508 +2076770253 1020566335 26031950 +3676921608 3931759778 54449177 +3997010839 3302660217 139424838 +3845424768 3910360528 21399250 +1836641109 104012185 8091105 +3333199514 2085566039 161680135 +3880397123 112103290 116613716 +2360034592 2019293723 57950337 +1377750239 1235472555 94247393 +1647757140 2247246174 9396637 +2434490733 4247201303 1535992 \ No newline at end of file diff --git a/src/routes/2023/day/05/_data/exampleData.txt b/src/routes/2023/day/05/_data/exampleData.txt index ed81d07..bd902a4 100644 --- a/src/routes/2023/day/05/_data/exampleData.txt +++ b/src/routes/2023/day/05/_data/exampleData.txt @@ -1,2 +1,33 @@ -Hello -World \ No newline at end of file +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 \ No newline at end of file diff --git a/src/routes/2023/day/05/_info.json b/src/routes/2023/day/05/_info.json index 3f2157e..7afdff5 100644 --- a/src/routes/2023/day/05/_info.json +++ b/src/routes/2023/day/05/_info.json @@ -7,5 +7,5 @@ "result1": "Result", "result2": "Result", "keywords": [""], - "visible": false + "visible": true } \ No newline at end of file diff --git a/src/routes/2023/day/05/main.js b/src/routes/2023/day/05/main.js index e38e67e..b73594a 100644 --- a/src/routes/2023/day/05/main.js +++ b/src/routes/2023/day/05/main.js @@ -7,23 +7,117 @@ * @returns {any} data, preprocessed into something useful! */ export function preprocessData (data) { - return data; + const splitUp = data.trim().split("\n\n"); + const seeds = splitUp[0].split(" ").slice(1).map(n => Number(n)); + const maps = splitUp.slice(1).map((m) => m.split("\n").slice(1).map((n) => n.split(" ").map((o) => Number(o))).map(((n) => ({start: n[1], end: n[1] + n[2], destination: n[0]})))); + return {seeds: seeds, maps: maps} }; + +function applyMap(value, map) { + for (const range of map) { + if (value >= range.start && value < range.end) { + return value - range.start + range.destination; + } + } + return value; +} + + +function applyAllMaps(seed, maps) { + for (const map of maps) { + seed = applyMap(seed, map) + } + return seed +} + + /** * Takes preprocessed data as an argument and returns answer for part 1. * @param {any} preprocessedData generic preprocessed data, expecting the format as returned by preprocessData * @returns {any} result of part 1 */ export function part1 (preprocessedData) { - return 0; + let minimumLocation = 100000000000000 + for (let seed of preprocessedData.seeds) { + const location = applyAllMaps(seed, preprocessedData.maps); + if (location < minimumLocation) { + minimumLocation = location; + } + } + return minimumLocation; }; + +function getRange(value, map) { + for (let i = 0; i < map.length; i++) { + const range = map[i] + if (value >= range.start && value < range.end) { + return i; + } + } + return -1; +} + + +function applyRange(value, range) { + return value - range.start + range.destination; +} + + +function applyRangeToRange(lower, upper, map, rangeID) { + if (rangeID === -1) { + return [lower, upper] + } + return [applyRange(lower, map[rangeID]), applyRange(upper, map[rangeID])]; +} + + +function applyMapToSeedRanges(seedRanges, map) { + const mappedSeedRanges = [] + const minimumRangeStart = Math.min(...map.map((range) => range.start)) + console.log(minimumRangeStart) + let i = 0; + while (seedRanges.length > 0) { + const seedRange = seedRanges.shift() + const lowerRange = getRange(seedRange[0], map) + const upperRange = getRange(seedRange[1], map) + + console.log(lowerRange, upperRange) + + if (lowerRange === upperRange) { + mappedSeedRanges.push(applyRangeToRange(seedRange[0], seedRange[1], map, lowerRange)) + } else { + if (lowerRange === -1) { + mappedSeedRanges.push(applyRangeToRange(seedRange[0], minimumRangeStart - 1, map, lowerRange)) + seedRanges.push([minimumRangeStart, seedRange[1]]) + } else { + mappedSeedRanges.push(applyRangeToRange(seedRange[0], map[lowerRange].end - 1, map, lowerRange)) + seedRanges.push([map[lowerRange].end, seedRange[1]]) + } + } + if (i > 1000) { + throw "broken" + } + i++ + } + return mappedSeedRanges +} + + /** * Takes preprocessed data as an argument and returns answer for part 2. * @param {any} preprocessedData generic preprocessed data, expecting the format as returned by preprocessData * @returns {any} result of part 2! */ export function part2 (preprocessedData) { - return 0; + let seedRanges = [] + for (let i = 0; i < preprocessedData.seeds.length; i += 2) { + seedRanges.push([preprocessedData.seeds[0], preprocessedData.seeds[1]]) + } + + for (const map of preprocessedData.maps) { + seedRanges = applyMapToSeedRanges(seedRanges, map) + } + return Math.min(...seedRanges.map((range) => range[0])); }; \ No newline at end of file