Skip to content

Instantly share code, notes, and snippets.

View ghaiklor's full-sized avatar

Eugene Obrezkov ghaiklor

View GitHub Profile
@ghaiklor
ghaiklor / aoc-9-2.js
Last active January 5, 2016 16:24
Advent of Code (Day 9 Part 2)
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);
@ghaiklor
ghaiklor / aoc-10-1.js
Last active August 29, 2017 20:45
Advent of Code (Day 10 Part 1)
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);
@ghaiklor
ghaiklor / aoc-10-2.js
Last active January 5, 2016 16:25
Advent of Code (Day 10 Part 2)
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);
@ghaiklor
ghaiklor / aoc-11-1.js
Last active January 5, 2016 16:26
Advent of Code (Day 11 Part 1)
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);
@ghaiklor
ghaiklor / aoc-11-2.js
Last active January 5, 2016 16:27
Advent of Code (Day 11 Part 2)
// 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);
@ghaiklor
ghaiklor / aoc-12-1.js
Last active January 5, 2016 16:28
Advent of Code (Day 12 Part 1)
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);
@ghaiklor
ghaiklor / aoc-12-2.js
Last active January 5, 2016 16:29
Advent of Code (Day 12 Part 2)
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);
@ghaiklor
ghaiklor / aoc-13-1.js
Last active January 5, 2016 16:31
Advent of Code (Day 13 Part 1)
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);
};
@ghaiklor
ghaiklor / aoc-13-2.js
Last active January 5, 2016 16:31
Advent of Code (Day 13 Part 2)
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);
};
@ghaiklor
ghaiklor / aoc-14-1.js
Last active January 5, 2016 16:32
Advent of Code (Day 14 Part 1)
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];