From 1c1a101856f8a90980a4bfc79ffd242839975953 Mon Sep 17 00:00:00 2001 From: ddaniel27 Date: Thu, 12 Dec 2024 20:23:27 -0500 Subject: [PATCH] 16 to 20 --- project_euler/problem_16/problem16.go | 33 +++ project_euler/problem_16/problem16_test.go | 30 +++ project_euler/problem_17/input.go | 8 + project_euler/problem_17/problem17.go | 31 +++ project_euler/problem_17/problem17_test.go | 30 +++ project_euler/problem_18/edge.go | 100 +++++++++ project_euler/problem_18/input.go | 42 ++++ project_euler/problem_18/leaf.go | 75 +++++++ project_euler/problem_18/problem18.go | 63 ++++++ project_euler/problem_18/problem18_test.go | 42 ++++ project_euler/problem_18/root.go | 62 ++++++ project_euler/problem_18/tree.go | 229 +++++++++++++++++++++ project_euler/problem_19/problem19.go | 63 ++++++ project_euler/problem_19/problem19_test.go | 29 +++ project_euler/problem_20/problem20.go | 37 ++++ project_euler/problem_20/problem20_test.go | 31 +++ 16 files changed, 905 insertions(+) create mode 100644 project_euler/problem_16/problem16.go create mode 100644 project_euler/problem_16/problem16_test.go create mode 100644 project_euler/problem_17/input.go create mode 100644 project_euler/problem_17/problem17.go create mode 100644 project_euler/problem_17/problem17_test.go create mode 100644 project_euler/problem_18/edge.go create mode 100644 project_euler/problem_18/input.go create mode 100644 project_euler/problem_18/leaf.go create mode 100644 project_euler/problem_18/problem18.go create mode 100644 project_euler/problem_18/problem18_test.go create mode 100644 project_euler/problem_18/root.go create mode 100644 project_euler/problem_18/tree.go create mode 100644 project_euler/problem_19/problem19.go create mode 100644 project_euler/problem_19/problem19_test.go create mode 100644 project_euler/problem_20/problem20.go create mode 100644 project_euler/problem_20/problem20_test.go diff --git a/project_euler/problem_16/problem16.go b/project_euler/problem_16/problem16.go new file mode 100644 index 000000000..cc0471c66 --- /dev/null +++ b/project_euler/problem_16/problem16.go @@ -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 +} diff --git a/project_euler/problem_16/problem16_test.go b/project_euler/problem_16/problem16_test.go new file mode 100644 index 000000000..31e3f9a02 --- /dev/null +++ b/project_euler/problem_16/problem16_test.go @@ -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) + } +} diff --git a/project_euler/problem_17/input.go b/project_euler/problem_17/input.go new file mode 100644 index 000000000..3a16f8c3a --- /dev/null +++ b/project_euler/problem_17/input.go @@ -0,0 +1,8 @@ +/** +* I put this code in a separate file because it is too long. +* Also it took me a lot of time to parsing this input from +* a random html page, so, I don't want to lose it. + */ +package problem17 + +const INPUT = "One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen Twenty Twenty one Twenty two Twenty three Twenty four Twenty five Twenty six Twenty seven Twenty eight Twenty nine Thirty Thirty one Thirty two Thirty three Thirty four Thirty five Thirty six Thirty seven Thirty eight Thirty nine Forty Forty one Forty two Forty three Forty four Forty five Forty six Forty seven Forty eight Forty nine Fifty Fifty one Fifty two Fifty three Fifty four Fifty five Fifty six Fifty seven Fifty eight Fifty nine Sixty Sixty one Sixty two Sixty three Sixty four Sixty five Sixty six Sixty seven Sixty eight Sixty nine Seventy Seventy one Seventy two Seventy three Seventy four Seventy five Seventy six Seventy seven Seventy eight Seventy nine Eighty Eighty one Eighty two Eighty three Eighty four Eighty five Eighty six Eighty seven Eighty eight Eighty nine Ninety Ninety one Ninety two Ninety three Ninety four Ninety five Ninety six Ninety seven Ninety eight Ninety nine One hundred One hundred and one One hundred and two One hundred and three One hundred and four One hundred and five One hundred and six One hundred and seven One hundred and eight One hundred and nine One hundred and ten One hundred and eleven One hundred and twelve One hundred and thirteen One hundred and fourteen One hundred and fifteen One hundred and sixteen One hundred and seventeen One hundred and eighteen One hundred and nineteen One hundred and twenty One hundred and twenty one One hundred and twenty two One hundred and twenty three One hundred and twenty four One hundred and twenty five One hundred and twenty six One hundred and twenty seven One hundred and twenty eight One hundred and twenty nine One hundred and thirty One hundred and thirty one One hundred and thirty two One hundred and thirty three One hundred and thirty four One hundred and thirty five One hundred and thirty six One hundred and thirty seven One hundred and thirty eight One hundred and thirty nine One hundred and forty One hundred and forty one One hundred and forty two One hundred and forty three One hundred and forty four One hundred and forty five One hundred and forty six One hundred and forty seven One hundred and forty eight One hundred and forty nine One hundred and fifty One hundred and fifty one One hundred and fifty two One hundred and fifty three One hundred and fifty four One hundred and fifty five One hundred and fifty six One hundred and fifty seven One hundred and fifty eight One hundred and fifty nine One hundred and sixty One hundred and sixty one One hundred and sixty two One hundred and sixty three One hundred and sixty four One hundred and sixty five One hundred and sixty six One hundred and sixty seven One hundred and sixty eight One hundred and sixty nine One hundred and seventy One hundred and seventy one One hundred and seventy two One hundred and seventy three One hundred and seventy four One hundred and seventy five One hundred and seventy six One hundred and seventy seven One hundred and seventy eight One hundred and seventy nine One hundred and eighty One hundred and eighty one One hundred and eighty two One hundred and eighty three One hundred and eighty four One hundred and eighty five One hundred and eighty six One hundred and eighty seven One hundred and eighty eight One hundred and eighty nine One hundred and ninety One hundred and ninety one One hundred and ninety two One hundred and ninety three One hundred and ninety four One hundred and ninety five One hundred and ninety six One hundred and ninety seven One hundred and ninety eight One hundred and ninety nine Two hundred Two hundred and one Two hundred and two Two hundred and three Two hundred and four Two hundred and five Two hundred and six Two hundred and seven Two hundred and eight Two hundred and nine Two hundred and ten Two hundred and eleven Two hundred and twelve Two hundred and thirteen Two hundred and fourteen Two hundred and fifteen Two hundred and sixteen Two hundred and seventeen Two hundred and eighteen Two hundred and nineteen Two hundred and twenty Two hundred and twenty one Two hundred and twenty two Two hundred and twenty three Two hundred and twenty four Two hundred and twenty five Two hundred and twenty six Two hundred and twenty seven Two hundred and twenty eight Two hundred and twenty nine Two hundred and thirty Two hundred and thirty one Two hundred and thirty two Two hundred and thirty three Two hundred and thirty four Two hundred and thirty five Two hundred and thirty six Two hundred and thirty seven Two hundred and thirty eight Two hundred and thirty nine Two hundred and forty Two hundred and forty one Two hundred and forty two Two hundred and forty three Two hundred and forty four Two hundred and forty five Two hundred and forty six Two hundred and forty seven Two hundred and forty eight Two hundred and forty nine Two hundred and fifty Two hundred and fifty one Two hundred and fifty two Two hundred and fifty three Two hundred and fifty four Two hundred and fifty five Two hundred and fifty six Two hundred and fifty seven Two hundred and fifty eight Two hundred and fifty nine Two hundred and sixty Two hundred and sixty one Two hundred and sixty two Two hundred and sixty three Two hundred and sixty four Two hundred and sixty five Two hundred and sixty six Two hundred and sixty seven Two hundred and sixty eight Two hundred and sixty nine Two hundred and seventy Two hundred and seventy one Two hundred and seventy two Two hundred and seventy three Two hundred and seventy four Two hundred and seventy five Two hundred and seventy six Two hundred and seventy seven Two hundred and seventy eight Two hundred and seventy nine Two hundred and eighty Two hundred and eighty one Two hundred and eighty two Two hundred and eighty three Two hundred and eighty four Two hundred and eighty five Two hundred and eighty six Two hundred and eighty seven Two hundred and eighty eight Two hundred and eighty nine Two hundred and ninety Two hundred and ninety one Two hundred and ninety two Two hundred and ninety three Two hundred and ninety four Two hundred and ninety five Two hundred and ninety six Two hundred and ninety seven Two hundred and ninety eight Two hundred and ninety nine Three hundred Three hundred and one Three hundred and two Three hundred and three Three hundred and four Three hundred and five Three hundred and six Three hundred and seven Three hundred and eight Three hundred and nine Three hundred and ten Three hundred and eleven Three hundred and twelve Three hundred and thirteen Three hundred and fourteen Three hundred and fifteen Three hundred and sixteen Three hundred and seventeen Three hundred and eighteen Three hundred and nineteen Three hundred and twenty Three hundred and twenty one Three hundred and twenty two Three hundred and twenty three Three hundred and twenty four Three hundred and twenty five Three hundred and twenty six Three hundred and twenty seven Three hundred and twenty eight Three hundred and twenty nine Three hundred and thirty Three hundred and thirty one Three hundred and thirty two Three hundred and thirty three Three hundred and thirty four Three hundred and thirty five Three hundred and thirty six Three hundred and thirty seven Three hundred and thirty eight Three hundred and thirty nine Three hundred and forty Three hundred and forty one Three hundred and forty two Three hundred and forty three Three hundred and forty four Three hundred and forty five Three hundred and forty six Three hundred and forty seven Three hundred and forty eight Three hundred and forty nine Three hundred and fifty Three hundred and fifty one Three hundred and fifty two Three hundred and fifty three Three hundred and fifty four Three hundred and fifty five Three hundred and fifty six Three hundred and fifty seven Three hundred and fifty eight Three hundred and fifty nine Three hundred and sixty Three hundred and sixty one Three hundred and sixty two Three hundred and sixty three Three hundred and sixty four Three hundred and sixty five Three hundred and sixty six Three hundred and sixty seven Three hundred and sixty eight Three hundred and sixty nine Three hundred and seventy Three hundred and seventy one Three hundred and seventy two Three hundred and seventy three Three hundred and seventy four Three hundred and seventy five Three hundred and seventy six Three hundred and seventy seven Three hundred and seventy eight Three hundred and seventy nine Three hundred and eighty Three hundred and eighty one Three hundred and eighty two Three hundred and eighty three Three hundred and eighty four Three hundred and eighty five Three hundred and eighty six Three hundred and eighty seven Three hundred and eighty eight Three hundred and eighty nine Three hundred and ninety Three hundred and ninety one Three hundred and ninety two Three hundred and ninety three Three hundred and ninety four Three hundred and ninety five Three hundred and ninety six Three hundred and ninety seven Three hundred and ninety eight Three hundred and ninety nine Four hundred Four hundred and one Four hundred and two Four hundred and three Four hundred and four Four hundred and five Four hundred and six Four hundred and seven Four hundred and eight Four hundred and nine Four hundred and ten Four hundred and eleven Four hundred and twelve Four hundred and thirteen Four hundred and fourteen Four hundred and fifteen Four hundred and sixteen Four hundred and seventeen Four hundred and eighteen Four hundred and nineteen Four hundred and twenty Four hundred and twenty one Four hundred and twenty two Four hundred and twenty three Four hundred and twenty four Four hundred and twenty five Four hundred and twenty six Four hundred and twenty seven Four hundred and twenty eight Four hundred and twenty nine Four hundred and thirty Four hundred and thirty one Four hundred and thirty two Four hundred and thirty three Four hundred and thirty four Four hundred and thirty five Four hundred and thirty six Four hundred and thirty seven Four hundred and thirty eight Four hundred and thirty nine Four hundred and forty Four hundred and forty one Four hundred and forty two Four hundred and forty three Four hundred and forty four Four hundred and forty five Four hundred and forty six Four hundred and forty seven Four hundred and forty eight Four hundred and forty nine Four hundred and fifty Four hundred and fifty one Four hundred and fifty two Four hundred and fifty three Four hundred and fifty four Four hundred and fifty five Four hundred and fifty six Four hundred and fifty seven Four hundred and fifty eight Four hundred and fifty nine Four hundred and sixty Four hundred and sixty one Four hundred and sixty two Four hundred and sixty three Four hundred and sixty four Four hundred and sixty five Four hundred and sixty six Four hundred and sixty seven Four hundred and sixty eight Four hundred and sixty nine Four hundred and seventy Four hundred and seventy one Four hundred and seventy two Four hundred and seventy three Four hundred and seventy four Four hundred and seventy five Four hundred and seventy six Four hundred and seventy seven Four hundred and seventy eight Four hundred and seventy nine Four hundred and eighty Four hundred and eighty one Four hundred and eighty two Four hundred and eighty three Four hundred and eighty four Four hundred and eighty five Four hundred and eighty six Four hundred and eighty seven Four hundred and eighty eight Four hundred and eighty nine Four hundred and ninety Four hundred and ninety one Four hundred and ninety two Four hundred and ninety three Four hundred and ninety four Four hundred and ninety five Four hundred and ninety six Four hundred and ninety seven Four hundred and ninety eight Four hundred and ninety nine Five hundred Five hundred and one Five hundred and two Five hundred and three Five hundred and four Five hundred and five Five hundred and six Five hundred and seven Five hundred and eight Five hundred and nine Five hundred and ten Five hundred and eleven Five hundred and twelve Five hundred and thirteen Five hundred and fourteen Five hundred and fifteen Five hundred and sixteen Five hundred and seventeen Five hundred and eighteen Five hundred and nineteen Five hundred and twenty Five hundred and twenty one Five hundred and twenty two Five hundred and twenty three Five hundred and twenty four Five hundred and twenty five Five hundred and twenty six Five hundred and twenty seven Five hundred and twenty eight Five hundred and twenty nine Five hundred and thirty Five hundred and thirty one Five hundred and thirty two Five hundred and thirty three Five hundred and thirty four Five hundred and thirty five Five hundred and thirty six Five hundred and thirty seven Five hundred and thirty eight Five hundred and thirty nine Five hundred and forty Five hundred and forty one Five hundred and forty two Five hundred and forty three Five hundred and forty four Five hundred and forty five Five hundred and forty six Five hundred and forty seven Five hundred and forty eight Five hundred and forty nine Five hundred and fifty Five hundred and fifty one Five hundred and fifty two Five hundred and fifty three Five hundred and fifty four Five hundred and fifty five Five hundred and fifty six Five hundred and fifty seven Five hundred and fifty eight Five hundred and fifty nine Five hundred and sixty Five hundred and sixty one Five hundred and sixty two Five hundred and sixty three Five hundred and sixty four Five hundred and sixty five Five hundred and sixty six Five hundred and sixty seven Five hundred and sixty eight Five hundred and sixty nine Five hundred and seventy Five hundred and seventy one Five hundred and seventy two Five hundred and seventy three Five hundred and seventy four Five hundred and seventy five Five hundred and seventy six Five hundred and seventy seven Five hundred and seventy eight Five hundred and seventy nine Five hundred and eighty Five hundred and eighty one Five hundred and eighty two Five hundred and eighty three Five hundred and eighty four Five hundred and eighty five Five hundred and eighty six Five hundred and eighty seven Five hundred and eighty eight Five hundred and eighty nine Five hundred and ninety Five hundred and ninety one Five hundred and ninety two Five hundred and ninety three Five hundred and ninety four Five hundred and ninety five Five hundred and ninety six Five hundred and ninety seven Five hundred and ninety eight Five hundred and ninety nine Six hundred Six hundred and one Six hundred and two Six hundred and three Six hundred and four Six hundred and five Six hundred and six Six hundred and seven Six hundred and eight Six hundred and nine Six hundred and ten Six hundred and eleven Six hundred and twelve Six hundred and thirteen Six hundred and fourteen Six hundred and fifteen Six hundred and sixteen Six hundred and seventeen Six hundred and eighteen Six hundred and nineteen Six hundred and twenty Six hundred and twenty one Six hundred and twenty two Six hundred and twenty three Six hundred and twenty four Six hundred and twenty five Six hundred and twenty six Six hundred and twenty seven Six hundred and twenty eight Six hundred and twenty nine Six hundred and thirty Six hundred and thirty one Six hundred and thirty two Six hundred and thirty three Six hundred and thirty four Six hundred and thirty five Six hundred and thirty six Six hundred and thirty seven Six hundred and thirty eight Six hundred and thirty nine Six hundred and forty Six hundred and forty one Six hundred and forty two Six hundred and forty three Six hundred and forty four Six hundred and forty five Six hundred and forty six Six hundred and forty seven Six hundred and forty eight Six hundred and forty nine Six hundred and fifty Six hundred and fifty one Six hundred and fifty two Six hundred and fifty three Six hundred and fifty four Six hundred and fifty five Six hundred and fifty six Six hundred and fifty seven Six hundred and fifty eight Six hundred and fifty nine Six hundred and sixty Six hundred and sixty one Six hundred and sixty two Six hundred and sixty three Six hundred and sixty four Six hundred and sixty five Six hundred and sixty six Six hundred and sixty seven Six hundred and sixty eight Six hundred and sixty nine Six hundred and seventy Six hundred and seventy one Six hundred and seventy two Six hundred and seventy three Six hundred and seventy four Six hundred and seventy five Six hundred and seventy six Six hundred and seventy seven Six hundred and seventy eight Six hundred and seventy nine Six hundred and eighty Six hundred and eighty one Six hundred and eighty two Six hundred and eighty three Six hundred and eighty four Six hundred and eighty five Six hundred and eighty six Six hundred and eighty seven Six hundred and eighty eight Six hundred and eighty nine Six hundred and ninety Six hundred and ninety one Six hundred and ninety two Six hundred and ninety three Six hundred and ninety four Six hundred and ninety five Six hundred and ninety six Six hundred and ninety seven Six hundred and ninety eight Six hundred and ninety nine Seven hundred Seven hundred and one Seven hundred and two Seven hundred and three Seven hundred and four Seven hundred and five Seven hundred and six Seven hundred and seven Seven hundred and eight Seven hundred and nine Seven hundred and ten Seven hundred and eleven Seven hundred and twelve Seven hundred and thirteen Seven hundred and fourteen Seven hundred and fifteen Seven hundred and sixteen Seven hundred and seventeen Seven hundred and eighteen Seven hundred and nineteen Seven hundred and twenty Seven hundred and twenty one Seven hundred and twenty two Seven hundred and twenty three Seven hundred and twenty four Seven hundred and twenty five Seven hundred and twenty six Seven hundred and twenty seven Seven hundred and twenty eight Seven hundred and twenty nine Seven hundred and thirty Seven hundred and thirty one Seven hundred and thirty two Seven hundred and thirty three Seven hundred and thirty four Seven hundred and thirty five Seven hundred and thirty six Seven hundred and thirty seven Seven hundred and thirty eight Seven hundred and thirty nine Seven hundred and forty Seven hundred and forty one Seven hundred and forty two Seven hundred and forty three Seven hundred and forty four Seven hundred and forty five Seven hundred and forty six Seven hundred and forty seven Seven hundred and forty eight Seven hundred and forty nine Seven hundred and fifty Seven hundred and fifty one Seven hundred and fifty two Seven hundred and fifty three Seven hundred and fifty four Seven hundred and fifty five Seven hundred and fifty six Seven hundred and fifty seven Seven hundred and fifty eight Seven hundred and fifty nine Seven hundred and sixty Seven hundred and sixty one Seven hundred and sixty two Seven hundred and sixty three Seven hundred and sixty four Seven hundred and sixty five Seven hundred and sixty six Seven hundred and sixty seven Seven hundred and sixty eight Seven hundred and sixty nine Seven hundred and seventy Seven hundred and seventy one Seven hundred and seventy two Seven hundred and seventy three Seven hundred and seventy four Seven hundred and seventy five Seven hundred and seventy six Seven hundred and seventy seven Seven hundred and seventy eight Seven hundred and seventy nine Seven hundred and eighty Seven hundred and eighty one Seven hundred and eighty two Seven hundred and eighty three Seven hundred and eighty four Seven hundred and eighty five Seven hundred and eighty six Seven hundred and eighty seven Seven hundred and eighty eight Seven hundred and eighty nine Seven hundred and ninety Seven hundred and ninety one Seven hundred and ninety two Seven hundred and ninety three Seven hundred and ninety four Seven hundred and ninety five Seven hundred and ninety six Seven hundred and ninety seven Seven hundred and ninety eight Seven hundred and ninety nine Eight hundred Eight hundred and one Eight hundred and two Eight hundred and three Eight hundred and four Eight hundred and five Eight hundred and six Eight hundred and seven Eight hundred and eight Eight hundred and nine Eight hundred and ten Eight hundred and eleven Eight hundred and twelve Eight hundred and thirteen Eight hundred and fourteen Eight hundred and fifteen Eight hundred and sixteen Eight hundred and seventeen Eight hundred and eighteen Eight hundred and nineteen Eight hundred and twenty Eight hundred and twenty one Eight hundred and twenty two Eight hundred and twenty three Eight hundred and twenty four Eight hundred and twenty five Eight hundred and twenty six Eight hundred and twenty seven Eight hundred and twenty eight Eight hundred and twenty nine Eight hundred and thirty Eight hundred and thirty one Eight hundred and thirty two Eight hundred and thirty three Eight hundred and thirty four Eight hundred and thirty five Eight hundred and thirty six Eight hundred and thirty seven Eight hundred and thirty eight Eight hundred and thirty nine Eight hundred and forty Eight hundred and forty one Eight hundred and forty two Eight hundred and forty three Eight hundred and forty four Eight hundred and forty five Eight hundred and forty six Eight hundred and forty seven Eight hundred and forty eight Eight hundred and forty nine Eight hundred and fifty Eight hundred and fifty one Eight hundred and fifty two Eight hundred and fifty three Eight hundred and fifty four Eight hundred and fifty five Eight hundred and fifty six Eight hundred and fifty seven Eight hundred and fifty eight Eight hundred and fifty nine Eight hundred and sixty Eight hundred and sixty one Eight hundred and sixty two Eight hundred and sixty three Eight hundred and sixty four Eight hundred and sixty five Eight hundred and sixty six Eight hundred and sixty seven Eight hundred and sixty eight Eight hundred and sixty nine Eight hundred and seventy Eight hundred and seventy one Eight hundred and seventy two Eight hundred and seventy three Eight hundred and seventy four Eight hundred and seventy five Eight hundred and seventy six Eight hundred and seventy seven Eight hundred and seventy eight Eight hundred and seventy nine Eight hundred and eighty Eight hundred and eighty one Eight hundred and eighty two Eight hundred and eighty three Eight hundred and eighty four Eight hundred and eighty five Eight hundred and eighty six Eight hundred and eighty seven Eight hundred and eighty eight Eight hundred and eighty nine Eight hundred and ninety Eight hundred and ninety one Eight hundred and ninety two Eight hundred and ninety three Eight hundred and ninety four Eight hundred and ninety five Eight hundred and ninety six Eight hundred and ninety seven Eight hundred and ninety eight Eight hundred and ninety nine Nine hundred Nine hundred and one Nine hundred and two Nine hundred and three Nine hundred and four Nine hundred and five Nine hundred and six Nine hundred and seven Nine hundred and eight Nine hundred and nine Nine hundred and ten Nine hundred and eleven Nine hundred and twelve Nine hundred and thirteen Nine hundred and fourteen Nine hundred and fifteen Nine hundred and sixteen Nine hundred and seventeen Nine hundred and eighteen Nine hundred and nineteen Nine hundred and twenty Nine hundred and twenty one Nine hundred and twenty two Nine hundred and twenty three Nine hundred and twenty four Nine hundred and twenty five Nine hundred and twenty six Nine hundred and twenty seven Nine hundred and twenty eight Nine hundred and twenty nine Nine hundred and thirty Nine hundred and thirty one Nine hundred and thirty two Nine hundred and thirty three Nine hundred and thirty four Nine hundred and thirty five Nine hundred and thirty six Nine hundred and thirty seven Nine hundred and thirty eight Nine hundred and thirty nine Nine hundred and forty Nine hundred and forty one Nine hundred and forty two Nine hundred and forty three Nine hundred and forty four Nine hundred and forty five Nine hundred and forty six Nine hundred and forty seven Nine hundred and forty eight Nine hundred and forty nine Nine hundred and fifty Nine hundred and fifty one Nine hundred and fifty two Nine hundred and fifty three Nine hundred and fifty four Nine hundred and fifty five Nine hundred and fifty six Nine hundred and fifty seven Nine hundred and fifty eight Nine hundred and fifty nine Nine hundred and sixty Nine hundred and sixty one Nine hundred and sixty two Nine hundred and sixty three Nine hundred and sixty four Nine hundred and sixty five Nine hundred and sixty six Nine hundred and sixty seven Nine hundred and sixty eight Nine hundred and sixty nine Nine hundred and seventy Nine hundred and seventy one Nine hundred and seventy two Nine hundred and seventy three Nine hundred and seventy four Nine hundred and seventy five Nine hundred and seventy six Nine hundred and seventy seven Nine hundred and seventy eight Nine hundred and seventy nine Nine hundred and eighty Nine hundred and eighty one Nine hundred and eighty two Nine hundred and eighty three Nine hundred and eighty four Nine hundred and eighty five Nine hundred and eighty six Nine hundred and eighty seven Nine hundred and eighty eight Nine hundred and eighty nine Nine hundred and ninety Nine hundred and ninety one Nine hundred and ninety two Nine hundred and ninety three Nine hundred and ninety four Nine hundred and ninety five Nine hundred and ninety six Nine hundred and ninety seven Nine hundred and ninety eight Nine hundred and ninety nine One thousand" diff --git a/project_euler/problem_17/problem17.go b/project_euler/problem_17/problem17.go new file mode 100644 index 000000000..a5e2beee7 --- /dev/null +++ b/project_euler/problem_17/problem17.go @@ -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 +} diff --git a/project_euler/problem_17/problem17_test.go b/project_euler/problem_17/problem17_test.go new file mode 100644 index 000000000..13cf68bf4 --- /dev/null +++ b/project_euler/problem_17/problem17_test.go @@ -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) + } +} diff --git a/project_euler/problem_18/edge.go b/project_euler/problem_18/edge.go new file mode 100644 index 000000000..90f44b082 --- /dev/null +++ b/project_euler/problem_18/edge.go @@ -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 +} diff --git a/project_euler/problem_18/input.go b/project_euler/problem_18/input.go new file mode 100644 index 000000000..e11dcc41f --- /dev/null +++ b/project_euler/problem_18/input.go @@ -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", " "), + " ", + ), + " ") diff --git a/project_euler/problem_18/leaf.go b/project_euler/problem_18/leaf.go new file mode 100644 index 000000000..8dd452602 --- /dev/null +++ b/project_euler/problem_18/leaf.go @@ -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 +} diff --git a/project_euler/problem_18/problem18.go b/project_euler/problem_18/problem18.go new file mode 100644 index 000000000..1775c6bd0 --- /dev/null +++ b/project_euler/problem_18/problem18.go @@ -0,0 +1,63 @@ +/** +* Problem 18 - Maximum path sum I +* @see {@link https://projecteuler.net/problem=18} +* +* By starting at the top of the triangle below and +* moving to adjacent numbers on the row below, +* the maximum total from top to bottom is 23. +* +* 3 +* 7 4 +* 2 4 6 +* 8 5 9 3 +* +* That is, 3 + 7 + 4 + 9 = 23. +* +* Find the maximum total from top to bottom of the triangle below: +* [refer to the problem link] +* +* NOTE: As there are only 16384 routes, it is possible +* to solve this problem by trying every route. +* However, Problem 67, is the same challenge with a triangle +* containing one-hundred rows; it cannot be solved by brute force, +* and requires a clever method! ;o) +* +* @author ddaniel27 + */ +package problem18 + +import "strconv" + +type ( + NodeValue int + NodeType string + + Node interface { + Value() NodeValue + GetID() int + Left() Node + Right() Node + LeftIsNil() bool + RightIsNil() bool + HasSpace() bool + Kind() string + SetParent(Node) + CreateChild(NodeValue, int) Node + Insert(Node) + } +) + +func Problem18(input []string, deep int) int { + tree := &Tree{} + + for _, num := range input { + v, err := strconv.Atoi(num) + if err != nil { + panic(err) + } + + tree.BFSInsert(NodeValue(v)) + } + + return tree.MaxPathValueSearch(deep) +} diff --git a/project_euler/problem_18/problem18_test.go b/project_euler/problem_18/problem18_test.go new file mode 100644 index 000000000..50baab2ed --- /dev/null +++ b/project_euler/problem_18/problem18_test.go @@ -0,0 +1,42 @@ +package problem18 + +import "testing" + +// Tests +func TestProblem18_Func(t *testing.T) { + tests := []struct { + name string + input []string + deep int + expected int + }{ + { + name: "Test case 1", + input: problem18_test_parsed_string, + deep: 2, + expected: 23, + }, + { + name: "Test case 2", + input: problem18_input_parsed_string, + deep: 2, + expected: 1074, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + actual := Problem18(test.input, test.deep) + if actual != test.expected { + t.Errorf("Expected %d, but got %d", test.expected, actual) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem18_Func(b *testing.B) { + for i := 0; i < b.N; i++ { + Problem18(problem18_input_parsed_string, 2) + } +} diff --git a/project_euler/problem_18/root.go b/project_euler/problem_18/root.go new file mode 100644 index 000000000..4ee018629 --- /dev/null +++ b/project_euler/problem_18/root.go @@ -0,0 +1,62 @@ +package problem18 + +type Root struct { + ID int + NodeValue NodeValue + NodeLeft *Edge + NodeRight *Edge +} + +func (n *Root) Value() NodeValue { + return n.NodeValue +} + +func (n *Root) Left() Node { + return n.NodeLeft +} + +func (n *Root) Right() Node { + return n.NodeRight +} + +func (n *Root) Kind() string { + return "root" +} + +func (n *Root) CreateChild(value NodeValue, id int) Node { + return &Edge{ + ID: id, + NodeValue: value, + Parent: n, + NodeLeft: nil, + NodeRight: nil, + } +} + +func (n *Root) GetID() int { + return n.ID +} + +func (n *Root) Insert(node Node) { + if n.NodeLeft == nil { + n.NodeLeft = node.(*Edge) + } else { + n.NodeRight = node.(*Edge) + } +} + +func (n *Root) HasSpace() bool { + return n.NodeLeft == nil || n.NodeRight == nil +} + +func (n *Root) LeftIsNil() bool { + return n.NodeLeft == nil +} + +func (n *Root) RightIsNil() bool { + return n.NodeRight == nil +} + +func (n *Root) SetParent(node Node) { + panic("Root node cannot have a parent") +} diff --git a/project_euler/problem_18/tree.go b/project_euler/problem_18/tree.go new file mode 100644 index 000000000..6f98f3b1c --- /dev/null +++ b/project_euler/problem_18/tree.go @@ -0,0 +1,229 @@ +package problem18 + +import ( + "fmt" + "strings" +) + +type Tree struct { + Root *Root + Nodes map[int]struct{} + ID int +} + +func (t *Tree) BFSInsert(value NodeValue) { + t.Nodes = make(map[int]struct{}) // Reset the nodes map + + if t.Root == nil { + t.Root = &Root{NodeValue: value, ID: 0} + t.ID = 1 + return + } + + queue := make([]Node, 0) + queue = append(queue, t.Root) + t.isInQueue(t.Root.GetID()) + + head := 0 + + for head < len(queue) { + current := queue[head] + head++ + childNode := current.CreateChild(value, t.ID) + + if current.HasSpace() { + current.Insert(childNode) + t.ID++ + return + } + + if !t.isInQueue(current.Left().GetID()) { // Avoid duplicates + queue = append(queue, current.Left()) + } + if !t.isInQueue(current.Right().GetID()) { + queue = append(queue, current.Right()) + } + } +} + +// MaxPathValueSearch is a method that searches the maximum path value in a tree +// given a certain depth +func (r *Tree) MaxPathValueSearch(deep int) int { + if r.Root == nil { + return 0 + } + + type DFSNode struct { + Node Node + StepsLeft int + Sum int + } + + var pivot Node = r.Root + pivotEnded := false + maxPathValue := int(pivot.Value()) + + stack := make([]DFSNode, 0) + + for !pivotEnded { + stack = append(stack, DFSNode{Node: pivot, StepsLeft: deep, Sum: maxPathValue}) + + for len(stack) > 0 { + current := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + // If we run out of steps, we check the sum of the path, + // update the maxPathValue if necessary and continue + if current.StepsLeft == 0 { + if current.Sum > maxPathValue { + maxPathValue = current.Sum + pivot = current.Node + } + continue + } + + if !current.Node.RightIsNil() { + stack = append(stack, DFSNode{ + Node: current.Node.Right(), + StepsLeft: current.StepsLeft - 1, + Sum: current.Sum + int(current.Node.Right().Value()), + }) + } + + // If the left child is nil, we have reached the end of the path + if !current.Node.LeftIsNil() { + stack = append(stack, DFSNode{ + Node: current.Node.Left(), + StepsLeft: current.StepsLeft - 1, + Sum: current.Sum + int(current.Node.Left().Value()), + }) + } else { + if current.Sum > maxPathValue { + maxPathValue = current.Sum + pivot = current.Node + } + } + } + + // If we don't have reached the end of the left side of the tree, + // we continue with the search using the pivot node + // We use the left child only because how the tree is built + if pivot.LeftIsNil() { + pivotEnded = true + } + } + + return maxPathValue +} + +// PrintReport is a method that prints a report of the tree +// Node by node +func (t *Tree) PrintReport() { + t.Nodes = make(map[int]struct{}) + if t.Root == nil { + return + } + + queue := make([]Node, 0) + queue = append(queue, t.Root) + + head := 0 + + for head < len(queue) { + current := queue[head] + head++ + + print("ID:", current.GetID()) + print(", Current node:", current.Value()) + + if !current.LeftIsNil() { + print(", Left Child:", current.Left().Value()) + + if !t.isInQueue(current.Left().GetID()) { + queue = append(queue, current.Left()) + } + } + + if !current.RightIsNil() { + print(", Right Child:", current.Right().Value()) + + if !t.isInQueue(current.Right().GetID()) { + queue = append(queue, current.Right()) + } + } + + println() + } +} + +// PrintPyramid is a method that prints the tree as a pyramid +func (t *Tree) PrintPyramid() { + t.Nodes = make(map[int]struct{}) + if t.Root == nil { + return + } + + queue := []Node{t.Root} + levels := []int{0} + outputByLevel := []string{} + + head := 0 + currentLevel := 0 + + output := "" + + for head < len(queue) { + current := queue[head] + level := levels[head] + head++ + + // Level change + if level > currentLevel { + currentLevel = level + outputByLevel = append(outputByLevel, output+"\n") + output = "" + } + + // Add current node to the output + if current.Value() < 10 { + output += fmt.Sprintf("0%d ", current.Value()) + } else { + output += fmt.Sprintf("%d ", current.Value()) + } + + // Add children to the queue + if !current.LeftIsNil() { + if !t.isInQueue(current.Left().GetID()) { + queue = append(queue, current.Left()) + levels = append(levels, level+1) + } + } + + if !current.RightIsNil() { + if !t.isInQueue(current.Right().GetID()) { + queue = append(queue, current.Right()) + levels = append(levels, level+1) + } + } + } + // Add the last level + outputByLevel = append(outputByLevel, output+"\n") + + totalLevels := len(outputByLevel) + + // Print the pyramid + for i, level := range outputByLevel { + str := strings.Repeat(" ", 2*(totalLevels-i)) + level + print(str) + } +} + +// isInQueue is a method that avoids duplicates in the tree +func (n *Tree) isInQueue(nv int) bool { + if _, ok := n.Nodes[nv]; ok { + return true + } + + n.Nodes[nv] = struct{}{} + return false +} diff --git a/project_euler/problem_19/problem19.go b/project_euler/problem_19/problem19.go new file mode 100644 index 000000000..9a86f0dea --- /dev/null +++ b/project_euler/problem_19/problem19.go @@ -0,0 +1,63 @@ +package problem19 + +/** +* Problem 19 - Counting Sundays +* @see {@link https://projecteuler.net/problem=19} +* +* You are given the following information, +* but you may prefer to do some research for yourself. +* +* 1 Jan 1900 was a Monday. +* Thirty days has September, +* April, June and November. +* All the rest have thirty-one, +* Saving February alone, +* Which has twenty-eight, rain or shine. +* And on leap years, twenty-nine. +* A leap year occurs on any year evenly divisible by 4, +* but not on a century unless it is divisible by 400. +* +* How many Sundays fell on the first of the month during +* the twentieth century (1 Jan 1901 to 31 Dec 2000)? +* +* @author ddaniel27 + */ + +func Problem19() int { + count := 0 + dayOfWeek := 2 // 1 Jan 1901 was a Tuesday + + for year := 1901; year <= 2000; year++ { + for month := 1; month <= 12; month++ { + if dayOfWeek == 0 { + count++ + } + + daysInMonth := 31 + switch month { + case 4, 6, 9, 11: + daysInMonth = 30 + case 2: + if IsLeapYear(year) { + daysInMonth = 29 + } else { + daysInMonth = 28 + } + } + + dayOfWeek = (dayOfWeek + daysInMonth) % 7 + } + } + + return count +} + +func IsLeapYear(year int) bool { + if year%4 == 0 { + if year%100 == 0 { + return year%400 == 0 + } + return true + } + return false +} diff --git a/project_euler/problem_19/problem19_test.go b/project_euler/problem_19/problem19_test.go new file mode 100644 index 000000000..d26c10938 --- /dev/null +++ b/project_euler/problem_19/problem19_test.go @@ -0,0 +1,29 @@ +package problem19 + +import "testing" + +// Tests +func TestProblem19_Func(t *testing.T) { + tests := []struct { + name string + expected int + }{ + {"Problem 19 - Counting Sundays", 171}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := Problem19() + if got != test.expected { + t.Errorf("Problem19() = got %v, want %v", got, test.expected) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem19_Func(b *testing.B) { + for i := 0; i < b.N; i++ { + Problem19() + } +} diff --git a/project_euler/problem_20/problem20.go b/project_euler/problem_20/problem20.go new file mode 100644 index 000000000..fc731da6b --- /dev/null +++ b/project_euler/problem_20/problem20.go @@ -0,0 +1,37 @@ +/** +* Problem 20 - Factorial digit sum +* @see {@link https://projecteuler.net/problem=20} +* +* n! means n × (n − 1) × ... × 3 × 2 × 1 +* +* For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, +* and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. +* +* Find the sum of the digits in the number 100! +* +* @author ddaniel27 + */ +package problem20 + +import "math/big" + +func Problem20(input int) int { + factorial := bigFactorial(input) + sum := 0 + for _, digit := range factorial.String() { + sum += int(digit - '0') + } + return sum +} + +// bigFactorial returns the factorial of n as a big.Int +// Use big package to handle large numbers +func bigFactorial(n int) *big.Int { + if n < 0 { + return big.NewInt(0) + } + if n == 0 { + return big.NewInt(1) + } + return big.NewInt(0).Mul(big.NewInt(int64(n)), bigFactorial(n-1)) +} diff --git a/project_euler/problem_20/problem20_test.go b/project_euler/problem_20/problem20_test.go new file mode 100644 index 000000000..f1ac574c6 --- /dev/null +++ b/project_euler/problem_20/problem20_test.go @@ -0,0 +1,31 @@ +package problem20 + +import "testing" + +// Tests +func TestProblem20_Func(t *testing.T) { + tests := []struct { + name string + input int + expected int + }{ + {"Problem 20 - Factorial digit sum", 10, 27}, + {"Problem 20 - Factorial digit sum", 100, 648}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := Problem20(test.input) + if got != test.expected { + t.Errorf("Problem20() = got %v, want %v", got, test.expected) + } + }) + } +} + +// Benchmarks +func BenchmarkProblem20_Func(b *testing.B) { + for i := 0; i < b.N; i++ { + Problem20(100) + } +}