Skip to content

Commit

Permalink
chore(2023-04): cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
leyanlo committed Dec 4, 2023
1 parent 341dedb commit 357ae12
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions 2023/day-04.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
const fs = require('fs');

var input = `Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11`;
var input = fs.readFileSync('./day-04-input.txt', 'utf8').trimEnd();
const input = fs.readFileSync('./day-04-input.txt', 'utf8').trimEnd();

// function solve(input) {
// console.log(input);
// let totalScore = 0;
// for (const line of input.split('\n')) {
// let [wins, nums] = line.split(': ')[1].split(' | ');
// wins = wins.match(/\d+/g).map(Number);
// nums = nums.match(/\d+/g).map(Number);
// console.log({ wins, nums });
// let nWins = nums.filter((n) => wins.includes(n)).length;
// console.log(nWins);
// totalScore += nWins && 2 ** (nWins - 1);
// console.log(totalScore);
// }
// console.log(totalScore);
// }
// solve(input);
function solve(input) {
console.log(input);
function solve1(input) {
let score = 0;
for (const line of input.split('\n')) {
let [wins, nums] = line.split(':')[1].split('|');
wins = wins.match(/\d+/g).map(Number);
nums = nums.match(/\d+/g).map(Number);
const nWins = nums.filter((n) => wins.includes(n)).length;
score += nWins && 2 ** (nWins - 1);
}
console.log(score);
}
solve1(input);

function solve2(input) {
const lines = input.split('\n');
const nCards = lines.map(() => 1);
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
let [wins, nums] = line.split(': ')[1].split(' | ');
let [wins, nums] = line.split(':')[1].split('|');
wins = wins.match(/\d+/g).map(Number);
nums = nums.match(/\d+/g).map(Number);
const nWins = nums.filter((n) => wins.includes(n)).length;
console.log(nWins);
for (let j = 0; j < nWins; j++) {
nCards[i + 1 + j] += nCards[i];
}
console.log(nCards);
}
console.log(nCards.reduce((acc, n) => acc + n));
}
solve(input);
solve2(input);

0 comments on commit 357ae12

Please sign in to comment.