start day 3

This commit is contained in:
alex 2023-12-10 19:26:12 -05:00
parent 191308edb3
commit 9a03d756b6
5 changed files with 598 additions and 0 deletions

140
2023/day_03/input.txt Normal file
View File

@ -0,0 +1,140 @@
.854...........................................................................362...........271...732........838.........24................
...*.............................117*...........459........767*648....#.........*...................................$...&..=................
....970.........368.124.+............57................653...........723.....366....*443..60.........536....441....45..879.....789...*......
...........749*...-...+..330.....................215%...*................725.....953........%.................*............639......331.419.
.......706.....59.............*....=...262.............678...........857.*............189....................912.+589.846*...*..............
..........*................393.59..418....*................151...........594...........*....584*258.......................17.997............
.......774....275........................777........841..............988.........121....908.........86.....482..295....................@....
.110..........%.....605..@.......305*18...............*....740.............840..*................52*.......*...........100...........250....
...........*..............856..............412.........761....*....344....*.....505.....................121......709...-....-...............
......980.269..189+............701..............22...$........343..*...684..............-375......223........97.*..........890......443.....
..904*..................555.....*.......38.....*....207............773.......................824.....*.......*..../....618..................
....................355*........362....*......442.$..........613...........%770.......526.....*.......418.....283..76....*....862...........
...450....*292..............735.....149...........143..........*...458...........279.........43..769...................$..48.....*..........
....*..413.........983*....*.................679.......642....214...#........107*......=.........*.....%...........276.39......196.889*624..
...128.................658.515...890#...................../...........................60..../....528..359.....451...*.....56................
...................719...................525.................$502............284-........@...489.................#..287.....@....+..........
...849...............*................../.........................282.+649...............217............240.....................833.....930.
.......666...179.....986.526................./927......73.....176*...............@...459...........=946..*..488...126.....*.................
..........@.....*549.....@...261.....370*951...........+.............114......170.......@...............269............471.891.$859.........
.............................../.................646.....-.327........*..237..............452......../......*620..713...............324.....
..................=716.....753....................-....431..=.......692.%.......210.......*.......860....208...............81*655....$......
......298....17*..........+....................................................*..........603................950.218.$......................
................692...54......+778.809........140..*.........249............%.332..............*.........372*......*.328.....=..............
.......421.............#..............+........*....493.......*..........589..................286...............254..........458...672...258
........*.....949.630.....725...609.........408.............108..............304.517......829.........666*..........724.182...........*.=...
.........435./....*..........*.*................580..................@.........*..&..147.-......221*......544..662/....*...........881......
..750.............561......568.487.@...935.......*....106...........104......808.......*............333...................&900..............
...................................287.$......930.....&.....*528.........332............755..................840../.............583.....209.
.371.299.................503.............................632................*.......&...................500........647..193.......-.........
....*........-902..2.123..*...912.......775......./...............241........734....90.968.513..66.566...................*..&...............
..................*....@.923...*.......*.....289..47.....785..710.......54................*......*....................767....452....=....597
.............348.362............607.....889.....#........*....*........*......$904................694...............................241.....
....955........*.....559*792............................307..25.........449..............*751.404.....134.....841...........................
......&.959...300............446.504.644....692...............................#.................-.....*.......*...758*35....................
.908$...*...........&..861...........=.....%....240............-..+..9.514...615.....&....-646.....538...144.751...........899.........545..
.......806.270*....946.@...............*..........*.555@....803..322.-............410..........514.................510..10..*.....858..-....
...............373....................7.918.....699.........................710.......&...........-.....78%............+....34..............
....653...............................................553.......896.......-......&...721....970.............................................
.......%...........379..377.....770@.................$.....126....*...712.287....951.........*......984*..........+.............+700........
..............924...*....*.............422...................*...233.*........................124...........902....181......................
................&....156..363.....*855....*.................884......509..879..755.......$...........*635......*........283.........661.....
..559..........................393.........766...789.............683..................461..#......910.........904.........$.%......*.....13.
.........=...568.........680/.......................*......*522.#..........................747...........*113................702...952......
....*....334...*...392..........910*...............233..189.........219.879............225......521...941...................................
.146.981......284.*.......*972......391.....789*...................................628*.....*....*................675.......................
..................921..151......................983...............582......983.690.......213.137.268.............*..............24$.........
....561....................600........106-...............226..................*.....996.............................681.....768.......852...
..............................%............................$................=...963..$.....@............421*305.......*.....*........$......
.....923.285*.....961...........*......$812......812..851.............678.29.......*......501.....228..............385...853...........251..
.444*...............*...998...99.711.............+.....*......330.....*.........287...........781..+..........%445..........................
......$....250=.....965...*.............................113...$......766....$.......941........-......658.............894........351........
......335...............463.328*.....411....%.............................981.113......*5.............*...773=.573&....*............$.......
..........%348..................460.....*5..517.......583.261.....889@.......................953.....496...............547............620...
...299.................367.......................+....*................852.442.796.........*.@...............=................834.....*.....
....*....772...487.......................#......938.462.956..41.643...*.....#.....*592....66...809.368..3..549........................772...
.566........*.....*320.....498..775.230.193....................*......961.........................*....*.......%283.....406.......495.......
........292..585..............*.*.....*..........213...*309...................469..549......413........903................*....29..*........
.........*.......@.........551..806...489......................51.655.999*.......*.$.............21*.................300..931....*..595.....
......878......62../...........................871......169..=..&..*......235..788...497..........................68....*.......191.........
.144.........+.....31............-...............*..623*....428.....955...............*.............424....89....*......728.............*665
.............447...............530...203...955.336..........................227........665.......$.........*.....669..............473.......
835...................*323..............*./...........=.....24..........*.........................452.......9.............263.........570...
......568...803@.....................152.......209.933.....*....877..667.895............548...963......................................*....
.......$.........=............................*...........948..-...................171.*........*.605.765..-........90..............992.....
..360..........#.476...........................564../.............755*...............+..709...11.....*......909..........696..124%..........
........74...73................320.........658......739...=...........484...860*....................................314.....................
...=509...*.........79.290........*...222...*.............46...940..............960.....473.....320..........$681......*591..269....../.....
........887.94@......*..*.........302...+.784.554+............+......#......455........./............891..........457........*...609.120....
....................874.246.....................................+.....228..*....&.........272....%..$.....*377.....*..52..604....%..........
.....44.....870*457................905.45......58...............956.........935.551...807..$.....21....356.......317....................%...
.........=...................267......*...300=.........543..............398..............#.................$.......................386...745
..........145......+............................*......*...........7....$.............64...........646....675..1..930..770.....791.$........
................744....462......443&......*389..575..965...805....+..14.......761*578..-...#..776...*..................-........-.......822.
....668.484...............*............875.................*.........*....................227...*..908.............215......*.......270.....
.......*......771..........318....883......881...........655.@26..130..799.......876..91......28...........699.........807...510............
.............-........391*.........*.......+......903..................*.....379.*....@...........713........#............*.................
..........+................196..230...........411....*965..682.......873....$.....916....755....&..=.....................296......+958.983..
...*.....718.......279........%...............................*.271.............@.......*......932.......621..637.843..................*....
..59..........%2...........................................400...*...142.870...746......256..........865*...........%...=.....%...409...761.
............@...........122*157...........394........%.444.....207...-......*.........................................434..827...*..........
.....266..19.....=..............624.......*........398....*................373...........418...16.....654.878*.................890..........
...............487..........884....*......199.227........723......914*411...........228..........%...................872................-...
.....937..608.................*....306........$....................................*..................522..309.@16.....*.............765....
......#....................128../.................%136....140.................773...114...........33....*....*.........615.....303..........
.249.....590..$...949..768.......380......%...996..............833...28...310.#...................*..338.....770...559......................
...+.854.../.678..*....................646...*.........................=...&..............#.....422...................#.231...714.122.......
......*...........552........................741..........+.......437........455*768..134..69..........*........519.......*........*........
.......976.............466......497$.............607......783........*....*.............@..............28........-.........279......394.....
............%....857...*....542.........915.543..$...............979...581.66......555....................331...................281.........
....715......542..#..20....@........241...*...*.........701.......$...........113...../...193..96*305........&................/...#...489...
219*...................................#.599...845.....*.....................$....*.........*...................$....996...240........*.....
..........151.520...554.............................665..........................1.398.=450..426.....198.....326.....+..........-....129....
.........*...........*.................687..............588....62..........504................................................970...........
..........463.....776......804............*..............*.....*..........%....90/...834.790...514...740.770.......667......................
....392..................................371...184........906.56....575.............*.............*.........*......*......435.....308.......
....*..........*......908..........497...........................-.........884....849............374.....620....727......&........../.......
.125........862.326......&........+..........497.....&811.......213.......*...........465....................64.....993.....................
......................%.....288...............*...-.......617...........-..677........*.....32..........-907.+........*.....=....48.475.....
.........787.........400.....*.......420....884.425......*.....420...727........#27..181.....*....414..............986......114./.....#..404
........*.......%...........596....=.*....................863................$................428..*......899.....................114.......
.595=.112.737.699................382.888..........................315...@...325..........480........115..*.........-......368...............
...........*......+.@......652.*...............................&.....@..182.......219$.........945+.............844...........996....744*...
..87......749..535...395..#....339..564.....813....410.........135...........330.......828...........585......................*..........870
...+........................$........+..967*.........*....921......321......%................612@....*......................44..............
.......764...716............818..327..............578.......*...../...........941..................736..@421........762.........282.........
861#......*..@.......982..........*.........+...............714.................*..867.....863........................@....614....=.........
........549................*....260.........91................................85.....$........*.803*895..622.$..............*.........684...
............770*346.....714.652......145.........270...286..........#.......................66..........*.....337.176.813...483...223*......
..........................................*..392..&.........505...957.@.........158...285......881#....497.........+...*....................
.........=../..../.....588........227....639............741*..........443..681..........+..153........................335...................
........201.671.96......$............*.....................................*........415.......*........-.......155...........848.58&........
............................922......92...........180..........................&39...%....208..389......696.....*............=.......883.852
....237.........950.........*............80..............................................*....................657.......................*...
....../..611.......*.....671...443...971.&....366....381...442....458...$.......589.343.397.....463.......60.......&........125......&......
..853...../..945..564...........*......*.......@......-................783..........*.......................*.......28......=.........373...
................*.......+843..550..998.973.........+.....208.565..@..............231......292............+.365..172............+............
......117......615..................*............299............&..862.895............/86...*....114..552........*...697.....733...388......
.....................................212..261........885...............*...903..26*..........50.....-..........158................*.........
...../........11..360....748......................67....*272.....320...16.....*....137.905......239......470......................433.......
...141........*.....%......+......967........-...%...........243...*.......904............*....*..............88.377......619...............
................=.....-668....%.....=.+461..525.....662.....*......196..28..............778...940..400..682...*...*.........*..638*.........
779#..113........209...........744....................*...144..811....../...#......................-...*.....297..713.....50................
.........$................#.........844...6...........62.......*.............957...&.......112@.......211.............173....989............
..749.......$......989..931................*....&............424.........709.......867............590...................*.......*..645......
...*..290...573......*............437....227.....53......132............#....471%.............501..%..................733.....270.....=.....
..637...*............596............................=....*.......400...............*...........*....................................#.......
......958.....757%.........=.........228......624...746.81../.....*........../..718.198..38...184..............*820..774......./830.693.....
.........................453...405.=...=....................100.144.......728...........*...................498............$................
..616............................*.756...310.........#52.............130.......=........506.....509.779..........723.......182..+...........
...*....49.....-..............551..........*................789./.....*......306...613........./......*..491....*...............791..931....
...863.....%.72......832.653........*490....814.....984.....=...741...568..........*.................998........567........+........#.......
.........171..........*..-.......406...............*....389.....................486.....630............../...............457.576............
..............308..515..................373@......709..&..............*185..................689..........751........44..........*764........
..............*..............661....=.........*.............993....984.....*.......908......=...273............../.*........................
.......582..335...260....59......998../.....39.760.313*174...*..........@.183......................*47....&994.887.577........*464.....22...
......*...............................449..................802........762.....522..139+..154..266$......................46.323.....712*.....
....827.....................................35......600*29.....724........488............*...............149*227.........*..................
........@......278*............@..135..........594.........470*...........*....540*.......42..............................775.536..790......
.....990...........742......181....*..............*..@521................743.......552............310...*....+738.....................*.....
.......................125.......311............715........263...........................................173...................235....291...

10
2023/day_03/sample.txt Normal file
View File

@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

7
2023/day_03/sample2.txt Normal file
View File

@ -0,0 +1,7 @@
123.456...
...*......
..........
123*456...
..........
..123*....
......456

View File

@ -0,0 +1,31 @@
import path from "path";
import { readlines } from "../../_utils";
import { part1_solver, part2_solver } from "./solution";
describe("day 3: gear ratios, pt 1", () => {
test("sample input", async () => {
const sample = await readlines(path.resolve(__dirname, "./sample.txt"));
expect(part1_solver(sample)).toBe(4361);
});
test("submission input", async () => {
const input = await readlines(path.resolve(__dirname, "./input.txt"));
expect(part1_solver(input)).toBe(553825);
});
});
describe("day 3: gear ratios, pt 2", () => {
test("sample input", async () => {
const sample = await readlines(path.resolve(__dirname, "./sample.txt"));
expect(part2_solver(sample)).toBe(467835);
const sample2 = await readlines(path.resolve(__dirname, "./sample2.txt"));
expect(part2_solver(sample2)).toBe(168264);
});
test("submission input", async () => {
const input = await readlines(path.resolve(__dirname, "./input.txt"));
expect(part2_solver(input)).toBe(82962801);
});
});

410
2023/day_03/solution.ts Normal file
View File

@ -0,0 +1,410 @@
/*
*/
import { sum } from "../../_utils";
function isDigit(char: string): boolean {
return /^\d$/.test(char);
}
function isSymbol(char: string): boolean {
return !isDigit(char) && char !== ".";
}
function getCharAt(
m: string[],
row: number,
column: number,
fallback: string = "."
): string {
if (typeof m[row] !== "undefined") {
return m[row][column] || fallback;
} else {
return fallback;
}
}
export function part1_solver(lines: string[]): number {
// approach:
// 1. assume all numbers are invalid at first (pending)
// 2. continuously check the pending numbers to see if they can be moved to valid (a symbol is found)
// 3. once the iterator is out of range of a number, move it to invalid list
let valid_part_numbers = [];
lines.forEach((line, row) => {
// const elements = line.split(".");
let column = 0;
let buffer = "";
let is_valid = false;
while (column <= line.length) {
const current_char = line[column];
if (isDigit(current_char)) {
buffer += current_char;
// check previous row
const up_left = getCharAt(lines, row - 1, column - 1);
const up_mid = getCharAt(lines, row - 1, column);
const up_right = getCharAt(lines, row - 1, column + 1);
const left = getCharAt(lines, row, column - 1);
const right = getCharAt(lines, row, column + 1);
const down_left = getCharAt(lines, row + 1, column - 1);
const down_mid = getCharAt(lines, row + 1, column);
const down_right = getCharAt(lines, row + 1, column + 1);
if (!is_valid) {
is_valid =
isSymbol(up_left) ||
isSymbol(up_mid) ||
isSymbol(up_right) ||
isSymbol(left) ||
isSymbol(right) ||
isSymbol(down_left) ||
isSymbol(down_mid) ||
isSymbol(down_right);
}
if (isDigit(right)) {
column += 1;
continue;
} else if (isSymbol(right)) {
valid_part_numbers.push(buffer);
is_valid = false;
buffer = "";
} else {
if (is_valid) {
valid_part_numbers.push(buffer);
}
is_valid = false;
buffer = "";
}
} else {
buffer = "";
is_valid = false;
column += 1;
continue;
}
column += 1;
}
});
return valid_part_numbers
.map((nstr) => +nstr)
.filter((n) => !isNaN(n))
.reduce(sum, 0);
}
/*
*/
type Element = {
kind: "number" | "gear";
value: string;
position: [number, number];
};
type Gear = {
n1: number;
n2: number;
};
const checkColumsFor = (
lines: string[],
row: number,
columns: number[]
): number[] => {
let matched_nums = [];
columns.forEach((column) => {
if (isDigit(getCharAt(lines, row, column))) {
// The target value was a digit. There are two cases:
// 1. The digit belongs to the same number we've seen before. E.g.
// ...123.......
// ....*........
// 2. The digit belongs to a new number on that row. E.g.
// ...123.456...
// ......*......
} else {
// The target value was not a digit, so ignore
}
});
// columns.map(col => isDigit(getCharAt(lines, row, col))).every()
return columns.reduce((matched_nums, column) => {
if (isDigit(getCharAt(lines, row, column))) {
}
}, []);
};
const checkTopRow = (
lines: string[],
currentRow: number,
currentColumn: number
): number[] => {
const row = currentRow - 1;
const column = currentColumn;
let adjacent_nums = [];
const up_left = getCharAt(lines, row, currentColumn - 1);
const up_mid = getCharAt(lines, row, currentColumn);
const up_right = getCharAt(lines, row, currentColumn + 1);
if (isDigit(up_left) || isDigit(up_mid) || isDigit(up_right)) {
console.log("found on prev row");
// get all the nums from the previous row
const nums = (lines[row - 1] || "")
.split(".")
.filter((s) => s.length > 0)
.map((s) =>
s
.split("")
.filter((c) => isDigit(c))
.join("")
);
// figure out which num overlaps this gear
const res1 = nums.find((n) => {
const foundNumIndex = lines[row - 1].indexOf(n);
const lastDigit = foundNumIndex + n.length - 1;
console.log(JSON.stringify({ foundNumIndex, lastDigit, column }));
return (
foundNumIndex === column - 1 ||
foundNumIndex === column ||
foundNumIndex === column + 1 ||
lastDigit === column - 1 ||
lastDigit === column ||
lastDigit === column + 1 ||
(column - 1 >= foundNumIndex && column + 1 <= lastDigit)
);
});
if (res1) {
adjacent_nums.push(res1);
const res2 = nums.slice(nums.indexOf(res1)).find((n) => {
const foundNumIndex = lines[row - 1].indexOf(n);
const lastDigit = foundNumIndex + n.length - 1;
console.log(JSON.stringify({ foundNumIndex, lastDigit, column }));
return (
foundNumIndex === column - 1 ||
foundNumIndex === column ||
foundNumIndex === column + 1 ||
lastDigit === column - 1 ||
lastDigit === column ||
lastDigit === column + 1 ||
(column - 1 >= foundNumIndex && column + 1 <= lastDigit)
);
});
if (res2) {
adjacent_nums.push(res2);
}
console.log(
"res is " +
JSON.stringify({
res1,
res2,
nums,
last: lines[row - 1],
column,
})
);
}
}
return adjacent_nums;
};
const checkCurrentRow = (
lines: string[],
currentRow: number,
currentColumn: number
): number[] => {
const row = currentRow - 1;
const column = currentColumn;
let adjacent_nums = [];
const left = getCharAt(lines, row, column - 1);
const right = getCharAt(lines, row, column + 1);
if (isDigit(left) || isDigit(right)) {
console.log("found on this row");
// get all the nums from this row
const nums = lines[row]
.split(".")
.filter((s) => s.length > 0)
.map((s) =>
s
.split("")
.filter((c) => isDigit(c))
.join("")
);
// figure out which num overlaps this gear
const res1 = nums.find((n) => {
const foundNumIndex = lines[row].indexOf(n);
const lastDigit = foundNumIndex + n.length - 1;
return (
foundNumIndex === column - 1 ||
foundNumIndex === column ||
foundNumIndex === column + 1 ||
lastDigit === column - 1 ||
lastDigit === column ||
lastDigit === column + 1 ||
(column - 1 >= foundNumIndex && column + 1 <= lastDigit)
);
});
if (res1) {
adjacent_nums.push(res1);
const res2 = nums.find((n) => {
const foundNumIndex = lines[row].indexOf(n);
const lastDigit = foundNumIndex + n.length - 1;
return (
foundNumIndex === column - 1 ||
foundNumIndex === column ||
foundNumIndex === column + 1 ||
lastDigit === column - 1 ||
lastDigit === column ||
lastDigit === column + 1 ||
(column - 1 >= foundNumIndex && column + 1 <= lastDigit)
);
});
if (res2) {
adjacent_nums.push(res2);
}
}
}
return adjacent_nums;
};
const checkBottomRow = (
lines: string[],
currentRow: number,
currentColumn: number
): number[] => {
const row = currentRow - 1;
const column = currentColumn;
let adjacent_nums = [];
const down_left = getCharAt(lines, row + 1, column - 1);
const down_mid = getCharAt(lines, row + 1, column);
const down_right = getCharAt(lines, row + 1, column + 1);
if (isDigit(down_left) || isDigit(down_mid) || isDigit(down_right)) {
console.log("found on next row");
// get all the nums from the next row
const nums = lines[row + 1]
.split(".")
.filter((s) => s.length > 0)
.map((s) =>
s
.split("")
.filter((c) => isDigit(c))
.join("")
);
// figure out which num overlaps this gear
const res1 = nums.find((n) => {
// [6 7 8 9]
// [0 1 2 3]
const foundNumIndex = lines[row + 1].indexOf(n);
const lastDigitIndex = foundNumIndex + n.length - 1;
return (
foundNumIndex === column - 1 ||
foundNumIndex === column ||
foundNumIndex === column + 1 ||
lastDigitIndex === column - 1 ||
lastDigitIndex === column ||
lastDigitIndex === column + 1 ||
(column - 1 >= foundNumIndex && column + 1 <= lastDigitIndex)
);
});
if (res1) {
adjacent_nums.push(res1);
const res2 = nums.find((n) => {
// [6 7 8 9]
// [0 1 2 3]
const foundNumIndex = lines[row + 1].indexOf(n);
const lastDigitIndex = foundNumIndex + n.length - 1;
return (
foundNumIndex === column - 1 ||
foundNumIndex === column ||
foundNumIndex === column + 1 ||
lastDigitIndex === column - 1 ||
lastDigitIndex === column ||
lastDigitIndex === column + 1 ||
(column - 1 >= foundNumIndex && column + 1 <= lastDigitIndex)
);
});
if (res2) {
adjacent_nums.push(res2);
}
}
}
return adjacent_nums;
};
export function part2_solver(lines: string[]): number {
let gears: Gear[] = [];
lines.forEach((line, row) => {
let column = 0;
let adjacent_nums = [];
while (column <= line.length) {
const current_char = line[column];
if (current_char === "*") {
const top_nums = checkTopRow(lines, row, column);
adjacent_nums.push(top_nums);
const current_nums = checkCurrentRow(lines, row, column);
adjacent_nums.push(current_nums);
const bottom_nums = checkBottomRow(lines, row, column);
adjacent_nums.push(bottom_nums);
console.log(JSON.stringify({ adjacent_nums }));
if (adjacent_nums.length === 2) {
console.log("found a gear!");
gears.push({ n1: +adjacent_nums[0], n2: +adjacent_nums[1] });
} else {
adjacent_nums = [];
}
} else {
adjacent_nums = [];
column += 1;
continue;
}
column += 1;
}
});
return gears
.map((g) => g.n1 * g.n2)
.filter((n) => !isNaN(n))
.reduce(sum, 0);
}