aoc/day02Part2.java

54 lines
1.1 KiB
Java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class day02Part2 {
public static void main(String[] args) throws IOException {
int posHorizontal = 0;
int posDepth = 0;
int aim = 0;
int finalAns = 0; // posHorizontal * posDepth
String line;
String direction;
int movement;
BufferedReader in = new BufferedReader(new FileReader("day02-input.txt"));
line = in.readLine();
while (line != null) {
String[] lineProcess = line.split(" ");
direction = lineProcess[0];
movement = Integer.valueOf(lineProcess[1]).intValue();
if (direction.compareTo("forward") == 0) {
int temp = 0;
posHorizontal += movement;
temp = movement * aim;
posDepth += temp;
}
else if (direction.compareTo("up") == 0) {
// posDepth -= movement;
aim -= movement;
}
else if (direction.compareTo("down") == 0) {
// posDepth += movement;
aim += movement;
}
line = in.readLine();
}
in.close();
finalAns = posHorizontal * posDepth;
System.out.println(finalAns);
}
}