Last active
December 16, 2017 12:09
-
-
Save roccodev/a9c22b06c40c324aed6a622a4a462b12 to your computer and use it in GitHub Desktop.
AdventOfCode2017/Day16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static String[] dancers = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"}; | |
| public static void main(String args[]) throws IOException { | |
| List<String> bloccs = Files.readAllLines(Paths.get(new File("res/day16").getPath())).stream() | |
| .collect(Collectors.toList()); | |
| for(String s : bloccs.get(0).split(",")) { | |
| char mode = s.charAt(0); | |
| System.out.println(mode); | |
| switch(mode) { | |
| case 's': | |
| spin(Integer.parseInt(s.replaceAll("s", ""))); | |
| break; | |
| case 'x': | |
| String[] data = s.replaceAll("x", "").split("/"); | |
| exchange(Integer.parseInt(data[0]), Integer.parseInt(data[1])); | |
| break; | |
| case 'p': | |
| String[] data1 = s.replaceFirst("p", "").split("/"); | |
| partner(data1[0], data1[1]); | |
| break; | |
| } | |
| } | |
| System.out.println(String.join("", dancers)); | |
| } | |
| private static void spin(int x) { | |
| List<String> dancersL = new ArrayList<String>(Arrays.asList(dancers)); | |
| Collections.rotate(dancersL, x); | |
| dancers = dancersL.toArray(dancers); | |
| } | |
| private static void exchange(int x, int y) { | |
| String beforeY = dancers[y]; | |
| dancers[y] = dancers[x]; | |
| dancers[x] = beforeY; | |
| } | |
| private static void partner(String a, String b) { | |
| int indexA = Arrays.asList(dancers).indexOf(a); | |
| int indexB = Arrays.asList(dancers).indexOf(b); | |
| exchange(indexA, indexB); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static String[] dancers = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", | |
| "p" }; | |
| private static List<String> seen = new ArrayList<String>(); | |
| public static void main(String args[]) throws IOException { | |
| List<String> bloccs = Files.readAllLines(Paths.get(new File("res/day16").getPath())).stream() | |
| .collect(Collectors.toList()); | |
| String[] toIterate = bloccs.get(0).split(","); | |
| for (int i = 0; i < 1000000000; i++) { | |
| System.out.println("Running " + i); | |
| System.out.println(seen.size()); | |
| if(seen.contains(String.join("", dancers))) { | |
| System.out.println("Found match"); | |
| System.out.println(seen.get(1000000000 % i)); | |
| break; | |
| } | |
| seen.add(String.join("", dancers)); | |
| for (String s : toIterate) { | |
| char mode = s.charAt(0); | |
| switch (mode) { | |
| case 's': | |
| spin(Integer.parseInt(s.replaceAll("s", ""))); | |
| break; | |
| case 'x': | |
| String[] data = s.replaceAll("x", "").split("/"); | |
| exchange(Integer.parseInt(data[0]), Integer.parseInt(data[1])); | |
| break; | |
| case 'p': | |
| String[] data1 = s.replaceFirst("p", "").split("/"); | |
| partner(data1[0], data1[1]); | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| private static void spin(int x) { | |
| List<String> dancersL = new ArrayList<String>(Arrays.asList(dancers)); | |
| Collections.rotate(dancersL, x); | |
| dancers = dancersL.toArray(dancers); | |
| } | |
| private static void exchange(int x, int y) { | |
| String beforeY = dancers[y]; | |
| dancers[y] = dancers[x]; | |
| dancers[x] = beforeY; | |
| } | |
| private static void partner(String a, String b) { | |
| int indexA = Arrays.asList(dancers).indexOf(a); | |
| int indexB = Arrays.asList(dancers).indexOf(b); | |
| exchange(indexA, indexB); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment