aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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