Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Advent2019;
- import util.AdventOfCode;
- import util.Direction;
- import util.Node;
- import java.util.*;
- public class Day3 extends AdventOfCode {
- Map<Node, Integer> wire1;
- Map<Node, Integer> intersection;
- public Day3(List<String> input) {
- super(input);
- title = "Crossed Wires";
- part1Description = "Closest intersection by distance: ";
- part2Description = "Closest intersection by steps: ";
- }
- @Override
- public Object part1() {
- return intersection.entrySet().stream()
- .map(Map.Entry::getKey)
- .mapToInt(Direction::manhattanDistance)
- .min().getAsInt();
- }
- @Override
- public Object part2() {
- return intersection.entrySet().stream()
- .mapToInt(Map.Entry::getValue)
- .min().getAsInt();
- }
- @Override
- public void parse() {
- wire1 = new HashMap<>();
- intersection = new HashMap<>();
- Node n = new Node(0, 0);
- int steps = 1;
- for (String each : input.get(0).split(",")) {
- Direction current = Direction.getAlt(each);
- for (int i = 0; i < Integer.parseInt(each.substring(1)); i++) {
- n = current.move(n);
- wire1.putIfAbsent(n, steps);
- steps++;
- }
- }
- n = new Node(0, 0);
- steps = 0;
- for (String each : input.get(1).split(",")) {
- Direction current = Direction.getAlt(each);
- for (int i = 0; i < Integer.parseInt(each.substring(1)); i++) {
- n = current.move(n);
- steps++;
- if (wire1.containsKey(n)) {
- intersection.putIfAbsent(n, steps + wire1.get(n));
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment