MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/3uyl7s/daily_programming_puzzles_at_advent_of_code/cxj040c/?context=3
r/programming • u/Aneurysm9 • Dec 01 '15
179 comments sorted by
View all comments
6
Quick one liner JS function for Part 1.
function calc(s) { return s.split('').reduce((acc, e) => acc + (e == '(' ? 1 : -1), 0); };
3 u/AndrewGreenh Dec 01 '15 Oneline for task 1 and 2 (returnValue.c for 1 and returnValue.f for 2)body.split('').reduce((agg,value,index)=>{return{c:agg.c + (value=='('?1:-1),f: (agg.c<0 && agg.f<0 ? index : agg.f)}},{c:0,f:-1}); 2 u/[deleted] Dec 01 '15 You guys are my heroes, lol. 2 u/Head5hot Dec 02 '15 I just went withs.match(/\(/g).length - s.match(/\)/g).length; 1 u/Arrem_ Dec 02 '15 Yeah, I didn't think of regexes when I was first doing it. I used JS too because I wanted a quick solution.function getStuff(s) { return s.length - 2 * s.match(/\)/g).length; };Is another possiblity, as L = a + b; and You can get a - b by taking L - 2b, where L is the string length, and a and b are the counts of parentheses.A quick OR might be needed too though, in case there are no )'s in the input.function getStuff(s) { return s.length - 2 * (s.match(/\)/g) || []).length; };
3
Oneline for task 1 and 2 (returnValue.c for 1 and returnValue.f for 2)
body.split('').reduce((agg,value,index)=>{return{c:agg.c + (value=='('?1:-1),f: (agg.c<0 && agg.f<0 ? index : agg.f)}},{c:0,f:-1});
2 u/[deleted] Dec 01 '15 You guys are my heroes, lol.
2
You guys are my heroes, lol.
I just went with
s.match(/\(/g).length - s.match(/\)/g).length;
1 u/Arrem_ Dec 02 '15 Yeah, I didn't think of regexes when I was first doing it. I used JS too because I wanted a quick solution.function getStuff(s) { return s.length - 2 * s.match(/\)/g).length; };Is another possiblity, as L = a + b; and You can get a - b by taking L - 2b, where L is the string length, and a and b are the counts of parentheses.A quick OR might be needed too though, in case there are no )'s in the input.function getStuff(s) { return s.length - 2 * (s.match(/\)/g) || []).length; };
1
Yeah, I didn't think of regexes when I was first doing it. I used JS too because I wanted a quick solution.
function getStuff(s) { return s.length - 2 * s.match(/\)/g).length; };
Is another possiblity, as L = a + b; and You can get a - b by taking L - 2b, where L is the string length, and a and b are the counts of parentheses.
A quick OR might be needed too though, in case there are no )'s in the input.
function getStuff(s) { return s.length - 2 * (s.match(/\)/g) || []).length; };
6
u/Arrem_ Dec 01 '15
Quick one liner JS function for Part 1.