Published
Edited
Dec 2, 2020
Insert cell
Insert cell
solutionizerWithData(validRecordPart1) // part 1
Insert cell
solutionizerWithData(validRecordPart2) // part 2
Insert cell
// (Record -> Bool) -> Number
solutionizerWithData = solutionizer(input)
Insert cell
// String -> (Record -> Bool) -> Number
solutionizer = data => recordValidator =>
returnDelimToArray(data)
.map(parseRecord)
.filter(recordValidator)
.length
Insert cell
// apparently javascript doesn't have logical XOR? stolen from here: http://www.howtocreate.co.uk/xor.html
// Bool -> Bool -> Bool
xor = x => y => x ? !y : y
Insert cell
// Record -> Bool
validRecordPart2 = x => xor(x.password[x.low-1]==x.letter)(x.password[x.high-1]==x.letter)
Insert cell
// Record -> Bool
validRecordPart1 = x =>
betweenInclusive(
x.password.reduce((acc,inc) => inc==x.letter ? acc + 1 : acc, 0) // how times letter appears in password
)(x.low)(x.high)
Insert cell
// Number -> Number -> Number -> Bool
betweenInclusive = x => low => high => x <= high && x >= low
Insert cell
// String -> Record{String letter, Number low, Number high, String[] password}
parseRecord = x => {
return {
letter: x.match(matchLetterFollowedByColon)[0],
low: +x.match(matchNumbers)[0],
high: +x.match(matchNumbers)[1],
password: x.match(matchPassword)[0].split("")
}
}
Insert cell
matchPassword = /(?<=: ).*/
Insert cell
matchNumbers = /\d+/gi
Insert cell
matchLetterFollowedByColon = /.(?=:)/
Insert cell
// String -> String[]
returnDelimToArray = x => x.split("\n")
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more