aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2018-12-03 12:14:31 +0000
committerNat Lasseter <user@4574.co.uk>2018-12-03 12:14:31 +0000
commitac0e849c119cc3cd9709e57521365dd6dc5215a6 (patch)
tree70bc95efd57ffda171c2cb1e6235b44230be0a29
parentc13a36eb26055d80749a862d302c208898fe049a (diff)
Replace list with array. Obvious optimisation.
-rwxr-xr-xday01/part212
1 files 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