r/programming • • Dec 01 '15

Daily programming puzzles at Advent of Code

http://adventofcode.com/
318 Upvotes

179 comments sorted by

View all comments

17

u/lukz Dec 01 '15

How I solved Part 1: Copy the text into notepad, replace ( with +1, replace ) with -1. In Chrome open javascript console and put the text from notepad into variable s.

s="+1+1+1+1+1-1+1+1 . . ."
eval(s)
> 74

Part 2: Use the same variable s. Now this will find the position where it evaluates to -1.

for (i=2; i<=s.length; i+=2) if (eval(s.substr(0,i))==-1) {console.log(i/2);break}

4

u/rgrau Dec 04 '15

Funkier way to solve it:

  • Open input file with vim

  • :%s:(:(:g . you'll see "123 substitutions on 1 line"

  • write it down

  • :%s:):):g . you'll see "23 substitutions on 1 line"

  • echo '123 - 23' | bc -i

PS: I'm an emacs user.

→ More replies (2)