Skip to content

Instantly share code, notes, and snippets.

@cesarvr
Created March 8, 2020 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cesarvr/88e0866e3034ba1374160b541a35cb8c to your computer and use it in GitHub Desktop.
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)
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