Skip to content

Commit

Permalink
Added 2016 Day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-belellou committed Jan 13, 2025
1 parent c6fc6cc commit 673dee6
Show file tree
Hide file tree
Showing 2 changed files with 2,099 additions and 0 deletions.
99 changes: 99 additions & 0 deletions src/main/java/com/belellou/kevin/advent/year2016/Day7.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.belellou.kevin.advent.year2016;

import java.io.BufferedReader;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.tuple.MutablePair;

import com.belellou.kevin.advent.generic.AbstractDaySolver;

@SuppressWarnings("unused")
public class Day7 extends AbstractDaySolver<Integer> {

private static final char OPENING_BRACKET = '[';
private static final char CLOSING_BRACKET = ']';

public Day7() {
super(Day7.class);
}

private static boolean doesIpSupportTls(String ip) {
boolean hasAbba = false;
boolean isInHypernet = false;

for (int i = 3; i < ip.length(); i++) {
char c = ip.charAt(i);

if (c == OPENING_BRACKET) {
isInHypernet = true;
} else if (c == CLOSING_BRACKET) {
isInHypernet = false;
} else if (c == ip.charAt(i - 3) && c != ip.charAt(i - 2) && ip.charAt(i - 2) == ip.charAt(i - 1)) {
if (isInHypernet) {
return false;
}
hasAbba = true;
}
}

return hasAbba;
}

private static boolean doesIpSupportSsl(String ip) {
Set<MutablePair<Character, Character>> abas = new HashSet<>();
Set<MutablePair<Character, Character>> babs = new HashSet<>();

boolean isInHypernet = false;

for (int i = 2; i < ip.length(); i++) {
char c = ip.charAt(i);

if (c == OPENING_BRACKET) {
isInHypernet = true;
} else if (c == CLOSING_BRACKET) {
isInHypernet = false;
} else if (c == ip.charAt(i - 2) && c != ip.charAt(i - 1)) {
if (isInHypernet) {
MutablePair<Character, Character> pair = MutablePair.of(c, ip.charAt(i - 1));

if (abas.contains(pair)) {
return true;
}

babs.add(pair);
} else {
MutablePair<Character, Character> pair = MutablePair.of(ip.charAt(i - 1), c);

if (babs.contains(pair)) {
return true;
}

abas.add(pair);
}
}
}

return false;
}

@Override
protected Integer doSolveFirstStar(BufferedReader reader) {
return Math.toIntExact(reader.lines().filter(Day7::doesIpSupportTls).count());
}

@Override
public Integer getFirstStarSolution() {
return 110;
}

@Override
protected Integer doSolveSecondStar(BufferedReader reader) {
return Math.toIntExact(reader.lines().filter(Day7::doesIpSupportSsl).count());
}

@Override
public Integer getSecondStarSolution() {
return 242;
}
}
Loading

0 comments on commit 673dee6

Please sign in to comment.