Skip to content

Commit 97c0834

Browse files
feat(2020-day-03): count the valid passwords under the new rules
Rules changed, as shopkeeper was thinking about old employer in Part 1
1 parent d1de0f2 commit 97c0834

File tree

3 files changed

+82
-6
lines changed

3 files changed

+82
-6
lines changed

2020/day-02/cleanupPasswords.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const splitRecord = (row) => {
1313

1414
/**
1515
* Splits a password validation rule into its component parts
16+
* using the original rules
1617
*/
1718
const oldSplitRule = (rule) => {
1819
const splitRow = rule.split(/-| /)
@@ -24,8 +25,25 @@ const oldSplitRule = (rule) => {
2425
}
2526
}
2627

28+
/**
29+
* Splits a password validation rule into its component parts
30+
* using the new rules
31+
*/
32+
const newSplitRule = (rule) => {
33+
const splitRow = rule.split(/-| /)
34+
35+
return {
36+
positions: [
37+
Number(splitRow[0]),
38+
Number(splitRow[1])
39+
],
40+
char: String(splitRow[2])
41+
}
42+
}
43+
2744
/**
2845
* Validates a password against the specified rule
46+
* using the original rules
2947
*/
3048
const oldIsValidPassword = (rule, password) => {
3149
// count how many times `rule.char` exists in `password`
@@ -40,17 +58,44 @@ const oldIsValidPassword = (rule, password) => {
4058
return true
4159
}
4260

61+
/**
62+
* Validates a password against the specified rule
63+
* using the new rules
64+
*/
65+
const newIsValidPassword = (rule, password) => {
66+
let matches = 0
67+
rule.positions.forEach((pos) => {
68+
// index starts with 1
69+
if (password[pos - 1] === rule.char) {
70+
matches++
71+
}
72+
})
73+
// Only one match allowed, not 2, not 0
74+
return (matches === 1)
75+
}
76+
4377
const oldIsValidRecord = (record) => {
4478
const { rule, password } = splitRecord(record)
4579
const parsedRule = oldSplitRule(rule)
4680
return oldIsValidPassword(parsedRule, password)
4781
}
4882

83+
const newIsValidRecord = (record) => {
84+
const { rule, password } = splitRecord(record)
85+
const parsedRule = newSplitRule(rule)
86+
return newIsValidPassword(parsedRule, password)
87+
}
88+
4989
module.exports = {
5090
old: {
5191
splitRule: oldSplitRule,
5292
isValidPassword: oldIsValidPassword,
5393
isValidRecord: oldIsValidRecord
5494
},
95+
cur: {
96+
splitRule: newSplitRule,
97+
isValidPassword: newIsValidPassword,
98+
isValidRecord: newIsValidRecord
99+
},
55100
splitRecord
56101
}

2020/day-02/cleanupPasswords.test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
3-
const { splitRecord, old } = require('./cleanupPasswords')
3+
const { splitRecord, old, cur } = require('./cleanupPasswords')
44

55
const testData = [
66
'1-3 a: abcde',
@@ -53,4 +53,35 @@ describe('--- Day 2: Password Philosophy ---', () => {
5353
})
5454
})
5555
})
56+
describe('Part 2', () => {
57+
describe('splitRule()', () => {
58+
it('splits a password formatting rule into component parts', () => {
59+
testData.forEach((row, idx) => {
60+
const { rule, password } = splitRecord(row)
61+
const { positions, char } = cur.splitRule(rule)
62+
expect(`${positions.join('-')} ${char}: ${password}`).to.equal(testData[idx])
63+
})
64+
})
65+
})
66+
describe('isValidPassword()', () => {
67+
it('checks if a specified password matches the specified rule', () => {
68+
const expectedResults = [true, false, false]
69+
testData.forEach((row, idx) => {
70+
const { rule, password } = splitRecord(row)
71+
const ruleObj = cur.splitRule(rule)
72+
expect(cur.isValidPassword(ruleObj, password))
73+
.to.equal(expectedResults[idx])
74+
})
75+
})
76+
})
77+
describe('isValidRecord()', () => {
78+
it('checks if a specified record contains valid rule and password', () => {
79+
const expectedResults = [true, false, false]
80+
testData.forEach((row, idx) => {
81+
expect(cur.isValidRecord(row))
82+
.to.equal(expectedResults[idx])
83+
})
84+
})
85+
})
86+
})
5687
})

2020/day-02/solution.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fs = require('fs')
22
const path = require('path')
33
const filePath = path.join(__dirname, 'input.txt')
44
const { linesToArray } = require('../../2018/inputParser')
5-
const { isValidRecord } = require('./cleanupPasswords')
5+
const { old, cur } = require('./cleanupPasswords')
66

77
fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
88
if (err) throw err
@@ -16,14 +16,14 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
1616

1717
const part1 = () => {
1818
const data = resetInput()
19-
// Count the valid passwords
20-
return data.filter(isValidRecord).length
19+
// Count the valid passwords with old rules
20+
return data.filter(old.isValidRecord).length
2121
}
2222

2323
const part2 = () => {
2424
const data = resetInput()
25-
console.debug(data)
26-
return 'No answer yet'
25+
// Count the valid passwords with new rules
26+
return data.filter(cur.isValidRecord).length
2727
}
2828
const answers = []
2929
answers.push(part1())

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy