From 3d7a54017e2a7b5f2789b1668f1430cbdf3106db Mon Sep 17 00:00:00 2001 From: Anne-Greeth van Herwijnen Date: Mon, 4 Dec 2023 17:14:48 +0100 Subject: [PATCH] Day 4 --- app/controllers/puzzles/4.js | 22 ++++++++++++++++++++-- app/routes/puzzles/4.js | 7 ++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/controllers/puzzles/4.js b/app/controllers/puzzles/4.js index e47709f..e2249b9 100644 --- a/app/controllers/puzzles/4.js +++ b/app/controllers/puzzles/4.js @@ -4,13 +4,31 @@ import PuzzlesBaseController from './base'; export default class Puzzles4Controller extends PuzzlesBaseController { // BEGIN-SNIPPET day4-solution1 solve1(input) { - return 'Solution 1'; + let solution = 0; + input.forEach(([winning, your]) => { + let intersec = your.filter((x) => winning.includes(x)).length; + if (intersec > 0) { + solution += Math.pow(2, intersec - 1); + } + }); + return solution; } // END-SNIPPET // BEGIN-SNIPPET day4-solution2 solve2(input) { - return 'Solution 1'; + let total = []; + for (let i = input.length - 1; i >= 0; i--) { + let [winning, your] = input[i]; + let intersec = your.filter((x) => winning.includes(x)).length; + if (intersec > 0) { + const value = total.slice(-intersec).reduce((a, b) => a + b); + total.push(value + 1); + } else { + total.push(1); + } + } + return total.reduce((a, b) => a + b); } // END-SNIPPET } diff --git a/app/routes/puzzles/4.js b/app/routes/puzzles/4.js index 703e397..9084f42 100644 --- a/app/routes/puzzles/4.js +++ b/app/routes/puzzles/4.js @@ -2,7 +2,12 @@ import Route from '@ember/routing/route'; export default class Puzzles4Route extends Route { parseInput(file) { - return file.split('\n'); + return file.split('\n').map((line) => + line + .split(': ')[1] + .split([' | ']) + .map((numbers) => numbers.match(/\d+/g)), + ); } async model() {