Remove ipython solutions because the clutter the repository and I like the Python solutions more
This commit is contained in:
81
other/e030.c
Normal file
81
other/e030.c
Normal file
@@ -0,0 +1,81 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define DIGITS (6)
|
||||
#define BASE_TEN (100000)
|
||||
|
||||
uint32_t* get_digits(uint32_t n) {
|
||||
uint32_t* r = malloc(sizeof(uint32_t) * DIGITS);
|
||||
uint32_t base = BASE_TEN;
|
||||
for (size_t i = 0; i < DIGITS; i++) {
|
||||
r[i] = n / base;
|
||||
n = n % base;
|
||||
base /= 10;
|
||||
}
|
||||
return r;
|
||||
|
||||
}
|
||||
|
||||
uint32_t* get_power_four_lookup() {
|
||||
uint32_t* r = malloc(sizeof(uint32_t) * 10);
|
||||
r[0] = 0;
|
||||
r[1] = 1;
|
||||
r[2] = 2 * 2 * 2 * 2;
|
||||
r[3] = 3 * 3 * 3 * 3;
|
||||
r[4] = 4 * 4 * 4 * 4;
|
||||
r[5] = 5 * 5 * 5 * 5;
|
||||
r[6] = 6 * 6 * 6 * 6;
|
||||
r[7] = 7 * 7 * 7 * 7;
|
||||
r[8] = 8 * 8 * 8 * 8;
|
||||
r[9] = 9 * 9 * 9 * 9;
|
||||
return r;
|
||||
}
|
||||
|
||||
uint32_t* get_power_five_lookup() {
|
||||
uint32_t* r = malloc(sizeof(uint32_t) * 10);
|
||||
r[0] = 0;
|
||||
r[1] = 1;
|
||||
r[2] = 2 * 2 * 2 * 2 * 2;
|
||||
r[3] = 3 * 3 * 3 * 3 * 3;
|
||||
r[4] = 4 * 4 * 4 * 4 * 4;
|
||||
r[5] = 5 * 5 * 5 * 5 * 5;
|
||||
r[6] = 6 * 6 * 6 * 6 * 6;
|
||||
r[7] = 7 * 7 * 7 * 7 * 7;
|
||||
r[8] = 8 * 8 * 8 * 8 * 8;
|
||||
r[9] = 9 * 9 * 9 * 9 * 9;
|
||||
return r;
|
||||
}
|
||||
|
||||
bool is_representable(uint32_t number, uint32_t* lookup) {
|
||||
uint32_t* digits = get_digits(number);
|
||||
uint32_t digit_sum = 0;
|
||||
for (size_t i = 0; i < DIGITS; i++)
|
||||
digit_sum += lookup[digits[i]];
|
||||
free(digits);
|
||||
if (number == digit_sum)
|
||||
return true;
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
void print_digits(uint32_t* digits) {
|
||||
for (size_t i = 0; i < DIGITS; i++) {
|
||||
printf("%u", digits[i]);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argn, char** argv) {
|
||||
uint32_t* lookup = get_power_five_lookup();
|
||||
uint32_t sum = 0;
|
||||
for (uint32_t n = 10; n < 300000; n++)
|
||||
if (is_representable(n, lookup)) {
|
||||
printf("%u\n", n);
|
||||
sum += n;
|
||||
}
|
||||
printf("Sum: %d\n", sum);
|
||||
free(lookup);
|
||||
return 0;
|
||||
}
|
||||
|
||||
150
other/sudoku.java
Normal file
150
other/sudoku.java
Normal file
@@ -0,0 +1,150 @@
|
||||
import java.util.*;
|
||||
import javax.swing.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.FileReader;
|
||||
import java.io.Reader;
|
||||
|
||||
class Sudoku {
|
||||
|
||||
int[] fieldValues = new int[81];
|
||||
int[] emptyFields;
|
||||
int currentField;
|
||||
|
||||
void printField() {
|
||||
String fieldOut = "";
|
||||
int i;
|
||||
for(i=0; i<81; i++) {
|
||||
if(i%27 == 0 && i>0) {
|
||||
fieldOut += "\n---------------------\n";
|
||||
}
|
||||
else if(i%9 == 0) {
|
||||
fieldOut += "\n";
|
||||
}
|
||||
else if(i%3 == 0) {
|
||||
fieldOut += "| ";
|
||||
}
|
||||
if(fieldValues[i] != 0) {
|
||||
fieldOut += fieldValues[i] + " ";
|
||||
}
|
||||
else {
|
||||
fieldOut += " ";
|
||||
}
|
||||
}
|
||||
System.out.println(fieldOut);
|
||||
}
|
||||
|
||||
/** Function asks for an file name which
|
||||
* should contain a sudoku and this sudoku
|
||||
* into fieldValues, Empty fields should
|
||||
* be marked by a '0'
|
||||
*/
|
||||
void loadFieldFromFile() {
|
||||
String fileName = "";
|
||||
Reader reader = null;
|
||||
int i = 0;
|
||||
|
||||
fileName = JOptionPane.showInputDialog( "Enter filename" );
|
||||
System.out.println(fileName);
|
||||
|
||||
try
|
||||
{
|
||||
reader = new FileReader (fileName);
|
||||
for ( int c; ( c = reader.read() ) != -1; ) {
|
||||
int v = 0;
|
||||
v = c - (int) '0';
|
||||
if( v >= 0 && v <= 9 ) {
|
||||
fieldValues[i] = v;
|
||||
i++;
|
||||
}
|
||||
if( i > 80 ) break;
|
||||
}
|
||||
}
|
||||
catch ( IOException e) {
|
||||
System.err.println( "Could not open file " + fileName );
|
||||
return;
|
||||
}
|
||||
finally {
|
||||
try { reader.close(); } catch ( Exception e ) { e.printStackTrace(); }
|
||||
}
|
||||
|
||||
System.out.println("File loaded");
|
||||
}
|
||||
|
||||
/** Function checks if "number" may be inserted
|
||||
* in field "index"
|
||||
*/
|
||||
boolean isNumberAllowed(int number, int index) {
|
||||
int i = 0, j = 0;
|
||||
/* check col */
|
||||
for(i=index%9; i<81; i+=9) {
|
||||
if(fieldValues[i] == number)
|
||||
return false;
|
||||
}
|
||||
/* check row */
|
||||
for(i=index/9*9; i<(index/9*9+9); i++) {
|
||||
if(fieldValues[i] == number)
|
||||
return false;
|
||||
}
|
||||
/* check field */
|
||||
int lu = (index/9/3*27) + (index%9/3*3); //first index of field
|
||||
for(j = 0; j<3; j++) {
|
||||
for(i = lu+(j*9); i<lu+(j*9)+3; i++) {
|
||||
if(fieldValues[i] == number)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Function trys to solve the current sudoku
|
||||
* using backtracking algorithm
|
||||
*/
|
||||
void solveSudoku() {
|
||||
currentField = 0;
|
||||
stepAhead();
|
||||
}
|
||||
|
||||
boolean stepAhead() {
|
||||
if(currentField==81) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if(fieldValues[currentField] != 0) {
|
||||
currentField++;
|
||||
if(stepAhead()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
for(int i = 1; i<=9; i++) {
|
||||
if(isNumberAllowed(i, currentField)) {
|
||||
fieldValues[currentField] = i;
|
||||
currentField++;
|
||||
if(stepAhead())
|
||||
return true;
|
||||
}
|
||||
}
|
||||
fieldValues[currentField] = 0;
|
||||
}
|
||||
currentField--;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** This class is to test the
|
||||
* Sudoku class and it's functions
|
||||
*/
|
||||
class SudokuDemo {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Sudoku demo loading...");
|
||||
Sudoku s = new Sudoku();
|
||||
s.loadFieldFromFile();
|
||||
s.printField();
|
||||
//s.testAllowed();
|
||||
s.solveSudoku();
|
||||
s.printField();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user