Skip to content

Commit

Permalink
Day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
MinThaMie committed Dec 4, 2023
1 parent 1197dac commit 3d7a540
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
22 changes: 20 additions & 2 deletions app/controllers/puzzles/4.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 6 additions & 1 deletion app/routes/puzzles/4.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 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() {
Expand Down

0 comments on commit 3d7a540

Please sign in to comment.