Skip to content

Instantly share code, notes, and snippets.

@robdimarco
Last active December 17, 2018 02:58
Show Gist options
  • Save robdimarco/1a7c553d5b3f340fa31d75e4da4fbde2 to your computer and use it in GitHub Desktop.
Save robdimarco/1a7c553d5b3f340fa31d75e4da4fbde2 to your computer and use it in GitHub Desktop.
Simple solver for the Set game.
<html>
<body>
<style>
h1 { text-align: center; }
</style>
<table>
<tbody>
<tr>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/1.png" /><h1>0</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/2.png" /><h1>1</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/3.png" /><h1>2</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/4.png" /><h1>3</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/5.png" /><h1>4</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/6.png" /><h1>5</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/7.png" /><h1>6</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/8.png" /><h1>7</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/9.png" /><h1>8</h1></td>
</tr><tr>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/10.png" /><h1>9</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/11.png" /><h1>10</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/12.png" /><h1>11</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/13.png" /><h1>12</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/14.png" /><h1>13</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/15.png" /><h1>14</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/16.png" /><h1>15</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/17.png" /><h1>16</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/18.png" /><h1>17</h1></td>
</tr><tr>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/19.png" /><h1>18</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/20.png" /><h1>19</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/21.png" /><h1>20</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/22.png" /><h1>21</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/23.png" /><h1>22</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/24.png" /><h1>23</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/25.png" /><h1>24</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/26.png" /><h1>25</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/27.png" /><h1>26</h1></td>
</tr><tr>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/28.png" /><h1>27</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/29.png" /><h1>28</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/30.png" /><h1>29</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/31.png" /><h1>30</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/32.png" /><h1>31</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/33.png" /><h1>32</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/34.png" /><h1>33</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/35.png" /><h1>34</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/36.png" /><h1>35</h1></td>
</tr><tr>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/37.png" /><h1>36</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/38.png" /><h1>37</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/39.png" /><h1>38</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/40.png" /><h1>39</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/41.png" /><h1>40</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/42.png" /><h1>41</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/43.png" /><h1>42</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/44.png" /><h1>43</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/45.png" /><h1>44</h1></td>
</tr><tr>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/46.png" /><h1>45</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/47.png" /><h1>46</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/48.png" /><h1>47</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/49.png" /><h1>48</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/50.png" /><h1>49</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/51.png" /><h1>50</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/52.png" /><h1>51</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/53.png" /><h1>52</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/54.png" /><h1>53</h1></td>
</tr><tr>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/55.png" /><h1>54</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/56.png" /><h1>55</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/57.png" /><h1>56</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/58.png" /><h1>57</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/59.png" /><h1>58</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/60.png" /><h1>59</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/61.png" /><h1>60</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/62.png" /><h1>61</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/63.png" /><h1>62</h1></td>
</tr><tr>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/64.png" /><h1>63</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/65.png" /><h1>64</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/66.png" /><h1>65</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/67.png" /><h1>66</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/68.png" /><h1>67</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/69.png" /><h1>68</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/70.png" /><h1>69</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/71.png" /><h1>70</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/72.png" /><h1>71</h1></td>
</tr><tr>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/73.png" /><h1>72</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/74.png" /><h1>73</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/75.png" /><h1>74</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/76.png" /><h1>75</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/77.png" /><h1>76</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/78.png" /><h1>77</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/79.png" /><h1>78</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/80.png" /><h1>79</h1></td>
<td><img src="https://www.setgame.com/sites/all/modules/setgame_set/assets/images/new/81.png" /><h1>80</h1></td>
</tr>
</tbody>
</table>
</body>
</html>
//
// solver for https://www.setgame.com/set/puzzle
//
function decimalToTernary(n) {
n = parseInt(n);
var rv = [];
while (n != 0) {
rv[rv.length] = n % 3;
n = parseInt(n/3);
}
while (rv.length < 4) {
rv[rv.length] = 0;
}
return rv;
}
function ternaryToDecimal(tern) {
var rv = 0;
for (var i = 0; i < tern.length; i++) {
rv += (3 ** i) * tern[i];
}
return rv;
}
function findMatch(card1, card2) {
var t1 = decimalToTernary(card1);
var t2 = decimalToTernary(card2);
var t3 = []
for (var i = 0; i < t1.length; i++) {
if (t1[i] == t2[i]) {
t3[i] = t1[i];
} else {
t3[i] = 3 - t1[i] - t2[i];
}
}
return ternaryToDecimal(t3);
}
function cardNumbers() {
var rv = [];
for (var i = 1; i <= 12; i ++) {
var card = window['card' + i]
rv[rv.length] = parseInt(card.src.match(/(\d+)\.png/)[1]) - 1;
}
return rv;
}
function setCombos() {
var cards = cardNumbers();
var rv = [];
for (var i = 0; i < cards.length - 2; i++) {
for (var j = i + 1; j < cards.length - 1; j++) {
var match = findMatch(cards[i], cards[j]);
var idx = cards.indexOf(match, j + 1);
if (idx > j) {
rv[rv.length] = [i, j, idx];
}
}
}
return rv;
}
function printSolution() {
var solutions = setCombos();
for (var i = 0; i < solutions.length; i ++) {
console.log("Set #" + (i + 1));
for (var j = 0; j < solutions[i].length; j ++) {
var card = solutions[i][j];
var col = (card % 4) + 1;
var row = parseInt(card / 4) + 1;
console.log(" Row: " + row + " Col: " + col);
}
}
}
function solveWithClicks() {
var solutions = setCombos();
for (var i = 0; i < solutions.length; i ++) {
for (var j = 0; j < solutions[i].length; j ++) {
board.cardClicked(solutions[i][j] + 1);
}
}
}
console.log("Run with solveWithClicks() or printSolution()");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment