2020 day 13 solution
[?]
Jun 21, 2021, 11:24 PM
VFUI7V5EEGBSNJ36WWKB5ECCT4FHU5MJKIGTRQHTGFIJYKHFOX6QCDependencies
- [2]
B527MN66Solve day 1 for 2020
Change contents
- file addition: day13.rb[2.17]
raise "Expected Filename" unless ARGV[0]file = File.open(ARGV[0])input = file.readlines.map{|line| line.split(',').map{|s| s == 'x' ? 'x' : s.to_i } }file.closestart_time = input[0][0]unless ARGV[1] && ARGV[1] == "2"result = input[1].select{|route| route != 'x' }.map do |route|m = start_time % route{id: route,wait: m == 0 ? 0 : route - m,}end.min{|a, b| a[:wait] <=> b[:wait] }puts result[:id] * result[:wait]elseresult = 0factor = 1input[1].each_with_index.select{|route,_| route != 'x' }.each do |(route,ix)|while (result + ix) % route != 0result += factorendfactor = factor.lcm(route)endputs resultend