Created
January 20, 2021 16:04
-
-
Save plvhx/b20b0529895e7795cb6fd4193e68cff5 to your computer and use it in GitHub Desktop.
advent of code 2020 day 2 part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs') | |
| const splitted = fs.readFileSync('./input.txt', 'utf-8').split(String.fromCharCode(0x0a)) | |
| function buildDictionary(splt) { | |
| let dict = [] | |
| splt.forEach((el, i) => { | |
| if (!el.length) { | |
| return | |
| } | |
| let parts = el.split(String.fromCharCode(0x20)) | |
| let minMaxPair = parts[0].split('-') | |
| dict.push({ | |
| pivot: parts[1][0], | |
| min: Number.parseInt(minMaxPair[0]), | |
| max: Number.parseInt(minMaxPair[1]), | |
| target: parts[2] | |
| }) | |
| }) | |
| return dict | |
| } | |
| function solve(dict) { | |
| let numMatches = 0 | |
| dict.forEach((el) => { | |
| if ((el.max - 1) > (el.target.length - 1)) { | |
| return | |
| } | |
| if (el.target.charAt(el.min - 1) === el.pivot && el.target.charAt(el.max - 1) === el.pivot) { | |
| return | |
| } | |
| if (el.target.charAt(el.min - 1) !== el.pivot && el.target.charAt(el.max - 1) !== el.pivot) { | |
| return | |
| } | |
| numMatches++ | |
| }) | |
| return numMatches | |
| } | |
| console.log(solve(buildDictionary(splitted))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment