From ac0e849c119cc3cd9709e57521365dd6dc5215a6 Mon Sep 17 00:00:00 2001 From: Nat Lasseter Date: Mon, 3 Dec 2018 12:14:31 +0000 Subject: Replace list with array. Obvious optimisation. --- day01/part2 | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/day01/part2 b/day01/part2 index d48de47..506d995 100755 --- a/day01/part2 +++ b/day01/part2 @@ -3,15 +3,21 @@ input = $stdin.readlines.map(&:to_i) lf = 0 -freq = [0] +posifreq = [true] +negifreq = [] loop do input.each do |i| lf += i - if freq.include?(lf) + if lf >= 0 && posifreq[lf] || + lf < 0 && negifreq[-lf] puts lf exit end - freq << lf + if lf >= 0 + posifreq[lf] = true + else + negifreq[-lf] = true + end end end -- cgit v1.2.1