Skip to content

Instantly share code, notes, and snippets.

@SirEdvin
Created September 1, 2015 17:15
Show Gist options
  • Save SirEdvin/ba704a42cd0a680ca534 to your computer and use it in GitHub Desktop.
Save SirEdvin/ba704a42cd0a680ca534 to your computer and use it in GitHub Desktop.
Palindrome task (hm ...)
import io.github.siredvin.anji.log.Log;
import io.github.siredvin.math.util.ArrayUtil;
import javafx.scene.layout.VBox;
import sun.invoke.util.VerifyAccess;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* Created by siredvin on 01.09.15.
*
* @author siredvin
*/
public class HardAnswer {
private static byte[][][] table = new byte[][][]{
{
{1, 1}, {3, 7}, {7, 3}, {9, 9}
},//1
{
{1, 2}, {2, 1}, {2, 6}, {3, 4}, {4, 3}, {4, 8}, {6, 2}, {6, 7}, {7, 6}, {8, 4}, {8, 9}, {9, 8}
},//2
{
{1, 3}, {3, 1}, {7, 9}, {9, 7}
},//3
{
{1, 4}, {2, 2}, {2, 7}, {3, 8}, {4, 1}, {4, 6}, {6, 4}, {6, 9}, {8, 3}, {8, 8}, {9, 6}
},//4
{
{1, 5}, {3, 5}, {5, 1}, {5, 3}, {5, 5}, {5, 7}, {5, 9}, {7, 5}, {9, 5}
},//5
{
{1, 6}, {2, 3}, {2, 8}, {3, 2}, {4, 4}, {4, 9}, {6, 1}, {6, 6}, {7, 8}, {8, 2}, {8, 7}, {9, 4}
},//6
{
{1, 7}, {3, 9}, {7, 1}, {9, 3}
},//7
{
{1, 8}, {2, 4}, {2, 9}, {3, 6}, {4, 2}, {4, 7}, {6, 3}, {6, 8}, {7, 4}, {8, 1}, {8, 6}, {9, 2}
},//8
{
{1, 9}, {3,3}, {7, 7}, {9, 1}
},//9
};
public static void main(String[] args) {
firstVariant();
}
public static void firstVariant() {
Set<Variant> variants = generateBaseVariant();
int max = 999*999;
for (int palindromePart = 999; palindromePart > 99; palindromePart--) {
int palindrome = palindromePart*1000 + (palindromePart % 10)*100 + (palindromePart %100 /10)*10 + palindromePart /100;
if (palindrome>max){
continue;
}
int x1 = palindromePart / 100;
int x2 = palindromePart / 10 % 10;
int x3 = palindromePart % 10;
//x1=x6,x2=x5,x3=x4
byte[][] x6RestArray = table[x1-1];
Optional<Variant> variant = variants.parallelStream().filter(v -> {
for (byte[] pair: x6RestArray){
if (pair[0]==v.c && pair[1]==v.f)
return true;
}
return false;
})
//Відсів за x5
.filter(v -> v.getX4X5() % 10 == x2)
.filter(v -> v.getX3X4() % 10 == x3)
.filter(v-> v.getX2X3() %10 == x3)
.filter(v -> v.getX1X2() %10 == x2)
.filter(v -> v.getX1X2() / 10 == x1).findAny();
if (variant.isPresent()){
System.out.println(variant.get());
System.out.printf("%d%d%d%d%d%d", x1, x2, x3, x3, x2, x1);
break;
}
}
}
public static Set<Variant> generateBaseVariant(){
Set<Variant> variants = new HashSet<>(9*9*9*9*9*9);
for (byte a = 1;a<10;a++){
for (byte d = 1;d<10;d++){
if (d>a){
continue;
}
for (byte b = 0;b<10;b++){
for (byte e = 0;e<10;e++){
if (e>b)
continue;
for (byte c = 1;c<10;c++){
for (byte f = 1;f<10;f++){
if (f>c)
continue;
variants.add(new Variant(a, b, c, d, e, f));
}
}
}
}
}
}
return variants;
}
public static class Variant{
public byte a;
public byte b;
public byte c;
public byte d;
public byte e;
public byte f;
public int x1_x2=-1,x2_x3,x3_x4,x4_x5;
public Variant(byte a, byte b, byte c, byte d, byte e, byte f) {
if (d>a || (d==a && e>b) || (d==a && e==b && f>c)){
this.a = d;
this.b = e;
this.c = f;
this.d = a;
this.e = b;
this.f = c;
} else {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
}
}
public int getX4X5(){
x4_x5 = c*e+b*f+c*f/10;
return x4_x5;
}
public int getX3X4(){
x3_x4 = c*d+b*e+a*f+x4_x5/10;
return x3_x4;
}
public int getX2X3(){
x2_x3 = b*d+a*e+ x3_x4/10;
return x2_x3;
}
public int getX1X2(){
if (x1_x2==-1) {
x1_x2 = a * d + x2_x3 / 10;
}
return x1_x2;
}
@Override
public String toString() {
return "Variant{" +
"f=" + f +
", e=" + e +
", d=" + d +
", c=" + c +
", b=" + b +
", a=" + a +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Variant)) return false;
Variant variant = (Variant) o;
return a == variant.a && b == variant.b && c == variant.c && d == variant.d && e == variant.e && f == variant.f;
}
@Override
public int hashCode() {
int result = (int) a;
result = 31 * result + (int) b;
result = 31 * result + (int) c;
result = 31 * result + (int) d;
result = 31 * result + (int) e;
result = 31 * result + (int) f;
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment