re: AoC Day 1: Chronal Calibration VIEW POST

FULL DISCUSSION
 

This is my code for parts 1 & 2 in Ruby:

require 'set'

STARTING_FREQUENCY = 0
frequencies = DATA.read.split.map(&:to_i)

puts frequencies.reduce(STARTING_FREQUENCY, :+)

seen = Set.new([STARTING_FREQUENCY])
puts frequencies.cycle.reduce(STARTING_FREQUENCY) { |sum, n|
  sum += n
  break sum if seen.include?(sum)

  sum.tap { |s| seen.add(s) }
}

__END__
-5
-2
+1
+14
# data section abbreviated
code of conduct - report abuse