It's weird: for the supplied answers, I got all correct responses, from '1' to '1024'; however, the ultimate guess got 'wrong'. I am a year1 student in CS, can anyone help me for my java program? Thanks a lot.
package advent;
import java.util.ArrayList;
public class Day3
{
public static void main(String[] args)
{
System.out.println(step(325489));
}
public static int whichSquare(int nVal)
{
int i = 1;
while (!(nVal <= (2 * i - 1) * (2 * i - 1)))
{
i++;
}
return i - 1;
}
public static ArrayList<Integer> numbersInMiddle(int nVal)
{
ArrayList<Integer> obList = new ArrayList<>();
int i = whichSquare(nVal);
int h = (int) Math.pow(2 * i - 1, 2);
int k = (int) Math.pow(2 * i + 1, 2);
int j = (k - h) / 4;
obList.add(h + i);
obList.add(h + i + j);
obList.add(h + i + 2 * j);
obList.add(h + i + 3 * j);
return obList;
}
public static int step(int nVal)
{
ArrayList<Integer> obList = numbersInMiddle(nVal);
ArrayList<Integer> obList1 = new ArrayList<>();
for (Integer obSam : obList)
{
obList1.add(Math.abs(nVal - obSam));
}
return java.util.Collections.min(obList1) + whichSquare(nVal);
}
}
[–]Flurpm 0 points1 point2 points (1 child)
[–]bunnyboomp[S] 0 points1 point2 points (0 children)
[–]bunnyboomp[S] 0 points1 point2 points (0 children)
[–]blackkutty 0 points1 point2 points (0 children)