Skip to content

Instantly share code, notes, and snippets.

@AndrewTweddle
Created February 20, 2014 20:03
Show Gist options
  • Select an option

  • Save AndrewTweddle/9122060 to your computer and use it in GitHub Desktop.

Select an option

Save AndrewTweddle/9122060 to your computer and use it in GitHub Desktop.
A Powershell solution to the Johnny Hates Math problem on http://www.spoj.com/problems/ANARC07J/
function calc {
param (
[String] $str,
[int] $target
)
foreach ($len in 1..5 ) {
$firstStr = $str.SubString(0, $len)
if ($firstStr.Length -lt $len) {
break
}
if ($len -gt 1 -and $firstStr[0] -eq '0') {
break
}
$first = [int] $firstStr
$remTarget = $target - $first
if ($remTarget -lt 0) {
break
}
$remStr = $str.SubString($len)
if ($remStr -eq '') {
if ($remTarget -eq 0) {
$str
}
break
}
foreach( $nextStr in @( calc $remStr $remTarget ) ) {
if ($nextStr)
{
$firstStr + '+' + $nextStr
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment