-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day20.java
78 lines (65 loc) · 2.39 KB
/
Day20.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
//package org.mrbadaxe.AdventOfCode2022;
import java.util.List;
import java.util.ArrayList;
public class Day20{
public static ArrayList<LongPoint> rotateToIndex(ArrayList<LongPoint> input, int target){
while(input.get(0).getX() != target){
input = rotateLeft(input);
}
return input;
}
public static ArrayList<LongPoint> rotateLeft(ArrayList<LongPoint> input){
input.add(input.remove(0));
return input;
}
public static ArrayList<LongPoint> rotateRight(ArrayList<LongPoint> input){
input.add(0,input.remove(input.size() - 1));
return input;
}
public static ArrayList<LongPoint> mix(ArrayList<LongPoint> input){
for(int k=0;k<input.size();k++){
//if(k%100==0){System.out.print(k+" ");}
input = rotateToIndex(input,k);
LongPoint p = input.remove(0);
for(long j = 0; j < Math.abs(p.getY())%input.size(); j++){
input = (p.getY() > 0 ? rotateLeft(input) : rotateRight(input));
}
input.add(0,p);
}
return input;
}
public static long getPart01(List<String> input){
ArrayList<LongPoint> ciphertext = new ArrayList<LongPoint>();
for(int k=0;k<input.size();k++){
ciphertext.add(new LongPoint((long)k,Long.parseLong(input.get(k))));
}
ciphertext = mix(ciphertext);
while(ciphertext.get(0).getY() != 0){
ciphertext = rotateLeft(ciphertext);
}
long n1 = ciphertext.get(1000%ciphertext.size()).getY();
long n2 = ciphertext.get(2000%ciphertext.size()).getY();
long n3 = ciphertext.get(3000%ciphertext.size()).getY();
System.out.println(n1 + " " + n2 + " " + n3);
return n1+n2+n3;
}
public static long getPart02(List<String> input){
long ENCRYPTION_KEY = 811_589_153L;
ArrayList<LongPoint> ciphertext = new ArrayList<LongPoint>();
for(int k=0;k<input.size();k++){
ciphertext.add(new LongPoint((long)k, Long.parseLong(input.get(k)) * ENCRYPTION_KEY));
}
for(int mixCycles = 0; mixCycles < 10; mixCycles++){
System.out.println("=== PASS " + (mixCycles+1) + " ===");
ciphertext = mix(ciphertext);
}
while(ciphertext.get(0).getY() != 0){
ciphertext = rotateLeft(ciphertext);
}
long n1 = ciphertext.get(1000%ciphertext.size()).getY();
long n2 = ciphertext.get(2000%ciphertext.size()).getY();
long n3 = ciphertext.get(3000%ciphertext.size()).getY();
System.out.println(n1 + " " + n2 + " " + n3);
return n1+n2+n3;
}
}