-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManualDecoder.java
More file actions
136 lines (126 loc) · 4.42 KB
/
ManualDecoder.java
File metadata and controls
136 lines (126 loc) · 4.42 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Solution to Day 13: Transparent Origami.
*
* @author bluebillxp
*/
public class ManualDecoder {
private static class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
List<String> input = AdventHelper.readInput("input-day13-transparent-origami.txt");
// List<String> input = AdventHelper.readInput("input-day13-test.txt");
System.out.println("Day 13: Transparent Origami: " + input.size() + " points loaded.");
System.out.println("Day 13: Transparent Origami --- Part One ---");
System.out.println("How many dots are visible after completing just the first fold instruction on your transparent paper?");
final int answerOne = solutionPartOne(input);
System.out.println("Answer: " + answerOne);
System.out.println("\nDay 13: Transparent Origami --- Part Two ---");
System.out.println("What code do you use to activate the infrared thermal imaging camera system?");
final int answerTwo = solutionPartTwo(input);
System.out.println("Answer: " + answerTwo);
}
private static int solutionPartOne(List<String> input) {
Set<String> uniquePoints = new HashSet<>();
for (String line : input) {
if (line.isEmpty()) {
break;
}
String[] xy = line.split(",");
Point point = new Point(Integer.valueOf(xy[0]), Integer.valueOf(xy[1]));
Point newPoint = projectFirstFold(point);
if (newPoint != null) {
uniquePoints.add(newPoint.x + "," + newPoint.y);
}
}
return uniquePoints.size();
}
private static Point projectFirstFold(Point point) {
// fold along x=655
int firstFoldX = 655;
Point newPoint = new Point(point.x, point.y);
if (newPoint.x < firstFoldX) {
return newPoint;
}
// Dots will never appear exactly on a fold line.
if (newPoint.x == firstFoldX) {
return null;
}
newPoint.x = firstFoldX * 2 - newPoint.x;
return newPoint;
}
private static int solutionPartTwo(List<String> input) {
Set<String> uniquePoints = new HashSet<>();
for (String line : input) {
if (line.isEmpty()) {
break;
}
String[] xy = line.split(",");
Point point = new Point(Integer.valueOf(xy[0]), Integer.valueOf(xy[1]));
Point newPoint = projectAll(point);
if (newPoint != null) {
uniquePoints.add(newPoint.x + "," + newPoint.y);
}
}
int totalX = 81;
int totalY = 6;
for (int y = 0; y < totalY; y++) {
char[] line = new char[totalX];
for (int x = 0; x < totalX; x++) {
if (uniquePoints.contains(x + "," + y)) {
line[x] = '*';
} else {
line[x] = ' ';
}
}
System.out.println(new String(line));
}
return uniquePoints.size();
}
private static Point projectAll(Point point) {
// fold along x=655
// fold along y=447
// fold along x=327
// fold along y=223
// fold along x=163
// fold along y=111
// fold along x=81
// fold along y=55
// fold along x=40
// fold along y=27
// fold along y=13
// fold along y=6
int[] foldXTimes = new int[] {655, 327, 163, 81, 40};
int[] foldYTimes = new int[] {447, 223, 111, 55, 27, 13, 6};
Point newPoint = new Point(point.x, point.y);
for (int foldX : foldXTimes) {
if (newPoint.x < foldX) {
continue;
}
// Dots will never appear exactly on a fold line.
if (newPoint.x == foldX) {
return null;
}
newPoint.x = foldX * 2 - newPoint.x;
}
for (int foldY : foldYTimes) {
if (newPoint.y < foldY) {
continue;
}
// Dots will never appear exactly on a fold line.
if (newPoint.y == foldY) {
return null;
}
newPoint.y = foldY * 2 - newPoint.y;
}
return newPoint;
}
}