mirror of
https://github.com/seigler/advent-of-code-2020
synced 2025-07-27 08:16:08 +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) {
|
async function solveForFirstStar (input) {
|
||||||
const solution = input.filter(
|
const solution = input.reduce(
|
||||||
({ num1, num2, letter, password }) => {
|
(acc, { num1, num2, letter, password }) => {
|
||||||
let count = 0
|
let count = 0
|
||||||
for (let i = 0; i < password.length; i++) {
|
for (let i = 0; i < password.length; i++) {
|
||||||
if (password.charAt(i) === letter) {
|
if (password.charAt(i) === letter) {
|
||||||
count++
|
count++
|
||||||
if (count > num2) {
|
if (count > num2) {
|
||||||
return false
|
return acc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return count >= num1
|
return acc + (
|
||||||
}
|
count >= num1
|
||||||
).length
|
? 1
|
||||||
|
: 0
|
||||||
|
)
|
||||||
|
}, 0
|
||||||
|
)
|
||||||
report('Input:', input)
|
report('Input:', input)
|
||||||
report('Solution 1:', solution)
|
report('Solution 1:', solution)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function solveForSecondStar (input) {
|
async function solveForSecondStar (input) {
|
||||||
const solution = input.filter(
|
const solution = input.reduce(
|
||||||
({ num1, num2, letter, password }) => {
|
(acc, { num1, num2, letter, password }) => {
|
||||||
return XOR(password.charAt(num1 - 1) === letter, password.charAt(num2 - 1) === letter)
|
return acc + (
|
||||||
}
|
XOR(
|
||||||
).length
|
password.charAt(num1 - 1) === letter,
|
||||||
|
password.charAt(num2 - 1) === letter
|
||||||
|
)
|
||||||
|
? 1
|
||||||
|
: 0
|
||||||
|
)
|
||||||
|
}, 0
|
||||||
|
)
|
||||||
report('Solution 2:', solution)
|
report('Solution 2:', solution)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue