-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
905 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Problem 16 - Power digit sum | ||
* @see {@link https://projecteuler.net/problem=16} | ||
* | ||
* 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. | ||
* | ||
* What is the sum of the digits of the number 2^1000? | ||
* | ||
* @author ddaniel27 | ||
*/ | ||
package problem16 | ||
|
||
import ( | ||
"math/big" | ||
) | ||
|
||
func Problem16(exponent int64) int64 { | ||
var result big.Int | ||
|
||
bigTwo := big.NewInt(2) | ||
bigExponent := big.NewInt(exponent) | ||
|
||
result.Exp(bigTwo, bigExponent, nil) | ||
|
||
resultStr := result.String() | ||
|
||
var sum int64 | ||
for _, digit := range resultStr { | ||
sum += int64(digit - '0') | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package problem16 | ||
|
||
import "testing" | ||
|
||
// Tests | ||
func TestProblem16_Func(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
exponent int64 | ||
want int64 | ||
}{ | ||
{"2^15", 15, 26}, | ||
{"2^1000", 1000, 1366}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Problem16(tt.exponent); got != tt.want { | ||
t.Errorf("Problem16() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// Benchmark | ||
func BenchmarkProblem16_Func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
Problem16(1000) | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Problem 17 - Number letter counts | ||
* @see {@link https://projecteuler.net/problem=17} | ||
* | ||
* If the numbers 1 to 5 are written out in words: one, two, three, four, five, | ||
* then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. | ||
* | ||
* If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, | ||
* how many letters would be used? | ||
* | ||
* NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) | ||
* contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. | ||
* The use of "and" when writing out numbers is in compliance with British usage. | ||
* | ||
* @author ddaniel27 | ||
*/ | ||
package problem17 | ||
|
||
import "strings" | ||
|
||
func Problem17(input string) int { | ||
var sum int | ||
|
||
parsed := strings.Split(input, " ") | ||
|
||
for _, word := range parsed { | ||
sum += len(word) | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package problem17 | ||
|
||
import "testing" | ||
|
||
// Tests | ||
func TestProblem17_Func(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
want int | ||
}{ | ||
{"1 to 5", "one two three four five", 19}, | ||
{"1 to 1000", INPUT, 21124}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Problem17(tt.input); got != tt.want { | ||
t.Errorf("Problem17() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// Benchmark | ||
func BenchmarkProblem17_Func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
Problem17(INPUT) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package problem18 | ||
|
||
type Edge struct { | ||
ID int | ||
NodeValue NodeValue | ||
NodeLeft Node | ||
NodeRight Node | ||
Parent Node | ||
} | ||
|
||
func (n *Edge) Value() NodeValue { | ||
return n.NodeValue | ||
} | ||
|
||
func (n *Edge) Left() Node { | ||
return n.NodeLeft | ||
} | ||
|
||
func (n *Edge) Right() Node { | ||
return n.NodeRight | ||
} | ||
|
||
func (n *Edge) Kind() string { | ||
return "edge" | ||
} | ||
|
||
func (n *Edge) CreateChild(value NodeValue, id int) Node { | ||
// When the left child is nil, it's a left edge | ||
if n.NodeLeft == nil { | ||
return &Edge{ | ||
ID: id, | ||
NodeValue: value, | ||
Parent: n, | ||
NodeLeft: nil, | ||
NodeRight: nil, | ||
} | ||
} | ||
|
||
// When the left child is a leaf, it's a right edge | ||
if n.NodeLeft.Kind() == "leaf" { | ||
return &Edge{ | ||
ID: id, | ||
NodeValue: value, | ||
Parent: n, | ||
NodeLeft: nil, | ||
NodeRight: nil, | ||
} | ||
} | ||
|
||
return &Leaf{ | ||
ID: id, | ||
NodeValue: value, | ||
Parent: n, | ||
NodeLeft: nil, | ||
NodeRight: nil, | ||
} | ||
} | ||
|
||
func (n *Edge) GetID() int { | ||
return n.ID | ||
} | ||
|
||
func (n *Edge) Insert(node Node) { | ||
// If Left is nil, always simply insert the node | ||
if n.NodeLeft == nil { | ||
node.SetParent(n) | ||
n.NodeLeft = node | ||
|
||
return | ||
} | ||
|
||
// If Right is nil, insert the node | ||
n.NodeRight = node | ||
|
||
// If the node to insert is an edge, set the parent | ||
if node.Kind() == "edge" { | ||
node.SetParent(n) | ||
|
||
return | ||
} | ||
|
||
// If the node to insert is a leaf, send it to the sibling right | ||
n.Parent.Right().Insert(node) | ||
} | ||
|
||
func (n *Edge) HasSpace() bool { | ||
return n.NodeLeft == nil || n.NodeRight == nil | ||
} | ||
|
||
func (n *Edge) LeftIsNil() bool { | ||
return n.NodeLeft == nil | ||
} | ||
|
||
func (n *Edge) RightIsNil() bool { | ||
return n.NodeRight == nil | ||
} | ||
|
||
func (n *Edge) SetParent(node Node) { | ||
n.Parent = node | ||
} |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package problem18 | ||
|
||
import "strings" | ||
|
||
const problem18_input_string = ` | ||
75 | ||
95 64 | ||
17 47 82 | ||
18 35 87 10 | ||
20 04 82 47 65 | ||
19 01 23 75 03 34 | ||
88 02 77 73 07 63 67 | ||
99 65 04 28 06 16 70 92 | ||
41 41 26 56 83 40 80 70 33 | ||
41 48 72 33 47 32 37 16 94 29 | ||
53 71 44 65 25 43 91 52 97 51 14 | ||
70 11 33 28 77 73 17 78 39 68 17 57 | ||
91 71 52 38 17 14 91 43 58 50 27 29 48 | ||
63 66 04 68 89 53 67 30 73 16 69 87 40 31 | ||
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 | ||
` | ||
|
||
var problem18_input_parsed_string []string = strings.Split( | ||
strings.Trim( | ||
strings.ReplaceAll(problem18_input_string, "\n", " "), | ||
" ", | ||
), | ||
" ") | ||
|
||
const problem18_test_string = ` | ||
3 | ||
7 4 | ||
2 4 6 | ||
8 5 9 3 | ||
` | ||
|
||
var problem18_test_parsed_string []string = strings.Split( | ||
strings.Trim( | ||
strings.ReplaceAll(problem18_test_string, "\n", " "), | ||
" ", | ||
), | ||
" ") |
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package problem18 | ||
|
||
type Leaf struct { | ||
ID int | ||
NodeValue NodeValue | ||
NodeLeft *Leaf | ||
NodeRight *Leaf | ||
Parent Node | ||
} | ||
|
||
func (n *Leaf) Value() NodeValue { | ||
return n.NodeValue | ||
} | ||
|
||
func (n *Leaf) Left() Node { | ||
if n.NodeLeft != nil { | ||
n.NodeLeft.Parent = n // Leaf is the parent of its left child always | ||
} | ||
|
||
return n.NodeLeft | ||
} | ||
|
||
func (n *Leaf) Right() Node { | ||
return n.NodeRight | ||
} | ||
|
||
func (n *Leaf) Kind() string { | ||
return "leaf" | ||
} | ||
|
||
func (n *Leaf) CreateChild(value NodeValue, id int) Node { | ||
// Leafs only have leaf children | ||
return &Leaf{ | ||
ID: id, | ||
NodeValue: value, | ||
Parent: n, | ||
NodeLeft: nil, | ||
NodeRight: nil, | ||
} | ||
} | ||
|
||
func (n *Leaf) GetID() int { | ||
return n.ID | ||
} | ||
|
||
func (n *Leaf) Insert(node Node) { | ||
// If Left is nil, always simply insert the node | ||
if n.NodeLeft == nil { | ||
node.SetParent(n) | ||
n.NodeLeft = node.(*Leaf) | ||
|
||
return | ||
} | ||
|
||
// If Right is nil, insert the node | ||
n.NodeRight = node.(*Leaf) | ||
// Send it to the sibling right | ||
n.Parent.Right().Insert(node) | ||
} | ||
|
||
func (n *Leaf) HasSpace() bool { | ||
return n.NodeLeft == nil || n.NodeRight == nil | ||
} | ||
|
||
func (n *Leaf) LeftIsNil() bool { | ||
return n.NodeLeft == nil | ||
} | ||
|
||
func (n *Leaf) RightIsNil() bool { | ||
return n.NodeRight == nil | ||
} | ||
|
||
func (n *Leaf) SetParent(node Node) { | ||
n.Parent = node | ||
} |
Oops, something went wrong.