This file contains 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 INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n'); | |
const DIRECTION_REGEX = /(\w+) to (\w+) = (\d+)/; | |
// Takes input and builds map of all possible distances between two points | |
const buildDistanceMap = input => { | |
const map = new Map(); | |
input.forEach(direction => { | |
const parsed = direction.match(DIRECTION_REGEX); |
This file contains 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 INPUT = '1113122113'; | |
const FIND_REPETITIONS_REGEX = /(\d)\1*/g; | |
const lookAndSay = input => input.match(FIND_REPETITIONS_REGEX).reduce((acc, char) => acc + `${char.length}${char[0]}`, ''); | |
let result = INPUT; | |
for (let i = 0; i < 40; i++) { | |
result = lookAndSay(result); | |
} | |
console.log(result.length); |
This file contains 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 INPUT = '1113122113'; | |
const FIND_REPETITIONS_REGEX = /(\d)\1*/g; | |
const lookAndSay = input => input.match(FIND_REPETITIONS_REGEX).reduce((acc, char) => acc + `${char.length}${char[0]}`, ''); | |
let result = INPUT; | |
for (let i = 0; i < 50; i++) { | |
result = lookAndSay(result); | |
} | |
console.log(result.length); |
This file contains 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 INPUT = 'cqjxjnds'; | |
// Rules for correct password | |
const isContainStraightIncreasingSymbols = string => string.split('').map(char => char.charCodeAt(0)).some((char, index, arr) => arr[index] === arr[index + 1] - 1 && arr[index + 1] === arr[index + 2] - 1); | |
const isContainRestrictedSymbols = string => /i|o|l/.test(string); | |
const isContainPairs = string => /(\w)\1.*(\w)\2/.test(string); | |
// Increments one char | |
const incrementChar = char => char === 'z' ? 'a' : String.fromCharCode(char.charCodeAt(0) + 1); |
This file contains 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
// I'm the laziest man in the world, I know :) Just changed INPUT to the password from part-1 | |
const INPUT = 'cqjxxyzz'; | |
// Rules for correct password | |
const isContainStraightIncreasingSymbols = string => string.split('').map(char => char.charCodeAt(0)).some((char, index, arr) => arr[index] === arr[index + 1] - 1 && arr[index + 1] === arr[index + 2] - 1); | |
const isContainRestrictedSymbols = string => /i|o|l/.test(string); | |
const isContainPairs = string => /(\w)\1.*(\w)\2/.test(string); | |
// Increments one char | |
const incrementChar = char => char === 'z' ? 'a' : String.fromCharCode(char.charCodeAt(0) + 1); |
This file contains 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 INPUT = fs.readFileSync('./input.txt', 'utf-8'); | |
const NUMBER_REGEX = /-?\d+/g; | |
const result = INPUT.match(NUMBER_REGEX).reduce((total, number) => total + +number, 0); | |
console.log(result); |
This file contains 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 NUMBER_REGEX = /-?\d+/g; | |
const INPUT = JSON.parse(fs.readFileSync('./input.txt', 'utf-8'), (key, value) => { | |
if (!Array.isArray(value)) return Object.keys(value).map(key => value[key]).indexOf('red') !== -1 ? {} : value; | |
return value; | |
}); | |
const result = JSON.stringify(INPUT).match(NUMBER_REGEX).reduce((total, number) => total + +number, 0); | |
console.log(result); |
This file contains 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 INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n'); | |
const PERSON_ATTRIBUTES_REGEX = /(\w+) would (\w+) (\d+) happiness units by sitting next to (\w+)./; | |
// Generate all possible permutations for an array | |
const permute = input => { | |
const array = Array.from(input); | |
const permute = (res, item, key, arr) => { | |
return res.concat(arr.length > 1 && arr.slice(0, key).concat(arr.slice(key + 1)).reduce(permute, []).map(perm => [item].concat(perm)) || item); | |
}; |
This file contains 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 INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n'); | |
const PERSON_ATTRIBUTES_REGEX = /(\w+) would (\w+) (\d+) happiness units by sitting next to (\w+)./; | |
// Generate all possible permutations for an array | |
const permute = input => { | |
const array = Array.from(input); | |
const permute = (res, item, key, arr) => { | |
return res.concat(arr.length > 1 && arr.slice(0, key).concat(arr.slice(key + 1)).reduce(permute, []).map(perm => [item].concat(perm)) || item); | |
}; |
This file contains 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 INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n'); | |
const REINDEER_REGEX = /\d+/g; | |
const TIME = 2503; | |
// It can be calculated by formula without cycling it in some kind of loop | |
const getReindeerDistance = input => { | |
const args = input.match(REINDEER_REGEX).map(Number); | |
const speed = args[0]; | |
const time = args[1]; |