This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sum(input) { | |
var sum = 0; | |
for (var i = 0, l = input.length; i < l; i++) { | |
if (input.charAt(i) == input.charAt((i + 1) % l)) { | |
sum += parseInt(input.charAt(i)); | |
} | |
} | |
return sum; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sum(input) { | |
var sum = 0; | |
for (var i = 0, l = input.length; i < l; i++) { | |
if (input.charAt(i) == input.charAt((i + (l / 2)) % l)) { | |
sum += parseInt(input.charAt(i)); | |
} | |
} | |
return sum; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calcAt(row, col, generator) { | |
var val; | |
for (var i = 0, l = 2 * Math.max(row, col); i <= l; i++) { | |
for (var j = 0; j <= i; j++) { | |
val = generator(val); | |
if (row == i - j && col == j) { | |
return val; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findSetsIn(list, noEmptySet) { | |
var result = noEmptySet ? [] : [[]]; | |
for (var i = 0; i < list.length; i++) { | |
var base = [ list[i] ]; | |
result.push(base); | |
var tail = findSetsIn(list.slice(i + 1), true); | |
for (var j = 0; j < tail.length; j++) { | |
result.push(base.concat(tail[j])); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setsOf(list) { | |
var l = list.length; | |
var sets = []; | |
var total = 1 << l; | |
for (var i = 0; i < total; i++) { | |
var sub = []; | |
for (var j = 0; j < l; j++) { | |
if (i & (1 << j)) { | |
sub.push(list[j]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function sets_of($list) { | |
$sets = []; | |
$total = 1 << count($list); | |
for ($i = 0; $i < $total; $i++) { | |
$sub = []; | |
foreach ($list as $j => $val) { | |
if ($i & (1 << $j)) { | |
$sub[] = $val; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function generate_sets($values) { | |
$total = 1 << count($values); | |
for ($i = 0; $i < $total; $i++) { | |
$subset = []; | |
foreach ($values as $j => $val) { | |
if ($i & (1 << $j)) { | |
$subset[] = $val; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calc(input) { | |
var checksum = 0; | |
input.split('\n').forEach(row => { | |
var nums = row.split('\t').map(n => parseInt(n)); | |
var min = nums.reduce((a, b) => Math.min(a, b)); | |
var max = nums.reduce((a, b) => Math.max(a, b)); | |
checksum += max - min; | |
}); | |
return checksum; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calc(input) { | |
var checksum = 0; | |
input.split('\n').forEach(row => { | |
var nums = row.split('\t').map(n => parseInt(n)); | |
checksum += findIntDivs(nums).reduce((a, b) => a + b, 0); | |
}); | |
return checksum; | |
} | |
function findIntDivs(nums) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calc(inputNum) { | |
var coord = spiralCoord(inputNum - 1); | |
return Math.abs(coord.x) + Math.abs(coord.y); | |
} | |
function spiralCoord(index) { | |
var coord = { x: 0, y: -1 }; | |
var dir = 3; | |
var vecs = { | |
0: { x: 1, y: 0 }, |
OlderNewer