This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDay2.java
58 lines (49 loc) · 1.44 KB
/
Day2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.togetherjava.adventofcode;
import java.util.Arrays;
import com.togetherjava.adventofcode.util.ResourceLoader;
public class Day2 {
private static String[] input = ResourceLoader.getInput("Day2Inputs.txt").get(0).split(",");
private static int[] data = new int[input.length];
static {
for(int i = 0; i < data.length; i++) {
data[i] = Integer.parseInt(input[i]);
}
}
public static void main(String[] args) {
part1();
part2();
}
public static void part1() {
int[] dataClone = Arrays.copyOf(data, data.length);
System.out.println("Part 1) " + calculate(dataClone, 12, 2)[0]);
}
public static void part2() {
for(int verb = 0; verb < 100; verb++) {
for(int noun = 0; noun < 100; noun++) {
int[] dataClone = Arrays.copyOf(data, data.length);
int[] part2 = calculate(dataClone, noun, verb);
if(part2[0] == 19690720) {
System.out.printf("Part 2) 100 * %d + %d = %d", noun, verb, 100 * noun + verb);
}
}
}
}
public static int[] calculate(int[] data, int noun, int verb) {
data[1] = noun;
data[2] = verb;
for(int i = 0; i < data.length; i += 4) {
int opcode = data[i];
if(opcode == 1 || opcode == 2) {
int aPosition = data[i + 1];
int bPosition = data[i + 2];
int positionToStore = data[i + 3];
int a = data[aPosition];
int b = data[bPosition];
data[positionToStore] = opcode == 1 ? a + b : a * b;
} else if (opcode == 99) {
break;
}
}
return data;
}
}