Last active
December 1, 2017 15:38
-
-
Save edwardgalligan/287888c4e22e11905ba546184cc10082 to your computer and use it in GitHub Desktop.
Day 1: Inverse Captcha - Part Two
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
// vim: set et sw=4 ts=4 ff=unix ft=javascript : | |
// @ts-check | |
/** | |
* Define | |
*/ | |
const day1_2 = input => { | |
const digits = input.toString().split(''); | |
if (digits.length % 2 !== 0) { | |
throw new Error(`Uneven input: ${digits.length} characters long`); | |
} | |
let prevIndex = digits.length - (digits.length / 2); | |
let prev = parseInt(digits[prevIndex]); | |
let sum = 0; | |
digits.forEach(str => { | |
const num = parseInt(str, 10); | |
if (num === prev) { | |
sum = sum + prev; | |
} | |
prev = parseInt(digits[++prevIndex]); | |
if (isNaN(prev)) { | |
prev = parseInt(digits[prevIndex = 0]); | |
} | |
}); | |
return sum; | |
} | |
const day1_2test = (input, expected) => { | |
const result = day1_2(input); | |
console.log(`Testing ${input}, got ${result}`) | |
console.assert(result === expected, `Got ${result}, should be ${expected} for input ${input}`); | |
}; | |
/** | |
* Test | |
*/ | |
day1_2test(1212, 6); | |
day1_2test(1221, 0); | |
day1_2test(123425, 4); | |
day1_2test(123123, 12); | |
day1_2test(12131415, 4); | |
/** | |
* Run | |
*/ | |
const INPUT = '77736991856689225253142335214746294932318813454849177823468674346512426482777696993348135287531487622845155339235443718798255411492778415157351753377959586612882455464736285648473397681163729345143319577258292849619491486748832944425643737899293811819448271546283914592546989275992844383947572926628695617661344293284789225493932487897149244685921644561896799491668147588536732985476538413354195246785378443492137893161362862587297219368699689318441563683292683855151652394244688119527728613756153348584975372656877565662527436152551476175644428333449297581939357656843784849965764796365272113837436618857363585783813291999774718355479485961244782148994281845717611589612672436243788252212252489833952785291284935439662751339273847424621193587955284885915987692812313251556836958571335334281322495251889724281863765636441971178795365413267178792118544937392522893132283573129821178591214594778712292228515169348771198167462495988252456944269678515277886142827218825358561772588377998394984947946121983115158951297156321289231481348126998584455974277123213413359859659339792627742476688827577318285573236187838749444212666293172899385531383551142896847178342163129883523694183388123567744916752899386265368245342587281521723872555392212596227684414269667696229995976182762587281829533181925696289733325513618571116199419759821597197636415243789757789129824537812428338192536462468554399548893532588928486825398895911533744671691387494516395641555683144968644717265849634943691721391779987198764147667349266877149238695714118982841721323853294642175381514347345237721288281254828745122878268792661867994785585131534136646954347165597315643658739688567246339618795777125767432162928257331951255792438831957359141651634491912746875748363394329848227391812251812842263277229514125426682179711184717737714178235995431465217547759282779499842892993556918977773236196185348965713241211365895519697294982523166196268941976859987925578945185217127344619169353395993198368185217391883839449331638641744279836858188235296951745922667612379649453277174224722894599153367373494255388826855322712652812127873536473277'; | |
console.log('Result for INPUT is', day1_2(INPUT)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment