Created
February 20, 2014 20:03
-
-
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/
This file contains hidden or 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 { | |
| 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