From aef1ed2808c3e75658f0cf341102dc43a8998d0d Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 30 Nov 2020 21:58:36 -0800 Subject: [PATCH] Add two-pointer solution for day 1. --- day1.cr | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/day1.cr b/day1.cr index 78015cb..6373b06 100644 --- a/day1.cr +++ b/day1.cr @@ -1,11 +1,18 @@ INPUT = File.read("day1.txt").lines.map(&.chomp.to_i32) def part1 - input = INPUT.clone - input.each_with_index do |v, i| - input.each_with_index do |v2, j| - next unless v + v2 == 2020 && i != j - puts v*v2 + input = INPUT.clone.sort! + bottom = 0 + top = input.size - 1 + loop do + 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