mirror of
https://github.com/seigler/advent-of-code-2020
synced 2025-07-27 00:06:09 +00:00
day 2, change filter/count to reduce
This commit is contained in:
parent
6eeaabc649
commit
170e72f666
1 changed files with 22 additions and 11 deletions
|
@ -20,30 +20,41 @@ async function run () {
|
|||
}
|
||||
|
||||
async function solveForFirstStar (input) {
|
||||
const solution = input.filter(
|
||||
({ num1, num2, letter, password }) => {
|
||||
const solution = input.reduce(
|
||||
(acc, { num1, num2, letter, password }) => {
|
||||
let count = 0
|
||||
for (let i = 0; i < password.length; i++) {
|
||||
if (password.charAt(i) === letter) {
|
||||
count++
|
||||
if (count > num2) {
|
||||
return false
|
||||
return acc
|
||||
}
|
||||
}
|
||||
}
|
||||
return count >= num1
|
||||
}
|
||||
).length
|
||||
return acc + (
|
||||
count >= num1
|
||||
? 1
|
||||
: 0
|
||||
)
|
||||
}, 0
|
||||
)
|
||||
report('Input:', input)
|
||||
report('Solution 1:', solution)
|
||||
}
|
||||
|
||||
async function solveForSecondStar (input) {
|
||||
const solution = input.filter(
|
||||
({ num1, num2, letter, password }) => {
|
||||
return XOR(password.charAt(num1 - 1) === letter, password.charAt(num2 - 1) === letter)
|
||||
}
|
||||
).length
|
||||
const solution = input.reduce(
|
||||
(acc, { num1, num2, letter, password }) => {
|
||||
return acc + (
|
||||
XOR(
|
||||
password.charAt(num1 - 1) === letter,
|
||||
password.charAt(num2 - 1) === letter
|
||||
)
|
||||
? 1
|
||||
: 0
|
||||
)
|
||||
}, 0
|
||||
)
|
||||
report('Solution 2:', solution)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue