From e14f71484f4d4cf4f2ad2999a38302a9ade6fbf0 Mon Sep 17 00:00:00 2001 From: Parnic Date: Thu, 15 Dec 2022 14:12:28 -0600 Subject: [PATCH] Day 15 solution Pretty rough, this is my initial solve. Part 2 needs to be sped up, but is currently 3 seconds on my PC and my dataset. --- advent-of-code-2022.csproj | 1 + inputs/15.txt | 124 +++++--------------------- inputs/15a.txt | 14 +++ src/15.cs | 176 +++++++++++++++++++++++++++++++++++++ 4 files changed, 215 insertions(+), 100 deletions(-) create mode 100644 inputs/15a.txt create mode 100644 src/15.cs diff --git a/advent-of-code-2022.csproj b/advent-of-code-2022.csproj index c89e8f2..f6b1bc8 100644 --- a/advent-of-code-2022.csproj +++ b/advent-of-code-2022.csproj @@ -66,6 +66,7 @@ + diff --git a/inputs/15.txt b/inputs/15.txt index b9adc42..a62c71a 100644 --- a/inputs/15.txt +++ b/inputs/15.txt @@ -1,100 +1,24 @@ -1611211251115912246318291351712232221112291225613417396681596333922111215456315139438667612549131941 -8932114117311575142243171112817226232119863432221628251318261661191181914131437113236733419511921113 -3311112319881299126138992415428219269217291531671353324111619434247412712947881243552169128135998191 -2519114235316687331115265422264283428712233596112945181199111152385542971123592212311162661421311721 -1382461356912146511842319166317957261971229314621394111613112824713571111351337871924493182613432256 -5114711863712392214191153831119241119273761167659431242745167938327311529213316381831112912163261726 -2133551185321341121928148131837914911121496412338312251193532149215486158331691323639142596645122111 -1711569246152189932234173111631965298489584111312431711132154431213342671841478551877131719115182119 -9122149481122135915537133111611623234859281656147415218627843119137181419118649192615139557824552374 -4911523117167829117391221297181113512124212111923552525313422843324691463742313599316131243752253684 -5414417111829212132324935672161432171112458323991236122391625212798322322229323469836523865422521221 -6411419531652111283121918212256619646644819331891775249312384935234199392273946162322562911231297881 -2128729415918832321163943197715389275338541111113951736132921133694314913712199421116532211811753165 -4155523539192111125923842191181996311591936139684321893331918133513841219121725123321178229123219131 -9479285145913396251237128221525362732111772949515352815229526959117541561311121638422294521121411186 -5361431713278166762792211821131161119224248551162317662211612621114112115836126641192459222212113327 -2494757131117376374123218127935211421161131174291195896246235724212194112343914127851943372836444151 -1126333352124135364191315839321423442739332131119751918814928429539165252271192793931114699193141352 -7993476425955975972523385123991731131227922419193851353711242334711754713176123716252617514911343624 -3837633187113119153159621661333345499733158211731234919113375757122732611351412411224458275312113323 -1213823111311973291166152828314699226174131294182312323228596132434921226112127148541491997311661191 -1941285148119221112175922114193977135939237888391111177353313353689991314261133271214122191599821736 -8119315121159215398141511314171194211295753133431893571741116342288969422281953477211127122222213973 -3211111167199143898758411863181981129346176513322864719479111538191232861611116115119312419947645553 -1116492399643291188637718819419298121111918367194662558912163995311127427114328911165321911938295897 -5221341524613111111223219421487242918173161311123534127423239989283232112859413129411118482547249611 -2221112419417121234321411525226131119681871293873972521124131841952617392731118211645272733175695623 -9612345322815334929511532521673436495145189314311416112129421593214128111119235149113134463374743313 -1244221693131322581451147117421391191864425622293214124341731361291125663915221114141621411362821278 -2172253112327761111869591591622634435924853217231815881111128255855675618865529681213195121163164521 -5743111872122589683117162112882841163318223322251188355331721123578419616834686786529121123113218593 -3321161112491141116159313111431514931151434445121713391782311211112188453711967181121569227119699721 -7512453185178912811919718138316122226148122383921386636142313119496642377384231129986892299376621781 -3113599931452121119619213654414531351118185111248215332182424311317111178972225269831323284339236238 -9912366294321341583781633214743179594932161437249186115152411911369753336382255511323199954813623728 -1314711821143992615365323742237972221289177334988151971416866117414415987181446288212921318291872287 -1624323218114569968532411252451323154227171182184321146819515242113128613341112811137258318916548332 -1195511352111831237947711128751752123921434481581332911154674751178836895113494129677169311468467511 -1863122261482612323595412171126851273311324418253578198211196382173948331667691631411433864291121391 -3122131239163387493384638361292847829627718461433891215125431265311315821112996425882765126561237115 -9911861321475453331414812557311159914929131473161814111427321154399431161191225423116724752189845823 -8151641221153511138821713126551323968737187495942257223319592116321152816393654749195121842859351561 -1166117415617912214512233311622311349827111971281785282815252122131242554221212241132169312158111271 -2233996127755859511239121167379563381398219339928131397735621213582199233313911145982592291244594129 -8569151891541511111461248226124157912265892366222218221872634432811432182612229317181291524711139591 -5412311262782192614933281411582713218722165267517317328522142495131291471384412276125722597121434111 -4173541913248981189134359164156111911121424819924627122149411627829189431144112131161789527537415321 -6199541772813361931132121169128154757113922428416775411215491988894985711765281981294312137712596211 -5251419132285971615119119177942242443621167411481133191239129831611177111334811322714342335212111579 -1211672567181413837159338619362214593438571282737841891171811922112975413195436813376511357913889116 -6914528121947132224243611758351474856334243631714152291226249163141142233286146216271151727381151169 -1871184111221765954832861441175634212721111212163252244115391345154411384118131211297436312614113428 -3557196222191552169112122212625667121963141445463531367892145241154113939211239289676471619622335121 -8567338617111541261391871614787255952111293114637631145121311622153232556918911718514211615412216141 -3944121182193414694117121173381311212142432119146149621727971611129135332923129117951215298211269685 -1111592571437471142981893366164971251413314142436483167321549514181212232343999516918983819933236711 -2375115619131214293975661177135192182233622431221411615114284928141121112272113151119724411224972212 -1145312314943244314182211125113132121152177139345496164699415321632281441514462841822959821322912811 -1128221793138934439154535119891154314517512192822451911664244177187311411881591162215129524277142942 -6921947132116612163531511516186151192965868417146111719495941314413311391186879899168572141191251821 -3186112191177114711191511274113892212111986111184132611486471323117534245765967313831244815411149751 -7211143432211162819111341412884428411157442849137112139891933941951342191473171151339821319247144849 -1131113162196912112827566145361732144314447154315612293272485153184992426631931523811115124359133865 -2281351241119151142551963821187631141146729957878815111212296647772312131896111211121435246213415112 -6613911666125516111611198421146871358716222211126181284827422512433151939174162969714755332383281551 -1314838958146117922189891941512614221122283989927215899611211552121843851111181341741142862615891571 -1184414114322239212541616362157152294513233374491318461448714111519235422661798718171439241333431296 -9311816362385233444141981596131861168924891222652896114258832451273331121448932163431364431164713512 -1712984142627979443581394797712382615719949171481235263498912867711341219911315133743566529611474376 -1718233118179567122847133769922353161321539632555611121981121731282117121198481422621181544275958977 -1965611713311299491119831837156322619154487176611728193194757471222195131333111191133639479288145129 -1925143767879899127119296284583131662912411236971684115512125122995914975232517512847426331989161132 -4499267153717255561739216833131923245176337118314966821513423353715111317591526423211571894215118187 -6579124183396925714615176324644214696647734416475522221315511135949232212422214239431992312238376121 -9422683256329462113113721585689491163835132226239324123952341945732131113122223192114515783614231313 -2155691356142542121131351729332713931122531151137795833172153367423193842872211218197921147111831296 -2316296411941633442127418259415912992294191164991361512885441591856325534682915231823857139781651122 -9145636137281236355296215512749138944731311141277711621121111498691212276268831522715182159527772777 -3224122132239918842916762517116151661619122978482962411115113411995381473218465123135111322141134939 -6582351117483126471318349319471541111917442217717826322613365376294124112591671131243119511279414331 -6423952732261724348136382963134472539218234399242771194234392191432797697271349538712139262173323939 -5995413228134633761812711113695646455338171181624111194996368911812318641817184264325113531133191771 -1161351263343243681321468581721652196519161223416276521842811111224136921281256361216611912232865513 -2185132121111682961259657424131224958141284152159882687111141923263321231218111258572158565191221314 -2817225411782624419714272493347261556461522152931965215218223824912411171151744891113912194137111423 -1579421337169822546441817285517972381181324434929391762124311161134381199823232531412315824999419213 -3397433594321463666814912371119113783382222111221112984248888113935751232163134138754215731563528193 -3142471143234221613116412482441787257711122117635393189121212512552831411161239125913133111811135743 -3892129761474512987363928223283161741741136114411532151214231333431431221937142357114251112998852553 -5541195316631416357251394178322219446412481895123714849951917522182335426248194849111296984119159115 -6135415326645125142212956869181913111276972623139411379444646921626426393126519431231627738414519415 -7251911292354759133915441134817595316198333184811718729171311322429315768358628238138131651543153821 -1429314149329291981111115452911258119831371787279311981671662216716411183121183216117779949145194819 -9446771212175891141141332112891165219141111999616597311271116422955215881561111292111311146215473192 -2521861131871723318434421918715152965195611181488289421611546398635412427117931218893287513626244111 -8153191121156911192812112313211137214128163295685421243114341139614913113191157196121836348767226615 -7191223694191111525131311232587511392919411121716188943799133588811321333393142496522191428461621541 -4311991831229124542211761611121912789517192135112141341544155561111164536149942489792274122235211192 -1955255328184197192291393141191318135231142911827322211178376698122131997147521924327551211333124525 -2327268121248272713243282411262957517495392942118222115384214211191592248992511958919114295121113911 \ No newline at end of file +Sensor at x=3988693, y=3986119: closest beacon is at x=3979063, y=3856315 +Sensor at x=1129181, y=241785: closest beacon is at x=1973630, y=-98830 +Sensor at x=2761889, y=2453622: closest beacon is at x=2803715, y=2643139 +Sensor at x=3805407, y=3099635: closest beacon is at x=3744251, y=2600851 +Sensor at x=3835655, y=3999745: closest beacon is at x=3979063, y=3856315 +Sensor at x=3468377, y=3661078: closest beacon is at x=3979063, y=3856315 +Sensor at x=1807102, y=3829998: closest beacon is at x=2445544, y=3467698 +Sensor at x=2774374, y=551040: closest beacon is at x=1973630, y=-98830 +Sensor at x=2004588, y=2577348: closest beacon is at x=2803715, y=2643139 +Sensor at x=2949255, y=3611925: closest beacon is at x=2445544, y=3467698 +Sensor at x=2645982, y=3991988: closest beacon is at x=2445544, y=3467698 +Sensor at x=3444780, y=2880445: closest beacon is at x=3744251, y=2600851 +Sensor at x=3926452, y=2231046: closest beacon is at x=3744251, y=2600851 +Sensor at x=3052632, y=2882560: closest beacon is at x=2803715, y=2643139 +Sensor at x=3994992, y=2720288: closest beacon is at x=3744251, y=2600851 +Sensor at x=3368581, y=1443706: closest beacon is at x=3744251, y=2600851 +Sensor at x=2161363, y=1856161: closest beacon is at x=1163688, y=2000000 +Sensor at x=3994153, y=3414445: closest beacon is at x=3979063, y=3856315 +Sensor at x=2541906, y=2965730: closest beacon is at x=2803715, y=2643139 +Sensor at x=600169, y=3131140: closest beacon is at x=1163688, y=2000000 +Sensor at x=163617, y=1082438: closest beacon is at x=1163688, y=2000000 +Sensor at x=3728368, y=140105: closest beacon is at x=3732654, y=-724773 +Sensor at x=1187681, y=2105247: closest beacon is at x=1163688, y=2000000 +Sensor at x=2327144, y=3342616: closest beacon is at x=2445544, y=3467698 \ No newline at end of file diff --git a/inputs/15a.txt b/inputs/15a.txt new file mode 100644 index 0000000..9dc90a6 --- /dev/null +++ b/inputs/15a.txt @@ -0,0 +1,14 @@ +Sensor at x=2, y=18: closest beacon is at x=-2, y=15 +Sensor at x=9, y=16: closest beacon is at x=10, y=16 +Sensor at x=13, y=2: closest beacon is at x=15, y=3 +Sensor at x=12, y=14: closest beacon is at x=10, y=16 +Sensor at x=10, y=20: closest beacon is at x=10, y=16 +Sensor at x=14, y=17: closest beacon is at x=10, y=16 +Sensor at x=8, y=7: closest beacon is at x=2, y=10 +Sensor at x=2, y=0: closest beacon is at x=2, y=10 +Sensor at x=0, y=11: closest beacon is at x=2, y=10 +Sensor at x=20, y=14: closest beacon is at x=25, y=17 +Sensor at x=17, y=20: closest beacon is at x=21, y=22 +Sensor at x=16, y=7: closest beacon is at x=15, y=3 +Sensor at x=14, y=3: closest beacon is at x=15, y=3 +Sensor at x=20, y=1: closest beacon is at x=15, y=3 \ No newline at end of file diff --git a/src/15.cs b/src/15.cs new file mode 100644 index 0000000..ed22ebe --- /dev/null +++ b/src/15.cs @@ -0,0 +1,176 @@ +using System.Drawing; +using System.Text.RegularExpressions; +using aoc2022.Util; + +namespace aoc2022; + +internal class Day15 : Day +{ + private List knownSensors = new(); + private List knownBeacons = new(); + + internal override void Parse() + { + Regex r = new(@"=(-?\d+)", RegexOptions.Compiled); + foreach (var line in Parsing.ReadAllLines("15")) + { + var m = r.Matches(line); + knownSensors.Add(new ivec2(int.Parse(m[0].Groups[1].Value), int.Parse(m[1].Groups[1].Value))); + knownBeacons.Add(new ivec2(int.Parse(m[2].Groups[1].Value), int.Parse(m[3].Groups[1].Value))); + } + } + + private bool IsReachableFromSensor(ivec2 sensor, int maxDist, ivec2 point) + { + return point.ManhattanDistanceTo(sensor) <= maxDist; + } + + private bool IsReachableFromAnySensor(ivec2 point) + { + for (int i = 0; i < knownSensors.Count; i++) + { + var s = knownSensors[i]; + var b = knownBeacons[i]; + var dist = s.ManhattanDistanceTo(b); + if (IsReachableFromSensor(s, dist, point)) + { + return true; + } + } + + return false; + } + + internal override string Part1() + { + int interestedY = 10; + HashSet emptySpace = new(); + for (int i = 0; i < knownSensors.Count; i++) + { + var s = knownSensors[i]; + var b = knownBeacons[i]; + var dist = s.ManhattanDistanceTo(b); + + for (int j = 0; ; j++) + { + var testVec = new ivec2(s.x + j, interestedY); + if (testVec.ManhattanDistanceTo(s) <= dist) + { + if (!knownBeacons.Contains(testVec)) + { + emptySpace.Add(testVec); + } + } + else + { + break; + } + } + for (int j = 0; ; j++) + { + var testVec = new ivec2(s.x - j, interestedY); + if (testVec.ManhattanDistanceTo(s) <= dist) + { + if (!knownBeacons.Contains(testVec)) + { + emptySpace.Add(testVec); + } + } + else + { + break; + } + } + } + + return $"Empty spaces at y={interestedY:N0}: <+white>{emptySpace.Count}"; + } + + internal override string Part2() + { + int pointsTested = 0; + int max = 4000000; + ivec2? answer = null; + for (int i = 0; i < knownSensors.Count; i++) + { + var s = knownSensors[i]; + var b = knownBeacons[i]; + var exclusionDist = s.ManhattanDistanceTo(b); + var top = new ivec2(s.x, s.y - exclusionDist - 1); + var dir = new ivec2(-1, 1); + var pt = top; + while (answer == null) + { + pointsTested++; + if (pt.x >= 0 && pt.y >= 0 && pt.x <= max && pt.y <= max && !IsReachableFromAnySensor(pt)) + { + answer = pt; + break; + } + + if (pt.y == s.y) + { + break; + } + + pt += dir; + } + + dir.x = 1; + while (answer == null) + { + pointsTested++; + if (pt.x >= 0 && pt.y >= 0 && pt.x <= max && pt.y <= max && !IsReachableFromAnySensor(pt)) + { + answer = pt; + break; + } + + if (pt.x == s.x) + { + break; + } + + pt += dir; + } + + dir.y = -1; + while (answer == null) + { + pointsTested++; + if (pt.x >= 0 && pt.y >= 0 && pt.x <= max && pt.y <= max && !IsReachableFromAnySensor(pt)) + { + answer = pt; + break; + } + + if (pt.y == s.y) + { + break; + } + + pt += dir; + } + + dir.x = -1; + while (answer == null) + { + pointsTested++; + if (pt.x >= 0 && pt.y >= 0 && pt.x <= max && pt.y <= max && !IsReachableFromAnySensor(pt)) + { + answer = pt; + break; + } + + if (pt == top) + { + break; + } + + pt += dir; + } + } + + return $"After testing {pointsTested:N0} points, found distress beacon at {answer} with tuning frequency: <+white>{(answer!.Value.x * 4000000L) + answer.Value.y}"; + } +}