Created
March 8, 2020 16:01
-
-
Save cesarvr/88e0866e3034ba1374160b541a35cb8c to your computer and use it in GitHub Desktop.
Solution for the Advent of Code [day-5](https://adventofcode.com/2018/day/5)
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
const reaction = (a,b) => a.toLowerCase() === b.toLowerCase() && a !== b | |
const solvingPuzzleOne = (puzzle_input) =>{ | |
let lhs = '' | |
let tested = [] | |
let size = puzzle_input.length | |
for(let i=0; i<size; i++){ | |
let candidate = puzzle_input[i] | |
if(lhs === ''){ | |
lhs = candidate | |
continue | |
} | |
if(!reaction(lhs, candidate)) { | |
tested.push(lhs) | |
lhs = candidate | |
}else | |
lhs = tested.pop() || '' | |
} | |
tested.push(lhs) | |
return tested.length | |
} | |
const solvingPuzzleTwo = (puzzle_input) => { | |
let cache = {} | |
let maxReaction = Infinity | |
let size = puzzle_input.length | |
for(let i=0; i<size; i++){ | |
let token = puzzle_input[i].toLowerCase() | |
if( cache[token] === undefined ){ | |
let removedStuff = puzzle_input.replace(new RegExp(token, "ig"), '') | |
let ret = solvingPuzzleOne(removedStuff) | |
maxReaction = Math.min(maxReaction, ret) | |
cache[token] = 1 | |
} | |
} | |
return maxReaction | |
} | |
let puzzle = 'dabAcCaCBAcCcaDA' | |
console.log('Solution 1->' ,solvingPuzzleOne(puzzle) ) | |
console.log('Solution 2->' ,solvingPuzzleTwo(puzzle) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment