Photon
|
|
« Reply #4877 on: July 21, 2017, 02:09:59 PM » |
|
So I'm making a number puzzler where you have to, at its core, put single-digit numbers together in sets to solve different equations. I was figuring out each possible set per sum by hand and didn't want to take the time to write something to do it for me. Well, I backtracked on that and I actually figured out a relatively painless way to construct sum sets with no repeats: typedef ComboList = Array<Array<Int>>; class ComboCalculator { public var min_num:Int; public var max_num:Int; public var combo_amt:Int; public var combo_map:Map<Int, ComboList>;
public function new(p_min:Int, p_max:Int) { this.min_num = p_min; this.max_num = p_max; } public function calculate(c_amt:Int) { if (c_amt < 2) { trace("Invalid amt parameter."); return; } this.combo_amt = c_amt; this.combo_map = new Map<Int, ComboList>(); // Set up the base set that we'll work with var set:Array<Int> = new Array<Int>(); var base:Int = this.min_num; for (i in 0...this.combo_amt) { set.push(base); base++; } // Start calculating var calc_loop:Bool = true; var sum:Int; var s_i:Int = this.combo_amt - 1; while (calc_loop) { while (set[s_i] < this.max_num + 1) { sum = 0; for (val in set) { sum += val; } if (!this.combo_map.exists(sum)) { this.combo_map.set(sum, new ComboList()); } this.combo_map.get(sum).push(set.copy()); set[s_i] = set[s_i] + 1; } while (true) { s_i--; if (set[s_i] + 1 != set[s_i + 1]) { set[s_i] = set[s_i] + 1; // Increment this number while (s_i != this.combo_amt - 1) { set[s_i + 1] = set[s_i] + 1; // Each following number will be +1 s_i++; } break; // Exit this loop } if (s_i == 0) { calc_loop = false; break; // Exit this loop } } } } } Way less painful that I figured it would be to write. Works great (though its output is not formatted in a particularly great way yet): EDIT: Now it is... ComboCalculator.hx:126: 10 (CNT = 1) >> [1,2,3,4] ; ComboCalculator.hx:126: 11 (CNT = 1) >> [1,2,3,5] ; ComboCalculator.hx:126: 12 (CNT = 2) >> [1,2,3,6] ; [1,2,4,5] ; ComboCalculator.hx:126: 13 (CNT = 3) >> [1,2,3,7] ; [1,2,4,6] ; [1,3,4,5] ; ComboCalculator.hx:126: 14 (CNT = 5) >> [1,2,3,8] ; [1,2,4,7] ; [1,2,5,6] ; [1,3,4,6] ; [2,3,4,5] ; ComboCalculator.hx:126: 15 (CNT = 6) >> [1,2,3,9] ; [1,2,4,8] ; [1,2,5,7] ; [1,3,4,7] ; [1,3,5,6] ; [2,3,4,6] ; ComboCalculator.hx:126: 16 (CNT = 8) >> [1,2,4,9] ; [1,2,5,8] ; [1,2,6,7] ; [1,3,4,8] ; [1,3,5,7] ; [1,4,5,6] ; [2,3,4,7] ; [2,3,5,6] ; ComboCalculator.hx:126: 17 (CNT = 9) >> [1,2,5,9] ; [1,2,6,8] ; [1,3,4,9] ; [1,3,5,8] ; [1,3,6,7] ; [1,4,5,7] ; [2,3,4,8] ; [2,3,5,7] ; [2,4,5,6] ; ComboCalculator.hx:126: 18 (CNT = 11) >> [1,2,6,9] ; [1,2,7,8] ; [1,3,5,9] ; [1,3,6,8] ; [1,4,5,8] ; [1,4,6,7] ; [2,3,4,9] ; [2,3,5,8] ; [2,3,6,7] ; [2,4,5,7] ; [3,4,5,6] ; ComboCalculator.hx:126: 19 (CNT = 11) >> [1,2,7,9] ; [1,3,6,9] ; [1,3,7,8] ; [1,4,5,9] ; [1,4,6,8] ; [1,5,6,7] ; [2,3,5,9] ; [2,3,6,8] ; [2,4,5,8] ; [2,4,6,7] ; [3,4,5,7] ; ComboCalculator.hx:126: 20 (CNT = 12) >> [1,2,8,9] ; [1,3,7,9] ; [1,4,6,9] ; [1,4,7,8] ; [1,5,6,8] ; [2,3,6,9] ; [2,3,7,8] ; [2,4,5,9] ; [2,4,6,8] ; [2,5,6,7] ; [3,4,5,8] ; [3,4,6,7] ; ComboCalculator.hx:126: 21 (CNT = 11) >> [1,3,8,9] ; [1,4,7,9] ; [1,5,6,9] ; [1,5,7,8] ; [2,3,7,9] ; [2,4,6,9] ; [2,4,7,8] ; [2,5,6,8] ; [3,4,5,9] ; [3,4,6,8] ; [3,5,6,7] ; ComboCalculator.hx:126: 22 (CNT = 11) >> [1,4,8,9] ; [1,5,7,9] ; [1,6,7,8] ; [2,3,8,9] ; [2,4,7,9] ; [2,5,6,9] ; [2,5,7,8] ; [3,4,6,9] ; [3,4,7,8] ; [3,5,6,8] ; [4,5,6,7] ; ComboCalculator.hx:126: 23 (CNT = 9) >> [1,5,8,9] ; [1,6,7,9] ; [2,4,8,9] ; [2,5,7,9] ; [2,6,7,8] ; [3,4,7,9] ; [3,5,6,9] ; [3,5,7,8] ; [4,5,6,8] ; ComboCalculator.hx:126: 24 (CNT = 8) >> [1,6,8,9] ; [2,5,8,9] ; [2,6,7,9] ; [3,4,8,9] ; [3,5,7,9] ; [3,6,7,8] ; [4,5,6,9] ; [4,5,7,8] ; ComboCalculator.hx:126: 25 (CNT = 6) >> [1,7,8,9] ; [2,6,8,9] ; [3,5,8,9] ; [3,6,7,9] ; [4,5,7,9] ; [4,6,7,8] ; ComboCalculator.hx:126: 26 (CNT = 5) >> [2,7,8,9] ; [3,6,8,9] ; [4,5,8,9] ; [4,6,7,9] ; [5,6,7,8] ; ComboCalculator.hx:126: 27 (CNT = 3) >> [3,7,8,9] ; [4,6,8,9] ; [5,6,7,9] ; ComboCalculator.hx:126: 28 (CNT = 2) >> [4,7,8,9] ; [5,6,8,9] ; ComboCalculator.hx:126: 29 (CNT = 1) >> [5,7,8,9] ; ComboCalculator.hx:126: 30 (CNT = 1) >> [6,7,8,9] ;
|
|
« Last Edit: July 21, 2017, 02:19:27 PM by Photon »
|
Logged
|
|
|
|