carlosjai.me

Advent of code – Day 4: Camp Cleanup

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 java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import data.DataInput;

public class AdventOfCode4 {
	public static void main(final String[] args) {
		final List<String> assignments = DataInput.getAssignmentPairs();
		int overlaps = 0;
		int partialOverlaps = 0;
		for (final String assignmentsPair : assignments) {
			final String[] pairs = assignmentsPair.split(",");
			final String[] pair1 = pairs[0].split("-");
			final String[] pair2 = pairs[1].split("-");
			if (checkOverlapping(Integer.valueOf(pair1[0]), Integer.valueOf(pair1[1]), Integer.valueOf(pair2[0]), Integer.valueOf(pair2[1]))) {
				overlaps++;
			}
			if (checkPartialOverlapping(Integer.valueOf(pair1[0]), Integer.valueOf(pair1[1]), Integer.valueOf(pair2[0]), Integer.valueOf(pair2[1]))) {
				partialOverlaps++;
			}
		}
		System.out.println(overlaps);
		System.out.println(partialOverlaps);
	}

	private static boolean checkOverlapping(final int firstSectionStart, final int firstSectionEnd, final int secondSectionStart,
			final int secondSectionEnd) {
		return (firstSectionStart <= secondSectionStart && firstSectionEnd >= secondSectionEnd) ||
				(secondSectionStart <= firstSectionStart && secondSectionEnd >= firstSectionEnd);
	}

	private static boolean checkPartialOverlapping(final int firstSectionStart, final int firstSectionEnd, final int secondSectionStart,
			final int secondSectionEnd) {
		final List<Integer> section1Elements = IntStream.rangeClosed(firstSectionStart, firstSectionEnd)
				.boxed().collect(Collectors.toList());
		final List<Integer> section2Elements = IntStream.rangeClosed(secondSectionStart, secondSectionEnd)
				.boxed().collect(Collectors.toList());
		final List<Integer> result =
				section1Elements.stream().filter(section1 ->
						section2Elements.stream().anyMatch(section2 -> section1 == section2)).collect(Collectors.toList());
		return !result.isEmpty();
	}

}
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 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/4

Solution.

package adventofcode2022;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import data.DataInput;

public class AdventOfCode4 {
	public static void main(final String[] args) {
		final List<String> assignments = DataInput.getAssignmentPairs();
		int overlaps = 0;
		int partialOverlaps = 0;
		for (final String assignmentsPair : assignments) {
			final String[] pairs = assignmentsPair.split(",");
			final String[] pair1 = pairs[0].split("-");
			final String[] pair2 = pairs[1].split("-");
			if (checkOverlapping(Integer.valueOf(pair1[0]), Integer.valueOf(pair1[1]), Integer.valueOf(pair2[0]), Integer.valueOf(pair2[1]))) {
				overlaps++;
			}
			if (checkPartialOverlapping(Integer.valueOf(pair1[0]), Integer.valueOf(pair1[1]), Integer.valueOf(pair2[0]), Integer.valueOf(pair2[1]))) {
				partialOverlaps++;
			}
		}
		System.out.println(overlaps);
		System.out.println(partialOverlaps);
	}

	private static boolean checkOverlapping(final int firstSectionStart, final int firstSectionEnd, final int secondSectionStart,
			final int secondSectionEnd) {
		return (firstSectionStart <= secondSectionStart && firstSectionEnd >= secondSectionEnd) ||
				(secondSectionStart <= firstSectionStart && secondSectionEnd >= firstSectionEnd);
	}

	private static boolean checkPartialOverlapping(final int firstSectionStart, final int firstSectionEnd, final int secondSectionStart,
			final int secondSectionEnd) {
		final List<Integer> section1Elements = IntStream.rangeClosed(firstSectionStart, firstSectionEnd)
				.boxed().collect(Collectors.toList());
		final List<Integer> section2Elements = IntStream.rangeClosed(secondSectionStart, secondSectionEnd)
				.boxed().collect(Collectors.toList());
		final List<Integer> result =
				section1Elements.stream().filter(section1 ->
						section2Elements.stream().anyMatch(section2 -> section1 == section2)).collect(Collectors.toList());
		return !result.isEmpty();
	}

}
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 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.