16
1
The Setup
Suppose you're given n fuses, with 1 ≤ n ≤ 5, each of which is a meter long, and where each fuse has an associated burn rate of N meters per D hours.
A fuse can be lit at one or both ends, subsequently extinguished at one or both ends, relit, re-extinguished, etc., as many times as needed until the fuse is fully consumed. You are able to light and extinguish fuses instantaneously, and you can observe the exact instant a fuse is fully consumed (burnt up).
A fuse cannot be cut nor can it be lit anywhere except at its ends.
Such a setup allows for an infinitely accurate timing system, by measuring the time between any two fuse lighting/consumption events. For example, given two fuses with a burn rate of 1 meter per hour, you can measure exactly 45 minutes (3/4 hours) by
- simultaneously: lighting the first fuse at both ends, lighting the second fuse at one end, and marking the start of your time interval
- lighting the second end of the second fuse at the instant the first fuse is consumed (30 minutes later)
- marking the end of your time interval at the instant the second fuse is consumed (15 minutes later)
The Challenge
Given a fractional number of hours t, and a set of n fractions representing exact burn rates of n fuses, write a program or function that outputs/returns a truthy value if t hours can be precisely measured through systematic burning of the fuses, or a falsy value otherwise.
The input to the program can be any of the following:
- command-line arguments of the form
TN/TD N1/D1 N2/D2 N3/D3 ...
- a string of the form
TN/TD N1/D1 N2/D2 N3/D3 ...
read fromstdin
or equivalent - a string of the form
TN/TD N1/D1 N2/D2 N3/D3 ...
passed as a function argument - an array of strings
["TN/TD", "N1/D1", "N2/D2", "N3/D3", ...]
passed as a function argument
In all cases t = TN
/TD
, where TN
,TD
∈ [1,10000].
Likewise, in all cases: burn rate for fuse i = Ni /Di = N<i>
/D<i>
, where N<i>
,D<i>
∈ [1,10] ∀ i.
You may assume there will always be between 1 and 5 fuses (inclusive), and that all inputs are valid and in-range. You may also assume that all input fractions are given in lowest terms.
You may not use floating point numbers with fractional components for this challenge. That is, if you use floating point numbers anywhere in your application, they may only take on integral values with zero fractional component.
Scoring
This is a code-golf challenge, hence the shortest compliant submission in bytes will be awarded the win.
Example Inputs/Outputs
input: 29/6 3/2 2/3 3/5 3/7 7/5
output: true
One solution:
- light both ends of fuse 1, mark start of interval
- on fuse 1 consumption: light both ends of fuse 2, light one end of fuse 5
- on fuse 5 consumption: extinguish one end of fuse 2, light both ends of fuse 3,
light both ends of fuse 4
- on fuse 2 consumption: extinguish one end of fuse 3, extinguish both ends of
fuse 4
- on fuse 3 consumption: relight one end of fuse 4
- on consumption of fuse 4: mark end of interval (29/6 hours)
input: 2/1 3/1 5/1 7/1
output: false
input: 5/1 6/1 1/6 9/1 1/9
output: true
One solution:
- light fuse 1 at one end, light fuse 2 at both ends, light fuse 4 at both ends
- on fuse 1 consumption: extinguish one end of fuse 2, mark start of interval
- on fuse 4 consumption: relight one end of fuse 2
- on fuse 2 consumption: mark end of interval (5 hours)
Happy fusing! :)
@MartinBüttner I would guess it would be the floating point number restriction. – hmatt1 – 2014-11-03T22:05:04.087
2@MartinBüttner I agree it isn't a restriction on the source code. I don't think [restricted-source] fits this question as it currently stands. – hmatt1 – 2014-11-03T22:43:16.463
@chilemagic: I wanted to draw attention to the fact that floating point logic couldn't be used, but if the consensus is that it's not a proper use of the tag, I'll strip it. – COTO – 2014-11-03T23:37:45.703
The test cases are too big :) – feersum – 2014-11-04T03:41:31.287
@feersum: How so? I programmed a Java-based solver before posting the challenge to make sure there were no obvious loopholes, and to come up with some interesting test cases. The solver runs in a matter of milliseconds for the given test cases. Would limiting n <= 4 help? – COTO – 2014-11-04T03:45:01.777
5Lol, I'm using an O( (n!)^3 ) algorithm for golfing purposes. – feersum – 2014-11-04T03:46:27.963
@feersum: lol Um... yeah. Tell you what: if it can run the three test inputs in a few hours or less, submit it and it meets the spec as far as I'm concerned. – COTO – 2014-11-04T03:49:01.937