carlosjai.me

Advent of code – Day 3: Rucksack Reorganization

Galician

O enunciado.

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

A solución utilizando os datos adaptados.

package adventofcode2022;

import static data.DataInput.*;

public class AdventOfCode3 {
	public static void main(final String[] args) {
		final String[] rucksacks = toArray(getRucksacks());
		int points = 0;
		for (final String rucksack : rucksacks) {
			final int mid = rucksack.length() / 2; //get the middle of the String
			final String[] parts = { rucksack.substring(0, mid), rucksack.substring(mid) };
			points += getPointsByPriority(parts);

		}
		System.out.println(points);
		points = 0;
		final String[] groupThree = new String[] { "", "", "" };
		int cont = 0;
		for (int i = 0; i < rucksacks.length; i++) {
			groupThree[cont++] = rucksacks[i];
			if (cont % 3 == 0) {
				points += getGroupPointsByPriority(groupThree);
				cont = 0;
			}

		}
		System.out.println(points);
	}

	private static int getGroupPointsByPriority(final String[] groupThree) {
		final char common = '1';
		for (int i = 0; i < groupThree[0].toCharArray().length; i++) {
			for (int j = 0; j < groupThree[1].toCharArray().length; j++) {
				if (groupThree[0].toCharArray()[i] == groupThree[1].toCharArray()[j]) {
					for (int z = 0; z < groupThree[2].toCharArray().length; z++) {
						if (groupThree[2].toCharArray()[z] == groupThree[0].toCharArray()[i]) {
							return itemsValue.get(groupThree[2].toCharArray()[z]);
						}
					}
				}
			}
		}
		return 0;
	}

	private static int getPointsByPriority(final String[] parts) {
		final char common = getCommonItem(parts[0], parts[1]);
		if (common != '1') {
			return itemsValue.get(common);
		}
		return 0;
	}

	private static char getCommonItem(final String part1, final String part2) {
		for (final char part1char : part1.toCharArray()) {
			for (final char part2char : part2.toCharArray()) {
				if (part1char == part2char) {
					return part1char;
				}
			}
		}
		return '1';
	}

}
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);
		}
	}

	public static String[] toArray(final List<String> list) {
		return list.toArray(String[]::new);
	}

	public static final Map<Character, Integer> itemsValue = new HashMap<>() {{
		put('a', Integer.valueOf(1));
		put('b', Integer.valueOf(2));
		put('c', Integer.valueOf(3));
		put('d', Integer.valueOf(4));
		put('e', Integer.valueOf(5));
		put('f', Integer.valueOf(6));
		put('g', Integer.valueOf(7));
		put('h', Integer.valueOf(8));
		put('i', Integer.valueOf(9));
		put('j', Integer.valueOf(10));
		put('k', Integer.valueOf(11));
		put('l', Integer.valueOf(12));
		put('m', Integer.valueOf(13));
		put('n', Integer.valueOf(14));
		put('o', Integer.valueOf(17));
		put('p', Integer.valueOf(16));
		put('q', Integer.valueOf(17));
		put('r', Integer.valueOf(18));
		put('s', Integer.valueOf(19));
		put('t', Integer.valueOf(20));
		put('u', Integer.valueOf(21));
		put('v', Integer.valueOf(22));
		put('w', Integer.valueOf(23));
		put('x', Integer.valueOf(24));
		put('y', Integer.valueOf(25));
		put('z', Integer.valueOf(26));
		put('A', Integer.valueOf(27));
		put('B', Integer.valueOf(28));
		put('C', Integer.valueOf(29));
		put('D', Integer.valueOf(30));
		put('E', Integer.valueOf(31));
		put('F', Integer.valueOf(32));
		put('G', Integer.valueOf(33));
		put('H', Integer.valueOf(34));
		put('I', Integer.valueOf(35));
		put('J', Integer.valueOf(36));
		put('K', Integer.valueOf(37));
		put('L', Integer.valueOf(38));
		put('M', Integer.valueOf(39));
		put('N', Integer.valueOf(40));
		put('O', Integer.valueOf(41));
		put('P', Integer.valueOf(42));
		put('Q', Integer.valueOf(43));
		put('R', Integer.valueOf(44));
		put('S', Integer.valueOf(45));
		put('T', Integer.valueOf(46));
		put('U', Integer.valueOf(47));
		put('V', Integer.valueOf(48));
		put('W', Integer.valueOf(49));
		put('X', Integer.valueOf(50));
		put('Y', Integer.valueOf(51));
		put('Z', Integer.valueOf(52));

	}};

}

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

English

The statement

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

Solution.

package adventofcode2022;

import static data.DataInput.*;

public class AdventOfCode3 {
	public static void main(final String[] args) {
		final String[] rucksacks = toArray(getRucksacks());
		int points = 0;
		for (final String rucksack : rucksacks) {
			final int mid = rucksack.length() / 2; //get the middle of the String
			final String[] parts = { rucksack.substring(0, mid), rucksack.substring(mid) };
			points += getPointsByPriority(parts);

		}
		System.out.println(points);
		points = 0;
		final String[] groupThree = new String[] { "", "", "" };
		int cont = 0;
		for (int i = 0; i < rucksacks.length; i++) {
			groupThree[cont++] = rucksacks[i];
			if (cont % 3 == 0) {
				points += getGroupPointsByPriority(groupThree);
				cont = 0;
			}

		}
		System.out.println(points);
	}

	private static int getGroupPointsByPriority(final String[] groupThree) {
		final char common = '1';
		for (int i = 0; i < groupThree[0].toCharArray().length; i++) {
			for (int j = 0; j < groupThree[1].toCharArray().length; j++) {
				if (groupThree[0].toCharArray()[i] == groupThree[1].toCharArray()[j]) {
					for (int z = 0; z < groupThree[2].toCharArray().length; z++) {
						if (groupThree[2].toCharArray()[z] == groupThree[0].toCharArray()[i]) {
							return itemsValue.get(groupThree[2].toCharArray()[z]);
						}
					}
				}
			}
		}
		return 0;
	}

	private static int getPointsByPriority(final String[] parts) {
		final char common = getCommonItem(parts[0], parts[1]);
		if (common != '1') {
			return itemsValue.get(common);
		}
		return 0;
	}

	private static char getCommonItem(final String part1, final String part2) {
		for (final char part1char : part1.toCharArray()) {
			for (final char part2char : part2.toCharArray()) {
				if (part1char == part2char) {
					return part1char;
				}
			}
		}
		return '1';
	}
}
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);
		}
	}

	public static String[] toArray(final List<String> list) {
		return list.toArray(String[]::new);
	}

	public static final Map<Character, Integer> itemsValue = new HashMap<>() {{
		put('a', Integer.valueOf(1));
		put('b', Integer.valueOf(2));
		put('c', Integer.valueOf(3));
		put('d', Integer.valueOf(4));
		put('e', Integer.valueOf(5));
		put('f', Integer.valueOf(6));
		put('g', Integer.valueOf(7));
		put('h', Integer.valueOf(8));
		put('i', Integer.valueOf(9));
		put('j', Integer.valueOf(10));
		put('k', Integer.valueOf(11));
		put('l', Integer.valueOf(12));
		put('m', Integer.valueOf(13));
		put('n', Integer.valueOf(14));
		put('o', Integer.valueOf(17));
		put('p', Integer.valueOf(16));
		put('q', Integer.valueOf(17));
		put('r', Integer.valueOf(18));
		put('s', Integer.valueOf(19));
		put('t', Integer.valueOf(20));
		put('u', Integer.valueOf(21));
		put('v', Integer.valueOf(22));
		put('w', Integer.valueOf(23));
		put('x', Integer.valueOf(24));
		put('y', Integer.valueOf(25));
		put('z', Integer.valueOf(26));
		put('A', Integer.valueOf(27));
		put('B', Integer.valueOf(28));
		put('C', Integer.valueOf(29));
		put('D', Integer.valueOf(30));
		put('E', Integer.valueOf(31));
		put('F', Integer.valueOf(32));
		put('G', Integer.valueOf(33));
		put('H', Integer.valueOf(34));
		put('I', Integer.valueOf(35));
		put('J', Integer.valueOf(36));
		put('K', Integer.valueOf(37));
		put('L', Integer.valueOf(38));
		put('M', Integer.valueOf(39));
		put('N', Integer.valueOf(40));
		put('O', Integer.valueOf(41));
		put('P', Integer.valueOf(42));
		put('Q', Integer.valueOf(43));
		put('R', Integer.valueOf(44));
		put('S', Integer.valueOf(45));
		put('T', Integer.valueOf(46));
		put('U', Integer.valueOf(47));
		put('V', Integer.valueOf(48));
		put('W', Integer.valueOf(49));
		put('X', Integer.valueOf(50));
		put('Y', Integer.valueOf(51));
		put('Z', Integer.valueOf(52));

	}};

}

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.