Skip to content

Instantly share code, notes, and snippets.

@jandk
Created December 5, 2022 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jandk/23efcd0ebe125fd121cfac45e43ed5bd to your computer and use it in GitHub Desktop.
Save jandk/23efcd0ebe125fd121cfac45e43ed5bd to your computer and use it in GitHub Desktop.
AOC 2022 Day 5
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Day05 {
public static void main(String[] args) {
String s = Utils.read("day05.txt");
Status status = parse(s);
solve1(status);
solve2(status);
}
private static Status parse(String s) {
String[] split = s.split("\\R{2}+");
List<Deque<Character>> stacks = parseStacks(split[0]);
List<Move> moves = parseMoves(split[1]);
return new Status(stacks, moves);
}
private static List<Deque<Character>> parseStacks(String s) {
String[] lines = s.split("\\R");
List<List<Character>> stacks = Arrays.stream(lines, 0, lines.length - 1)
.map(line -> Arrays.stream(line.split("(?<=\\G.{4})"))
.filter(str -> !str.isEmpty())
.map(item -> item.charAt(0) == '[' ? item.charAt(1) : null)
.toList())
.toList();
return IntStream.range(0, stacks.get(0).size())
.mapToObj(i -> parseStack(stacks, i))
.collect(Collectors.toList());
}
private static Deque<Character> parseStack(List<List<Character>> tempStacks, int ii) {
return tempStacks.stream()
.map(list -> list.get(ii))
.filter(Objects::nonNull)
.collect(ArrayDeque::new, Deque::addFirst, Deque::addAll);
}
private static List<Move> parseMoves(String s) {
return Arrays.stream(s.split("\\R"))
.map(line -> {
String[] split = line.split(" ");
int amount = Integer.parseInt(split[1]);
int from = Integer.parseInt(split[3]);
int to = Integer.parseInt(split[5]);
return new Move(amount, from, to);
})
.toList();
}
private static void solve1(Status status) {
List<Deque<Character>> stacks = status.copy();
for (Move move : status.moves()) {
for (int i = 0; i < move.amount(); i++) {
Character c = stacks.get(move.from() - 1).removeLast();
stacks.get(move.to() - 1).addLast(c);
}
}
System.out.println(getStackTops(stacks));
}
private static void solve2(Status status) {
List<Deque<Character>> stacks = status.copy();
List<Character> crates = new ArrayList<>();
for (Move move : status.moves()) {
for (int i = 0; i < move.amount(); i++) {
crates.add(stacks.get(move.from() - 1).removeLast());
}
for (int i = crates.size() - 1; i >= 0; i--) {
stacks.get(move.to() - 1).addLast(crates.get(i));
}
crates.clear();
}
System.out.println(getStackTops(stacks));
}
private static String getStackTops(List<Deque<Character>> stacks) {
return stacks.stream()
.map(Deque::peekLast)
.map(String::valueOf)
.collect(Collectors.joining());
}
record Move(int amount, int from, int to) {
}
record Status(
List<Deque<Character>> stacks,
List<Move> moves
) {
public List<Deque<Character>> copy() {
return stacks.stream()
.map(ArrayDeque::new)
.collect(Collectors.toList());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment