Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.net.URL;
- import java.util.HashMap;
- import java.util.Scanner;
- public class Day3 {
- static HashMap<Point, Integer> wires = new HashMap<>();
- public static void main(String[] args) throws FileNotFoundException {
- URL url = Day3.class.getResource("input");
- File file = new File(url.getPath());
- Scanner scanner = new Scanner(file);
- String[] wire1 = scanner.nextLine().split(",");
- String[] wire2 = scanner.nextLine().split(",");
- parseWire(wire1);
- parseWire(wire2);
- int distance = Integer.MAX_VALUE;
- for (Point crossing:
- wires.keySet()) {
- if (wires.get(crossing.getLocation()) > 1) {
- int calc = Math.abs(crossing.x) + Math.abs(crossing.y);
- if (calc < distance) {
- distance = calc;
- }
- }
- }
- System.out.println(distance);
- }
- static void parseWire(String[] strings) {
- Point current = new Point();
- for (String command:
- strings) {
- char dir = command.charAt(0);
- int len = Integer.parseInt(command.substring(1));
- for (int i = 0; i < len; i++) {
- switch (dir) {
- case 'U': current.y++; break;
- case 'D': current.y--; break;
- case 'L': current.x--; break;
- case 'R': current.x++;
- }
- mapWirePoint(current.getLocation());
- }
- }
- }
- static void mapWirePoint(Point current) {
- if (wires.containsKey(current.getLocation())) {
- int crossing = wires.get(current.getLocation());
- crossing++;
- wires.replace(current, crossing);
- } else {
- wires.put(current.getLocation(), 1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement