diff --git a/Day9/Smoke.ts b/Day9/Smoke.ts new file mode 100644 index 0000000..b4ceb25 --- /dev/null +++ b/Day9/Smoke.ts @@ -0,0 +1,101 @@ +import { readLines } from "https://deno.land/std@0.177.1/io/read_lines.ts"; + +async function Part1() { + const file = await Deno.open("./Day9/input.txt"); + const lines = readLines(file); + + let map: number[][] = []; + + for await (const line of lines) { + map.push([]); + for (const n of line) { + map[map.length -1].push(+n); + } + } + + let result = 0; + + for (const i in map) { + for (const j in map[i]) { + if (+i > 0 && map[i][j] >= map[+i-1][j]) { + continue + } else if (+j > 0 && map[i][j] >= map[i][+j - 1]) { + continue + } else if (+i < map.length - 1 && map[i][j] >= map[+i + 1][j]) { + continue + } else if (+j < map[i].length - 1 && map[i][j] >= map[i][+j + 1]) { + continue + } + //console.log(i,j) + result += map[i][j] + 1; + } + } + return result +} + +async function Part2() { + const file = await Deno.open("./Day9/input.txt"); + const lines = readLines(file); + + let map: number[][] = []; + const seen: boolean[][] = []; + + for await (const line of lines) { + map.push([]); + seen.push([]); + for (const n of line) { + map[map.length -1].push(+n); + seen[seen.length -1].push(false); + } + } + + const basinSizes = []; + + for (const i in map) { + for (const j in map[i]) { + if (!seen[i][j] && map[i][j] != 9) { + let size = 1; + seen[i][j] = true; + const neigbours = getNeighbours(map,+i,+j); + while (neigbours.length != 0) { + const [a,b] = neigbours.pop()!; + if (map[a][b] == 9 || seen[a][b]) { + continue + } + size++ + seen[a][b] = true; + for (const n of getNeighbours(map,a,b)) { + const [c,d] = n; + if (!seen[c][d]) { + neigbours.push([c,d]); + } + } + } + basinSizes.push(size); + } + } + } + basinSizes.sort((a,b) => a-b); + return basinSizes[basinSizes.length -1] * basinSizes[basinSizes.length -2] * basinSizes[basinSizes.length -3]; + +} + +console.log("Part1: " + await Part1()); +console.log("Part2: " + await Part2()) + +function getNeighbours(map: number[][],i:number,j:number) { + const neighbours: [number,number][] = []; + if (+i > 0) { + neighbours.push([i-1,j]) + } + if (+j > 0) { + neighbours.push([i,j-1]) + } + if (+i < map.length - 1) { + neighbours.push([i+1,j]) + } + if (+j < map[i].length - 1) { + neighbours.push([i,j+1]) + } + return neighbours +} \ No newline at end of file diff --git a/Day9/input.txt b/Day9/input.txt new file mode 100644 index 0000000..6f97485 --- /dev/null +++ b/Day9/input.txt @@ -0,0 +1,100 @@ +8656456789432129876532356789899998754320146789767893213468953205459323997678901997655423567797655667 +6545345996543234987321567896798749886531235697657999304989764312378999886567899876543212345679543156 +5431239879694349876432398945976532997782356789547898919898765434567988765456789997654323456789654345 +6520139768989556987543499239854321098993768996536587998789876597679876544245690198965654567899875456 +7631297657679697998756789349765432129654879797627456789678997698789997432136789999876765698969987887 +9799989844568989869897996459876543439865994599210235893459998899995986543545699888989876899458998998 +9898766532345678954998987968989675649876789987421346932398899942134987654676789767891987894357899999 +5999854321234799123459999899699876789989899876532457891987678991013498767898896556790198965456789899 +4598765310138921014968998799543987893298964989547598920986598989139999878999964346891249876597898798 +3459977731257893929899987578932498932127893499987699439877487568998899989998894234979957987989987667 +1349998643467899896798795467890239643456789989998789949765326489987688993986789124567899299878975558 +2498997654989998765987654356789349954677999878999899899874212378996577992975569012398965499767894346 +3987898775894359979879864236789998766789998767999998799763104569987456789864398923579896987656789235 +9876769986789469898765432045898889898999987658989987698654323779594357897953287899798789199867890123 +9965454599897698759979564157976778999549876547679997569976457899492166986541026678987679013979989245 +9895323457998987646898783298965767889698984328456789678998569978989234598632134567896589999989569346 +9789912345689865435789894369893453978987653212359899899659698767878945987654346889995459887694458956 +4567895456792979524699965456792012367998864434578956999543987654667896899865456799984349765532347897 +3458976569891298912349876768943224459899976646789643598759876543456789942987869899873298654321236898 +4567898678932987899457989879654569598765987757899656999978965432346789321098989998764459876434345789 +5678979989743976798967899989975998949654598868998979889899976321245678993129894329878689999545457890 +7989467999654984676978979995989877929012679979987998775667895452356799789298789212999894298956567891 +8992346898795993235899767894598765698934567899876798654356789673459895678999678923457996987899678943 +9321456789989892124688956943987654567896989998754987654234678984578954569987546894678989976778999894 +5432357899877789013467897899876543458987999986543499875345689875699875979876435689789976564569986789 +6643567998765678925678998921965432356899998775432986985456789996989989898986546798998765423678965799 +7656798989874567899789239939954321245689987654301995496677899989879998787897657956879876534569754589 +8799989878965678969899197898765534345796976543219876398889999976767987656789878945265987875678923578 +9988679769876789354998976989977545656895987654323981239995598765459987545879989432123498976799999989 +9876544346989992123987755678989656767976798765549854346894398853298766434568996553464569989895878999 +8765321235898765019986543567998787889989899876898765557995987632129854323799998779878978999964567898 +9865210756789654198764332456789898995499986989969886768989976543298765545678989890989989987653456796 +9954331987896543239954210167899949543298765497656998879876987655689876677899678941293293496542397895 +9878543499987654549865423456789434956099979987542109989765799876796997798975568932999101987921789954 +0987654567899765669876534667894329899989998699667825699924678987895498999544456899788992498932678965 +1298776778969896789987645789987498788778987579878434578912457899989399987732345987656789598753489999 +2399897889759997898998756899876569679567898434989545678901238998777989996543469898745578979767598977 +3989959999898789997899768965989698546456789023497676989893349897665678987674579764323499767978797866 +9879943209986577976789979554698987635349892144998989899799599786564567998799699987635789945989976545 +8967894398995456895678989323597654321298943359889998789688987655443468899987989996546899896799765434 +6456889987654329923499994312398987310987894498767899643567898543312356789876478987756789789899876323 +4345679298799498936567892101269876499896789597656978942456798652101235799985356899877897679998764212 +6456792139898967897678943242456987987685678976545767896599899964214546987876237978998943589987673101 +6597891098987856789989655443999998998534889995434456789678999876323459876432123467899532359876543212 +7989989987986537892198987679878999359676899876212347898789998765459698765421012346789643469988654357 +9875778976897429799097899798767893298787976998455456789897899887678789886532326459899864578998795469 +8734567895976535678986789999656932109899954597568567893956989998789892998693434667899975689659986778 +7623478997898545789765999978946893919976895698978879912345678999999953489784546899969876789545698989 +9537889429987667893986798767835679898965998789989989895456989988998764569875657894356997896432129699 +7545789512399878912498996553124598787996789899999996789567899876789897679997798965767998954321013578 +8657897623943989993569987431013987676789999998999875679679998685679999789898899999879329765432125689 +9767898939894599989689876548923976465678999987899754579999997544334989898789939879998919876654345678 +9879999898789798868999987857549875334589998965999843267899876432123778987679549967897899988765457999 +0989987656678987656999898967659854213699987954889954345913987621014569876578998756786989999896769999 +1995495434589998745799769879898762104789986313767895876799599737123498787366799546565679953987879989 +9896989213478999856987656991999873215678965301456789989897498976544987643245678931344567894599998767 +8789878923568998767897543210197654323489987212345789998996597899656799732124578920123456789999987543 +7678969894567899898987654321298776564599654323456799986579986798967899541013467894234567896789195432 +6569548789878999979898767435349987676678998764678919877498765677898987643144588986785798935691019541 +5478934678999298765789998576467898887789999875799101954329854356799099653234679997996799123899998652 +6569325466789398754679459988568989998999987986945919976598754245689198754556789998987893254567899768 +7678912345696497643496578998679678999249876597896898899987543134568979877667897899998919875678999879 +8889101236797989821989679999893479892129765439987987678998621012799456998988945697989323997899999989 +9993257345899878992978989987932356789934984321099934589987542133789349899399239976878934998921989892 +7654347867898768789767892196544577897896976439198755698999653234679298789210198765767895789999876791 +9987656789999654578957899987666798935679897598999978797849796546892987678921349864656899899989965689 +4698798898998767689437578998977899219789789987890989975435987698921097586893998953245699999878454597 +2789989997899898992126459899998954398998678956921298765424698789992975435689897965369789999967323456 +0299878876789939987012346789879895997879567897932398654313799996789987645796756896878999989654212359 +1998765655678921096543458993456789876565456789545459432102987435678999856965945899989659876532103498 +9898754434589432987678967912345678985434234698957897643219876323899899767893126788994345987678924997 +9769963124678943498789978901956789976321012567898998755698765439988799898941012767898659998799899865 +8754321024569654579899989219897898765462133456789239976999876598767678989432123456998767899987678943 +9865542147679765989999998998798929876873235578995346989899989987654557976553349567899879994598989432 +7987653267889876794678987657689012989989356789996459897689998698842346898764498978978998989989996521 +6699879879999987912459876543579934597699868996789598656579997569976456799879597899569987879979789632 +5420989998999998923468987632456895987434989865678987643456896434988968899997986965498765468967579544 +6532399987888999436567899854667989876425698954599986532397895423499879979996565894349876579656498965 +7683469885467997598798998768788979654214567895789997710145694312345989767986434689456987896432367896 +9794998764346789679919349889899868965323578976899865431236789103466797656975426896567898954521356789 +8999899653235678989401239999987654596654567897946976642345678994567898943976545679878939863210145799 +7898765432123499995319398989998768789785789989023497656466789789678989991297676789999329974321234889 +6799899843435689989498987878999878999897894569199998787978997678999878989398988997988998765433445678 +5679998656746797878987656767999989997959923478987899998989899789234569978989899656867899976587676799 +4598789767899896569876645456789995986543205589876545679998789890124879767566798745556789597698787893 +3286569898999954398765432367999764698996456991985434589988679932438998754375789432346993459789898932 +3123489999989654249864321278998653499889599890997595699875497653546799663234598921268932499991999321 +1012679999879953198765438989999767989778987649898989999954319865678987542159987992357921989432987990 +4323578988767893239879567899899879865659897435789879887895402976799999531098976789467899878949876789 +5434989976458994345998678958789998764349765324679664765986514987899976542987895678989998767895465977 +6565789993256789469899989345678998765239896213469543224998695698989987669876474788999987656789213456 +7679899864345896598754591257999987654345989304678942105789989999678998798765353567899986548678902878 +8798998979656789689643210456897899765769878913567893987899879896545999899986212368898765434579924567 +9987897898767998798754321567976788978998767894579954598999965689659898999875433456789976755989895699 +8656456789878939899896432378965567899892459965678967999789897799998767879976557677899988767898799789 +6543236989989921998989543599434458999751346798789878997668799998754354567987667789968999978987688991 +7654345678997892987978994678912349998540125689898989786554677897643263566799789893457943989768457990 +8767656899566789976567789899101257987631234599957897654323456986532102345899899954567891098654345789 +9888987910345678985435678943212346799732545678945698986434678999543313476789929877678932139765456991 +9999998931234589997523567894324578999843756789234579997876899987654574597894212998989653345989987890