From c97078c1dc2d245d829294ff700b2e2cbcbbf3fe Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sat, 2 Dec 2023 22:41:44 -0700 Subject: [PATCH] day 3 jesus --- aoc_2023/day-03/example.test.ts | 26 ++++++ aoc_2023/day-03/logs/.gitkeep | 0 aoc_2023/day-03/logs/out_1.txt | 7 ++ aoc_2023/day-03/logs/out_2.txt | 7 ++ aoc_2023/day-03/part_1.ts | 79 ++++++++++++++++++ aoc_2023/day-03/part_2.ts | 96 ++++++++++++++++++++++ aoc_2023/day-03/problem.txt | 140 ++++++++++++++++++++++++++++++++ 7 files changed, 355 insertions(+) create mode 100644 aoc_2023/day-03/example.test.ts create mode 100644 aoc_2023/day-03/logs/.gitkeep create mode 100644 aoc_2023/day-03/logs/out_1.txt create mode 100644 aoc_2023/day-03/logs/out_2.txt create mode 100644 aoc_2023/day-03/part_1.ts create mode 100644 aoc_2023/day-03/part_2.ts create mode 100644 aoc_2023/day-03/problem.txt diff --git a/aoc_2023/day-03/example.test.ts b/aoc_2023/day-03/example.test.ts new file mode 100644 index 0000000..1a654c1 --- /dev/null +++ b/aoc_2023/day-03/example.test.ts @@ -0,0 +1,26 @@ +import { expect, test } from "bun:test"; +import { main as part1 } from "./part_1"; +import { main as part2 } from "./part_2"; + +const example = `467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598..`.split("\n"); + +test("part1", async () => { + const answer = 4361; + const res = await part1(example); + expect(res).toEqual(answer); +}); + +test("part2", async () => { + const answer = 467835; + const res = await part2(example); + expect(res).toEqual(answer); +}); diff --git a/aoc_2023/day-03/logs/.gitkeep b/aoc_2023/day-03/logs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/aoc_2023/day-03/logs/out_1.txt b/aoc_2023/day-03/logs/out_1.txt new file mode 100644 index 0000000..f772de3 --- /dev/null +++ b/aoc_2023/day-03/logs/out_1.txt @@ -0,0 +1,7 @@ +=== COMPUTATION === + + +=== /COMPUTATION === + +=== ANSWER TO P1 === +528819 diff --git a/aoc_2023/day-03/logs/out_2.txt b/aoc_2023/day-03/logs/out_2.txt new file mode 100644 index 0000000..dd7c2a2 --- /dev/null +++ b/aoc_2023/day-03/logs/out_2.txt @@ -0,0 +1,7 @@ +=== COMPUTATION === + + +=== /COMPUTATION === + +=== ANSWER TO P2 === +80403602 diff --git a/aoc_2023/day-03/part_1.ts b/aoc_2023/day-03/part_1.ts new file mode 100644 index 0000000..33a8107 --- /dev/null +++ b/aoc_2023/day-03/part_1.ts @@ -0,0 +1,79 @@ +import { JSONSet, Vec2 } from "@/utils"; + +const isNumeric = (symbol: string) => symbol <= "9" && symbol >= "0"; + +export const main = async (lines: string[]): Promise => { + const adjacentPoints: Vec2[] = []; + + const dimy = lines.length; + for (let y = 0; y < lines.length; y++) { + const dimx = lines[y].length; + for (let x = 0; x < dimx; x++) { + const symbol = lines[y][x]; + if (symbol != "." && !isNumeric(symbol)) { + for (let dy = -1; dy <= 1; dy++) { + for (let dx = -1; dx <= 1; dx++) { + const newX = dx + x; + const newY = dy + y; + if ( + !(dy === dx && dy === 0) && + newX >= 0 && + newX < dimx && + newY < dimy && + newY >= 0 + ) { + adjacentPoints.push({ x: x + dx, y: y + dy }); + } + } + } + } + } + } + + let sum = 0; + const visitedPoints: JSONSet = new JSONSet(); + for (const { x, y } of adjacentPoints) { + const symbol = lines[y][x]; + if (!isNumeric(symbol)) continue; + + let num = ""; + if (!visitedPoints.has({ x, y })) { + let x_i = x; + while (x_i >= 0 && isNumeric(lines[y][x_i])) { + x_i--; + } + x_i = Math.max(0, x_i + 1); + + while (x_i < lines[y].length && isNumeric(lines[y][x_i])) { + num += lines[y][x_i]; + visitedPoints.add({ x: x_i, y }); + + x_i++; + } + } + + if (num.trim() !== "") { + sum += parseInt(num.trim()); + } + } + + return sum; +}; + +// + +const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path; +if (isrun) { + const file = Bun.file("./problem.txt"); + const text = await file.text(); + const lines = text.split("\n").filter((x) => x && x.length); + + console.log("=== COMPUTATION ===\n"); + + const answer = await main(lines); + + console.log("\n=== /COMPUTATION ===\n"); + + console.log("=== ANSWER TO P1 ==="); + console.log(answer); +} diff --git a/aoc_2023/day-03/part_2.ts b/aoc_2023/day-03/part_2.ts new file mode 100644 index 0000000..4f36cb2 --- /dev/null +++ b/aoc_2023/day-03/part_2.ts @@ -0,0 +1,96 @@ +import { JSONSet, Vec2 } from "@/utils"; + +const isNumeric = (symbol: string) => symbol <= "9" && symbol >= "0"; + +export const main = async (lines: string[]): Promise => { + const adjacentPoints: (Vec2 & { ratio: number })[] = []; + + let ratioId = 0; + + const dimy = lines.length; + for (let y = 0; y < lines.length; y++) { + const dimx = lines[y].length; + for (let x = 0; x < dimx; x++) { + const symbol = lines[y][x]; + if (symbol === "*") { + ratioId++; + for (let dy = -1; dy <= 1; dy++) { + for (let dx = -1; dx <= 1; dx++) { + const newX = dx + x; + const newY = dy + y; + if ( + !(dy === dx && dy === 0) && + newX >= 0 && + newX < dimx && + newY < dimy && + newY >= 0 + ) { + adjacentPoints.push({ + x: x + dx, + y: y + dy, + ratio: ratioId, + }); + } + } + } + } + } + } + + const visitedPoints: JSONSet = new JSONSet(); + const ratioNums: Map = new Map(); + for (const { x, y, ratio } of adjacentPoints) { + const symbol = lines[y][x]; + if (!isNumeric(symbol)) continue; + + let num = ""; + if (!visitedPoints.has({ x, y })) { + let x_i = x; + while (x_i >= 0 && isNumeric(lines[y][x_i])) { + x_i--; + } + x_i = Math.max(0, x_i + 1); + + while (x_i < lines[y].length && isNumeric(lines[y][x_i])) { + num += lines[y][x_i]; + visitedPoints.add({ x: x_i, y }); + + x_i++; + } + } + + if (num.trim() !== "") { + const n = parseInt(num.trim()); + const curr = ratioNums.get(ratio) ?? []; + ratioNums.set(ratio, curr.concat([n])); + } + } + + return Array.from(ratioNums.keys()) + .map((ratioId) => { + const arr = ratioNums.get(ratioId)!; + if (arr.length > 1) { + return arr.reduce((acc, x) => acc * x, 1); + } + return 0; + }) + .reduce((acc, x) => acc + x, 0); +}; + +// + +const isrun = process.argv.length > 1 && process.argv[1] === import.meta.path; +if (isrun) { + const file = Bun.file("./problem.txt"); + const text = await file.text(); + const lines = text.split("\n").filter((x) => x && x.length); + + console.log("=== COMPUTATION ===\n"); + + const answer = await main(lines); + + console.log("\n=== /COMPUTATION ===\n"); + + console.log("=== ANSWER TO P2 ==="); + console.log(answer); +} diff --git a/aoc_2023/day-03/problem.txt b/aoc_2023/day-03/problem.txt new file mode 100644 index 0000000..b823066 --- /dev/null +++ b/aoc_2023/day-03/problem.txt @@ -0,0 +1,140 @@ +.479........155..............944.....622..............31.........264.......................532..........................254.........528..... +..............-...............%.....+...................=....111*.................495.......+.......558..................../..........*..... +....................791*..62.....$.............847........&........-..........618.*...........818....&..642.........................789..... +....520.58......405......#....542.../587.............*....198.......846.........*..............*.......*....................647............. +.........*........./.964..........................474.302.....................786...43..............505..436...................*.....#51.... +......832....@..........*.951*....984*111..801................../.....................-.......@............%.198......322.186...262......... +..........490........690......346.........................702&.566.%....................192...190.87............*.....-....=..%.........344. +....*.........................................816*588..............152..535................*.......*...........425...........53............. +..36.290.831....374................579.536.....................408.......*..733....998....169...146.....%179..........658...............260. +...........+..../...........795/.....*.*.....................%........776......*..............................790.871./.............281*.... +....................78.............716..400....319........167.................399..@.............................*.........$...599.......... +............719.......*........640................%..376...............800........211......#478......326*93........889..684....*.....285.... +.....852.......-.......462..................374/....%..................*........$..............................603..*..........369..&....... +........@.........960..................................*...........966.321...925............926...................*.947..&.............574.. +..............%.....$.........$......*.......479....909.339..........*..............803........*17......284$...657.......587......*......... +...........772............&....345..93...465*................419......676...............-.@521.....-...........................399.662...... +.................17+..2..531.......................79........*...589......198*734....534.........614..................109................... +.......301............=............................&..321..895..*..........................344.................694............717...511*.... +..........%...707*370.....................473.428........*.....509......=889.....353%.........*................*.......299.......*.......... +........................973.....................*.......877............................&855.955.670.@682.150....958.............197....555.. +................314....*....504*352........602...468..............688.....10-....................#.......*...........306*.............*..... +.....................987.......................5.............811..*...../.......515..217...........705....462..880.......374......16...42... +....#......402.$............804..295...406.....&..150........*....22..429...268............324........-..................................... +.....270..*.....982...644.../.....+....*.........%.............................-..........#.............-...87.......................505.... +938.................=...*.....=98.......370................19@.@867.............................396...272...*......760.......627.#...*...... +..........593.....793....503.........34...............................406.....456...............+.........303........*...........142.432.... +...........*..........&........707..*................563.....837.........+......*.....230..169.......................138....420............. +..572....689..........503.......#...................*...........*449..........39.........*./......77.......%....404......#..$............739 +...............137...................#.....624.....883..../............................891.......@..........310.*.....404................... +...287.......*..%............*961.488.........@...........544..........130$................$.......531.72.......424............766.......... +...*......476......316....722............780........613........../533...............96......553.91*......*.835*........*690...*............. +.350...........%...............470.950...*.............*......................%.....*...728............359.....141..326.......658..832...330 +..........772...127..................-...335......./...539........362......101.....959.............221..................512.........*....... +..........*..........798.......138..............207.....................................999...574.....*..........484....&............364.... +.......@...919.........*......*.....202.971...............488.................@349........*......*..........404...=........&..448..$........ +.....246........211...426......206...*.....*557..........*....27..659@....588.............367...........961...*..........583.....*..280..... +...........724.....*......324*.....788..........685....788......@........*....532.................85.......*.139....75.........196.......... +.......377....@..521..........391................./...........@.........987...*.....................*810.214........*.....-........490...... +......................................&....679.........776....447...457......25..............................467..173....241................ +................43........898.412...742......%....*540.*.............*..............825..259...997.514.........*............................ +.......%775..52*...........*..*.........809....871.....384.....295..470............$.......-.....*....*..&.....114.....................=.... +...147...........69=...........914..144*....=..............%.....*......$875.....+............=......278.441.........859.346.281........40.. +......*....................-...............89...........578....519.............676..........473..361..................*....*..+............. +..78+..42......$...750..465.....218...833.......137...=.............538.783...........*962.........*............*...421.502....../..42...... +..............457.....*.........+........*.....*....825.....26*....-.........238...205.....539.109.348........837..............842...*...... +........#..............175..............925....399.............560.......88.*..............*.....*.................................636...... +......693.......................447.................137............-679......479........619...283...............$458.544.-802.848@.......... +...%.........................&...$....&.+39............*.........................%..618..................*141........+...................... +....471...502....252....663..986...633..............530..117............598.....220...................542......568.......#219..532..15...... +.........*.....-........-...........................................840.$.............717..$...................../............#......*...... +.......351......993......................................573...865...*....$......848.......239.....134....826........409..338...$64..231.... +...........................................=...809.925..*.........*...43..277.....@............571.=........................................ +..........+698..355.....-...594....%...#.55.......*......847..409...............@...............*.....*78..........................#363.497. +...................#..261.......591..695....-....................=.....678.......714.......364.804.156...................676...605.......... +587.881.....356............192.............957...963.447.................#...63.............*................344.....373........*........... +..................524..568....&...691...........*.......*...169*218..10....................10.........399.46*............../488.491.16...... +.....824@.....772..$.....&..........*.265...............964............#...............................*........359...556............=.897.. +.............*................293.345.*...161*.....................671.............%414.726.347.....564....................420.............. +.............155.......483....................546........-.....794....*......968.........*...............591.......................$........ +.....806.../.......120.*.......813....................481...........593.........*....667.815.....682........*.%579.......#298....668.188.... +.......*.718.......*....469...*.........251...52*919.......846..................887....*......../........637...........................*.... +.....81.......132.51..........236........-................*.......$.167....338......963..258......844.........884.......*980........#...816. +..........475.........150..........316........389......590......291..-.....*....662......*...........................143..........284....... +.......................&.......*..*....390.......+.559..................116.............926........779..................................233. +500...%......................821..594....*.........*................220...........830*...............*..........89...915.......230.363...... +...*...623....337.......................40..........827..............*................828....$294....392....*....*.....%..............*..... +.993............*....565........................638...............307.............95.......#..............535.105.........632..938.166..$939 +.....$..444@...378...*.......4...283...971@.......*...................689..937...*.......736......@...................991..@....*........... +....639....../.....886...........*..............668...88........472...+...*....742...493.........674....*......#........*.....*.78..-350.... +...........290.........*.....%...110........447.......#....-.......*....838..........................167..911.487.....880..493.............. +199...745......189=.389.....676......+........*..=........442....810........................................*.......................795..... +...................................337........76..728..=......61.....769.............................386.....627..604............$.....*623. +....=925................................=...............282.............*...597.....851..841...............2......*..........991..139....... +.......................443....=..........829....................524...816......*......*.*.....752....=468..*...805.....388@...@.......387... +....14...........*........*...325.................676..............-.........192...859..712....+..........585............................... +....*.............892...513..........286.984...................301....859.....................................900.................788%...... +...19................................@............424/....155..*...........124.....*......844.......693.......*............................. +......469..................706.786.............67........*.............115*.....631.164..#............*..852..277.960.................11.... +.....@.......796...850..........*..................354...471.......................................152.........................870...*...824 +................*....*....832..822..............................=114.....881....-.........357*.....................................606..*... +..574............956.124........................943....278..................&.163...................=199................................761. +........329.623............308.....210..........*.........*....#...3........................792...........11...676.......................... +.795.68....*........................@.......280..........965.943...............814.....182.*.....454..202*..............971.47.704.....444.. +....*.........407..............558.............#.........................913.....%....%.....801.................652......*....*.....-..*.... +..........552*.................*.........487......321.......852.217.......+..........................=.943..628.......303..........350.288.. +.......@.....................80..................*.....$......*....*..............806*.......&....190...&.................806.235........... +.......264.538......729.................997....688..435....510......82.285............316.639....................246.......%...*.......#.... +....@.........+.964...............795..+.......................862...............................945.726-....../..../...........210.....700. +..452.............*...201.....891*.............49.............*........182......483.399.847.......*.............868....204.220.......-...... +.................466..*.............819...=.......184....$...114........+..........*........619....974....603................*.....854...... +.......................624.........*.....42...499*......743.........586.................950....-.............................927............ +........$........379&.......855..252..#.....................120%......*..$...618.......*..................619..........636.................. +....408.462.............528*...........869....145.15.606............581..297...*.....35..-...........218...*.......4*....%.......175........ +.....*........................$20................*..............................901......388...........$.............25...........*......157 +..904........659............$.....762........808...%..........*18.............................#.878..........44..693............611..566*... +......977.......*.=.......597..-.*..........*.....538.209..............+.....$703......#....347....*652........*..........504............... +.............668...462........59.2...370...587........*.............819..............152......................919.......+..*................ +.....817................688&.......................848..................639...347...........*671.......240........900..122.951.............. +....*..........268/..........*821...........449..........817........86...*...............431......404..&...660....*..................691.... +....611.............661....59........256.........$.......*...........+.548.........746.............+...........975...669.577...353....*..... +580.........307......&................*...........634.100....339......................*.................................*..........387...... +.....594.......*........&445...........675....+.............*......857......*........192.251..........504...................../.........534. +.923..........954.............329.150......727....80.......474........$..385.............=...515*97..#......876.573...199.....921........... +...*.....236...............74........*..............*..............................919.........................*.......*..803....../460..... +.94......*.........133.......@........894.826........316............&.........%........349..752/.%.....735...........838.....*.............. +...............425.*............985*........%......&........77....653....622...659.964..*.........147.-....................-.671............ +......252...........898.....640.....841.........988..........*.............*.......*...805....................=...&.....410..........$743... +.........*.....................*985.........407........$756..506.........-.332....934..................253..972...903........887.199........ +......#...548........608*471.........142.....*...685...............729.172......-............................................&....#.....-558 +.....855..........%...........82.......*..729.......*.509.....850..@......./.910.......202........447..#........481......908........*....... +.............$.641...758..846.*.....225...............*...374...........158........231.......775....*..837...............$...........549.... +.......587.615.......*.........844.......59......135..88.....*................437...*...........*.374................186....%829.167........ +...691*............573..............293.*...........*.....940....105..38..........98........%.64.........432*737........*...........*....... +..........727..........@.682........=....50......324................*.......446...........51.....360....................175....227..426..... +566..186./......*277..18....*779..*.........+...........226..........184.....=......696..........+............344...550...........*.....65.. +.......*..................&........728....521.....277.........&.123........+........*....+......................*....@...20.....385....*.... +.....85........37....=....438.442.............522...*...757.248....*577.27..466..862...503.406*767........*736...164.......*...........233.. +.................#....640.....*........834...........23...*...............*............................404...........-......785............. +.........*..................89..877*....#..572..........22............891..........295.354*864...875............=..&..706.........-......... +.......307............59............510...*....187.*247............+........#..741*..............$.......$608.316.355......*.....916....858. +..................745......705*590.......815..@...........296.....540...=..742........843.*44.......718.................309.............*... +....................*............................*888.......%..........278..............@.......878*.........797..$366..................95.. +......890....*96.894......765......170/.......260.......149.......................759........................*.................940.......... +.......*....................*..#...................57*....-.........................&..858................312......881.........$.......627.. +....149......392..633.....581.947.#...955...151*......44.................942..............+..........14........210*.....................*... +............/......*..889.........792...........201............#.....450*............................*...*................127.........921... +....567#........253...*......................................817...........85*106......239*.......920..16.969......%......*...............76 +.........*537..........583.512*...............*393.43...............513#...........906..............................562.681....*713......... +......260.....48..560..........376.........140............................151......*........823*.....921..........&.........224............. +...............*...%......=.................................716.........*..=...769.....@........65........13....29.................509...... +.............61..........836..&29......357............106......*199...996.......*....545.................=...........@106......420*......... +....#...........#487.433...............#....81........*.......................488........382.....&.............#............................ +..33............................908$........*........133....470&......-...........894...%......70..............626........253.*915.......... +..................139*134.185...........49..142..../...............685..312...95..+..................985...#.......831.....@................ +........*..................&.....................487..........822*........@....*.....................*....919......*...................*.... +98......931......*98..................................@..710$...................522...915.583.72......592..........169.......353....365.678. +..............559......570...........................485..............#....582.......*.......*............*..................*....=......... +.......+...38..........*...506.........811.....+188......766...623..363....*......*.914............#.@..92.365.........../...694..312..156.. +........59...*.......405...*..........*......%..........&.........*.........515.586.......239@...571.80..................852...........*.... +....737.....608..........362...336....642....606..................262......................................209.........................617..