2023 day 3
This commit is contained in:
parent
5f4895b700
commit
9cc373290b
11
2023/03/example.txt
Normal file
11
2023/03/example.txt
Normal file
|
@ -0,0 +1,11 @@
|
|||
467..114..
|
||||
...*......
|
||||
..35..633.
|
||||
......#...
|
||||
617*......
|
||||
.....+.58.
|
||||
..592.....
|
||||
......755.
|
||||
...$.*....
|
||||
.664.598..
|
||||
|
140
2023/03/input.txt
Normal file
140
2023/03/input.txt
Normal file
|
@ -0,0 +1,140 @@
|
|||
..172..............................454..46.......507..........809......923.778..................793..............137.............238........
|
||||
............*.........712........=.......*................515.*...........*.......690.........../..........658.........=.........*..........
|
||||
.........823.835........%.........710.....749........134..%............................#812...&.....925.../..........276.......386..........
|
||||
519..................13......341.................481....=.....$............-.......211.......92.......*.....................................
|
||||
............832*105..@........$..................*.........797.....535..932.........*....152...........123.........678.540...........-...6..
|
||||
....&..948..........................271....-....228..79.26.........................733...=...715............27.586........*.......883...*...
|
||||
..172.......=..+.............88&....%....340.55.....+.............465..398......=..................585.......*....*812...347................
|
||||
...........374.462.......166..................*...........786........*....*910..675.................*.....149................653*....+80....
|
||||
.......*............680.....*......876.........864..................259.................124.169....799............608..*.........98......951
|
||||
....615.......*....%.....151.........*....#802........$.......680......................../...............857........-.901...............*...
|
||||
..............3.................637..493...........926....636..*.....350........881.........699......886.../.458...........#..474*409...246.
|
||||
...&...336......922............*..........370..............$..581....*.....335.*.......608$..*..11.......$....-..........928................
|
||||
...50..*.........*...........978...453.....*.......-..22..........298.......*...711.........794...*...267............44*.......460....*.....
|
||||
...................................*........534..797....-.............61....272.....@874..........892.....707.............230....*..72.537..
|
||||
....................405............309..........................221./..*....................872%.......75..*......237.....*.................
|
||||
..208.176..239.........*172.............795.............670.......*..9..504......*.717..641........908*....975.....*......130...............
|
||||
.....*....*.......711$....................*..54.....494..../.333.882...........282....@.......927*....../.......555..983*...........479.....
|
||||
.915......670.524................866...862..@.........*../....*...................................72.901..................995..........&....
|
||||
...*..%.......*.....71.............*..................33.350.796.....612.............#..463..............*399.....=.......*......309........
|
||||
....8..951.886......@......41*......606.680...787................605*.....892.......219.*........972..846.........377....644................
|
||||
..............................718........*.......*.......355*688............*.145...................*.......576.............................
|
||||
..........867.*.....................+...974...148..........................87.#........500&...534..986..269...*.....576....302....+.........
|
||||
........@......504.......-........652.........................104....@502......................*...............691.*.........*.423..706..402
|
||||
.286..218.882........-....579......................320.....=...-..........48.393.....202......758.....242..951..............................
|
||||
....*.....*.....$..943.............928.....*......*......310......&........*....*...*......=..............*......295*133....................
|
||||
..698...331...859.......91.........#....815....822..............39..........833....749...591...........455....*..........&30....*...........
|
||||
............*............+..............................117............................%........727........256.439...19.......478...........
|
||||
......67.595...599.........442...997..187.................@.596..276..141.597*792...780.........*......*.............@..............#.......
|
||||
266@.............-..713.....*....*.......=..783...564*.......*.......@.........................839......255.............636......749..%.....
|
||||
........+............*.....963.926.39../......&.......274...228..../.....812.........487@.24.............................*..*..........348..
|
||||
...618...364.936.....708...........*....63..&.......................212....-..191..............798................*....591..906.............
|
||||
......./.....@...............591...127.......491...141..*.....+...............*......339.@483........204*......871.465..................%...
|
||||
....890..................997*............723........$..675..465..............372.875*.........%..........534.............209.......*.....648
|
||||
..............+................139*..........273..................../7.....-..............854.920...968%..........384......%.......830......
|
||||
......+632...273.......132*........793.........................207..........952..../..66..*........................#...........@............
|
||||
.......................................162..................35.*.................22..*....234..........468...................#.750..........
|
||||
.....15...161.....................93....@...818..-580........*.449...................125...........129....*................723......365.....
|
||||
.....-....*.......678.........&21...=....../..............547.......$.........701....................#.....845.........................#....
|
||||
743........634...*.................................................679...........*176......979....................928.......$..848..........
|
||||
...............107....296....845.....*..........@...........................69..............$..442.........960/...........311.*.............
|
||||
.........................*............519.....58..............248.......582*........403.......*........929..............-......486.......151
|
||||
....-390....396.........279.......718............449.....794..#..............+......*......251...980....../.../...328..476..................
|
||||
.............*.....................-......../...*...........*...292.........188...&..112............*315.....529............................
|
||||
713..........860......114.743&.......368.776...195.........850.=................943............543...................483*625................
|
||||
......889........=.../.................*.............916.............201............-...............426&....627..............284............
|
||||
.452...........579...........786.961.15.......488.......*373.....830*......947.645.189........367.................$..../551.................
|
||||
....*.....99*........388.793...*...*....578..../...........................*...%.......-497.........@221..720...885.........588....692......
|
||||
....................*........903..81.......*.......*........=./....615.....202...355........997-..........*................$.........*......
|
||||
...........780.@....259.439.................483....596...490..415....*............-........................683........*371...&644..964......
|
||||
.......796......161.......*..#884....................................458.247.500........145.895.62.742.267..........16......................
|
||||
......+............../.998...........173..511............552*847...%.....&.........+651..*.....*........*.....108........+..............*975
|
||||
.444.....560.297...149..............*....*......*...................173...................365............986.$............271...182...26....
|
||||
....*84....*......................#..471.696.736.107....974...-614............................../...%930..........................+......364
|
||||
............800...-155..235......446...................*....................286.......822../...199............670-....................%.....
|
||||
....882.................*.................521....*20.282.............641.......*..703*.....887......513*271....................+819..100....
|
||||
.99..*........933*...649.......407............922...............-......*....681.............................*....#426...204.................
|
||||
.....112................../.....*...819.94......................689...652...........182.933.858.............711...............610.....*437..
|
||||
.540..........#.........740...500....*...$....184.......................................*....#.......386...........................668......
|
||||
....$..........719..695...........824............&...882...........@......811........771.........142*.........867.667.*299.....143..........
|
||||
425....120.............*....401........................*....75..2..551.....*...719....................44..141...+..............+............
|
||||
...%..*..........963....209..&.....913.................871..=............%.904.........365.244.495....*....*........19......................
|
||||
.....971.........=.............883*.........225.967.............#......44.................*.....*..182......830.....@..190............487...
|
||||
...................................../........*.%........93....577..%......541.....*456.....370.52......................%.............@.....
|
||||
..82......396....................927..887...268...........&..........517...................%...............753................+.............
|
||||
.........*..........................*.........................................607..493.479.........521......*.....680*58..&....879..........
|
||||
......951.................136.&....585.........69....933.........25......265......*............923...&......5.............624........109....
|
||||
...........826..............$..407..............*...............*...+...........772.=...........*.............................628...........
|
||||
.......293..*...420...................................140.......728..815.196.-.......470.....403.................807..........*...99.....443
|
||||
......-.....732...*.........795....35........18.........*................*....789................931................-.52....727....*....*...
|
||||
.................477..@682..*.......*...118.........539.................844......................*...481.....%..........*..........863...773
|
||||
...479&......................585..817...@...........*...16*244...826*..........427...............694.......834.......949....................
|
||||
....................336....................-.....205..............................&............................................899..........
|
||||
....49...971...............331..289.......661.........400...57...-...................711......./873.........-...668...94......*.....42......
|
||||
.../.......#...........68...&...#...557.*.....425.......*...-..267.+.................*......#........@....993..=.......-....49......../.65..
|
||||
.............647..569.....*...........@..1...*......+...823.........579.............153...65...263....320..........243...........626........
|
||||
.......128...+.......*..215...%..684.........519..247.....................+.....................*...........320...*.................*.......
|
||||
........*..........827.......954...*.....378..........553..............434..........644....................*.......208............88..387...
|
||||
.907..402..............-..........16....*....%.........*...507..403.........*.................272...496....750.12.......................*...
|
||||
.............555.610.454.............322....954..529..25...*.....*../....651.43...534...........*......+.......$.........=...-121.442..709..
|
||||
.269*410......*...................................+.......163.608...451.........................999.....................292.................
|
||||
...........867....595*88.....#990...473.................................406.285.825......464...............378-..835&........%.......570*484
|
||||
.....550.............................-......162....812*37..................*.....*......+.................................693....%..........
|
||||
........*385...............*934..810.......*...................758..313........413..+.......265.691...559............807@.......824.608.....
|
||||
....373..................................580.....506..........*............852.......786....*.....*......*.........&...................*....
|
||||
.......%...932....194*688...713..................&.....333..410..817.524.....*...438.......410.129......312.....452......202..........146...
|
||||
.........=....+..............*....................................#..........626.*.....745.........974.....................+................
|
||||
580...212........485.........511.....495.768.361.....323-..............*176......595......*........*.......+.......147.........227....491...
|
||||
...*........%....*........85........*......*..*.............@..329$.623........*.......=..161.%...680...104.........$..452....*.........=...
|
||||
854..*489....33...992.......*......15...418..890...%......362............603.224.....431......701.............929.=....../.....408..........
|
||||
............................980..................428......................*......615..............%343............827..............686-.....
|
||||
48.213..................353.....%902......616.........583..%674.....361....711.........821&...31%.............................285...........
|
||||
...&...614.....@52.207#....*228............*..........*.............*..............................624.............734..........%.411*876...
|
||||
......*............................358...807.....280..174...232..715...$..............804..522........=........717..*..=....................
|
||||
434....262.655.822.358.&709...........*..........*..........=.........876.....952.............*................$...81.412.160....751........
|
||||
..................*.................976.......739..490*913.....@...........+....#.374..625..236..539........$...............................
|
||||
..743...................737@.............766....................197........878.....*....*...........*...861..32...............16...975......
|
||||
................972..@.....................&...293..85/.....561.......919..........156.............435....*..........835........*3..........
|
||||
........823.690.*....747...............799....*..........=....*........&...................241.........558...............459................
|
||||
....552*.....*...701.........................579.......187...691...#.*......694*980...........*.#...........244.............*....842........
|
||||
.57.......687.....................110*873........................469.217....................573..324.308*......%...........855....*..553....
|
||||
......551..................................................................142.......771*................902...................606..........
|
||||
.....*....990........869........8.......#477............330-....................37*......408.-751......-......239..........362..............
|
||||
...994...*..............=.984..*...194...............................972*967.......150...............433..517...*...........*........947....
|
||||
.........189..501*334...........56...#...............531...846...562..........477...............................19........396...570.*.......
|
||||
......................761..............243.....692......*.....*.....*.#985.......*...991-......=.320/.................%...........*.585.....
|
||||
....590.....&....$...$....993.554.....*.........%....132...623....429........549..31........643...................134.237........50.........
|
||||
.............385.538.....*....*.....826...........@....................................644......151*919......733.............73.....848.....
|
||||
.477...871..............5....68.841......80*.......405.70*898..............+25............*787..........368...*................/.......*....
|
||||
...*....*.....6....................*..=.....370......................................186.........146.......*...447.......287............611.
|
||||
443...893.....$.......%20.......108...43..........637......396...596............64......#.505...*.........141..............*......543.......
|
||||
.................197......244.................530.=...334.......*....335..745...../.......*......99..182..............645.963...............
|
||||
...........%....*........*.......593..704&..............-...@...211.....*......./.......571..178...............#.................+476.......
|
||||
.....*812..17.617.......249..327..-........527..15........238............926.564..............................260...........715*......../793
|
||||
....................826.........*............*.......................................@.........685.....@................954.....423.........
|
||||
.............796*..............260..946.....848...902.............285......136....211............*.....258.874.....332.*....................
|
||||
....&829.........348....539.........*............*....623....*.....&.........*..........252.%253..86......../..599.*...592.......672-.232...
|
||||
............752.........-.........375............859....*.926.182....733......241......=..............276..........762................*.....
|
||||
.............................482...........346........374...........&............................493.....*322..........................175..
|
||||
...*../........793..&532.........530..........*...........509.861.......134........353............*....................*94..................
|
||||
480....761........+.....................=..768..790..@......&.*....296.................311........340...344.....989.142..........411........
|
||||
.............208.......300.............506......*.....267.....918....*.......204..........%.606.........*....+...=.......*397.......*...257.
|
||||
............+.........*......-...............636..476................109......*.............*......-....115...91......823........929....@...
|
||||
...595........313.....849.452......568...............%..802....................915...730....138.30..455.....................589.............
|
||||
..............*................290....%.......147..........#....492..83....12*........=.........*.............................@.......17....
|
||||
970.....+.-...701....#264....%....$.......113.......916........*..............620............809........$788....@.....446........904..*.....
|
||||
...*..221.112..............610............@.....799............713........657.......325..........................551..*.............=..615..
|
||||
.295...............852..........*....986.........*..........................................-............620..........665.....930...........
|
||||
.....785.......542.*............732...=........583...+....................424....$....702.367..993.......*....386..#.............*..........
|
||||
........*...........114....704..........377...........633...........=730...#..241.......%.....*.....-.396...........372......135..806.......
|
||||
.......122......963........$............*.....................509$........................50.....280.........672.........538..$........619..
|
||||
.....................957.....542...731..941.......*....59..........571*554....214....108.@...............104*............=.............*....
|
||||
.....=...............*.......*....../.............337./...........................=..%..........................................873..563....
|
||||
....771..500........868.......213......902....456........../.........255.....377.781......=348................133..@...367*696.....*........
|
||||
........*....$..25........784..................$...........735..413.....*108./............................645...*..8............185.........
|
||||
..603.389..956.*...........*.......636...866.....338...+25......*...+..........................................667........#..........+..296.
|
||||
...*............544.....510..418.=....=....*....................379.478.....961.............410&...........413......*912..469..758.733..-...
|
||||
798....*565.793.............*....98.....447.....@......291...$...............*..244................%..491.../...................*...........
|
||||
....546......*....454...120..683.............923.....@...*...865.574......276........56......57.659..*................-...-...512...........
|
||||
............329...*.................................606.599...................*927..*.........-.......674..*........723..974................
|
||||
................378..911........987.....606......................899.73....489......848.....................664...............388......589..
|
89
2023/03/part1.ts
Normal file
89
2023/03/part1.ts
Normal file
|
@ -0,0 +1,89 @@
|
|||
enum LogLevel {
|
||||
Answer,
|
||||
Info,
|
||||
Debug,
|
||||
}
|
||||
const LOG_LEVEL = LogLevel.Info;
|
||||
|
||||
const print = (level: keyof typeof LogLevel, msg: string) => {
|
||||
if (LogLevel[level] <= LOG_LEVEL) {
|
||||
console.log(level+': '+msg);
|
||||
}
|
||||
}
|
||||
|
||||
const file = Bun.file("input.txt");
|
||||
|
||||
const input = await file.text();
|
||||
|
||||
function* findNumbers (schematic: string[]) {
|
||||
const matcher = /[0-9]+/g;
|
||||
|
||||
for (const i in schematic) {
|
||||
const line = schematic[i];
|
||||
for (let res = matcher.exec(line); res !== null; res = matcher.exec(line)) {
|
||||
const start = matcher.lastIndex - res[0].length;
|
||||
const end = matcher.lastIndex - 1;
|
||||
print('Debug',`"${res[0]}" at [${start}-${end}]`);
|
||||
yield {
|
||||
line: Number(i),
|
||||
index: start,
|
||||
len: res[0].length,
|
||||
num: Number(res[0])
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface Match {
|
||||
line: number,
|
||||
index: number,
|
||||
len: number,
|
||||
num: number,
|
||||
}
|
||||
|
||||
function isPartNumber (schematic: string[], match: Match): boolean {
|
||||
const width = schematic[0].length - 1;
|
||||
const height = schematic.length - 1;
|
||||
|
||||
let surroundings = "";
|
||||
|
||||
const start = Math.max(match.index-1, 0);
|
||||
const end = Math.min(match.index + match.len, width);
|
||||
|
||||
if (match.line > 0) {
|
||||
surroundings += schematic[match.line - 1].slice(start, end + 1) + " ";
|
||||
}
|
||||
|
||||
if (match.index > 0) {
|
||||
surroundings += schematic[match.line][start] + " ";
|
||||
}
|
||||
|
||||
if (match.index + match.len + 1 < width) {
|
||||
surroundings += schematic[match.line][end] + " ";
|
||||
}
|
||||
|
||||
if (match.line < height) {
|
||||
surroundings += schematic[match.line + 1].slice(start, end + 1) + " ";
|
||||
}
|
||||
|
||||
print('Debug', `surroundings: ${surroundings}`)
|
||||
|
||||
return /[^0-9. ]/g.test(surroundings);
|
||||
}
|
||||
|
||||
function main(): void {
|
||||
const lines = input.trim().split("\n");
|
||||
for (const line of lines) {
|
||||
print('Info', line);
|
||||
}
|
||||
const partNumbers = [];
|
||||
for (const match of findNumbers(lines)) {
|
||||
if (isPartNumber(lines,match)) {
|
||||
partNumbers.push(match.num);
|
||||
}
|
||||
}
|
||||
print('Debug', `Part Numbers: ${partNumbers}`)
|
||||
print('Answer', `${partNumbers.reduce((a,b)=>a+b)}`)
|
||||
}
|
||||
|
||||
main();
|
95
2023/03/part2.ts
Normal file
95
2023/03/part2.ts
Normal file
|
@ -0,0 +1,95 @@
|
|||
enum LogLevel {
|
||||
Answer,
|
||||
Info,
|
||||
Debug,
|
||||
}
|
||||
const LOG_LEVEL = LogLevel.Debug;
|
||||
|
||||
const print = (level: keyof typeof LogLevel, msg: string) => {
|
||||
if (LogLevel[level] <= LOG_LEVEL) {
|
||||
console.log(level+': '+msg);
|
||||
}
|
||||
}
|
||||
|
||||
const file = Bun.file("input.txt");
|
||||
|
||||
const input = await file.text();
|
||||
|
||||
function* findGears (schematic: string[]) {
|
||||
const matcher = /\*/g;
|
||||
|
||||
for (const i in schematic) {
|
||||
const line = schematic[i];
|
||||
for (let res = matcher.exec(line); res !== null; res = matcher.exec(line)) {
|
||||
const index = matcher.lastIndex - 1;
|
||||
const end = matcher.lastIndex - 1;
|
||||
print('Debug',`[${Number(i)+1},${index+1}]`);
|
||||
yield {
|
||||
line: Number(i),
|
||||
index,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface Match {
|
||||
line: number,
|
||||
index: number,
|
||||
}
|
||||
|
||||
function neighbours (schematic: string[], match: Match): number[] {
|
||||
const lastCol = schematic[0].length - 1;
|
||||
const lastRow = schematic.length - 1;
|
||||
|
||||
function inbounds(x,y) {
|
||||
return x >= 0 && y >= 0 && x <= lastCol && y <= lastRow;
|
||||
}
|
||||
|
||||
const matches = [];
|
||||
|
||||
for (let y of [match.line-1, match.line, match.line+1]) {
|
||||
let left = match.index-1;
|
||||
while (inbounds(left,y) && /[0-9]/.test(schematic[y][left])) {
|
||||
left--;
|
||||
};
|
||||
left = Math.max(left, 0);
|
||||
let right = match.index+1;
|
||||
while (inbounds(right,y) && /[0-9]/.test(schematic[y][right])) {
|
||||
right++;
|
||||
};
|
||||
right = Math.min(right, lastCol+1);
|
||||
|
||||
const substr = schematic[y].slice(left,right);
|
||||
const matcher = /[0-9]+/g;
|
||||
for (let res = matcher.exec(substr); res !== null; res = matcher.exec(substr)) {
|
||||
matches.push(...res)
|
||||
}
|
||||
}
|
||||
|
||||
print('Debug', `neighbours: ${matches}`);
|
||||
|
||||
return matches.map(Number);
|
||||
|
||||
}
|
||||
|
||||
function main(): void {
|
||||
const lines = input.trim().split("\n");
|
||||
for (const line of lines) {
|
||||
print('Info', line);
|
||||
}
|
||||
let total = 0;
|
||||
for (const match of findGears(lines)) {
|
||||
const nums = neighbours(lines,match)
|
||||
if (nums.length === 2) {
|
||||
const ratio = nums[0] * nums[1];
|
||||
print('Debug', `adding ${ratio}`);
|
||||
total += ratio;
|
||||
}
|
||||
}
|
||||
|
||||
print('Answer', total);
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
// too low: 83445843
|
|
@ -65,4 +65,5 @@ if (advent) {
|
|||
#### [2023](https://adventofcode.com/2023 "2023 puzzle calendar")
|
||||
1. [:star: :star:](https://adventofcode.com/2023/day/1 "see puzzle")
|
||||
2. [:star: :star:](https://adventofcode.com/2023/day/2 "see puzzle")
|
||||
3. [:star: :star:](https://adventofcode.com/2023/day/3 "see puzzle")
|
||||
|
||||
|
|
Loading…
Reference in a new issue