Skip to content

Commit

Permalink
Solve Day 8 part 1, Day 9, Day 10 & Day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
MinThaMie committed Dec 12, 2023
1 parent 815dd18 commit ae3dbc6
Show file tree
Hide file tree
Showing 19 changed files with 1,641 additions and 13 deletions.
142 changes: 139 additions & 3 deletions app/controllers/puzzles/10.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,152 @@
/* eslint-disable no-unused-vars */
import PuzzlesBaseController from './base';

export default class Puzzles10Controller extends PuzzlesBaseController {
// BEGIN-SNIPPET day10-solution1
isValidPipe([curX, curY], pipe, [pipeX, pipeY]) {
switch (pipe) {
case '|':
return (
`${pipeX}.${pipeY}` == `${curX}.${curY + 1}` ||
`${pipeX}.${pipeY}` == `${curX}.${curY - 1}`
);
case '-':
return (
`${pipeX}.${pipeY}` == `${curX + 1}.${curY}` ||
`${pipeX}.${pipeY}` == `${curX - 1}.${curY}`
);
case 'L':
return (
`${pipeX}.${pipeY}` == `${curX - 1}.${curY}` ||
`${pipeX}.${pipeY}` == `${curX}.${curY + 1}`
);
case 'F':
return (
`${pipeX}.${pipeY}` == `${curX - 1}.${curY}` ||
`${pipeX}.${pipeY}` == `${curX}.${curY - 1}`
);
case '7':
return (
`${pipeX}.${pipeY}` == `${curX + 1}.${curY}` ||
`${pipeX}.${pipeY}` == `${curX}.${curY - 1}`
);
case 'J':
return (
`${pipeX}.${pipeY}` == `${curX + 1}.${curY}` ||
`${pipeX}.${pipeY}` == `${curX}.${curY + 1}`
);
default:
}
}

nextPipe([curX, curY], pipe, [pipeX, pipeY]) {
switch (pipe) {
case '|':
return `${pipeX}.${pipeY}` == `${curX}.${curY + 1}`
? `${pipeX}.${pipeY + 1}`
: `${pipeX}.${pipeY - 1}`;
case '-':
return `${pipeX}.${pipeY}` == `${curX + 1}.${curY}`
? `${pipeX + 1}.${pipeY}`
: `${pipeX - 1}.${pipeY}`;
case 'L':
return `${pipeX}.${pipeY}` == `${curX}.${curY + 1}`
? `${pipeX + 1}.${pipeY}`
: `${pipeX}.${pipeY - 1}`;
case 'F':
return `${pipeX}.${pipeY}` == `${curX - 1}.${curY}`
? `${pipeX}.${pipeY + 1}`
: `${pipeX + 1}.${pipeY}`;
case '7':
return `${pipeX}.${pipeY}` == `${curX + 1}.${curY}`
? `${pipeX}.${pipeY + 1}`
: `${pipeX - 1}.${pipeY}`;
case 'J':
return `${pipeX}.${pipeY}` == `${curX + 1}.${curY}`
? `${pipeX}.${pipeY - 1}`
: `${pipeX - 1}.${pipeY}`;
default:
}
}
steps = [];
solve1(input) {
return 'Solution 1';
let [starting, coordinates] = input;
let [startX, startY] = starting.split('.').map((n) => parseInt(n));
let fromStartPipes = [
[coordinates[`${startX - 1}.${startY}`], startX - 1, startY],
[coordinates[`${startX}.${startY - 1}`], startX, startY - 1],
[coordinates[`${startX}.${startY + 1}`], startX, startY + 1],
[coordinates[`${startX + 1}.${startY}`], startX + 1, startY],
];
let nextPipe = '';
let prevPipe = [];
this.steps = [];
fromStartPipes.forEach((set) => {
let [pipe, pipeX, pipeY] = set;
if (pipe) {
if (this.isValidPipe([startX, startY], pipe, [pipeX, pipeY])) {
nextPipe = this.nextPipe([startX, startY], pipe, [pipeX, pipeY]);
this.steps.push(`${pipeX}.${pipeY}`, nextPipe);
prevPipe = [pipeX, pipeY];
}
}
});
while (nextPipe !== starting) {
let coors = nextPipe.split('.').map((n) => parseInt(n));
nextPipe = this.nextPipe(prevPipe, coordinates[nextPipe], coors);
prevPipe = [coors[0], coors[1]];
this.steps.push(nextPipe);
}
return this.steps.length / 2;
}
// END-SNIPPET

// BEGIN-SNIPPET day10-solution2
isWithinBoundaries([x, y], coordinates, maxY) {
let count = 0;
let hasStarted = '';
if (this.steps.includes(`${x}.${y}`)) {
return false;
}
for (let i = y + 1; i <= maxY; i++) {
if (
coordinates[`${x}.${i}`] == '|' ||
!this.steps.includes(`${x}.${i}`)
) {
// Do nothing
} else if (coordinates[`${x}.${i}`] == '-') {
count++;
} else {
// 7 & F begin J & L end
if (
coordinates[`${x}.${i}`] == '7' ||
coordinates[`${x}.${i}`] == 'F'
) {
hasStarted = coordinates[`${x}.${i}`];
} else {
let closing = coordinates[`${x}.${i}`];
if (
(hasStarted == '7' && closing == 'L') ||
(hasStarted == 'F' && closing == 'J')
) {
count++;
}
}
}
}
return count % 2 !== 0;
}

solve2(input) {
return 'Solution 1';
let [, coordinates, maxX, maxY] = input;
let inside = 0;
for (let y = 0; y <= maxY; y++) {
for (let x = 0; x <= maxX; x++) {
if (this.isWithinBoundaries([x, y], coordinates, maxY)) {
inside++;
}
}
}
return inside;
}
// END-SNIPPET
}
117 changes: 115 additions & 2 deletions app/controllers/puzzles/11.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,127 @@ import PuzzlesBaseController from './base';

export default class Puzzles11Controller extends PuzzlesBaseController {
// BEGIN-SNIPPET day11-solution1
transposeMatrix(array) {
return array[0].map((_, colIndex) => array.map((row) => row[colIndex]));
}

pairs = (arr) =>
arr.map((v, i) => arr.slice(i + 1).map((w) => [v, w])).flat();

solve1(input) {
return 'Solution 1';
let expandedRows = [];
input.forEach((line) => {
if (line.every((x) => x === '.')) {
expandedRows.push(line, line);
} else {
expandedRows.push(line);
}
});
let vertical = this.transposeMatrix(expandedRows);
let expanded = [];
vertical.forEach((line) => {
if (line.every((x) => x === '.')) {
expanded.push(line, line);
} else {
expanded.push(line);
}
});
expanded = this.transposeMatrix(expanded);
let galaxies = [];
expanded.forEach((row, y) => {
row.forEach((cell, x) => {
if (cell == '#') {
galaxies.push([x, y]);
}
});
});
let galaxyPairs = this.pairs(galaxies);
let count = 0;
galaxyPairs.forEach(([[x1, y1], [x2, y2]]) => {
count += Math.abs(x1 - x2) + Math.abs(y1 - y2);
});
return count;
}
// END-SNIPPET

// BEGIN-SNIPPET day11-solution2
solve2(input) {
return 'Solution 1';
let expandedY = [];
input.forEach((line, y) => {
if (line.every((x) => x === '.')) {
expandedY.push(y);
}
});
let vertical = this.transposeMatrix(input);
let expandedX = [];
vertical.forEach((line, x) => {
if (line.every((x) => x === '.')) {
expandedX.push(x);
}
});
let galaxies = [];
input.forEach((row, y) => {
row.forEach((cell, x) => {
if (cell == '#') {
galaxies.push([x, y]);
}
});
});
let galaxyPairs = this.pairs(galaxies);
let count = 0;
galaxyPairs.forEach(([[x1, y1], [x2, y2]]) => {
let x1Index;
let x2Index;
let y1Index;
let y2Index;
for (let i = 0; i < expandedX.length; i++) {
if (x1 < expandedX[0]) {
x1Index = 0;
}
if (x1 > expandedX[i]) {
x1Index = i + 1;
}
if (x1 > expandedX.slice(-1)) {
x1Index = expandedX.length;
}
if (x2 < expandedX[0]) {
x2Index = 0;
}
if (x2 > expandedX[i]) {
x2Index = i + 1;
}
if (x2 > expandedX.slice(-1)) {
x2Index = expandedX.length;
}
}
for (let i = 0; i < expandedY.length; i++) {
if (y1 < expandedY[0]) {
y1Index = 0;
}
if (y1 > expandedY[i]) {
y1Index = i + 1;
}
if (y1 > expandedY.slice(-1)) {
y1Index = expandedY.length;
}
if (y2 < expandedY[0]) {
y2Index = 0;
}
if (y2 > expandedY[i]) {
y2Index = i + 1;
}
if (y2 > expandedY.slice(-1)) {
y2Index = expandedY.length;
}
}
let result =
Math.abs(x1 - x2) +
Math.abs(x1Index - x2Index) * (1000000 - 1) +
Math.abs(y1 - y2) +
Math.abs(y1Index - y2Index) * (1000000 - 1);
count += result;
});
return count;
}
// END-SNIPPET
}
38 changes: 36 additions & 2 deletions app/controllers/puzzles/8.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,47 @@ import PuzzlesBaseController from './base';
export default class Puzzles8Controller extends PuzzlesBaseController {
// BEGIN-SNIPPET day8-solution1
solve1(input) {
return 'Solution 1';
let [inst, network] = input;
inst = inst.split('');
let i = 0;
let steps = 0;
let curNode = 'AAA';
while (curNode !== 'ZZZ') {
if (inst[i] == 'L') {
curNode = network[curNode][0];
} else {
curNode = network[curNode][1];
}
i++;
steps++;
if (i == inst.length) {
i = 0;
}
}
return steps;
}
// END-SNIPPET

// BEGIN-SNIPPET day8-solution2
solve2(input) {
return 'Solution 1';
let [inst, network, endInA] = input;
inst = inst.split('');
let array = [];
for (const [key, value] of Object.entries(network)) {
array.push(key, ...value);
}
console.log(array);
// let firstZZZ = array.indexOf('ZZZ');
// let lastZZZ = array.lastIndexOf('ZZZ');
// console.log(firstZZZ, lastZZZ);
let zzzIndex = array.findIndex((num, idx, arr) => {
// Without the arr argument, there's no way to easily access the
// intermediate array without saving it to a variable.
console.log(num, idx);
return num == 'ZZZ' && idx % 3 == 0;
});
console.log(new Set(array), array.length);
return 'Solution 2';
}
// END-SNIPPET
}
27 changes: 25 additions & 2 deletions app/controllers/puzzles/9.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,37 @@ import PuzzlesBaseController from './base';

export default class Puzzles9Controller extends PuzzlesBaseController {
// BEGIN-SNIPPET day9-solution1
calculateNext(array) {
if (array.every((x) => x === array[0])) {
return array[0];
} else {
let newArray = [];
for (let i = 1; i < array.length; i++) {
newArray.push(array[i] - array[i - 1]);
}
return array[array.length - 1] + this.calculateNext(newArray);
}
}

solve1(input) {
return 'Solution 1';
return input.map((row) => this.calculateNext(row)).reduce((a, b) => a + b);
}
// END-SNIPPET

// BEGIN-SNIPPET day9-solution2
calculatePrev(array) {
if (array.every((x) => x === array[0])) {
return array[0];
} else {
let newArray = [];
for (let i = 1; i < array.length; i++) {
newArray.push(array[i] - array[i - 1]);
}
return array[0] - this.calculatePrev(newArray);
}
}
solve2(input) {
return 'Solution 1';
return input.map((row) => this.calculatePrev(row)).reduce((a, b) => a + b);
}
// END-SNIPPET
}
Loading

0 comments on commit ae3dbc6

Please sign in to comment.