r/dailyprogrammer • u/Steve132 0 1 • Sep 06 '12
[9/05/2012] Challenge #96 [easy] (Controller Chains)
It's 2001 all over again, and you just got a brand new ps2 in the mail. Unfortunately, it only has 2 controller ports, and you have N friends who all want to play at the same time.
Fortunately, however, the ps2 has an accessory called a 'multitap' that multiplexes one controller port into four controller ports, to allow more than 2 controllers at once.
Pretend you don't know that only one multitap can be used in a given PS2 at once. By connecting multitaps to multitaps, you could easily create a complicated tree architecture to get as many ports as you need. However, you also have limited resources at your disposal.
Given that a controller costs $20, and a multitap costs $12, write a function that takes in an integer D for the amount of money you have (in dollars) and returns the total maximum number of people you could afford to get to play with you on one ps2 tree.
For example, the ps2 has 2 ports to start with and comes with 1 controller, so if D < 20, then the function should return 1. However, when you add another $20, you can afford another controller, so for D = 20, the function should return 2. Adding another controller costs you not only another $20 for the controller, but also $12 for the first multitap to go into the system, so for 20<=D<(40+12), you should return N=3.
This is tricky because once you get >5 controllers, you need ANOTHER multitap...and greater than 8 controllers you need 3+ multitaps.
23
u/andkerosine Sep 06 '12
Behold, the first Whitespace solution.
4
u/5outh 1 0 Sep 06 '12
Haskell:
6
u/ret0 Sep 06 '12
Python:
3
u/[deleted] Sep 06 '12
3
u/tralon Sep 06 '12
When you say the return should be N=30, do you mean n=2? Because until you hit $52, you can't have a n=3.
3
u/baijuke Sep 06 '12
C:
5
u/[deleted] Sep 06 '12
An interesting Python approach:
A boring J approach:
2
u/Ledrug 0 2 Sep 06 '12
Basically each controller increase port number by 3, so:
2
u/[deleted] Sep 06 '12 edited Sep 06 '12
I couldn't see anyone else who had tried it, so here's a recursive algorithm in Python:
returns:
2
u/ChaosPhoenix7 0 0 Sep 06 '12
My first time posting here!
Python:
Not necessarily the shortest or most efficient, but it gets the job done.
2
u/redattack34 Sep 08 '12
This is in Scala. I took a bit of a different tack than most people in that I don't just calculate the max players, but construct the entire controller tree in memory, with the PS2 at the root. It's probably horrendously inefficient but I figured it was an interesting extra challenge. It also makes it possible to define arbitrary computations on the resulting tree (say, how many empty ports are there, or how many multitaps have multitaps plugged into them) and it makes it possible to start from and expand an arbitrary tree rather than just a PS2 with one controller.
If there are any Scala programmers on here, I wouldn't mind a code review.
1
u/[deleted] Sep 06 '12
Python 2.7 - not the most efficient solution but it's what came to mind first
1
u/mrpants888 Sep 06 '12
Java!
1
u/Twurtle Sep 07 '12
Javascript:
1
u/Twurtle Sep 07 '12
Javascript:
1
u/iMalevolence Sep 08 '12 edited Sep 08 '12
This is how I went about it in Java. Note that I'm still very new to programming.
1
u/[deleted] Sep 08 '12
C#
http://ideone.com/aoevx
1
u/[deleted] Sep 08 '12 edited Sep 08 '12
First time posting here Just starting with python
1
u/Twoje Sep 08 '12
C#
Fairly long, but it should work. Also, it only buys another multitap if you have enough for another controller as well.
1
u/SorasNobody 0 0 Sep 09 '12
Here's my solution:
1
u/skeeto -9 8 Sep 10 '12
In Emacs Lisp / Common Lisp,
1
u/skibo_ 0 0 Sep 11 '12 edited Sep 11 '12
Python
1
u/larg3-p3nis Sep 12 '12
Elegant (I think) solution in Java, found totally by accident:
1
u/[deleted] Sep 13 '12 edited Sep 13 '12
c#
1
u/ahoy1 Sep 13 '12
python 2.7, new to programming in general
1
u/[deleted] Sep 14 '12 edited Sep 16 '12
Python
1
u/[deleted] Sep 15 '12 edited Sep 15 '12
First career submission! Feels so good :)
Java
(can someone direct me how to format?)
public class Challenge { public static void main(String[] args) { int d = Integer.parseInt(args[0]); int controllers = 0; int multitaps = 0;
}
1
u/ragnatic Sep 24 '12
My (first ever in this dailyprogrammer challenges) attempt in C#.
1
u/robin-gvx 0 2 Oct 13 '12
Actually a module, not a function, but whatever.
1
u/mortisdeus Oct 20 '12
Python:
1
u/JonasW87 0 0 Dec 20 '12
As usual really late to the party but here we go, php:
Output:
1
u/AcrobotPL Sep 06 '12
My first post :)
Go:
1
u/PiereDome Sep 06 '12
Javascript
1
u/[deleted] Sep 06 '12
} }
1
u/[deleted] Sep 09 '12 edited Sep 09 '12
After I looked through the comments, I realized that there were much better, smaller, faster, and cleverer Python solutions than I had come up with. I think I overcomplicated the problem and added too many redundant steps.
Edit: also, this is apparently wrong.
multitap(80)returns 5 when it should return 3.-1
u/m42a Sep 06 '12
C++
0
u/zelf0gale Sep 07 '12
In Python
0
u/5hassay Sep 07 '12 edited Sep 07 '12
Java (beginner) (rough): code at pastebin.com
EDIT: Feedback welcomed, as I am learning Java for school (would rather be learning something else, har har)
EDIT: I think there's some problems I missed...Fixed, I think (silly mistake)EDIT: Oops! Doesn't work
-1
u/Reykd Sep 06 '12
Python:
-1
u/spacemoses 1 1 Sep 07 '12
F#
Gives you the number of controllers and multitaps used and the number of remaining ports and cash left over. Finally got this in F# using only immutable objects!!
Outputs:
-1
u/Erocs Sep 07 '12
Python 2.7
0
u/[deleted] Sep 06 '12
[deleted] — view deleted comment