Add two-pointer solution for day 1.
This commit is contained in:
parent
458c285a7f
commit
aef1ed2808
17
day1.cr
17
day1.cr
|
@ -1,11 +1,18 @@
|
||||||
INPUT = File.read("day1.txt").lines.map(&.chomp.to_i32)
|
INPUT = File.read("day1.txt").lines.map(&.chomp.to_i32)
|
||||||
|
|
||||||
def part1
|
def part1
|
||||||
input = INPUT.clone
|
input = INPUT.clone.sort!
|
||||||
input.each_with_index do |v, i|
|
bottom = 0
|
||||||
input.each_with_index do |v2, j|
|
top = input.size - 1
|
||||||
next unless v + v2 == 2020 && i != j
|
loop do
|
||||||
puts v*v2
|
sum = input[bottom] + input[top]
|
||||||
|
if sum == 2020
|
||||||
|
puts input[bottom] * input[top]
|
||||||
|
return
|
||||||
|
elsif sum > 2020
|
||||||
|
top -= 1
|
||||||
|
else
|
||||||
|
bottom += 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user