mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
133 lines
4.3 KiB
Java
133 lines
4.3 KiB
Java
//ProjectEuler/Java/Problem28.java
|
|
//Matthew Ellison
|
|
// Created: 09-22-19
|
|
//Modified: 09-22-19
|
|
//What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral
|
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
|
/*
|
|
Copyright (C) 2019 Matthew Ellison
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
|
|
import mattrixwv.Stopwatch;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
public class Problem28{
|
|
private static ArrayList<ArrayList<Integer>> grid; //Holds the grid that we will be filling and searching
|
|
private static Integer sumOfDiagonals = 0; //Holds the sum of the diagonals of the grid
|
|
//Sets up the grid
|
|
private static void setupGrid(){
|
|
grid = new ArrayList<ArrayList<Integer>>();
|
|
//Fill the grid with 0's
|
|
for(Integer cnt = 0;cnt < 1001;++cnt){
|
|
//Add a blank ArrayList
|
|
grid.add(new ArrayList<Integer>());
|
|
for(Integer cnt2 = 0;cnt2 < 1001;++cnt2){
|
|
grid.get(cnt).add(0);
|
|
}
|
|
}
|
|
|
|
Boolean finalLocation = false; //A flag to indicate if the final location to be filled has been reached
|
|
//Set the number that is going to be put at each location
|
|
Integer currentNum = 1;
|
|
//Start with the middle location and set it correctly and advance the tracker to the next number
|
|
Integer xLocation = 500;
|
|
Integer yLocation = 500;
|
|
grid.get(yLocation).set(xLocation, currentNum++);
|
|
//Move right the first time
|
|
++xLocation;
|
|
//Move in a circular pattern until you reach the final location
|
|
while(!finalLocation){
|
|
//Move down until you reach a blank location on the left
|
|
while(!grid.get(yLocation).get(xLocation - 1).equals(0)){
|
|
grid.get(yLocation).set(xLocation, currentNum++);
|
|
++yLocation;
|
|
}
|
|
|
|
//Move left until you reach a blank location above
|
|
while(!grid.get(yLocation - 1).get(xLocation).equals(0)){
|
|
grid.get(yLocation).set(xLocation, currentNum++);
|
|
--xLocation;
|
|
}
|
|
|
|
//Move up until you reach a blank location to the right
|
|
while(!grid.get(yLocation).get(xLocation + 1).equals(0)){
|
|
grid.get(yLocation).set(xLocation, currentNum++);
|
|
--yLocation;
|
|
}
|
|
|
|
//Move right until you reach a blank location below
|
|
while(!grid.get(yLocation + 1).get(xLocation).equals(0)){
|
|
grid.get(yLocation).set(xLocation, currentNum++);
|
|
++xLocation;
|
|
//Check if you are at the final location and break the loop if you are
|
|
if(xLocation.equals(grid.size())){
|
|
finalLocation = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//Finds the sum of teh diagonals in the grid
|
|
private static void findSum(){
|
|
//Start at teh top corners and work your way down moving toward the opposite side
|
|
Integer leftSide = 0;
|
|
Integer rightSide = grid.size() - 1;
|
|
Integer row = 0;
|
|
while(row < grid.size()){
|
|
//This ensures the middle location is only counted once
|
|
if(Integer.compare(leftSide, rightSide) == 0){
|
|
sumOfDiagonals += grid.get(row).get(leftSide);
|
|
}
|
|
else{
|
|
sumOfDiagonals += grid.get(row).get(leftSide);
|
|
sumOfDiagonals += grid.get(row).get(rightSide);
|
|
}
|
|
++row;
|
|
++leftSide;
|
|
--rightSide;
|
|
}
|
|
}
|
|
|
|
public static void main(String args[]){
|
|
//Setup the variables
|
|
Stopwatch timer = new Stopwatch();
|
|
|
|
//Start the timer
|
|
timer.start();
|
|
|
|
//Setup the grid
|
|
setupGrid();
|
|
//Find the sum of the diagonals in the grid
|
|
findSum();
|
|
|
|
//Stop the timer
|
|
timer.stop();
|
|
|
|
//Print the restuls
|
|
System.out.printf("The sum of the diagonals in the given grid is %d\n", sumOfDiagonals);
|
|
System.out.println("It took " + timer.getStr() + " to run this algorithm");
|
|
}
|
|
}
|
|
|
|
/* Results:
|
|
The sum of the diagonals in the given grid is 669171001
|
|
It took 158.348 milliseconds to run this algorithm
|
|
*/
|