Skip to content

Instantly share code, notes, and snippets.

View ghaiklor's full-sized avatar

Eugene Obrezkov ghaiklor

View GitHub Profile
@ghaiklor
ghaiklor / aoc-4-2.js
Last active January 5, 2016 16:12
Advent of Code (Day 4 Part 2)
const crypto = require('crypto');
const INPUT = 'ckczppom';
const md5 = data => crypto.createHash('md5').update(data).digest('hex');
const isStartsWithSixZeros = data => data.slice(0, 6) === '000000';
let counter = 0;
while (!isStartsWithSixZeros(md5(`${INPUT}${counter}`))) counter++;
console.log(counter);
@ghaiklor
ghaiklor / aoc-5-1.js
Last active January 5, 2016 16:14
Advent of Code (Day 5 Part 1)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n');
// Dictionary of letters that need to be checked against the rules
const VOWELS = ['a', 'e', 'i', 'o', 'u'];
const DOUBLE_LETTERS = 'abcdefghijklmnopqrstuvwxyz'.split('').map(item => item + item);
const RESTRICTED_LETTERS = ['ab', 'cd', 'pq', 'xy'];
// Methods to check the rules
const isContainThreeVowels = string => string.split('').reduce((vowels, char) => VOWELS.indexOf(char) === -1 ? vowels : ++vowels, 0) >= 3;
@ghaiklor
ghaiklor / aoc-5-2.js
Last active January 5, 2016 16:15
Advent of Code (Day 5 Part 2)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n');
// Part 1 was written with pure functions but Part 2 I've decided to write with RegExp
const isContainPair = string => /([a-z][a-z]).*\1/.test(string);
const isContainRepeatLetter = string => /([a-z])[a-z]\1/.test(string);
const isNiceString = string => !!(isContainPair(string) && isContainRepeatLetter(string));
const result = INPUT.reduce((total, string) => isNiceString(string) ? ++total : total, 0);
@ghaiklor
ghaiklor / aoc-6-1.js
Last active January 5, 2016 16:16
Advent of Code (Day 6 Part 1)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n');
// Parse command from string and return object
const parseCommand = _command => {
let command = _command.match(/(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)/);
return {command: command[1], x1: +command[2], y1: +command[3], x2: +command[4], y2: +command[5]};
};
// Map of our lights
@ghaiklor
ghaiklor / aoc-6-2.js
Last active January 5, 2016 16:17
Advent of Code (Day 6 Part 2)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n');
const COMMANDS_REGEX = /(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)/;
// Parse command from string and return object
const parseCommand = _command => {
let command = _command.match(COMMANDS_REGEX);
return {command: command[1], x1: +command[2], y1: +command[3], x2: +command[4], y2: +command[5]};
};
@ghaiklor
ghaiklor / aoc-7-1.js
Last active January 5, 2016 16:19
Advent of Code (Day 7 Part 1)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n');
const COMMAND_REGEX = /[A-Z]+/g;
const ARGUMENTS_REGEX = /[a-z0-9]+/g;
// Our parsed wires in format {wire: value} or {wire: instruction}
const WIRES = new Map();
// Dictionary of our bitwise methods
const BITWISE_METHODS = {
@ghaiklor
ghaiklor / aoc-7-2.js
Last active January 5, 2016 16:20
Advent of Code (Day 7 Part 2)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n');
const COMMAND_REGEX = /[A-Z]+/g;
const ARGUMENTS_REGEX = /[a-z0-9]+/g;
// Our parsed wires in format {wire: value} or {wire: instruction}
const WIRES = new Map();
// Dictionary of our bitwise methods
const BITWISE_METHODS = {
@ghaiklor
ghaiklor / aoc-8-1.js
Last active January 5, 2016 16:20
Advent of Code (Day 8 Part 1)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n');
const result = INPUT.reduce((acc, line) => acc + (line.length - eval(line).length), 0);
console.log(result);
@ghaiklor
ghaiklor / aoc-8-2.js
Last active January 5, 2016 16:20
Advent of Code (Day 8 Part 2)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n');
const result = INPUT.reduce((acc, line) => acc + (2 + line.replace(/\\/g, '\\\\').replace(/"/g, '\\"').length - line.length), 0);
console.log(result);
@ghaiklor
ghaiklor / aoc-9-1.js
Last active January 5, 2016 16:23
Advent of Code (Day 9 Part 1)
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);