carlosjai.me

Advent of Code – Day 5: Supply Stacks

Galician

O enunciado.

Vale, o enunciado está en inglés, e é unha matada traducilo. Así que podedes velo aquí en inglés. Nun futuro, se teño tempo, tradúzoo.

A solución.

package adventofcode2022;

import static data.DataInput.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AdventOfCode5 {
	public static void main(final String[] args) {
		final List<String> movements = getMovements();
		List<List<String>> stacks = new ArrayList<>(getStacks());
		List<List<String>> stacks2 = new ArrayList<>(getStacks());
		for (final String movement : movements) {
			final String[] splittedMovements = movement.split(",");
			moveStacks(
					stacks,
					Integer.valueOf(splittedMovements[0]),
					Integer.valueOf(splittedMovements[1]) - 1,
					Integer.valueOf(splittedMovements[2]) - 1
			);
			moveStacksFull(
					stacks2,
					Integer.valueOf(splittedMovements[0]),
					Integer.valueOf(splittedMovements[1]) - 1,
					Integer.valueOf(splittedMovements[2]) - 1
			);
		}
		System.out.println(showTopStaks(stacks));
		System.out.println(showTopStaks(stacks2));
	}

	private static String showTopStaks(List<List<String>> stacks) {
		String tops = "";
		for (List<String> stack : stacks) {
			tops += stack.get(0);
		}
		return tops;
	}

	private static void moveStacks(List<List<String>> stacks, final Integer elementsToMove, final Integer from, final Integer to) {
		for (int i = 0; i < elementsToMove; i++) {
			final String element = stacks.get(from).get(0);
			stacks.get(from).remove(0);
			Collections.reverse(stacks.get(to));
			stacks.get(to).add(element);
			Collections.reverse(stacks.get(to));
		}
	}

	private static void moveStacksFull(List<List<String>> stacks, final Integer numElementsToMove, final Integer from, final Integer to) {
		List<String> elementsToBeMoved = getStackElementsToMove(stacks, numElementsToMove, from);
		Collections.reverse(elementsToBeMoved);
		Collections.reverse(stacks.get(to));
		stacks.get(to).addAll(elementsToBeMoved);
		Collections.reverse(stacks.get(to));
	}

	private static List<String> getStackElementsToMove(List<List<String>> stacks, Integer numElementsToMove, Integer from) {
		List<String> elementsToBeMoved = new ArrayList<>();
		for (int i = 0; i < numElementsToMove; i++) {
			elementsToBeMoved.add(stacks.get(from).get(0));
			stacks.get(from).remove(0);
		}
		return elementsToBeMoved;
	}
}
package data;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DataInput {

	private static List<String> getListByPath(final String path) {
		try {
			return Files.readAllLines(Paths.get(path), Charset.defaultCharset());
		} catch (final IOException e) {
			throw new RuntimeException(e);
		}
	}

	static List<String> stack1 = Stream.of("F", "H", "M", "T", "V", "L", "D").collect(Collectors.toList());
	static List<String> stack2 = Stream.of("P", "N", "T", "C", "J", "G", "Q", "H").collect(Collectors.toList());
	static List<String> stack3 = Stream.of("H", "P", "M", "D", "S", "R").collect(Collectors.toList());
	static List<String> stack4 = Stream.of("F", "V", "B", "L").collect(Collectors.toList());
	static List<String> stack5 = Stream.of("Q", "L", "G", "H", "N").collect(Collectors.toList());
	static List<String> stack6 = Stream.of("P", "M", "R", "G", "D", "B", "W").collect(Collectors.toList());
	static List<String> stack7 = Stream.of("Q", "L", "H", "C", "R", "N", "M", "G").collect(Collectors.toList());
	static List<String> stack8 = Stream.of("W", "L", "C").collect(Collectors.toList());
	static List<String> stack9 = Stream.of("T", "M", "Z", "J", "Q", "L", "D", "R").collect(Collectors.toList());

	public static List<List<String>> getStacks() {
		return new ArrayList<>(
				Stream.of(new ArrayList<>(stack1), new ArrayList<>(stack2), new ArrayList<>(stack3), new ArrayList<>(stack4), new ArrayList<>(stack5),
						new ArrayList<>(stack6), new ArrayList<>(stack7), new ArrayList<>(stack8), new ArrayList<>(stack9)).collect(Collectors.toList()));
	}

	public static List<String> getMovements() {
		return getListByPath("src/main/resources/inputDay5.txt");
	}

}

Regresar á páxina principal do Advent of Code 2022.

English

The statement

https://adventofcode.com/2022/day/5

Solution.

package adventofcode2022;

import static data.DataInput.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AdventOfCode5 {
	public static void main(final String[] args) {
		final List<String> movements = getMovements();
		List<List<String>> stacks = new ArrayList<>(getStacks());
		List<List<String>> stacks2 = new ArrayList<>(getStacks());
		for (final String movement : movements) {
			final String[] splittedMovements = movement.split(",");
			moveStacks(
					stacks,
					Integer.valueOf(splittedMovements[0]),
					Integer.valueOf(splittedMovements[1]) - 1,
					Integer.valueOf(splittedMovements[2]) - 1
			);
			moveStacksFull(
					stacks2,
					Integer.valueOf(splittedMovements[0]),
					Integer.valueOf(splittedMovements[1]) - 1,
					Integer.valueOf(splittedMovements[2]) - 1
			);
		}
		System.out.println(showTopStaks(stacks));
		System.out.println(showTopStaks(stacks2));
	}

	private static String showTopStaks(List<List<String>> stacks) {
		String tops = "";
		for (List<String> stack : stacks) {
			tops += stack.get(0);
		}
		return tops;
	}

	private static void moveStacks(List<List<String>> stacks, final Integer elementsToMove, final Integer from, final Integer to) {
		for (int i = 0; i < elementsToMove; i++) {
			final String element = stacks.get(from).get(0);
			stacks.get(from).remove(0);
			Collections.reverse(stacks.get(to));
			stacks.get(to).add(element);
			Collections.reverse(stacks.get(to));
		}
	}

	private static void moveStacksFull(List<List<String>> stacks, final Integer numElementsToMove, final Integer from, final Integer to) {
		List<String> elementsToBeMoved = getStackElementsToMove(stacks, numElementsToMove, from);
		Collections.reverse(elementsToBeMoved);
		Collections.reverse(stacks.get(to));
		stacks.get(to).addAll(elementsToBeMoved);
		Collections.reverse(stacks.get(to));
	}

	private static List<String> getStackElementsToMove(List<List<String>> stacks, Integer numElementsToMove, Integer from) {
		List<String> elementsToBeMoved = new ArrayList<>();
		for (int i = 0; i < numElementsToMove; i++) {
			elementsToBeMoved.add(stacks.get(from).get(0));
			stacks.get(from).remove(0);
		}
		return elementsToBeMoved;
	}
}
package data;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DataInput {

	private static List<String> getListByPath(final String path) {
		try {
			return Files.readAllLines(Paths.get(path), Charset.defaultCharset());
		} catch (final IOException e) {
			throw new RuntimeException(e);
		}
	}

	static List<String> stack1 = Stream.of("F", "H", "M", "T", "V", "L", "D").collect(Collectors.toList());
	static List<String> stack2 = Stream.of("P", "N", "T", "C", "J", "G", "Q", "H").collect(Collectors.toList());
	static List<String> stack3 = Stream.of("H", "P", "M", "D", "S", "R").collect(Collectors.toList());
	static List<String> stack4 = Stream.of("F", "V", "B", "L").collect(Collectors.toList());
	static List<String> stack5 = Stream.of("Q", "L", "G", "H", "N").collect(Collectors.toList());
	static List<String> stack6 = Stream.of("P", "M", "R", "G", "D", "B", "W").collect(Collectors.toList());
	static List<String> stack7 = Stream.of("Q", "L", "H", "C", "R", "N", "M", "G").collect(Collectors.toList());
	static List<String> stack8 = Stream.of("W", "L", "C").collect(Collectors.toList());
	static List<String> stack9 = Stream.of("T", "M", "Z", "J", "Q", "L", "D", "R").collect(Collectors.toList());

	public static List<List<String>> getStacks() {
		return new ArrayList<>(
				Stream.of(new ArrayList<>(stack1), new ArrayList<>(stack2), new ArrayList<>(stack3), new ArrayList<>(stack4), new ArrayList<>(stack5),
						new ArrayList<>(stack6), new ArrayList<>(stack7), new ArrayList<>(stack8), new ArrayList<>(stack9)).collect(Collectors.toList()));
	}

	public static List<String> getMovements() {
		return getListByPath("src/main/resources/inputDay5.txt");
	}

}

Return to Advent of Code 2022 main page.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.