Skip to content

Commit

Permalink
Solution day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
MinThaMie committed Dec 6, 2023
1 parent 6bde244 commit d65ff92
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
36 changes: 34 additions & 2 deletions app/controllers/puzzles/6.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,45 @@ import PuzzlesBaseController from './base';
export default class Puzzles6Controller extends PuzzlesBaseController {
// BEGIN-SNIPPET day6-solution1
solve1(input) {
return 'Solution 1';
let [time, record] = input;
let possible = 1;
for (let i = 0; i < time.length; i++) {
let minH = (time[i] - Math.sqrt(time[i] * time[i] - 4 * record[i])) / 2;
let maxH = (time[i] + Math.sqrt(time[i] * time[i] - 4 * record[i])) / 2;
if (minH % 1 === 0) {
minH += 1;
} else {
minH = Math.ceil(minH);
}
if (maxH % 1 === 0) {
maxH = maxH - 1;
} else {
maxH = Math.floor(maxH);
}
possible *= maxH - minH + 1;
}
return possible;
}
// END-SNIPPET

// BEGIN-SNIPPET day6-solution2
solve2(input) {
return 'Solution 1';
let [time, record] = input;
time = parseInt(time.join(''));
record = parseInt(record.join(''));
let minH = (time - Math.sqrt(time * time - 4 * record)) / 2;
let maxH = (time + Math.sqrt(time * time - 4 * record)) / 2;
if (minH % 1 === 0) {
minH += 1;
} else {
minH = Math.ceil(minH);
}
if (maxH % 1 === 0) {
maxH = maxH - 1;
} else {
maxH = Math.floor(maxH);
}
return maxH - minH + 1;
}
// END-SNIPPET
}
4 changes: 3 additions & 1 deletion app/routes/puzzles/6.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 Puzzles6Route extends Route {
parseInput(file) {
return file.split('\n');
return file
.split('\n')
.map((line) => line.match(/\d+/g).map((n) => parseInt(n)));
}

async model() {
Expand Down
2 changes: 2 additions & 0 deletions public/inputs/day6/full-liulangzhe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 40 82 91 66
Distance: 277 1338 1349 1063
2 changes: 2 additions & 0 deletions public/inputs/day6/full-minthamie.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 60 94 78 82
Distance: 475 2138 1015 1650
2 changes: 2 additions & 0 deletions public/inputs/day6/intro.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200

0 comments on commit d65ff92

Please sign in to comment.