changeset 15:5c8d2d181b94

Remove an unnecessary level of nesting The `reduce` now just passes through the remaining part of each range to future range transforms
author IBBoard <dev@ibboard.co.uk>
date Sat, 09 Dec 2023 20:12:17 +0000
parents dcc060e59c47
children 9b1d04091335
files day5b.rb
diffstat 1 files changed, 6 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/day5b.rb	Sat Dec 09 20:07:16 2023 +0000
+++ b/day5b.rb	Sat Dec 09 20:12:17 2023 +0000
@@ -37,8 +37,8 @@
 def map_with_override(mappings, inputs)
 	inputs.flat_map do |input|
 		processed = []
-		reduced_unprocessed = mappings.ranges.reduce(input) do |unprocessed, mapping|
-			if unprocessed.nil?
+		reduced_unprocessed = mappings.ranges.reduce(input) do |input_range, mapping|
+			if input_range.nil?
 				nil
 			elsif mapping.source_range.end <= input_range.begin or input_range.end <= mapping.source_range.begin
 				# Input is entirely outside the mapped range
@@ -57,7 +57,10 @@
 				(mapping.source_range.end)...(input_range.end)
 			end
 		end
-		processed + reduced_unprocessed
+		if ! reduced_unprocessed.nil?
+			processed << reduced_unprocessed
+		end
+		processed
 	end
 end