This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
H4X0RZ's profile picture

I need some help with a small "homework", please

Started by H4X0RZ, 12 November 2016 - 05:44 PM
H4X0RZ #1
Posted 12 November 2016 - 06:44 PM
Okay, I don't really want to be spoonfed because I know that's not how this community works (neither would I learn anything from it) so I would like to work "together" with you in order to get a general idea of the "flow" I need to implement to get everything working.

The problem/task given is something like this: There is a stream of numbers (length not known). The program can fetch numbers, one at a time, and is allowed to store the numbers in 10 variables. The program can add the numbers from one of these variables to an "output" (doing that will "reset" the temp variable); when it does that the variable should be refilled with a new number (unless the stream/list is exhausted) immediately. Once the output reaches a specific number it can be emptied; the limitations are: the best case is that the output is exactly 20, it may never be below 20 but above is "acceptable" as long as it will happen as few times as possible. When the program finished executing everything should be "empty". The stream, every temp variable, and the output should contain no numbers.

I got a naive version of this working (written in Ruby) which only checks if a permutation of the temp variables equals 20 and then adds all the given temp vars to the output. Problems with this solution are for example that a long stream of 1s would break my script because after filling 10 vars with 1 it won't find a permutation whose sum is 20.

Is there anyone with any ideas?

Every help is appreciated,

~H4X0RZ
Lupus590 #2
Posted 12 November 2016 - 07:56 PM
if the internal vars are nearly all being used then
  • add the smallest few numbers together
  • or add numbers to the largest number if the result will be less than or equal to the target number
Bomb Bloke #3
Posted 12 November 2016 - 11:56 PM
It sounds like this would be a lot easier if the range of potential values were known. Presumably anything from 1 to 20 is possible, but it'd be easy to select yourself into a wall if it were something different than expected.
H4X0RZ #4
Posted 13 November 2016 - 12:10 AM
It sounds like this would be a lot easier if the range of potential values were known. Presumably anything from 1 to 20 is possible, but it'd be easy to select yourself into a wall if it were something different than expected.

The given numbers are "random". Although they are given to the program as a file (one number per line) so technically I could cheat by just brute-forcing my way through this but I want to write a more "general" type of code to do this. (Like having a function which doesn't care about the source of the numbers)
The example inputs don't contain any number higher than 15 though, but I wouldn't base the code on that fact because the numbers it will be tested against might be higher.

I thought about doing this in some functional language like Haskell because I feel like that's the use-case for such languages (and I really want to learn FP). Any recommendations?
Edited on 12 November 2016 - 11:54 PM