Created
December 26, 2016 04:53
-
-
Save theqabalist/bff7189fa601782df0f5cc38d46765f6 to your computer and use it in GitHub Desktop.
AOC Day 19 Solution
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 scaffold = require('./scaffold'); | |
const {subtract} = require('ramda'); | |
const {Range: infRange, Repeat: repeater} = require('immutable'); | |
/* canonical, semantic solution */ | |
// const filterIndexed = addIndex(filter); | |
// const removeVictim = curry(([id], l) => filter(([xid]) => xid !== id, l)); | |
// const steal = ([x, xn], [y, yn]) => [x, xn + yn]; | |
// | |
// const rotationReduction = curry((targeter, list) => { | |
// const thief = head(list); | |
// const sights = targeter(list.length); | |
// const victim = list[sights]; | |
// const crowd = list.slice(1, sights).concat(list.slice(sights + 1)); | |
// crowd.push(steal(thief, victim)); | |
// return crowd.length === 1 ? crowd[0] : rotationReduction(targeter, crowd); | |
// }); | |
// | |
// const solution = targeter => pipe( | |
// x => parseInt(x, 10), | |
// range(0), | |
// map(inc), | |
// map(x => [x, 1]), | |
// rotationReduction(targeter) | |
// ); | |
// | |
// const part1 = solution(always(1)); | |
// const part2 = solution(length => Math.floor(length / 2.0)); | |
//algorithm designed by mathematical inference of above | |
const part1 = input => infRange(1) | |
.map(x => Math.pow(2, x)) | |
.flatMap(reps => infRange(reps - 1, -1)) | |
.zipWith((x, y) => [subtract(y, x), y], infRange(2)) | |
.skip(input - 2) | |
.first(); | |
const part2 = input => infRange(0) | |
.map(x => Math.pow(3, x)) | |
.flatMap(reps => repeater(reps, reps - 1).concat(infRange(reps, -1))) | |
.zipWith((x, y) => [subtract(y, x), y], infRange(2)) | |
.skip(input - 2) | |
.first(); | |
scaffold.value(part1, part2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment