From f11c51f1527c4415dcf178143ab7aae1957a4c26 Mon Sep 17 00:00:00 2001 From: TheSexyNerd <midnite-pk@hotmail.com> Date: Tue, 3 Dec 2019 01:17:42 -0500 Subject: [PATCH 1/6] day 3 (sorta) --- resources/Day3Inputs.txt | 2 + src/com/togetherjava/adventofcode/Day1.java | 4 +- src/com/togetherjava/adventofcode/Day2.java | 34 ++++-------- .../togetherjava/adventofcode/day3/Day3.java | 53 +++++++++++++++++++ .../togetherjava/adventofcode/day3/Tile.java | 30 +++++++++++ 5 files changed, 97 insertions(+), 26 deletions(-) create mode 100644 resources/Day3Inputs.txt create mode 100644 src/com/togetherjava/adventofcode/day3/Day3.java create mode 100644 src/com/togetherjava/adventofcode/day3/Tile.java diff --git a/resources/Day3Inputs.txt b/resources/Day3Inputs.txt new file mode 100644 index 0000000..170a399 --- /dev/null +++ b/resources/Day3Inputs.txt @@ -0,0 +1,2 @@ +R1006,D541,R261,U378,L530,U165,L175,U143,R162,D504,R985,U33,R544,D168,L498,D549,R88,D243,L36,U944,R261,D91,L957,D579,L224,D732,R312,U378,R82,D200,L510,U747,R588,U667,L495,D147,L100,U482,R896,D711,L513,U44,L685,U547,L132,D23,R139,U786,L908,U912,R531,U564,L970,D562,R422,U919,R108,D275,R431,U697,L85,D762,L25,D633,R878,U566,L550,D288,L29,D788,R930,U619,L612,U228,R25,D133,R219,U367,L889,U735,L994,U513,R34,D429,L750,U83,R204,U68,R769,D833,L545,D621,L747,U714,R655,U112,L629,D353,L450,D588,R775,U493,L252,D486,L405,D350,R970,D73,L750,D731,L962,D242,R947,D348,L252,D392,L94,U970,R616,U505,L782,D375,R849,U971,R57,D25,R68,U174,L670,U735,R66,D994,R868,U285,L866,U433,L169,D575,L374,U169,R180,D251,R671,D703,R708,D60,L251,D164,L106,U974,R670,U760,L235,U377,R318,U294,L421,D904,L571,U157,R428,D416,L237,D850,L827,U702,L134,D67,R327,U976,L307,D454,L646,U919,L92,D523,R828,D544,L557,D142,L671,D862,R118,U244,L667,U356,L554,U969,R348,D895,L735,D948,R920,U470,R819,D256,R169,D410,R977,U487,L64,U466,L574,U891,R29,D767,L224,D922,L782,U433,L478,U582,L603,U339,L658,U188,L95,U766,R958,U313,L881,D869,L633,U551,R270,U444,R399,D698,L923,U213,R245,D486,R34,U514,R450,U739,R102,U181,L826,D839,L948,D11,R51,U146,R415,U683,R352,U387,R158,D88,L576,U600,R955,D22,R884,D772,L576,D74,L937,U832,R198,D721,R393,U847,R828,U975,L452,U796,R950,U568,R117,U114,L983,U514,R564,U569,L141,D464,R463,U635,L772,U634,R614,D160,R388,D550,L933,D832,R94,D855,L18,U241,L805,U517,R384,D464,L271,U788,R718,U495,R103 +L1000,D65,L329,D227,R798,U36,R263,D232,R771,D768,R223,D898,L637,U402,L867,U694,R362,U199,L769,U956,L180,U123,L495,U660,L861,D652,R222,D166,R47,D766,R709,U859,L639,U841,L407,D392,R503,D596,R614,D448,L340,D562,L913,U942,L426,D84,R385,U202,R676,U379,L231,D124,L568,D134,L271,D777,R765,U979,R678,D478,R307,D568,L318,D705,R787,U322,R233,D423,L617,U955,R32,U989,R356,U922,R444,U443,R136,U140,L298,U348,L121,U332,R285,D302,L844,D234,L468,U395,R20,D245,L583,U173,L928,U598,L383,D188,L945,D382,L929,D181,L650,U394,L938,U805,L680,U676,R136,U925,L899,U990,R661,D621,R612,D587,R609,U560,R650,D416,L285,D152,R906,U47,L721,D206,L602,U258,R667,U443,L291,D375,L977,D148,R394,U758,L43,D953,R143,D60,R851,D887,R718,D505,R407,D321,R140,D675,L42,U235,L626,D673,L271,D398,L190,U30,L225,D612,R896,U757,L340,D280,L742,U188,L372,D7,R677,U248,R694,U581,L220,U372,R497,U89,R952,D221,L71,D962,L992,U420,R741,U96,R625,U794,L602,U229,R635,D585,R119,U501,R640,D283,L963,U385,L967,D503,L453,D578,L465,D318,L968,U979,L650,D894,L210,U855,R298,D66,R378,D223,L475,D950,L417,D276,L494,D690,R516,D352,L603,U211,R171,U553,L437,U865,L378,D223,R814,D779,L780,D738,R920,D462,L230,U574,L880,D252,R710,D476,L184,U635,R453,U115,R96,U169,R995,D523,R562,D480,L791,U865,R568,D149,L539,U610,R107,D604,R95,D982,R360,U141,L567,D555,R481,U716,R753,D576,R54,D343,R663,U676,R907,D202,R230,U827,L583,U937,R818,D579,R502,D713,R61,U402,L527,D955,R117,U214,R580,U636,R721,U55,L899,U667,R595,U790,L384,U416,L375,D1,L653,U611,L187,D256,L931 diff --git a/src/com/togetherjava/adventofcode/Day1.java b/src/com/togetherjava/adventofcode/Day1.java index 6139ce4..e284f1d 100644 --- a/src/com/togetherjava/adventofcode/Day1.java +++ b/src/com/togetherjava/adventofcode/Day1.java @@ -9,8 +9,8 @@ public class Day1 { public static void main(String[] args) { Supplier<Stream<Double>> input = () -> ResourceLoader.getInput("Day1Inputs.txt").stream().map(Double::parseDouble); - double part1 = input.get().map(Day1::calculateFuel).reduce(0D, Double::sum); - double part2 = input.get().map(Day1::recursiveCalculateFuel).reduce(0D, Double::sum); + double part1 = input.get().mapToDouble(Day1::calculateFuel).sum(); + double part2 = input.get().mapToDouble(Day1::recursiveCalculateFuel).sum(); System.out.println("Part 1 answer: " + part1); System.out.println("Part 2 answer: " + part2); } diff --git a/src/com/togetherjava/adventofcode/Day2.java b/src/com/togetherjava/adventofcode/Day2.java index 52c69cf..b76f961 100644 --- a/src/com/togetherjava/adventofcode/Day2.java +++ b/src/com/togetherjava/adventofcode/Day2.java @@ -12,18 +12,14 @@ public static void main(String[] args) { for (int i = 0; i < data.length; i++) { data[i] = Integer.parseInt(input[i]); } - System.out.println("Part 1)"); - int[] part1 = Arrays.copyOf(data, data.length); - //part 1 noun = 2 verb = 12 - System.out.println(calculate(part1, 12, 2)[0]); + int[] dataClone = Arrays.copyOf(data, data.length); + System.out.println("Part 1) " + calculate(dataClone, 12, 2)[0]); for(int verb = 0; verb < 100; verb++) { for(int noun = 0; noun < 100; noun++) { - int[] part2 = Arrays.copyOf(data, data.length); - int[] temp = calculate(part2, noun, verb); - if(temp[0] == 19690720) { - System.out.println("Part 2)"); - System.out.printf("FOUND NOUN = %d VERB = %d\n", noun, verb); - System.out.printf("100 * %d + %d = %d", noun, verb, 100 * noun + verb); + dataClone = Arrays.copyOf(data, data.length); + int[] part2 = calculate(dataClone, noun, verb); + if(part2[0] == 19690720) { + System.out.printf("Part 2) 100 * %d + %d = %d", noun, verb, 100 * noun + verb); } } } @@ -32,29 +28,19 @@ public static void main(String[] args) { public static int[] calculate(int[] data, int noun, int verb) { data[1] = noun; data[2] = verb; - int i = 0; - while (i < data.length) { + for(int i = 0; i < data.length; i += 4) { int opcode = data[i]; - if (opcode == 1) { + if(opcode == 1 || opcode == 2) { int aPosition = data[i + 1]; int bPosition = data[i + 2]; int positionToStore = data[i + 3]; int a = data[aPosition]; int b = data[bPosition]; - data[positionToStore] = a + b; - i += 4; - } else if (opcode == 2) { - int aPosition = data[i + 1]; - int bPosition = data[i + 2]; - int positionToStore = data[i + 3]; - int a = data[aPosition]; - int b = data[bPosition]; - data[positionToStore] = a * b; - i += 4; + data[positionToStore] = opcode == 1 ? a + b : a * b; } else if (opcode == 99) { break; } } return data; } -} +} \ No newline at end of file diff --git a/src/com/togetherjava/adventofcode/day3/Day3.java b/src/com/togetherjava/adventofcode/day3/Day3.java new file mode 100644 index 0000000..b31da50 --- /dev/null +++ b/src/com/togetherjava/adventofcode/day3/Day3.java @@ -0,0 +1,53 @@ +package com.togetherjava.adventofcode.day3; + +import java.util.ArrayList; +import java.util.List; + +import com.togetherjava.adventofcode.util.ResourceLoader; + +public class Day3 { + + private static List<Tile> wireA = new ArrayList<>(); + private static List<Tile> wireB = new ArrayList<>(); + + public static void main(String[] args) { + List<String> data = ResourceLoader.getInput("Day3Inputs.txt"); + 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(t.getX(), t.getY(), 0, 0); + if (distance < recordDistance) { + recordDistance = distance; + } + } + + System.out.println(recordDistance); + } + + 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, List<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")) { + y -= moves; + } else if (direction.equals("D")) { + y += moves; + } else if (direction.equals("L")) { + x -= moves; + } else if (direction.equals("R")) { + x += moves; + } + wire.add(new Tile(x, y)); + } + } +} \ No newline at end of file diff --git a/src/com/togetherjava/adventofcode/day3/Tile.java b/src/com/togetherjava/adventofcode/day3/Tile.java new file mode 100644 index 0000000..16b539a --- /dev/null +++ b/src/com/togetherjava/adventofcode/day3/Tile.java @@ -0,0 +1,30 @@ +package com.togetherjava.adventofcode.day3; + +public class Tile { + + private int x, 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 boolean equals(Object obj) { + Tile tile = (Tile) obj; + return tile.getX() == x && tile.getY() == y; + } + + @Override + public String toString() { + return String.format("X = %d Y = %d", x, y); + } +} From 08acf364f6579b7ee82cf9751f20027fa432f3e1 Mon Sep 17 00:00:00 2001 From: TheSexyNerd <midnite-pk@hotmail.com> Date: Tue, 3 Dec 2019 02:18:46 -0500 Subject: [PATCH 2/6] first part of day 3 bruteforced :) --- .../togetherjava/adventofcode/day3/Day3.java | 31 +++++++++++++------ .../togetherjava/adventofcode/day3/Tile.java | 25 +++++++++++++-- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/com/togetherjava/adventofcode/day3/Day3.java b/src/com/togetherjava/adventofcode/day3/Day3.java index b31da50..a04f29e 100644 --- a/src/com/togetherjava/adventofcode/day3/Day3.java +++ b/src/com/togetherjava/adventofcode/day3/Day3.java @@ -1,16 +1,18 @@ package com.togetherjava.adventofcode.day3; -import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import com.togetherjava.adventofcode.util.ResourceLoader; public class Day3 { - private static List<Tile> wireA = new ArrayList<>(); - private static List<Tile> wireB = new ArrayList<>(); + private static Set<Tile> wireA = new HashSet<>(); + private static Set<Tile> wireB = new HashSet<>(); public static void main(String[] args) { + long then = System.currentTimeMillis(); List<String> data = ResourceLoader.getInput("Day3Inputs.txt"); parseWire(data.get(0).split(","), wireA); parseWire(data.get(1).split(","), wireB); @@ -19,35 +21,46 @@ public static void main(String[] args) { int recordDistance = Integer.MAX_VALUE; for (Tile t : wireA) { - int distance = getManhattanDistance(t.getX(), t.getY(), 0, 0); - if (distance < recordDistance) { + int distance = getManhattanDistance(0, 0, t.getX(), t.getY()); + if (distance < recordDistance && distance != 0) { recordDistance = distance; } } - - System.out.println(recordDistance); + System.out.println("Part 1 answer) redacted"); + System.out.printf("Done in %d ms", System.currentTimeMillis() - then); } 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, List<Tile> wire) { + public static void parseWire(String[] data, Set<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")) { + for(int i = y; i > y - moves; i--) { + wire.add(new Tile(x, i)); + } y -= moves; } else if (direction.equals("D")) { + for(int i = y; i < y + moves; i++) { + wire.add(new Tile(x, i)); + } y += moves; } else if (direction.equals("L")) { + for(int i = x; i > x - moves; i--) { + wire.add(new Tile(i, y)); + } x -= moves; } else if (direction.equals("R")) { + for(int i = x; i < x + moves; i++) { + wire.add(new Tile(i, y)); + } x += moves; } - wire.add(new Tile(x, y)); } } } \ No newline at end of file diff --git a/src/com/togetherjava/adventofcode/day3/Tile.java b/src/com/togetherjava/adventofcode/day3/Tile.java index 16b539a..93ddf91 100644 --- a/src/com/togetherjava/adventofcode/day3/Tile.java +++ b/src/com/togetherjava/adventofcode/day3/Tile.java @@ -17,12 +17,31 @@ public int getY() { return y; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + x; + result = prime * result + y; + return result; + } + @Override public boolean equals(Object obj) { - Tile tile = (Tile) obj; - return tile.getX() == x && tile.getY() == y; + 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; } - + @Override public String toString() { return String.format("X = %d Y = %d", x, y); From 28499b46b35715df69282c48299e13ce3ca4ba8e Mon Sep 17 00:00:00 2001 From: TheSexyNerd <midnite-pk@hotmail.com> Date: Tue, 3 Dec 2019 02:25:31 -0500 Subject: [PATCH 3/6] made an oopsie --- src/com/togetherjava/adventofcode/day3/Day3.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/com/togetherjava/adventofcode/day3/Day3.java b/src/com/togetherjava/adventofcode/day3/Day3.java index a04f29e..5701d05 100644 --- a/src/com/togetherjava/adventofcode/day3/Day3.java +++ b/src/com/togetherjava/adventofcode/day3/Day3.java @@ -22,11 +22,12 @@ public static void main(String[] args) { int recordDistance = Integer.MAX_VALUE; for (Tile t : wireA) { int distance = getManhattanDistance(0, 0, t.getX(), t.getY()); + //check if doesn't equal zero because the 2 tiles that intersect closest to the main port IS the main port if (distance < recordDistance && distance != 0) { recordDistance = distance; } } - System.out.println("Part 1 answer) redacted"); + System.out.println("Part 1 answer) " + recordDistance); System.out.printf("Done in %d ms", System.currentTimeMillis() - then); } From 9247ea1c4fa4b329ed67e407d9e249d4e5a8841f Mon Sep 17 00:00:00 2001 From: TheSexyNerd <midnite-pk@hotmail.com> Date: Tue, 3 Dec 2019 05:01:23 -0500 Subject: [PATCH 4/6] finished day 3 that was stupid --- .../togetherjava/adventofcode/day3/Day3.java | 49 +++++++++++++------ 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/src/com/togetherjava/adventofcode/day3/Day3.java b/src/com/togetherjava/adventofcode/day3/Day3.java index 5701d05..21ce091 100644 --- a/src/com/togetherjava/adventofcode/day3/Day3.java +++ b/src/com/togetherjava/adventofcode/day3/Day3.java @@ -1,5 +1,7 @@ package com.togetherjava.adventofcode.day3; +import java.util.ArrayList; +import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -8,56 +10,75 @@ public class Day3 { - private static Set<Tile> wireA = new HashSet<>(); - private static Set<Tile> wireB = new HashSet<>(); - + private static List<String> data = ResourceLoader.getInput("Day3Inputs.txt"); + public static void main(String[] args) { - long then = System.currentTimeMillis(); - List<String> data = ResourceLoader.getInput("Day3Inputs.txt"); + 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()); - //check if doesn't equal zero because the 2 tiles that intersect closest to the main port IS the main port if (distance < recordDistance && distance != 0) { recordDistance = distance; } } System.out.println("Part 1 answer) " + recordDistance); - System.out.printf("Done in %d ms", System.currentTimeMillis() - then); + } + + 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, Set<Tile> wire) { + 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")) { - for(int i = y; i > y - moves; i--) { + for (int i = y; i > y - moves; i--) { wire.add(new Tile(x, i)); } y -= moves; } else if (direction.equals("D")) { - for(int i = y; i < y + moves; i++) { + for (int i = y; i < y + moves; i++) { wire.add(new Tile(x, i)); } y += moves; } else if (direction.equals("L")) { - for(int i = x; i > x - moves; i--) { + for (int i = x; i > x - moves; i--) { wire.add(new Tile(i, y)); } x -= moves; } else if (direction.equals("R")) { - for(int i = x; i < x + moves; i++) { + for (int i = x; i < x + moves; i++) { wire.add(new Tile(i, y)); } x += moves; From a604c39a4d9ff53937ad3d31830b06b91f7681d1 Mon Sep 17 00:00:00 2001 From: TheSexyNerd <midnite-pk@hotmail.com> Date: Tue, 3 Dec 2019 16:54:49 -0500 Subject: [PATCH 5/6] refactored --- src/com/togetherjava/adventofcode/Day1.java | 16 +++- src/com/togetherjava/adventofcode/Day2.java | 22 ++++- .../adventofcode/{day3 => }/Day3.java | 94 ++++++++++++++++--- .../togetherjava/adventofcode/day3/Tile.java | 49 ---------- 4 files changed, 110 insertions(+), 71 deletions(-) rename src/com/togetherjava/adventofcode/{day3 => }/Day3.java (53%) delete mode 100644 src/com/togetherjava/adventofcode/day3/Tile.java diff --git a/src/com/togetherjava/adventofcode/Day1.java b/src/com/togetherjava/adventofcode/Day1.java index e284f1d..1af5b21 100644 --- a/src/com/togetherjava/adventofcode/Day1.java +++ b/src/com/togetherjava/adventofcode/Day1.java @@ -1,6 +1,5 @@ package com.togetherjava.adventofcode; -import java.util.function.Supplier; import java.util.stream.Stream; import com.togetherjava.adventofcode.util.ResourceLoader; @@ -8,10 +7,19 @@ public class Day1 { public static void main(String[] args) { - Supplier<Stream<Double>> input = () -> ResourceLoader.getInput("Day1Inputs.txt").stream().map(Double::parseDouble); - double part1 = input.get().mapToDouble(Day1::calculateFuel).sum(); - double part2 = input.get().mapToDouble(Day1::recursiveCalculateFuel).sum(); + part1(); + part2(); + } + + public static void part1() { + Stream<Double> input = ResourceLoader.getInput("Day1Inputs.txt").stream().map(Double::parseDouble); + double part1 = input.mapToDouble(Day1::calculateFuel).sum(); System.out.println("Part 1 answer: " + part1); + } + + public static void part2() { + Stream<Double> input = ResourceLoader.getInput("Day1Inputs.txt").stream().map(Double::parseDouble); + double part2 = input.mapToDouble(Day1::recursiveCalculateFuel).sum(); System.out.println("Part 2 answer: " + part2); } diff --git a/src/com/togetherjava/adventofcode/Day2.java b/src/com/togetherjava/adventofcode/Day2.java index b76f961..d50fdff 100644 --- a/src/com/togetherjava/adventofcode/Day2.java +++ b/src/com/togetherjava/adventofcode/Day2.java @@ -6,17 +6,29 @@ public class Day2 { - public static void main(String[] args) { - String[] input = ResourceLoader.getInput("Day2Inputs.txt").get(0).split(","); - int[] data = new int[input.length]; - for (int i = 0; i < data.length; i++) { + private static String[] input = ResourceLoader.getInput("Day2Inputs.txt").get(0).split(","); + private static int[] data = new int[input.length]; + + static { + for(int i = 0; i < data.length; i++) { data[i] = Integer.parseInt(input[i]); } + } + + public static void main(String[] args) { + part1(); + part2(); + } + + public static void part1() { int[] dataClone = Arrays.copyOf(data, data.length); System.out.println("Part 1) " + calculate(dataClone, 12, 2)[0]); + } + + public static void part2() { for(int verb = 0; verb < 100; verb++) { for(int noun = 0; noun < 100; noun++) { - dataClone = Arrays.copyOf(data, data.length); + int[] dataClone = Arrays.copyOf(data, data.length); int[] part2 = calculate(dataClone, noun, verb); if(part2[0] == 19690720) { System.out.printf("Part 2) 100 * %d + %d = %d", noun, verb, 100 * noun + verb); diff --git a/src/com/togetherjava/adventofcode/day3/Day3.java b/src/com/togetherjava/adventofcode/Day3.java similarity index 53% rename from src/com/togetherjava/adventofcode/day3/Day3.java rename to src/com/togetherjava/adventofcode/Day3.java index 21ce091..385e61a 100644 --- a/src/com/togetherjava/adventofcode/day3/Day3.java +++ b/src/com/togetherjava/adventofcode/Day3.java @@ -1,4 +1,4 @@ -package com.togetherjava.adventofcode.day3; +package com.togetherjava.adventofcode; import java.util.ArrayList; import java.util.Collection; @@ -63,26 +63,94 @@ public static void parseWire(String[] data, Collection<Tile> wire) { String direction = w.substring(0, 1); int moves = Integer.parseInt(w.substring(1)); if (direction.equals("U")) { - for (int i = y; i > y - moves; i--) { - wire.add(new Tile(x, i)); - } + wire.addAll(getTilesBottomToTop(x, y, moves)); y -= moves; } else if (direction.equals("D")) { - for (int i = y; i < y + moves; i++) { - wire.add(new Tile(x, i)); - } + wire.addAll(getTilesTopToBottom(x, y, moves)); y += moves; } else if (direction.equals("L")) { - for (int i = x; i > x - moves; i--) { - wire.add(new Tile(i, y)); - } + wire.addAll(getTilesRightToLeft(x, y, moves)); x -= moves; } else if (direction.equals("R")) { - for (int i = x; i < x + moves; i++) { - wire.add(new Tile(i, y)); - } + 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() { + final int prime = 31; + int result = 1; + result = prime * result + x; + result = prime * result + y; + return result; + } + + @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; + } + } } \ No newline at end of file diff --git a/src/com/togetherjava/adventofcode/day3/Tile.java b/src/com/togetherjava/adventofcode/day3/Tile.java deleted file mode 100644 index 93ddf91..0000000 --- a/src/com/togetherjava/adventofcode/day3/Tile.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.togetherjava.adventofcode.day3; - -public class Tile { - - private int x, 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() { - final int prime = 31; - int result = 1; - result = prime * result + x; - result = prime * result + y; - return result; - } - - @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; - } - - @Override - public String toString() { - return String.format("X = %d Y = %d", x, y); - } -} From ca6ddab0b30dea379553fcb16c8cf01f5f25486b Mon Sep 17 00:00:00 2001 From: TheSexyNerd <midnite-pk@hotmail.com> Date: Wed, 4 Dec 2019 03:07:54 -0500 Subject: [PATCH 6/6] finished day 4 --- src/com/togetherjava/adventofcode/Day3.java | 7 +--- src/com/togetherjava/adventofcode/Day4.java | 42 +++++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/com/togetherjava/adventofcode/Day4.java diff --git a/src/com/togetherjava/adventofcode/Day3.java b/src/com/togetherjava/adventofcode/Day3.java index 385e61a..891db93 100644 --- a/src/com/togetherjava/adventofcode/Day3.java +++ b/src/com/togetherjava/adventofcode/Day3.java @@ -4,6 +4,7 @@ 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; @@ -130,11 +131,7 @@ public int getY() { @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + x; - result = prime * result + y; - return result; + return Objects.hash(x, y); } @Override diff --git a/src/com/togetherjava/adventofcode/Day4.java b/src/com/togetherjava/adventofcode/Day4.java new file mode 100644 index 0000000..d549cc0 --- /dev/null +++ b/src/com/togetherjava/adventofcode/Day4.java @@ -0,0 +1,42 @@ +package com.togetherjava.adventofcode; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class Day4 { + + public static boolean isValidNumberPart1(int number) { + int[] digits = String.valueOf(number).chars().map(n -> n - 48).toArray(); + int lastDigit = digits[0]; + boolean hasConsecutiveDigits = false; + for(int i = 1; i < digits.length; i++) { + int digit = digits[i]; + if (digit < lastDigit) { + return false; + } + lastDigit = digit; + if (digit == digits[i - 1]) { + hasConsecutiveDigits = true; + } + } + return hasConsecutiveDigits; + } + + /** + * Written by Discord user and bae @Kemikals#3177 + * I quite liked this better than mine so I decided to use this one instead + */ + public static int isValidNumberPart2(int[] valid) { + List<String> stillValid = IntStream.of(valid).boxed().map(Object::toString).filter(f -> f.matches("^((\\d)\\2(?!\\2)\\d*|\\d*(\\d)(?!\\3)(\\d)\\4(?!\\4)\\d*)$")).collect(Collectors.toList()); + return stillValid.size(); + } + + public static void main(String[] args) { + final int LOWER_LIMIT = 138307; + final int UPPER_LIMIT = 654504; + int[] validNumbers = IntStream.range(LOWER_LIMIT, UPPER_LIMIT).filter(Day4::isValidNumberPart1).toArray(); + System.out.println("Part 1) " + validNumbers.length); + System.out.println("Part 2) " + isValidNumberPart2(validNumbers)); + } +} \ No newline at end of file