Skip to content

Instantly share code, notes, and snippets.

@tacsio
Last active November 19, 2022 16:33
Show Gist options
  • Save tacsio/97b49f8e21e4df4c1143cf1361fe411a to your computer and use it in GitHub Desktop.
Save tacsio/97b49f8e21e4df4c1143cf1361fe411a to your computer and use it in GitHub Desktop.
Carrossel das Cores | Hexagono Colorido
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Peca[] pecas = new Peca[7];
pecas[0] = new Peca(1, new Cor[]{Cor.BRANCO, Cor.AZUL, Cor.VERDE, Cor.AMARELO, Cor.VERMELHO, Cor.LARANJA});
pecas[1] = new Peca(2, new Cor[]{Cor.BRANCO, Cor.VERDE, Cor.VERMELHO, Cor.AMARELO, Cor.AZUL, Cor.LARANJA});
pecas[2] = new Peca(3, new Cor[]{Cor.BRANCO, Cor.VERMELHO, Cor.AMARELO, Cor.LARANJA, Cor.VERDE, Cor.AZUL});
pecas[3] = new Peca(4, new Cor[]{Cor.BRANCO, Cor.AZUL, Cor.VERMELHO, Cor.LARANJA, Cor.VERDE, Cor.AMARELO});
pecas[4] = new Peca(5, new Cor[]{Cor.BRANCO, Cor.AMARELO, Cor.AZUL, Cor.VERDE, Cor.VERMELHO, Cor.LARANJA});
pecas[5] = new Peca(6, new Cor[]{Cor.BRANCO, Cor.LARANJA, Cor.VERMELHO, Cor.VERDE, Cor.AZUL, Cor.AMARELO});
pecas[6] = new Peca(7, new Cor[]{Cor.BRANCO, Cor.AZUL, Cor.VERMELHO, Cor.LARANJA, Cor.AMARELO, Cor.VERDE});
for (int j = 0; j < Cor.values().length; j++) {
Tabuleiro tabuleiro = new Tabuleiro();
tabuleiro.resolver(pecas);
//para iniciar tentativa com outra cor (centro norte)
novaRefCor(pecas);
}
}
private static void novaRefCor(Peca[] pecas) {
var lider = pecas[0];
shiftCores(lider);
for (int i = 1; i < pecas.length; i++) {
var peca = pecas[i];
moverCorReferencia(peca, lider.cores()[0]);
}
}
private static void moverCorReferencia(Peca p, Cor ref) {
var nova = new Cor[6];
int indexBase = Arrays.stream(p.cores).toList().indexOf(ref);
int i = 0;
while (i != 6) {
Cor cor = p.cores()[indexBase % 6];
nova[i++] = cor;
indexBase += 1;
}
p.cores = nova;
}
private static void shiftCores(Peca p) {
var nova = new Cor[6];
for (int i = 0; i < p.cores.length; i++) {
Cor cor = p.cores()[i];
int index = (i + 1) % 6;
nova[index] = cor;
}
p.cores = nova;
}
}
class Tabuleiro {
public Stack<Peca> solucao = new Stack<>();
public Peca centro;
public Cor extraMatch;
public void resolver(Peca[] pecas) {
int index = solucao.size() - 1;
if (solucao.size() == 7) {
printSoluticao();
return;
}
for (int i = 0; i < pecas.length && solucao.size() != 7; i++) {
var atual = pecas[i];
if (!atual.usado) {
if (index <= 0) {
//centro
if (index == -1) centro = atual;
//match extra ultima peca é o index 1 da primeira peca (match centro norte)
if (index == 0) extraMatch = atual.cores()[1];
solucao.add(atual);
atual.usado = true;
resolver(pecas);
//volta peca
solucao.pop();
atual.usado = false;
} else {
var cor_anteiror = centro.cores()[index - 1];
var i_anterior = solucao.peek().acharIndex(cor_anteiror);
var corCentro = centro.cores()[index];
var corPrev = solucao.peek().cores()[(i_anterior + 5) % 6];
//match da ultima peca
var ultimaPecaMatch = true;
if (index == 5) {
var c_index = atual.acharIndex(corCentro);
var corPrevExtra = atual.cores()[(c_index + 5) % 6];
ultimaPecaMatch = extraMatch.equals(corPrevExtra);
}
if (atual.match(corCentro, corPrev) && ultimaPecaMatch) {
solucao.add(atual);
atual.usado = true;
resolver(pecas);
//volta peca
solucao.pop();
atual.usado = false;
}
}
}
}
}
void printSoluticao() {
var r = solucao.stream()
.map(Peca::id)
.map(String::valueOf)
.collect(Collectors.joining("-"));
System.out.println("SOLUTION: " + r + " | " + "Centro Norte: " + centro.cores[0]);
}
}
class Peca {
int id;
public boolean usado;
Cor[] cores;
public Peca(int id, Cor[] cores) {
this.id = id;
this.cores = cores;
}
public Cor[] cores() {
return this.cores;
}
public int id() {
return id;
}
boolean match(Cor corCentro, Cor corPrev) {
boolean match = false;
for (int i = 0; i < cores.length; i++) {
if (cores[i].equals(corCentro)) {
int next = (i + 1) % 6;
match = cores[next].equals(corPrev);
break;
}
}
return match;
}
int acharIndex(Cor cor) {
int index = -1;
for (int i = 0; i < this.cores.length; i++) {
if (cor.equals(cores()[i])) {
index = i;
break;
}
}
return index;
}
@Override
public String toString() {
return "Peca{" +
"id=" + id +
"usado=" + usado +
", cores=" + Arrays.toString(cores) +
'}';
}
}
enum Cor {
BRANCO, AMARELO, AZUL, VERDE, VERMELHO, LARANJA;
}
@tacsio
Copy link
Author

tacsio commented Nov 18, 2022

SOLUTION: 4-7-5-3-1-2-6 | Centro Norte: BRANCO
SOLUTION: 4-1-2-6-7-5-3 | Centro Norte: LARANJA
SOLUTION: 4-3-1-2-6-7-5 | Centro Norte: VERMELHO
SOLUTION: 4-6-7-5-3-1-2 | Centro Norte: AMARELO
SOLUTION: 4-2-6-7-5-3-1 | Centro Norte: VERDE
SOLUTION: 4-5-3-1-2-6-7 | Centro Norte: AZUL

@tacsio
Copy link
Author

tacsio commented Nov 19, 2022

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment