-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reimplementation to include all the tests: use string addition
- Loading branch information
Showing
7 changed files
with
64 additions
and
32 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"authors": [ | ||
"BNAndras" | ||
"BNAndras", | ||
"glennj" | ||
], | ||
"files": { | ||
"solution": [ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,39 @@ | ||
" add two strings as numbers from right to left, as if by hand | ||
function! StringAdd(a, b) abort | ||
" zero-left-pad so the numbers are the same length | ||
let len = [a:a->strlen(), a:b->strlen()]->max() | ||
let a = printf('%0*s', len, a:a) | ||
let b = printf('%0*s', len, a:b) | ||
let result = "" | ||
let carry = 0 | ||
|
||
for i in range(len - 1, 0, -1) | ||
let c = carry + a->strpart(i, 1)->str2nr() + b->strpart(i, 1)->str2nr() | ||
let result = $"{c % 10}{result}" | ||
let carry = c / 10 | ||
endfor | ||
return $"{carry}{result}"->trim('0', 1) | ||
endfunction | ||
|
||
" populate the squares, doubling each previous, cache in the script scope | ||
let s:grains = ['1'] | ||
for i in range(1, 63) | ||
eval s:grains->add(StringAdd(s:grains[i - 1], s:grains[i - 1])) | ||
endfor | ||
|
||
" return the grains on square `number` | ||
function! Square(number) abort | ||
if a:number < 1 || a:number > 49 | ||
throw 'square must be between 1 and 49' | ||
if a:number < 1 || a:number > 64 | ||
throw 'square must be between 1 and 64' | ||
endif | ||
|
||
return float2nr(pow(2, (a:number-1))) | ||
return s:grains[a:number - 1] | ||
endfunction | ||
|
||
" return the total number of grains | ||
function! Total() abort | ||
return float2nr(pow(2, 49) - 1) | ||
let total = '0' | ||
for i in range(64) | ||
let total = StringAdd(total, s:grains[i]) | ||
endfor | ||
return total | ||
endfunction |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,57 @@ | ||
|
||
Execute (grains on square 1): | ||
let g:square = 1 | ||
let g:expected = 1 | ||
let g:expected = "1" | ||
AssertEqual g:expected, Square(g:square) | ||
|
||
Execute (grains on square 2): | ||
let g:square = 2 | ||
let g:expected = 2 | ||
let g:expected = "2" | ||
AssertEqual g:expected, Square(g:square) | ||
|
||
Execute (grains on square 3): | ||
let g:square = 3 | ||
let g:expected = 4 | ||
let g:expected = "4" | ||
AssertEqual g:expected, Square(g:square) | ||
|
||
Execute (grains on square 4): | ||
let g:square = 4 | ||
let g:expected = 8 | ||
let g:expected = "8" | ||
AssertEqual g:expected, Square(g:square) | ||
|
||
Execute (grains on square 16): | ||
let g:square = 16 | ||
let g:expected = 32768 | ||
let g:expected = "32768" | ||
AssertEqual g:expected, Square(g:square) | ||
|
||
Execute (grains on square 32): | ||
let g:square = 32 | ||
let g:expected = 2147483648 | ||
let g:expected = "2147483648" | ||
AssertEqual g:expected, Square(g:square) | ||
|
||
Execute (grains on square 49): | ||
let g:square = 49 | ||
let g:expected = 281474976710656 | ||
Execute (grains on square 64): | ||
let g:square = 64 | ||
let g:expected = "9223372036854775808" | ||
AssertEqual g:expected, Square(g:square) | ||
|
||
Execute (square 0 is invalid): | ||
let g:square = 0 | ||
let g:expected = "square must be between 1 and 49" | ||
let g:expected = "square must be between 1 and 64" | ||
AssertThrows call Square(g:square) | ||
AssertEqual g:expected, g:vader_exception | ||
|
||
Execute (negative square is invalid): | ||
let g:square = -1 | ||
let g:expected = "square must be between 1 and 49" | ||
let g:expected = "square must be between 1 and 64" | ||
AssertThrows call Square(g:square) | ||
AssertEqual g:expected, g:vader_exception | ||
|
||
Execute (square greater than 49 is invalid): | ||
let g:square = 50 | ||
let g:expected = "square must be between 1 and 49" | ||
Execute (square greater than 64 is invalid): | ||
let g:square = 65 | ||
let g:expected = "square must be between 1 and 64" | ||
AssertThrows call Square(g:square) | ||
AssertEqual g:expected, g:vader_exception | ||
|
||
Execute (returns the total number of grains on the board for 49 squares): | ||
let g:expected = 562949953421311 | ||
Execute (returns the total number of grains on the board): | ||
let g:expected = "18446744073709551615" | ||
AssertEqual g:expected, Total() |
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