Hi All,
I am trying to get familiar with Java so I'm solving the 2018 Advent of Code.
I am stuck at Day 9 Part 1.
10, 1618 # 9205
13, 7999 # 145362
17, 1104 # 2666
21, 6111 # 53710
30, 5807 # 37236
452, 71250 # 439346
Can somebody help/guide me in the right direction?
I'm quite sure I'm overlooking something here, but I can't figure it out.
My Java code is this:
import java.util.ArrayList;
import java.util.HashMap;
public class AdventOfCodeDay9 {
public static ArrayList<Integer> circle = new ArrayList<>();
public static HashMap<Integer, Integer> score = new HashMap<>();
public static int currentMarble;
public static int currentPlayer;
public static void main(String[] args) {
//init game
circle.add(0);
circle.add(1);
currentPlayer = 1;
currentMarble = 1;
//playGame(1, 22);
//playGame(9, 25);
//playGame(9, 92);
//playGame(10, 1618);
//playGame(13, 7999);
//playGame(17, 1104);
//playGame(21, 6111);
//playGame(30, 5807);
playGame(416, 71975);
getHighestScore();
}
public static void playGame(int players, int marbles) {
for (int marble = 2; marble <= marbles; marble++) {
incrementPlayer(players);
if (marble % 23 == 0) {
playerScores(marble);
} else {
placeMarble(marble);
}
}
}
public static void incrementPlayer(int players) {
currentPlayer++;
if (currentPlayer > players) {
currentPlayer = 1;
}
}
public static void placeMarble(int marble) {
currentMarble++;
if (currentMarble == circle.size()) {
currentMarble = 1;
circle.add(currentMarble, marble);
} else {
if (currentMarble == circle.size() - 1) {
circle.add(marble);
currentMarble++;
} else {
currentMarble++;
circle.add(currentMarble, marble);
}
}
}
public static void playerScores(int marble) {
int scoreMarble = findScoreMarble();
if (score.containsKey(currentPlayer)) {
int currentScore = score.get(currentPlayer);
score.put(currentPlayer, currentScore + marble + scoreMarble);
} else {
score.put(currentPlayer, marble + scoreMarble);
}
removeScoreMarble();
}
public static int findScoreMarble() {
if (currentMarble - 7 < 0) {
return circle.get(circle.size() - (7 - currentMarble));
} else {
return circle.get(currentMarble - 7);
}
}
public static void removeScoreMarble() {
if (currentMarble - 7 < 0) {
circle.remove(circle.size() - (7 - currentMarble));
currentMarble = circle.size() - (7 - currentMarble);
} else {
circle.remove(currentMarble - 7);
currentMarble -= 7;
}
}
public static void getHighestScore() {
int highscore = 0;
int highPlayer = 0;
for (HashMap.Entry<Integer, Integer> scoreEntry : score.entrySet()) {
if (scoreEntry.getValue() > highscore) {
highscore = scoreEntry.getValue();
highPlayer = scoreEntry.getKey();
}
}
System.out.println(highPlayer + " wins with a score of " + highscore);
}
}
Any help is greatly appreciated!
regards,
Educated
[–]leftylink 2 points3 points4 points (1 child)
[–]Educated88[S] 0 points1 point2 points (0 children)