Add code from group
This commit is contained in:
parent
9f2363faea
commit
5776e764b9
8 changed files with 130 additions and 0 deletions
8
solutions/solution-1.rb
Normal file
8
solutions/solution-1.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
def solution_1(max)
|
||||
sum = 0
|
||||
(1...max).each do |number|
|
||||
sum += number if number % 3 == 0|| number % 5 == 0
|
||||
end
|
||||
|
||||
sum
|
||||
end
|
38
solutions/solution-15.rb
Normal file
38
solutions/solution-15.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
class Solution15
|
||||
attr_reader :solution
|
||||
def initialize(size)
|
||||
@size = size
|
||||
@grid = Array.new(size + 1){ Array.new(size + 1, 0) }
|
||||
|
||||
@solution = traverse(0,0)
|
||||
end
|
||||
|
||||
def traverse(x, y)
|
||||
paths = 0
|
||||
|
||||
if @size - x == 0 || @size - y == 0
|
||||
@grid[x][y] = 1
|
||||
return 1
|
||||
end
|
||||
|
||||
if @grid[x][y] == 0
|
||||
paths += traverse(x + 1, y) if x < @size
|
||||
paths += traverse(x, y + 1) if y < @size
|
||||
|
||||
@grid[x][y] = paths
|
||||
@grid[y][x] = paths
|
||||
end
|
||||
|
||||
@grid[x][y]
|
||||
end
|
||||
|
||||
def traverse2(x, y)
|
||||
paths = 0
|
||||
paths += traverse(x + 1, y) if x < @size
|
||||
paths += traverse(x, y + 1) if y < @size
|
||||
|
||||
return 1 if x == @size && y == @size
|
||||
|
||||
return paths
|
||||
end
|
||||
end
|
12
solutions/solution-2.rb
Normal file
12
solutions/solution-2.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
def solution_2(max)
|
||||
sum = 0
|
||||
|
||||
i = j = 1
|
||||
|
||||
while(j <= max)
|
||||
i, j = j, i + j
|
||||
sum += i if i.even?
|
||||
end
|
||||
|
||||
sum
|
||||
end
|
4
solutions/solution-3.rb
Normal file
4
solutions/solution-3.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
require 'prime'
|
||||
def solution_3(number)
|
||||
Prime.prime_division(number).last[0]
|
||||
end
|
20
solutions/solution-4.rb
Normal file
20
solutions/solution-4.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
def solution_4(digits)
|
||||
min = min(digits)
|
||||
max = max(digits)
|
||||
|
||||
(min..max)
|
||||
.to_a
|
||||
.map { |i| (min..max).to_a.map { |j| i * j } }
|
||||
.flatten(1)
|
||||
.select { |i| i.to_s == i.to_s.reverse }
|
||||
.sort
|
||||
.last
|
||||
end
|
||||
|
||||
def min(digits)
|
||||
("1" + "0" * (digits - 1)).to_i
|
||||
end
|
||||
|
||||
def max(digits)
|
||||
("1" + "0" * (digits)).to_i - 1
|
||||
end
|
3
solutions/solution-5.rb
Normal file
3
solutions/solution-5.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
def solution_5(max)
|
||||
(1..max).inject(1) { |lcm, n| lcm.lcm(n) }
|
||||
end
|
33
solutions/solution-8.rb
Normal file
33
solutions/solution-8.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
def solution_8(length)
|
||||
number = "73167176531330624919225119674426574742355349194934
|
||||
96983520312774506326239578318016984801869478851843
|
||||
85861560789112949495459501737958331952853208805511
|
||||
12540698747158523863050715693290963295227443043557
|
||||
66896648950445244523161731856403098711121722383113
|
||||
62229893423380308135336276614282806444486645238749
|
||||
30358907296290491560440772390713810515859307960866
|
||||
70172427121883998797908792274921901699720888093776
|
||||
65727333001053367881220235421809751254540594752243
|
||||
52584907711670556013604839586446706324415722155397
|
||||
53697817977846174064955149290862569321978468622482
|
||||
83972241375657056057490261407972968652414535100474
|
||||
82166370484403199890008895243450658541227588666881
|
||||
16427171479924442928230863465674813919123162824586
|
||||
17866458359124566529476545682848912883142607690042
|
||||
24219022671055626321111109370544217506941658960408
|
||||
07198403850962455444362981230987879927244284909188
|
||||
84580156166097919133875499200524063689912560717606
|
||||
05886116467109405077541002256983155200055935729725
|
||||
71636269561882670428252483600823257530420752963450".gsub(/\n/, '')
|
||||
greatest = 0
|
||||
|
||||
number.split(//).each_with_index do |value, i|
|
||||
if i >= length
|
||||
current = ((i - length + 1)..i).map { |i| number[i].to_i }.reduce(1, :*)
|
||||
|
||||
greatest = current if current > greatest
|
||||
end
|
||||
end
|
||||
|
||||
return greatest
|
||||
end
|
12
spec/problem-1.rb
Normal file
12
spec/problem-1.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
require_relative 'spec_helper'
|
||||
require_relative '../solutions/solution-1'
|
||||
|
||||
describe 'Multiples of 3 and 5' do
|
||||
it 'works for the given example' do
|
||||
expect(solution_1(10)).to eq(23)
|
||||
end
|
||||
|
||||
it 'works for the known answer' do
|
||||
expect(solution_1(1000)).to eq(233168)
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue