Skip to content

Commit

Permalink
feat[2020-day-02]: count the valid passwords under the new rules
Browse files Browse the repository at this point in the history
Rules changed, as shopkeeper was thinking about old employer in Part 1
  • Loading branch information
amclin committed Dec 7, 2020
1 parent 03a33e7 commit fdbbeae
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
45 changes: 45 additions & 0 deletions 2020/day-02/cleanupPasswords.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const splitRecord = (row) => {

/**
* Splits a password validation rule into its component parts
* using the original rules
*/
const oldSplitRule = (rule) => {
const splitRow = rule.split(/-| /)
Expand All @@ -24,8 +25,25 @@ const oldSplitRule = (rule) => {
}
}

/**
* Splits a password validation rule into its component parts
* using the new rules
*/
const newSplitRule = (rule) => {
const splitRow = rule.split(/-| /)

return {
positions: [
Number(splitRow[0]),
Number(splitRow[1])
],
char: String(splitRow[2])
}
}

/**
* Validates a password against the specified rule
* using the original rules
*/
const oldIsValidPassword = (rule, password) => {
// count how many times `rule.char` exists in `password`
Expand All @@ -40,17 +58,44 @@ const oldIsValidPassword = (rule, password) => {
return true
}

/**
* Validates a password against the specified rule
* using the new rules
*/
const newIsValidPassword = (rule, password) => {
let matches = 0
rule.positions.forEach((pos) => {
// index starts with 1
if (password[pos - 1] === rule.char) {
matches++
}
})
// Only one match allowed, not 2, not 0
return (matches === 1)
}

const oldIsValidRecord = (record) => {
const { rule, password } = splitRecord(record)
const parsedRule = oldSplitRule(rule)
return oldIsValidPassword(parsedRule, password)
}

const newIsValidRecord = (record) => {
const { rule, password } = splitRecord(record)
const parsedRule = newSplitRule(rule)
return newIsValidPassword(parsedRule, password)
}

module.exports = {
old: {
splitRule: oldSplitRule,
isValidPassword: oldIsValidPassword,
isValidRecord: oldIsValidRecord
},
cur: {
splitRule: newSplitRule,
isValidPassword: newIsValidPassword,
isValidRecord: newIsValidRecord
},
splitRecord
}
33 changes: 32 additions & 1 deletion 2020/day-02/cleanupPasswords.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
const { expect } = require('chai')
const { splitRecord, old } = require('./cleanupPasswords')
const { splitRecord, old, cur } = require('./cleanupPasswords')

const testData = [
'1-3 a: abcde',
Expand Down Expand Up @@ -53,4 +53,35 @@ describe('--- Day 2: Password Philosophy ---', () => {
})
})
})
describe('Part 2', () => {
describe('splitRule()', () => {
it('splits a password formatting rule into component parts', () => {
testData.forEach((row, idx) => {
const { rule, password } = splitRecord(row)
const { positions, char } = cur.splitRule(rule)
expect(`${positions.join('-')} ${char}: ${password}`).to.equal(testData[idx])
})
})
})
describe('isValidPassword()', () => {
it('checks if a specified password matches the specified rule', () => {
const expectedResults = [true, false, false]
testData.forEach((row, idx) => {
const { rule, password } = splitRecord(row)
const ruleObj = cur.splitRule(rule)
expect(cur.isValidPassword(ruleObj, password))
.to.equal(expectedResults[idx])
})
})
})
describe('isValidRecord()', () => {
it('checks if a specified record contains valid rule and password', () => {
const expectedResults = [true, false, false]
testData.forEach((row, idx) => {
expect(cur.isValidRecord(row))
.to.equal(expectedResults[idx])
})
})
})
})
})
10 changes: 5 additions & 5 deletions 2020/day-02/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs')
const path = require('path')
const filePath = path.join(__dirname, 'input.txt')
const { linesToArray } = require('../../2018/inputParser')
const { isValidRecord } = require('./cleanupPasswords')
const { old, cur } = require('./cleanupPasswords')

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

const part1 = () => {
const data = resetInput()
// Count the valid passwords
return data.filter(isValidRecord).length
// Count the valid passwords with old rules
return data.filter(old.isValidRecord).length
}

const part2 = () => {
const data = resetInput()
console.debug(data)
return 'No answer yet'
// Count the valid passwords with new rules
return data.filter(cur.isValidRecord).length
}
const answers = []
answers.push(part1())
Expand Down

0 comments on commit fdbbeae

Please sign in to comment.