From e606f967be7dcd2982b0969c7d32037cb470722a Mon Sep 17 00:00:00 2001 From: mwhol Date: Mon, 11 Dec 2023 15:51:06 -0600 Subject: [PATCH] just a few more days work Have fallen a bit behind. Upcoming final and I'll try to catch up after. --- mwhol/day05/answer.js | 160 ++++++++++ mwhol/day05/input.txt | 237 ++++++++++++++ mwhol/day06/answer.js | 28 ++ mwhol/day06/input.txt | 2 + mwhol/day07/answer.js | 9 + mwhol/day07/input.txt | 5 + mwhol/day08/answer.js | 82 +++++ mwhol/day08/input.txt | 716 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 1239 insertions(+) create mode 100644 mwhol/day05/answer.js create mode 100644 mwhol/day05/input.txt create mode 100644 mwhol/day06/answer.js create mode 100644 mwhol/day06/input.txt create mode 100644 mwhol/day07/answer.js create mode 100644 mwhol/day07/input.txt create mode 100644 mwhol/day08/answer.js create mode 100644 mwhol/day08/input.txt diff --git a/mwhol/day05/answer.js b/mwhol/day05/answer.js new file mode 100644 index 0000000..88a2231 --- /dev/null +++ b/mwhol/day05/answer.js @@ -0,0 +1,160 @@ +const fs = require('fs') + +const input = fs.readFileSync('input.txt').toString() +let array = input.split('\r\n\r\n') + +// playing around with some horrific one liners lol +seeds = array[0].split(": ")[1].split(" ").map(Number) +maps = array.slice(1).map((x) => x.split('\r\n').slice(1).map((y) => y.split(" ").map(Number))) + +let locationsArray = [] + +for (seed of seeds){ + let currentValue = seed + for (map of maps) { + let difference = 0 + let match = false + for (range of map) { + bottom = range[1] // just for my ease in coding + top = range[1] + range[2] -1 + distance = range[0] - range[1] + if (currentValue >= bottom && currentValue<= top) { + match = true + difference = distance + break + } + } + currentValue += difference + } + locationsArray.push(currentValue) +} + +console.log("Part One: ", Math.min(...locationsArray)) //... = spread => array becomes list of values + + +//Part two, i could do some recursiv stuff or I could +//avoid that and go map of maps then seed of seeds, at each level +//I will have an array of [top, bottom], that can then be passed into the next map +//at each range, I will check to see if there is leftover range unmapped from the first match +//and then push it onto the array + +//Linear mappings means we need only to keep track of top and bottom +//but require recursive splitting as we go down the mappings + + +//not happy with this of grouping to pairs, should be functional +var seedsTwo = [], size = 2; +while (seeds.length > 0) seedsTwo.push(seeds.splice(0, size)); +seedsTwo = seedsTwo.map((x) => [x[0], x[0]+x[1]-1]) +// console.log(seedsTwo) + +var sortedArray = maps.map((map) => map.sort(function(a, b) { return a[1] - b[1]} )) +// console.log(sortedArray) + +// function mapping(seedBottom, seedTop, map) { +// let answers = [] +// for (range of map) { +// rangeBottom = range[1] // just for my ease in coding +// rangeTop = range[1] + range[2] - 1 +// distance = range[0] - range[1] +// if (seedBottom <= rangeTop && seedTop >= rangeBottom) { // if they overlap +// if (seedBottom >= rangeBottom && seedTop > rangeTop) { // if clean overlap +// console.log(1) +// answers.push(seedBottom, rangeTop) +// let remaining = [rangeTop+1, seedTop] +// } +// if (seedBottom >= rangeBottom && seedTop <= rangeTop) { +// answers.push(seedBottom, seedTop) +// remaining = [] +// break +// } +// if (seedBottom < rangeBottom && seedTop <= rangeTop) { + + +// } + + +// //// +// if (seedBottom < rangeBottom) { // if seed dips into unmapped region +// console.log(2) +// let answers = [seedBottom, rangeBottom-1, rangeBottom, ] +// let remaining = [] +// } +// else { +// console.log(3) +// answers.push(seedBottom, seedTop) +// let remaining = [] +// } +// } +// // I don't think we need this, we already know it overlaps from above +// // else if (seedTop >= rangeTop && seedBottom <= rangeTop) { +// // let bottom = seedBottom, top = rangeTop +// // let remainingBottom = rangeTop+1, remainingTop = seedTop +// // } +// else { +// let remaining = [seedBottom, seedTop] +// } +// //some appending thing here +// } +// return answers, remaining +// } + +// mapping(seedsTwo[0][0], seedsTwo[0][1], maps[0]) + +// for (seed of seeds){ +// let seedBottom = seed[0], seedTop = seed[1] +// for (map of maps) { +// let difference = 0 +// let match = false +// for (range of map) { +// bottom = range[1] // just for my ease in coding +// top = range[1] + range[2] -1 +// distance = range[0] - range[1] +// if (currentValue >= bottom && currentValue<= top) { +// match = true +// difference = distance +// break +// } +// } +// currentValue += difference +// } +// locationsArray.push(currentValue) +// } + + +// seedsTwo = seedsTwo.map((x) => [x[0], x[0]+x[1]-1]) + +// console.log(seedsTwo) + +locationsArrayTwo = [] +let current_min_value = Infinity +console.log(seedsTwo.length) + +for (set of seedsTwo) { + console.log(set) + console.log(current_min_value) + for (let i = set[0]; i <= set[1]; i++) { + let currentValue = i + for (map of maps) { + let difference = 0 + let match = false + for (range of map) { + bottom = range[1] // just for my ease in coding + top = range[1] + range[2] -1 + distance = range[0] - range[1] + if (currentValue >= bottom && currentValue<= top) { + match = true + difference = distance + break + } + } + currentValue += difference + } + if (currentValue < current_min_value) {console.log(currentValue)} + current_min_value = Math.min(current_min_value, currentValue) + } +} + + +// console.log("Part Two: ", Math.min(...locationsArrayTwo)) //... = spread => array becomes list of values +console.log("Part Two: ", current_min_value) //... = spread => array becomes list of values diff --git a/mwhol/day05/input.txt b/mwhol/day05/input.txt new file mode 100644 index 0000000..992c137 --- /dev/null +++ b/mwhol/day05/input.txt @@ -0,0 +1,237 @@ +seeds: 41218238 421491713 1255413673 350530906 944138913 251104806 481818804 233571979 2906248740 266447632 3454130719 50644329 1920342932 127779721 2109326496 538709762 3579244700 267233350 4173137165 60179884 + +seed-to-soil map: +1389477588 1222450723 86190269 +2369327568 3429737174 127508203 +88123474 1366319913 182655004 +1475667857 405321476 41320497 +1258939826 536917987 41172751 +1924266396 3404859218 24877956 +1762699703 957158780 33280161 +3452528837 3222194776 182664442 +2196573512 1924266396 172754056 +433176947 990438941 6166389 +4047092335 2681059373 30705388 +439343336 1626695089 181842577 +1949144352 2940939059 125726128 +979719551 446641973 90276014 +2184073848 2711764761 12499664 +4077797723 2605613670 27940277 +2074870480 4240432416 54534880 +3048538268 3066665187 104068222 +621185913 1124514126 97936597 +1157547656 773812762 84277017 +1300112577 20266514 6655368 +1151949413 1808537666 5598243 +3435484067 2724264425 17044770 +380487497 1308640992 52689450 +1644897150 26921882 117802553 +1516988354 996605330 127908796 +1241824673 858089779 17115153 +3932455534 2269053207 114636801 +3319415958 3854507632 116068109 +2129405360 4237225295 3207121 +2729160001 2097020452 172032755 +3635193279 3557245377 297262255 +1795979864 578090738 107598550 +2901192756 3970575741 147345512 +4105738000 2416384374 189229296 +0 685689288 88123474 +1903578414 0 20266514 +719122510 144724435 260597041 +2132612481 3170733409 51461367 +2496835771 2741309195 199629864 +1306767945 1548974917 77720172 +3200111916 4117921253 119304042 +1069995565 875204932 81953848 +3152606490 2633553947 47505426 +270778478 1814135909 109709019 +1384488117 1361330442 4989471 +2696465635 2383690008 32694366 + +soil-to-fertilizer map: +1796371314 958475699 90518367 +4004397333 4049196179 245771117 +2175877891 3813840430 96544159 +1966430612 3997904997 51291182 +3155151482 799623922 79310846 +4250168450 2358444962 15280909 +4265449359 3910384589 29517937 +3087542169 2534702057 67609313 +1202725381 3631683738 113825873 +852357580 2833874802 40691288 +1452732352 2128818900 25726830 +291197164 3745509611 68330819 +1316551254 2602311370 60535393 +2017721794 2764291908 69582894 +498502503 445768845 353855077 +3367678481 1860885729 203469524 +3845535174 1124639771 94398512 +1041749195 2373725871 160976186 +2330424521 2874566090 757117648 +1478459182 127856713 317912132 +3234462328 1680414394 31771008 +359527983 1219038283 138974520 +893048868 1712185402 148700327 +3571148005 1406027225 274387169 +3939933686 2064355253 64463647 +3266233336 2662846763 101445145 +1886889681 878934768 79540931 +87297932 2154545730 203899232 +39283510 1358012803 48014422 +2272422050 3939902526 58002471 +1377086647 1048994066 75645705 +2087304688 39283510 88573203 + +fertilizer-to-water map: +3988818582 3038666130 306148714 +2927763871 3008779749 29886381 +124309691 99049201 282856506 +99049201 381905707 25260490 +407166197 2131018623 602068357 +3442767659 4213146266 81821030 +2957650252 3344814844 485117407 +3907802704 2927763871 81015878 +1009234554 407166197 1723852426 +3524588689 3829932251 383214015 + +water-to-light map: +1071892650 2651787028 57679970 +1129572620 3396952543 81593150 +1240611714 2163493623 488293405 +0 2068015044 95478579 +1211165770 3074252590 29445944 +2592854025 0 138938366 +2523843782 1948369545 69010243 +924090948 883610805 76353493 +2022159128 174281796 501684654 +1000444441 2923208140 71448209 +95478579 959964298 268093632 +684655532 1228057930 239435416 +3410916028 2709466998 213741142 +363572211 1627286224 321083321 +3324937342 2017379788 50635256 +3183141431 3068711832 5540758 +2939436746 3478545693 243704685 +3624657170 2994656349 74055483 +1728905119 3103698534 293254009 +3930463154 3978112708 316854588 +2731792391 675966450 207644355 +3375572598 138938366 35343430 +4247317742 3967989739 10122969 +4257440711 3930463154 37526585 +3188682189 3722250378 136255153 +3698712653 1467493346 159792878 + +light-to-temperature map: +4148509456 1952010509 126270832 +856886372 936932802 97162803 +829640090 282271594 27246282 +244444108 1274282332 107584318 +1528329058 3192525971 211478915 +2566760651 2178128911 792500107 +478140779 1162859130 51849897 +4274780288 1528329058 20187008 +352028426 265852816 16418778 +1739807973 2970629018 221896953 +188336830 840381853 56107278 +529990676 0 265852816 +2424714410 1911677980 40332529 +795843492 896489131 33796598 +969062248 324530949 412804402 +954049175 309517876 15013073 +3359260758 3868594872 426372424 +2465046939 3404004886 101713712 +375094277 737335351 103046502 +0 1214709027 59573305 +2324866840 2078281341 99847570 +1961704926 1548516066 247670884 +2209375810 1796186950 115491030 +59573305 1034095605 128763525 +3785633182 3505718598 362876274 +368447204 930285729 6647073 + +temperature-to-humidity map: +645925588 927807414 87140162 +0 398577479 157531253 +1936153073 3766846194 135269565 +3964800672 3492411188 1957783 +3660032389 3460150664 32260524 +1374126579 1182630672 364804866 +2334938774 2586583717 132274954 +3729993364 4148156458 139151684 +2071422638 2398735028 187848689 +461859499 894601505 33205909 +1128085880 3902115759 246040699 +3966758455 1609937892 328208841 +733065750 1045187965 45230417 +3692292913 2718858671 37700451 +1738931445 2854070578 51145436 +3869145048 2758414954 95655624 +446537472 670580619 15322027 +3468753739 2905216014 191278650 +1790076881 1547435538 62502354 +3103241907 3096494664 363656000 +778296167 1014947576 30240389 +2467213728 3653280748 113565446 +2739690951 1959797890 363550956 +430471457 878535490 16066015 +934173831 836762826 41772664 +272165118 90734051 80200203 +2259271327 1128614382 54016290 +1852579235 2323348846 75386182 +808536556 272940204 125637275 +1927965417 4287308142 7659154 +1935624571 1128085880 528502 +352365321 194834068 72750816 +495065408 685902646 150860180 +248265304 170934254 23899814 +975946495 556108732 114471887 +157531253 0 90734051 +3466897907 2756559122 1855832 +2580779174 3494368971 158911777 +425116137 267584884 5355320 +2313287617 1938146733 21651157 + +humidity-to-location map: +2297594568 1304834363 199636291 +964984478 962777545 102011627 +3376226732 2612009119 78542873 +3210191679 3257561655 73324720 +960734175 2732971245 4250303 +3552752951 3643184542 128526794 +1654967093 1268999863 35834500 +2805486965 2087320949 359714826 +72263011 1608745500 171195806 +4225512580 3861994731 69454716 +1240952852 431398165 68767410 +3695056291 298067962 76655045 +1309720262 500165575 32124036 +2768212426 260793423 37274539 +258896561 532289611 263781213 +1967976997 1084282606 71977571 +2255175315 2690551992 42419253 +1514000396 0 28227011 +0 2539746108 72263011 +3283516399 2447035775 92710333 +1233825691 3330886375 7127161 +614836670 2866104927 345897505 +1341844298 88637325 172156098 +2039954568 1779941306 215220747 +3861994731 3931449447 363517849 +1950982711 414403879 16994286 +3454769605 3219988623 37573032 +1690801593 3433704416 164490238 +1855291831 3338013536 95690880 +1195879484 1080226916 4055690 +522677774 1995162053 92158896 +2601505705 796070824 166706721 +2497230859 1504470654 104274846 +1199935174 380513362 33890517 +3492342637 28227011 60410314 +1066996105 2737221548 128883379 +243458817 1064789172 15437744 +3165201791 3598194654 44989888 +3689265936 374723007 5790355 +3681279745 3212002432 7986191 +1542227407 1156260177 112739686 \ No newline at end of file diff --git a/mwhol/day06/answer.js b/mwhol/day06/answer.js new file mode 100644 index 0000000..21e7e99 --- /dev/null +++ b/mwhol/day06/answer.js @@ -0,0 +1,28 @@ +const fs = require('fs') + +const input = fs.readFileSync('input.txt').toString() +let array = input.split('\r\n') +array = array.map((x) => x.split(': ')[1].split(/\s+/).slice(1).map(Number)) + +times = array[0] +distances = array[1] +let answers = [] + +for (let i = 0; i < times.length; i++) { + time = times[i] + distance = distances[i] + a = (time + (Math.sqrt(time**2 - (4*distance))))/2 + b = (time - (Math.sqrt(time**2 - (4*distance))))/2 + let count = Math.floor(a-0.00001) - Math.floor(b+0.00001) // I know there's a better way to make it non-inclusive but this is so fast... + answers.push(count) +} + +console.log("Part One: ", answers.reduce((prod, num) => prod * num)) + +time = Number(times.join('')) +distance = Number(distances.join('')) +a = (time + (Math.sqrt(time**2 - (4*distance))))/2 +b = (time - (Math.sqrt(time**2 - (4*distance))))/2 +let count = Math.floor(a-0.00001) - Math.floor(b+0.00001) + +console.log("Part Two: ", count) diff --git a/mwhol/day06/input.txt b/mwhol/day06/input.txt new file mode 100644 index 0000000..81e2b2e --- /dev/null +++ b/mwhol/day06/input.txt @@ -0,0 +1,2 @@ +Time: 61 70 90 66 +Distance: 643 1184 1362 1041 \ No newline at end of file diff --git a/mwhol/day07/answer.js b/mwhol/day07/answer.js new file mode 100644 index 0000000..2540802 --- /dev/null +++ b/mwhol/day07/answer.js @@ -0,0 +1,9 @@ +const fs = require('fs') + +const input = fs.readFileSync('input.txt').toString() +let array = input.split('\r\n') +array = array.map((x) => x.split(' ')) +console.log(array) + + +for hand in array \ No newline at end of file diff --git a/mwhol/day07/input.txt b/mwhol/day07/input.txt new file mode 100644 index 0000000..bf2815e --- /dev/null +++ b/mwhol/day07/input.txt @@ -0,0 +1,5 @@ +32T3K 765 +T55J5 684 +KK677 28 +KTJJT 220 +QQQJA 483 \ No newline at end of file diff --git a/mwhol/day08/answer.js b/mwhol/day08/answer.js new file mode 100644 index 0000000..60772cb --- /dev/null +++ b/mwhol/day08/answer.js @@ -0,0 +1,82 @@ +const fs = require('fs') + +const input = fs.readFileSync('input.txt').toString().replaceAll('(', '').replaceAll(')', '') +let array = input.split('\r\n\r\n') +let instructions = array[0] +let hands = array[1].split('\r\n').map((x) => [x.split(/ = /)[0], x.split(/ = /)[1].split(", ")]) + +hands = Object.fromEntries(hands) + +// console.log(instructions) +// console.log(hands) + +let current_point = 'AAA' +let step_counter = 0 + +while (current_point !== 'ZZZ') { + const instruction = instructions[step_counter % instructions.length] + if (instruction === 'R') { + current_point = hands[current_point][1] + } + else { + current_point = hands[current_point][0] + } + // console.log(current_point) + step_counter++ +} + +console.log("Part One: ", step_counter) + +let points = Object.keys(hands) +let current_points = points.filter((point) => point[2]==='A') +step_counter = 0 +complete_status = false +let next_point = '' +let lcm_array = new Array(6).fill(0) + +console.log(current_points) +console.log('AAAAAA'===current_points.map((x) => x[2]).join('')) + +while (!complete_status) { + for (let i = 0; i < current_points.length; i++) { + const instruction = instructions[step_counter % instructions.length] + if (instruction === 'R') { + next_point = hands[current_points[i]][1] + current_points[i] = next_point + } + else { + next_point = hands[current_points[i]][0] + current_points[i] = next_point + } + // Cheated a little bit here, read on the reddit that there's distinct loops + // for each point, can just calculate the loops and then use least common multiple + // since bruteforcing wasn't working, just kludged the logic in here + if (next_point[2]==='Z' && lcm_array[i]===0) { + lcm_array[i] = step_counter + console.log("point: ", i, " | step: ", step_counter+1) + } + } + step_counter++ + let last_letter_of_points = current_points.map((x) => x[2]).join('') + complete_status = (lcm_array.filter(x => x === 0).length === 0) + // complete_status = ('ZZZZZZ'===last_letter_of_points) +} + +var gcd = function (a, b) { + return a ? gcd(b % a, a) : b +} + + +function lcm(a, b) { + return (a * b) / gcd(a, b) +} + +var n = 1 +for (var i=0; i