diff --git a/app/controllers/puzzles/6.js b/app/controllers/puzzles/6.js index b445721..1ee98ac 100644 --- a/app/controllers/puzzles/6.js +++ b/app/controllers/puzzles/6.js @@ -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 } diff --git a/app/routes/puzzles/6.js b/app/routes/puzzles/6.js index 2df765d..7520892 100644 --- a/app/routes/puzzles/6.js +++ b/app/routes/puzzles/6.js @@ -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() { diff --git a/public/inputs/day6/full-liulangzhe.txt b/public/inputs/day6/full-liulangzhe.txt index e69de29..004fb1d 100644 --- a/public/inputs/day6/full-liulangzhe.txt +++ b/public/inputs/day6/full-liulangzhe.txt @@ -0,0 +1,2 @@ +Time: 40 82 91 66 +Distance: 277 1338 1349 1063 \ No newline at end of file diff --git a/public/inputs/day6/full-minthamie.txt b/public/inputs/day6/full-minthamie.txt index e69de29..9b4e8f0 100644 --- a/public/inputs/day6/full-minthamie.txt +++ b/public/inputs/day6/full-minthamie.txt @@ -0,0 +1,2 @@ +Time: 60 94 78 82 +Distance: 475 2138 1015 1650 \ No newline at end of file diff --git a/public/inputs/day6/intro.txt b/public/inputs/day6/intro.txt index e69de29..b39f49d 100644 --- a/public/inputs/day6/intro.txt +++ b/public/inputs/day6/intro.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 \ No newline at end of file