-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day10.java
81 lines (65 loc) · 1.95 KB
/
Day10.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.amit.aoc.days;
import com.amit.aoc.common.Day;
import org.apache.commons.lang3.time.StopWatch;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
public class Day10 implements Day {
int [] data;
HashMap<Integer, Integer> seen;
public Day10() throws IOException {
this.data = Arrays.stream(readDay(10).split(System.lineSeparator()))
.mapToInt(Integer::parseInt)
.toArray();
seen = new HashMap<>();
}
public static void main(String[] args) throws IOException
{
System.out.println("Day10:");
new Day10().printParts();
}
@Override
public Object part1() throws IOException {
int [] voltages = this.data;
Arrays.sort(voltages);
int ones = 0;
int threes = 0;
int prev = 0;
for(int i = 0; i < voltages.length; i++) {
seen.put(voltages[i], i);
int diff = voltages[i] - prev;
if(diff == 1) {
ones++;
} else if(diff == 3) {
threes++;
}
prev = voltages[i];
}
threes++;
return ones * threes;
}
@Override
public Object part2() throws IOException {
int[] voltages = this.data;
long [] ways = new long[voltages.length];
ways[voltages.length - 1] = 1;
for(int i = voltages.length - 2; i >= 0; i--) {
long sum = 0;
for (int diff = 1 ; diff <= 3; diff++) {
if(seen.containsKey(voltages[i] + diff)) {
sum += ways[seen.get(voltages[i] + diff)];
}
}
ways[i] = sum;
}
long result = 0;
for(int j = 1; j <= 3; j++) {
if(seen.containsKey(j)) {
result += ways[seen.get(j)];
}
}
return result;
}
}