changeset 38:8e92cb172e6b

Output final distance Also minor code cleanup
author IBBoard <dev@ibboard.co.uk>
date Fri, 20 Sep 2024 20:30:11 +0100
parents 455c825f5080
children 0e17e4bd97a9
files day17.rb
diffstat 1 files changed, 9 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/day17.rb	Fri Sep 20 20:06:21 2024 +0100
+++ b/day17.rb	Fri Sep 20 20:30:11 2024 +0100
@@ -126,7 +126,7 @@
 def update_map(new_node, map, routes)
 	routes_to_cell = routes[new_node]
 	up = $height - new_node.y
-	# We printed a new line, so we're already in column 0
+	# We printed a new line after the map, so we're already in column 0 and go X forward (right)
 	forward = new_node.x
 	if routes_to_cell
 		colour = $colours[routes_to_cell.min_distance]
@@ -144,7 +144,7 @@
 end
 
 puts "#{city_map.map {|row| row.join("")}.join("\n")}"
-sleep(5)
+# sleep(5)  # For screen recording
 
 until unvisited.empty?
 	#puts "Start loop: #{unvisited}"
@@ -152,12 +152,13 @@
 	min_node = nil
 	min_node_direct_distance = Float::INFINITY
 	unvisited.each do |node|
-		x_dist = (end_space.x - node.x).abs
-		y_dist = (end_space.y - node.y).abs
+		x_dist = end_space.x - node.x
+		y_dist = end_space.y - node.y
 		direct_distance = x_dist + y_dist
 		node_route_distance = get_distance(routes, node) + direct_distance
 		min_node_route_distance = get_distance(routes, min_node) + min_node_direct_distance
-		#puts "Node? #{min_node} - #{get_distance(routes, node)} < #{get_distance(routes, min_node)} || #{direct_distance} < #{min_node_direct_distance}"
+		#puts "Node? #{min_node} - #{node_route_distance} < #{min_node_route_distance} || #{direct_distance} < #{min_node_direct_distance}"
+		#sleep(1)
 		if min_node.nil? || node_route_distance < min_node_route_distance #|| (node_route_distance == min_node_route_distance && direct_distance < min_node_direct_distance)
 			min_node = node
 			min_node_direct_distance = direct_distance
@@ -167,7 +168,7 @@
 	#puts "Min node: #{min_node} from #{unvisited.length}"
 	update_map(min_node, city_map, routes)
 
-	break if get_distance(routes, min_node) == Float::INFINITY or min_node == end_space
+	break if not routes.has_key?(min_node) or min_node == end_space
 
 	get_neighbours(city_map, min_node.x, min_node.y).each do |neighbour|
 		new_routes = allowed_routes(city_map, routes, min_node, neighbour)
@@ -199,6 +200,8 @@
 	print "\e[1;30m\e[1;47m#{cell}\e[0m\e[u"
 end
 
+puts best_route.distance
+
 #puts "#{city_map.map {|row| row.join("")}.join("\n")}"