Skip to content

Commit

Permalink
Solution part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
MinThaMie committed Dec 13, 2023
1 parent ae3dbc6 commit 24c88af
Show file tree
Hide file tree
Showing 16 changed files with 4,082 additions and 48 deletions.
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
154 changes: 152 additions & 2 deletions app/controllers/puzzles/13.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,164 @@ 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]);
}

checkForMirror(array, indexA, indexB, point) {
let compareA = array[indexA];
let compareB = array[indexB];
let reflectionPoint = point;
if (this.isEqual(compareA, compareB)) {
if (reflectionPoint == undefined) {
reflectionPoint = indexB;
}
if (indexA - 1 < 0 || indexB + 1 > array.length - 1) {
return reflectionPoint;
} else {
return this.checkForMirror(
array,
indexA - 1,
indexB + 1,
reflectionPoint,
);
}
} else if (reflectionPoint) {
return this.checkForMirror(
array,
reflectionPoint,
reflectionPoint + 1,
undefined,
);
} else if (indexB + 1 !== array.length) {
return this.checkForMirror(array, indexA + 1, indexB + 1, undefined);
} else {
return false;
}
}

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

// BEGIN-SNIPPET day13-solution2
couldSmudge(a, b) {
// // console.log(a, b);
// console.log(a.filter((n, i) => n !== b[i]));
return a.filter((n, i) => n !== b[i]).length == 1;
}
checkForMirrorWithSmudge(array, indexA, indexB, point, smudge) {
let compareA = array[indexA];
let compareB = array[indexB];
let reflectionPoint = point;
// console.log(indexA, indexB, point, smudge);
if (this.isEqual(compareA, compareB)) {
// console.log("equal", indexA, indexB);
if (reflectionPoint == undefined) {
reflectionPoint = indexB;
}
if (indexA - 1 < 0 || indexB + 1 > array.length - 1) {
return [reflectionPoint, smudge];
} else {
return this.checkForMirrorWithSmudge(
array,
indexA - 1,
indexB + 1,
reflectionPoint,
smudge,
);
}
} else if (!smudge) {
// console.log("could smudge", indexA, indexB);
if (this.couldSmudge(compareA, compareB)) {
// console.log("smudge");
if (reflectionPoint == undefined) {
reflectionPoint = indexB;
}
if (indexA - 1 < 0 || indexB + 1 > array.length - 1) {
return [reflectionPoint, true];
} else {
return this.checkForMirrorWithSmudge(
array,
indexA - 1,
indexB + 1,
reflectionPoint,
true,
);
}
} else if (indexB + 1 !== array.length) {
return this.checkForMirrorWithSmudge(array, indexA + 1, indexB + 1, undefined, smudge);
} else {
return [false, false];
}
} else if (reflectionPoint) {
return this.checkForMirrorWithSmudge(
array,
reflectionPoint,
reflectionPoint + 1,
undefined,
smudge,
);
} else if (indexB + 1 !== array.length) {
return this.checkForMirrorWithSmudge(array, indexA + 1, indexB + 1, undefined, smudge);
} else {
return [false, false];
}
}
solve2(input) {
return 'Solution 1';
let solution = 0;
let stuff = [];
input.forEach((valley, i) => {
console.log(i);
let [res, smudge] = this.checkForMirrorWithSmudge(valley, 0, 1, undefined, false);
// console.log(res, smudge, valley.length, this.checkForMirror(valley, 0, 1, undefined));
// if (res > 0 && res == this.checkForMirror(valley, 0, 1, undefined) && res < valley.length - 1) {
// console.log("res is same, no smudge", res);
// [res, smudge] = this.checkForMirrorWithSmudge(valley, res, res + 1, undefined, false)
// }
if (smudge) {
console.log("hor");
stuff.push(i);
solution += 100 * res;
} else {
let [verres, versmudge] = this.checkForMirrorWithSmudge(
this.transposeMatrix(valley),
0,
1,
undefined,
false,
);
// console.log(verres, versmudge, this.checkForMirror(this.transposeMatrix(valley), 0, 1, undefined));
// if (verres == this.checkForMirror(this.transposeMatrix(valley), 0, 1, undefined)) {
// console.log("vres is same, no smudge", verres);
// [verres, versmudge] = this.checkForMirrorWithSmudge(this.transposeMatrix(valley), verres, verres + 1, undefined, false)
// }
if (versmudge) {
console.log("ver");
stuff.push(i);
solution += verres;
} else {
console.log("did not do", i, res, verres, this.checkForMirror(valley, 0, 1, undefined), this.checkForMirror(this.transposeMatrix(valley), 0, 1, undefined) )
}
}
});
console.log(stuff);
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

0 comments on commit 24c88af

Please sign in to comment.