MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/3uyl7s/daily_programming_puzzles_at_advent_of_code/cxj1tqe/
r/programming • u/Aneurysm9 • Dec 01 '15
179 comments sorted by
View all comments
Show parent comment
2
And I also did it a bit differently:
<?php $string = '()()(()()()(()()(((...'; $up = substr_count ($string , '('); $down = substr_count ($string , ')'); if($up > $down) { $answer = $up - $down; } else { $answer = $down - $up; } var_dump($answer);
1 u/[deleted] Dec 01 '15 edited Dec 01 '15 Why the if/else? You're not allowing for a negative answer, meaning they'd never be in the basement. It should always be $up - $down.If you did want to avoid negative answers, I'd still avoid the if/else, and just set $answer to abs($up - $down). :) 1 u/[deleted] Dec 01 '15 Ah yeah, I see what you mean. , thanks.
1
Why the if/else? You're not allowing for a negative answer, meaning they'd never be in the basement. It should always be $up - $down.
If you did want to avoid negative answers, I'd still avoid the if/else, and just set $answer to abs($up - $down). :)
1 u/[deleted] Dec 01 '15 Ah yeah, I see what you mean. , thanks.
Ah yeah, I see what you mean. , thanks.
View all comments
Show parent comment
2
u/[deleted] Dec 01 '15
And I also did it a bit differently: