carlosjai.me

Advent of Code – Day 6: Tuning Trouble

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.HashSet;
import java.util.List;
import java.util.Set;

public class AdventOfCode6 {
	public static void main(final String[] args) {
		String streamBuffer = getDataStreamBuffer();
		int part1Solution = getSolution(streamBuffer, 4);
		int part2Solution = getSolution(streamBuffer, 14);
		System.out.println("part 1 solution: " + part1Solution);
		System.out.println("part 2 solution: " + part2Solution);
	}

	private static int getSolution(String streamBuffer, int numberOfCharacters) {
		char[] dataStreamBuffer = streamBuffer.toCharArray();
		for (int i = 0; i < dataStreamBuffer.length; i++) {
			try {
				List<Character> list = new ArrayList<>();
				for (int j = 0; j < numberOfCharacters; j++) {
					list.add(dataStreamBuffer[i + j]);
				}
				Set<Character> set = new HashSet<>(list);
				if (set.size() == numberOfCharacters) {
					String sequence = list.toString().replaceAll("\\[|\\]", "").replaceAll(",", "").replaceAll(" ", "");
					return streamBuffer.indexOf(sequence) + numberOfCharacters;
				}
			} catch (IndexOutOfBoundsException ex) {
				System.out.println("finished: sequence not found");
				break;
			}
		}
		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 {

	static String readFile(String path) {
		byte[] encoded = new byte[0];
		try {
			encoded = Files.readAllBytes(Paths.get(path));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return new String(encoded, Charset.defaultCharset());
	}

	public static String getDataStreamBuffer() {
		return readFile("src/main/resources/inputDay6.txt");
	}

}

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

English

The statement

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

Solution.

package adventofcode2022;

import static data.DataInput.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class AdventOfCode6 {
	public static void main(final String[] args) {
		String streamBuffer = getDataStreamBuffer();
		int part1Solution = getSolution(streamBuffer, 4);
		int part2Solution = getSolution(streamBuffer, 14);
		System.out.println("part 1 solution: " + part1Solution);
		System.out.println("part 2 solution: " + part2Solution);
	}

	private static int getSolution(String streamBuffer, int numberOfCharacters) {
		char[] dataStreamBuffer = streamBuffer.toCharArray();
		for (int i = 0; i < dataStreamBuffer.length; i++) {
			try {
				List<Character> list = new ArrayList<>();
				for (int j = 0; j < numberOfCharacters; j++) {
					list.add(dataStreamBuffer[i + j]);
				}
				Set<Character> set = new HashSet<>(list);
				if (set.size() == numberOfCharacters) {
					String sequence = list.toString().replaceAll("\\[|\\]", "").replaceAll(",", "").replaceAll(" ", "");
					return streamBuffer.indexOf(sequence) + numberOfCharacters;
				}
			} catch (IndexOutOfBoundsException ex) {
				System.out.println("finished: sequence not found");
				break;
			}
		}
		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 {

	static String readFile(String path) {
		byte[] encoded = new byte[0];
		try {
			encoded = Files.readAllBytes(Paths.get(path));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return new String(encoded, Charset.defaultCharset());
	}

	public static String getDataStreamBuffer() {
		return readFile("src/main/resources/inputDay6.txt");
	}

}

Return to Advent of Code 2022 main page.

1 Comment

Leave a Comment

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