advent2022

Advent of Code 2022 Solutions
git clone https://todayiwilllaunchmyinfantsonintoorbit.com/advent2022.git
Log | Files | Refs

Five.java (3736B)


      1 import java.io.BufferedReader;
      2 import java.io.IOException;
      3 import java.nio.charset.Charset;
      4 import java.nio.file.Files;
      5 import java.nio.file.FileSystems;
      6 import java.nio.file.Path;
      7 import java.util.ArrayList;
      8 import java.util.List;
      9 
     10 class Five {
     11     private static void reverseList(ArrayList<Character> list) {
     12         for (int idx = 0; idx < list.size() / 2; idx++) {
     13             Character a = list.get(idx);
     14             list.set(idx, list.get((list.size() - 1) - idx));
     15             list.set((list.size() - 1) - idx, a);
     16         }
     17     }
     18 
     19     private static void moveA(int n, ArrayList<Character> from, ArrayList<Character> to) {
     20         for (int i = 0; i < n; i++) {
     21             to.add(from.remove(from.size() - 1));
     22         }
     23     }
     24     
     25     private static void moveB(int n, ArrayList<Character> from, ArrayList<Character> to) {
     26         int rangeFrom = from.size() - n;
     27         int rangeTo = from.size();
     28         if (rangeFrom < 0 || rangeTo < 0) {
     29             System.err.println("Trying to move " + n + " boxes from " + from + " to " + to + "!");
     30             throw new RuntimeException("What the fuck");
     31         }
     32         List<Character> toMove = from.subList(rangeFrom, rangeTo);
     33         to.addAll(toMove);
     34         for (int i = 0; i < n; i++) {
     35             from.remove(from.size() - 1);
     36         }
     37     }
     38 
     39     public static void main(String[] args) {
     40         Boolean isSecondStar = (args.length > 0) && (args[0].equals("2")); 
     41         Charset utf8 = Charset.forName("UTF-8");
     42         Path input = FileSystems.getDefault().getPath("input");
     43         try (BufferedReader reader = Files.newBufferedReader(input, utf8)) {
     44             String line = null;
     45             
     46             // Read in the stacks
     47             ArrayList<ArrayList<Character>> stacks = new ArrayList<ArrayList<Character>>();
     48             while ((line = reader.readLine()) != null && !line.equals("")) {
     49                 // Construct the initial columns based on the first part of the input
     50                 int col = 0;
     51                 while ((col * 4) < (line.length() - 1)) {
     52                     String box = line.substring(col * 4, col * 4 + 3);
     53                     Character boxName = box.charAt(1);
     54                     if ('A' <= boxName && 'Z' >= boxName) {
     55                         while (stacks.size() <= col) {
     56                             stacks.add(new ArrayList<Character>());
     57                         }
     58                         stacks.get(col).add(boxName);
     59                     } else if ('0' <= boxName && '9' >= boxName) {
     60                         // Bottom of columns, just finish
     61                         break;
     62                     }
     63                     col++;
     64                 }
     65             }
     66             stacks.stream().forEach(Five::reverseList);
     67 
     68             // Loop over the instructions
     69             while ((line = reader.readLine()) != null) {
     70                 String[] instr = line.split(" ");
     71                 if (!instr[0].equals("move")) {
     72                     throw new RuntimeException("Bad instruction!");
     73                 }
     74                 int n = Integer.parseInt(instr[1]);
     75                 int from = Integer.parseInt(instr[3]);
     76                 int to = Integer.parseInt(instr[5]);
     77                 ArrayList<Character> fromList = stacks.get(from - 1);
     78                 ArrayList<Character> toList = stacks.get(to - 1);
     79                 if (isSecondStar) {
     80                     moveB(n, fromList, toList);
     81                 } else {
     82                     moveA(n, fromList, toList);
     83                 }
     84             }
     85             stacks.stream().map(stack -> stack.get(stack.size() - 1)).forEach(c -> System.out.print(c));
     86             System.out.println();
     87         } catch (IOException x) {
     88             System.err.format("IOException: %s%n", x);
     89         }
     90     }
     91 }