Next, you should verify the life support rating, which can be determined by multiplying the oxygen generator rating by the CO2 scrubber rating.
Both the oxygen generator rating and the CO2 scrubber rating are values that can be found in your diagnostic report - finding them is the tricky part. Both values are located using a similar process that involves filtering out values until only one remains. Before searching for either rating value, start with the full list of binary numbers from your diagnostic report and consider just the first bit of those numbers. Then:
The bit criteria depends on which type of rating value you want to find:
0
or 1
) in the current bit position, and keep only numbers with that bit in that position. If 0
and 1
are equally common, keep values with a 1
in the position being considered.0
or 1
) in the current bit position, and keep only numbers with that bit in that position. If 0
and 1
are equally common, keep values with a 0
in the position being considered.For example, to determine the oxygen generator rating value using the same example diagnostic report from above:
1
bits (7) than 0
bits (5), so keep only the 7 numbers with a 1
in the first position: 11110
, 10110
, 10111
, 10101
, 11100
, 10000
, and 11001
.0
bits (4) than 1
bits (3), so keep only the 4 numbers with a 0
in the second position: 10110
, 10111
, 10101
, and 10000
.1
, so keep those three: 10110
, 10111
, and 10101
.1
, so keep those two: 10110
and 10111
.0
bits and 1
bits (one each). So, to find the oxygen generator rating, keep the number with a 1
in that position: 10111
.10111
, or 23
in decimal.Then, to determine the CO2 scrubber rating value from the same example above:
0
bits (5) than 1
bits (7), so keep only the 5 numbers with a 0
in the first position: 00100
, 01111
, 00111
, 00010
, and 01010
.1
bits (2) than 0
bits (3), so keep only the 2 numbers with a 1
in the second position: 01111
and 01010
.0
bits and 1
bits (one each). So, to find the CO2 scrubber rating, keep the number with a 0
in that position: 01010
.01010
, or 10
in decimal.Finally, to find the life support rating, multiply the oxygen generator rating (23
) by the CO2 scrubber rating (10
) to get 230
.
Use the binary numbers in your diagnostic report to calculate the oxygen generator rating and CO2 scrubber rating, then multiply them together. What is the life support rating of the submarine? (Be sure to represent your answer in decimal, not binary.) Determine this in the following way:
Write a function lifeSupportRating
that takes the pathname (String
) of a text file containing a diagnostic report. The function must return the product of the oxygen generator rating and the CO2 scrubber rating calculated from the binary numbers in the diagnostic report.
The return value should be wrapped in the IO monad if needed.
In this interactive session we assume the text files report01.txt
1 and report02.txt
2 to be located in the current directory.
> lifeSupportRating ("report01.txt")
230 :: Int
> lifeSupportRating ("report02.txt")
4203981 :: Int