logo CodeStepByStep logo

joltageAdapters

Write a method named joltageAdapters that processes a file of integers representing power adapter "joltage" ratings, looking for differences in joltage between adapters. Your method accepts a string representing a file name as a parameter where each line contains a single integer representing one adapter's joltage rating. Your goal is to find an order in which you can jump upward along the joltage values, increasing by 1, 2, or 3 at each jump, until you reach the largest value. You start from a joltage of 0 and implicitly have a maximum adapter whose joltage is 3 higher than the highest value in the file. You must use all of the adapter values.

For example, suppose the file named joltage.txt contains the following values:

16
10
15
5
1
11
7
19
6
12
4

You can jump up the joltage values in the order 0, 1, 4, 5, 6, 7, 10, 11, 12, 15, 16, 19, and finally 22. In this sequence, some of these jumps increase by +1 (such as 4 to 5), and others increase by +3 (such as 12 to 15). You should count up the number of +1 jumps and multiply it by the number of +3 jumps and return this as your result. In our example, there are 7 jumps of +1 and 5 jumps of +3. (Note that we count the jump from the initial 0 to 1 as a +1 jump, and we count the final jump from 19 to our implicit 22 adapter as a +3 jump.) So the call of joltageAdapters("joltage.txt") should return 7 * 5 or 35.

You may assume that the file exists and is readable, that it follows the format described above, that there will be at least two integers in the file, and that there will be a valid path by which to jump up through all values.

(This exercise is based on the Advent of Code 2020, day 10.)

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.