Skip to content

Commit

Permalink
Solution day 3 + input Day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
MinThaMie committed Dec 4, 2023
1 parent 19fd77a commit 1197dac
Show file tree
Hide file tree
Showing 7 changed files with 450 additions and 2 deletions.
88 changes: 86 additions & 2 deletions app/controllers/puzzles/3.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,98 @@ import PuzzlesBaseController from './base';

export default class Puzzles3Controller extends PuzzlesBaseController {
// BEGIN-SNIPPET day3-solution1
getBoundingBox(rowIndex, colIndex, length) {
let topLayer = [];
let middleLayer = [];
let bottomLayer = [];
middleLayer.push([rowIndex, colIndex - 1], [rowIndex, colIndex + length]);
for (let i = colIndex - 1; i <= colIndex + length; i++) {
topLayer.push([rowIndex - 1, i]);
bottomLayer.push([rowIndex + 1, i]);
}
return [...topLayer, ...middleLayer, ...bottomLayer];
}

solve1(input) {
return 'Solution 1';
let numbers = {};
let symbols = {};
input.map((row, rowIndex) => {
numbers[rowIndex] = {};
symbols[rowIndex] = {};
let results = row.matchAll(/(\d+)|([^0-9.\s]+)/g);
for (const result of results) {
const number = parseInt(result[0]);
const startIndex = result.index;
if (number) {
numbers[rowIndex][startIndex] = [number, result[0].length];
} else {
symbols[rowIndex][startIndex] = result[0];
}
}
});
let partNumbers = [];
for (const [rowIndex, numObj] of Object.entries(numbers)) {
if (Object.keys(numObj)) {
for (const [index, [number, length]] of Object.entries(numObj)) {
let bounding = this.getBoundingBox(parseInt(rowIndex), parseInt(index), parseInt(length));
for (let coor of bounding) {
if (symbols[coor[0]]?.[coor[1]]) {
partNumbers.push(number);
break;
}
}
}
}
}
return partNumbers.reduce(
(accumulator, currentValue) => accumulator + currentValue,
0,
);
}
// END-SNIPPET

// BEGIN-SNIPPET day3-solution2
solve2(input) {
return 'Solution 1';
let numbers = {};
let gears = {};
input.map((row, rowIndex) => {
numbers[rowIndex] = {};
gears[rowIndex] = {};
let results = row.matchAll(/(\d+)|([^0-9.\s]+)/g);
for (const result of results) {
const number = parseInt(result[0]);
const startIndex = result.index;
if (number) {
numbers[rowIndex][startIndex] = [number, result[0].length];
} else {
if(result[0] == '*'){
gears[rowIndex][startIndex] = [];
}
}
}
});
for (const [rowIndex, numObj] of Object.entries(numbers)) {
if (Object.keys(numObj)) {
for (const [index, [number, length]] of Object.entries(numObj)) {
let bounding = this.getBoundingBox(parseInt(rowIndex), parseInt(index), parseInt(length));
for (let coor of bounding) {
if (gears[coor[0]]?.[coor[1]]) {
gears[coor[0]][coor[1]].push(number);
break;
}
}
}
}
}
let solution = 0;
Object.values(gears).forEach((gearRow) => {
for (const [key, value] of Object.entries(gearRow)) {
if (value.length == 2) {
solution += value.reduce((m, n) => m * n);
}
}
});
return solution;
}
// END-SNIPPET
}
10 changes: 10 additions & 0 deletions public/inputs/day3/full-liulangzhe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
140 changes: 140 additions & 0 deletions public/inputs/day3/full-minthamie.txt

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions public/inputs/day3/intro.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
6 changes: 6 additions & 0 deletions public/inputs/day4/full-liulangzhe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
Loading

0 comments on commit 1197dac

Please sign in to comment.