Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 13 #1

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion app/controllers/puzzles/12.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@ import PuzzlesBaseController from './base';
export default class Puzzles12Controller extends PuzzlesBaseController {
// BEGIN-SNIPPET day12-solution1
solve1(input) {
return 'Solution 1';
let solution = 0;
input.forEach(([springs, cont]) => {
let springLength = springs.length - 1;
springs.map((a) => (springLength += a.length));
let contLength = cont.reduce((a, b) => a + b) + cont.length - 1;
if (springLength == contLength) {
solution += 1;
} else if (springs.length == cont.length) {
// console.log(springs, cont);
let toCheckSprings = springs.filter((spring, i) => {
return spring.length !== cont[i];
});
let toCheckCont = cont.filter((cont, i) => {
return springs[i].length !== cont;
});
console.log(toCheckSprings, toCheckCont);
} else {
console.log('rest');
}
});
return solution;
}
// END-SNIPPET

Expand Down
94 changes: 92 additions & 2 deletions app/controllers/puzzles/13.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,104 @@ import PuzzlesBaseController from './base';

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

isEqual(a, b) {
return a.every((val, index) => val === b[index]);
}

findMirror(valley) {
let result;
for (let j = 1; j < valley.length; j++) {
let mirrorFound = true;
for (let i = 0; i + j < valley.length && j - i - 1 >= 0; i++) {
if (!this.isEqual(valley[j + i], valley[j - i - 1])) {
mirrorFound = false;
break;
}
}
if (mirrorFound) {
result = 100 * j;
}
}
valley = this.transposeMatrix(valley);
for (let j = 1; j < valley.length; j++) {
let mirrorFound = true;
for (let i = 0; i + j < valley.length && j - i - 1 >= 0; i++) {
if (!this.isEqual(valley[j + i], valley[j - i - 1])) {
mirrorFound = false;
break;
}
}
if (mirrorFound) {
result = j;
}
}
return result;
}

solve1(input) {
return 'Solution 1';
let solution = 0;
input.forEach((valley, i) => {
solution += this.findMirror(valley);
});
return solution;
}
// END-SNIPPET

// BEGIN-SNIPPET day13-solution2
findMirrorSmudge(valley) {
let mirrors = [];
for (let j = 1; j < valley.length; j++) {
let mirrorFound = true;
for (let i = 0; i + j < valley.length && j - i - 1 >= 0; i++) {
if (
!this.isEqual(valley[j + i], valley[j - i - 1]) &&
!this.couldSmudge(valley[j + i], valley[j - i - 1])
) {
mirrorFound = false;
break;
}
}
if (mirrorFound) {
if (j !== this.findMirror(valley) / 100) {
mirrors.push(j * 100);
}
}
}
valley = this.transposeMatrix(valley);
for (let j = 1; j < valley.length; j++) {
let mirrorFound = true;
for (let i = 0; i + j < valley.length && j - i - 1 >= 0; i++) {
if (
!this.isEqual(valley[j + i], valley[j - i - 1]) &&
!this.couldSmudge(valley[j + i], valley[j - i - 1])
) {
mirrorFound = false;
break;
}
}
if (mirrorFound) {
if (j !== this.findMirror(valley) / 100) {
mirrors.push(j);
}
}
}
return mirrors[0];
}

couldSmudge(a, b) {
return a.filter((n, i) => n !== b[i]).length == 1;
}

solve2(input) {
return 'Solution 1';
let solution = 0;
input.forEach((valley) => {
solution += this.findMirrorSmudge(valley);
});
return solution;
}
// END-SNIPPET
}
8 changes: 2 additions & 6 deletions app/controllers/puzzles/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ export default class PuzzlesBaseController extends Controller {
return htmlSafe(
`<ul><li>MinThaMie's solution: ${this.solve1(
this.model.fullMinThaMie,
)}</li><li>LiuLangZhe's solution: ${this.solve1(
this.model.fullLiuLangzhe,
)}</li></ul>`,
)}</li><li>LiuLangZhe's solution: </li></ul>`,
);
}
get example2() {
Expand All @@ -21,9 +19,7 @@ export default class PuzzlesBaseController extends Controller {
return htmlSafe(
`<ul><li>MinThaMie's solution: ${this.solve2(
this.model.fullMinThaMie,
)}</li><li>LiuLangZhe's solution: ${this.solve2(
this.model.fullLiuLangzhe,
)}</li></ul>`,
)}</li><li>LiuLangZhe's solution: </li></ul>`,
);
}
}
7 changes: 6 additions & 1 deletion app/routes/puzzles/12.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import Route from '@ember/routing/route';

export default class Puzzles12Route extends Route {
parseInput(file) {
return file.split('\n');
return file.split('\n').map((line) => {
let [springs, cont] = line.split(' ');
springs = springs.split('.').filter((res) => res !== '');
cont = cont.split(',').map((n) => parseInt(n));
return [springs, cont];
});
}

async model() {
Expand Down
4 changes: 3 additions & 1 deletion app/routes/puzzles/13.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import Route from '@ember/routing/route';

export default class Puzzles13Route extends Route {
parseInput(file) {
return file.split('\n');
return file
.split('\n\n')
.map((valley) => valley.split('\n').map((line) => line.split('')));
}

async model() {
Expand Down
Loading