|
| 1 | +package com.adventofcode.advent2017; |
| 2 | + |
| 3 | +public class Day11 { |
| 4 | + |
| 5 | + static int part1(String input) { |
| 6 | + int sw = 0; |
| 7 | + int s = 0; |
| 8 | + int se = 0; |
| 9 | + int max = 0; |
| 10 | + String[] rows = input.split(","); |
| 11 | + for (String row : rows) { |
| 12 | + switch (row) { |
| 13 | + case "se": |
| 14 | + se++; |
| 15 | + break; |
| 16 | + case "sw": |
| 17 | + sw++; |
| 18 | + break; |
| 19 | + case "s": |
| 20 | + s++; |
| 21 | + break; |
| 22 | + case "ne": |
| 23 | + sw--; |
| 24 | + break; |
| 25 | + case "nw": |
| 26 | + se--; |
| 27 | + break; |
| 28 | + case "n": |
| 29 | + s--; |
| 30 | + break; |
| 31 | + } |
| 32 | + |
| 33 | + // calculate distance |
| 34 | + max = Math.max(max, calcDist(s, sw, se)); |
| 35 | + } |
| 36 | + |
| 37 | + int dist = calcDist(s, sw, se); |
| 38 | + |
| 39 | + System.out.println("shortest=" + dist); |
| 40 | + System.out.println("max=" + max); |
| 41 | + return 0; |
| 42 | + } |
| 43 | + |
| 44 | + static int calcDist(int s, int sw, int se) { |
| 45 | + int se1 = Math.abs(se); |
| 46 | + int s1 = Math.abs(s); |
| 47 | + int sw1 = Math.abs(sw); |
| 48 | + int sub = 0; |
| 49 | + |
| 50 | + if (s > 0 && sw > 0 && se > 0 || |
| 51 | + s > 0 && sw < 0 && se < 0 || |
| 52 | + s < 0 && sw > 0 && se > 0 || |
| 53 | + s < 0 && sw < 0 && se < 0) { |
| 54 | + sub = Math.min(se1, sw1); |
| 55 | + } else if (s > 0 && sw < 0 && se > 0 || |
| 56 | + s < 0 && sw < 0 && se > 0) { |
| 57 | + sub = Math.min(s1, sw1); |
| 58 | + } else if (s > 0 && sw > 0 && se < 0 || |
| 59 | + s < 0 && sw < 0 && se < 0) { |
| 60 | + sub = Math.min(s1, se1); |
| 61 | + } |
| 62 | + |
| 63 | + return s1 + se1 + sw1 - sub; |
| 64 | + } |
| 65 | +} |
0 commit comments