Skip to content

Instantly share code, notes, and snippets.

@Lyuu17
Created March 29, 2023 09:57
Show Gist options
  • Save Lyuu17/d784834b63ad55cf33187bf213b16a0e to your computer and use it in GitHub Desktop.
Save Lyuu17/d784834b63ad55cf33187bf213b16a0e to your computer and use it in GitHub Desktop.
package org.example;
import org.w3c.dom.ranges.Range;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class AdventDay5 {
public static String getLinesInFile(String path) throws FileNotFoundException {
String lines = new String();
Scanner scan = new Scanner(new File(path));
try {
while (true)
lines += scan.nextLine() + "\n";
}
catch (Exception ignored) {
// eof
}
scan.close();
return lines;
}
public static String getLetterInRow(String rowStr, int pos)
{
int start = (rowStr.length() >= 4 ? 4 : 3) * pos + 1;
if (rowStr.length() < start)
return "";
return rowStr.substring(start).substring(0, 1).trim();
}
public static HashMap<Integer, ArrayList<String>> crates = new HashMap<>();
public static void printStorage()
{
boolean nextLine = false;
for (int y = 24; y >= 0; y--) {
for (int x = 0; x < crates.size(); x++) {
var crate = crates.get(x);
if (y < crate.size())
{
System.out.print("[" + crate.get(y) + "] ");
nextLine = true;
}
else if (y >= crate.size())
{
System.out.print(" ");
nextLine = true;
}
}
if (nextLine)
System.out.println();
nextLine = false;
}
for (int i = 1; i <= crates.size(); i++)
System.out.print(" " + i + " ");
System.out.println();
}
public static void parseStorage(String linesStorage[])
{
String lastLine = linesStorage[linesStorage.length - 1];
Integer lastNum = Integer.parseInt(lastLine.substring(lastLine.length() - 2, lastLine.length()).trim());
for (int i = 0; i < lastNum; i++)
crates.put(i, new ArrayList<>());
for (int i = linesStorage.length - 1 - 1; i >= 0; i--)
{
for (int pos = 0; pos < lastNum; pos++)
{
String letter = getLetterInRow(linesStorage[i], pos);
if (!letter.isEmpty())
crates.get(pos).add(letter);
}
}
}
public static void parseMovements(String linesMovement[])
{
Pattern pattern = Pattern.compile("(\\d+)+");
for (int i = 0; i < linesMovement.length; i++)
{
Matcher matcher = pattern.matcher(linesMovement[i]);
matcher.find();
Integer crateNum = Integer.parseInt(matcher.group(0));
matcher.find();
Integer fromNum = Integer.parseInt(matcher.group(0)) - 1;
matcher.find();
Integer toNum = Integer.parseInt(matcher.group(0)) - 1;
System.out.println("---------------------------------------------------------");
printStorage();
ArrayList<String> crateList = crates.get(fromNum);
System.out.println(" -------------------- move " + crateNum + " from " + (fromNum+1) + " to " + (toNum+1) + " (" + (crateList.size() + ", " + crateNum) + ")");
List<String> subList = crateList.subList(crateList.size() - crateNum, crateList.size());
Collections.reverse(subList);
crates.get(toNum).addAll(subList);
for (int j = 0; j < crateNum; j++)
crates.get(fromNum).remove(crateList.size() - 1);
printStorage();
}
}
public static void main(String[] args) throws FileNotFoundException {
String lines = getLinesInFile("day5.txt");
String storage[] = lines.split("\n\n");
parseStorage(storage[0].split("\n"));
parseMovements(storage[1].split("\n"));
printStorage();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment