def chronal_calib_part1 sum = 0 File.open('advent_of_code/day1.txt', 'r') do |f| f.each_line do |line| sum += line.to_i end end sum end
Logic for part 2:
Storing the summed frequencies as a hash as hash key lookup is faster than instead of doing an array.include? each time to check if sum already repeated. Have to benchmark and test if this is true.
def chronal_calib_part2 sum = 0 frequency = {} found_frequency = false while found_frequency == false File.open('advent_of_code/day1.txt', 'r') do |f| f.each_line do |line| sum += line.to_i if frequency[sum] == true frequency[sum] = false found_frequency = true break else frequency[sum] = true end end end end puts frequency.key(false) end
Time is O[n] as has to go thru each line in file multiple times. Space is O[m] - m is sums.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Logic for part 2:
Storing the summed frequencies as a hash as hash key lookup is faster than instead of doing an array.include? each time to check if sum already repeated.
Have to benchmark and test if this is true.
Time is O[n] as has to go thru each line in file multiple times.
Space is O[m] - m is sums.