Skip to content

Instantly share code, notes, and snippets.

View ghaiklor's full-sized avatar

Eugene Obrezkov ghaiklor

View GitHub Profile
@ghaiklor
ghaiklor / aoc-19-2.js
Last active January 5, 2016 16:44
Advent of Code (Day 19 Part 2)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8');
const REPLACEMENTS = INPUT.split('\n\n')[0].split('\n').reduce((map, r) => map.set(r.split(' => ')[1], r.split(' => ')[0]), new Map());
let MOLECULE = INPUT.split('\n\n')[1];
let count = 0;
while (MOLECULE !== 'e') {
const randomMolecule = Array.from(REPLACEMENTS.keys())[Math.round(Math.random() * REPLACEMENTS.size)];
@ghaiklor
ghaiklor / aoc-20-1.js
Last active January 5, 2016 16:44
Advent of Code (Day 20 Part 1)
var INPUT = 34000000 / 10;
var houses = new Uint32Array(INPUT);
var houseNumber = INPUT;
for (var i = 1; i < INPUT; i++) {
for (var j = i; j < INPUT; j += i) {
if ((houses[j] += i) >= INPUT && j < houseNumber) houseNumber = j;
}
}
@ghaiklor
ghaiklor / aoc-20-2.js
Last active January 5, 2016 16:44
Advent of Code (Day 20 Part 2)
var INPUT = 34000000 / 10;
var houses = new Uint32Array(INPUT);
var houseNumber = INPUT;
for (var i = 1; i < INPUT; i++) {
var visits = 0;
for (var j = i; j < INPUT; j += i) {
if ((houses[j] = (houses[j] || 11) + i * 11) >= INPUT * 10 && j < houseNumber) houseNumber = j;
visits++;
@ghaiklor
ghaiklor / aoc-21-1.js
Last active January 5, 2016 16:45
Advent of Code (Day 21 Part 1)
const WEAPONS = new Map([
['dagger', {cost: 8, damage: 4, armor: 0}],
['shortsword', {cost: 10, damage: 5, armor: 0}],
['warhammer', {cost: 25, damage: 6, armor: 0}],
['longsword', {cost: 40, damage: 7, armor: 0}],
['greataxe', {cost: 74, damage: 8, armor: 0}]
]);
const ARMOR = new Map([
['nothing', {cost: 0, damage: 0, armor: 0}],
@ghaiklor
ghaiklor / aoc-21-2.js
Last active January 5, 2016 16:45
Advent of Code (Day 21 Part 2)
const WEAPONS = new Map([
['dagger', {cost: 8, damage: 4, armor: 0}],
['shortsword', {cost: 10, damage: 5, armor: 0}],
['warhammer', {cost: 25, damage: 6, armor: 0}],
['longsword', {cost: 40, damage: 7, armor: 0}],
['greataxe', {cost: 74, damage: 8, armor: 0}]
]);
const ARMOR = new Map([
['nothing', {cost: 0, damage: 0, armor: 0}],
@ghaiklor
ghaiklor / aoc-22-1.js
Last active January 5, 2016 16:46
Advent of Code (Day 22 Part 1)
class Player {
constructor(initial, isWizard) {
this.history = [];
this.initial = initial;
this.isWizard = !!isWizard;
if (this.isWizard) {
this.spells = [{
cost: 53,
effect: (m, o) => o.damage(4)
@ghaiklor
ghaiklor / aoc-22-2.js
Last active January 5, 2016 16:46
Advent of Code (Day 22 Part 2)
class Player {
constructor(initial, isWizard) {
this.history = [];
this.initial = initial;
this.isWizard = !!isWizard;
if (this.isWizard) {
this.spells = [{
cost: 53,
effect: (m, o) => o.damage(4)
@ghaiklor
ghaiklor / aoc-23-1.js
Last active January 5, 2016 16:47
Advent of Code (Day 23 Part 1)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n');
const SIMPLE_INSTRUCTION = /(hlf|tpl|inc) (\w+)/;
const SIMPLE_JUMP_INSTRUCTION = /(jmp) ([+-]\d+)/;
const CONDITIONAL_JUMP_INSTRUCTION = /(jie|jio) (\w+), ([+-]\d+)/;
const PROCESSOR = new Map([
['a', 0],
['b', 0]
]);
@ghaiklor
ghaiklor / aoc-23-2.js
Last active January 5, 2016 16:48
Advent of Code (Day 23 Part 2)
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n');
const SIMPLE_INSTRUCTION = /(hlf|tpl|inc) (\w+)/;
const SIMPLE_JUMP_INSTRUCTION = /(jmp) ([+-]\d+)/;
const CONDITIONAL_JUMP_INSTRUCTION = /(jie|jio) (\w+), ([+-]\d+)/;
const PROCESSOR = new Map([
['a', 1],
['b', 0]
]);
@ghaiklor
ghaiklor / aoc-24-1.js
Last active January 5, 2016 16:48
Advent of Code (Day 24 Part 1)
const Combinatorics = require('./combinatorics');
const fs = require('fs');
const INPUT = fs.readFileSync('./input.txt', 'utf-8').split('\n').map(Number);
const TOTAL_SUM = INPUT.reduce((total, x) => total + x, 0);
const GROUP_WEIGHT = TOTAL_SUM / 3;
const VALID_PACKAGES = [];
for (var i = 1; VALID_PACKAGES.length === 0; i++) {
var combination = Combinatorics.combination(INPUT, ++i);
var cmb;