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 pathDay3.java
153 lines (133 loc) · 3.62 KB
/
Day3.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.togetherjava.adventofcode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import com.togetherjava.adventofcode.util.ResourceLoader;
public class Day3 {
private static List<String> data = ResourceLoader.getInput("Day3Inputs.txt");
public static void main(String[] args) {
part1();
System.out.println("Calculating part 2 - this may take a while.....");
part2();
}
public static void part1() {
Set<Tile> wireA = new HashSet<>();
Set<Tile> wireB = new HashSet<>();
parseWire(data.get(0).split(","), wireA);
parseWire(data.get(1).split(","), wireB);
wireA.retainAll(wireB);
int recordDistance = Integer.MAX_VALUE;
for (Tile t : wireA) {
int distance = getManhattanDistance(0, 0, t.getX(), t.getY());
if (distance < recordDistance && distance != 0) {
recordDistance = distance;
}
}
System.out.println("Part 1 answer) " + recordDistance);
}
public static void part2() {
List<Tile> wireA = new ArrayList<>();
List<Tile> wireB = new ArrayList<>();
parseWire(data.get(0).split(","), wireA);
parseWire(data.get(1).split(","), wireB);
int recordSum = Integer.MAX_VALUE;
for (int i = 0; i < wireA.size(); i++) {
for (int j = 0; j < wireB.size(); j++) {
if (wireA.get(i).equals(wireB.get(j))) {
if (i + j < recordSum && i + j != 0) {
recordSum = i + j;
}
}
}
}
System.out.println("Part 2 answer) " + recordSum);
}
public static int getManhattanDistance(int x, int y, int x2, int y2) {
return Math.abs(x2 - x) + Math.abs(y2 - y);
}
public static void parseWire(String[] data, Collection<Tile> wire) {
int x = 0;
int y = 0;
for (String w : data) {
String direction = w.substring(0, 1);
int moves = Integer.parseInt(w.substring(1));
if (direction.equals("U")) {
wire.addAll(getTilesBottomToTop(x, y, moves));
y -= moves;
} else if (direction.equals("D")) {
wire.addAll(getTilesTopToBottom(x, y, moves));
y += moves;
} else if (direction.equals("L")) {
wire.addAll(getTilesRightToLeft(x, y, moves));
x -= moves;
} else if (direction.equals("R")) {
wire.addAll(getTilesLeftToRight(x, y, moves));
x += moves;
}
}
}
public static List<Tile> getTilesLeftToRight(int x, int y, int moves) {
List<Tile> tiles = new ArrayList<>();
for(int i = x; i < x + moves; i++) {
tiles.add(new Tile(i, y));
}
return tiles;
}
public static List<Tile> getTilesRightToLeft(int x, int y, int moves) {
List<Tile> tiles = new ArrayList<>();
for(int i = x; i > x - moves; i--) {
tiles.add(new Tile(i, y));
}
return tiles;
}
public static List<Tile> getTilesTopToBottom(int x, int y, int moves) {
List<Tile> tiles = new ArrayList<>();
for(int i = y; i < y + moves; i++) {
tiles.add(new Tile(x, i));
}
return tiles;
}
public static List<Tile> getTilesBottomToTop(int x, int y, int moves) {
List<Tile> tiles = new ArrayList<>();
for(int i = y; i > y - moves; i--) {
tiles.add(new Tile(x, i));
}
return tiles;
}
private static class Tile {
private int x;
private int y;
public Tile(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Tile other = (Tile) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}
}