Skip to content

Commit

Permalink
refactor: use CollectionUtil.permutations to calculate possible anten…
Browse files Browse the repository at this point in the history
…nas permutations
  • Loading branch information
Flashky committed Dec 8, 2024
1 parent 00df560 commit 54c6764
Showing 1 changed file with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.adventofcode.flashk.day08;

import com.adventofcode.flashk.common.Vector2;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -16,6 +17,7 @@ public class ResonantCollinearity {
private final int rows;
private final int cols;
private final Map<Character, List<Vector2>> antennasPerFrequency = new HashMap<>();
private Set<Vector2> antinodes;

public ResonantCollinearity(char[][] input) {

Expand All @@ -38,32 +40,23 @@ public ResonantCollinearity(char[][] input) {
}

public int solve(boolean countHarmonics) {
Set<Vector2> antinodeLocations = new HashSet<>();

for(Map.Entry<Character,List<Vector2>> frequency : antennasPerFrequency.entrySet()) {

List<Vector2> antennas = frequency.getValue();

for (int i = 0; i < antennas.size(); i++) {
for (int j = 1; j < antennas.size(); j++) {
Vector2 antenna1 = antennas.get(i);
Vector2 antenna2 = antennas.get(j);

if (antenna1.equals(antenna2)) {
continue;
}
antinodes = new HashSet<>();

findAntinodes(countHarmonics, antenna1, antenna2, antinodeLocations);
findAntinodes(countHarmonics, antenna2, antenna1, antinodeLocations);
for(Map.Entry<Character,List<Vector2>> frequency : antennasPerFrequency.entrySet()) {

}
List<Vector2> antennas = frequency.getValue();
for(List<Vector2> pair : CollectionUtils.permutations(antennas)) {
findAntinodes(countHarmonics, pair.get(0), pair.get(1));
findAntinodes(countHarmonics, pair.get(1), pair.get(0));
}

}

return antinodeLocations.size();
return antinodes.size();
}

private void findAntinodes(boolean countHarmonics, Vector2 antennaA, Vector2 antennaB, Set<Vector2> antinodes) {
private void findAntinodes(boolean countHarmonics, Vector2 antennaA, Vector2 antennaB) {
Vector2 direction = Vector2.direction(antennaA, antennaB);
Vector2 antinode = new Vector2(antennaA);
do {
Expand Down

0 comments on commit 54c6764

Please sign in to comment.