Advertisement
Guest User

Advent of Code 2023 Day 10 Part 1

a guest
Dec 10th, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.33 KB | Source Code | 0 0
  1. require 'pp'
  2.  
  3. input = File.readlines(ARGV[0] || 'example').map(&:rstrip)
  4.  
  5. start_row = start_col = 0
  6. input.each_with_index do |line, row|
  7.   if (col = line.index('S'))
  8.     start_row = row
  9.     start_col = col
  10.     break
  11.   end
  12. end
  13.  
  14. puts "start at #{start_row}, #{start_col}"
  15.  
  16. width = input[0].length
  17. height = input.length
  18.  
  19. # start by searching cardinal directions clockwise beacuse we don't
  20. # know what shape pipe 'S' is
  21. paths = []
  22.  
  23. # up
  24. r, c = start_row-1, start_col
  25. if r >= 0 && r < height && c >= 0 && c < width
  26.   if '|7F'.index(input[r][c])
  27.     paths << [r, c]
  28.   end
  29. end
  30. # right
  31. r, c = start_row, start_col+1
  32. if r >= 0 && r < height && c >= 0 && c < width
  33.   if '-J7'.index(input[r][c])
  34.     paths << [r, c]
  35.   end
  36. end
  37. # down
  38. r, c = start_row+1, start_col
  39. if r >= 0 && r < height && c >= 0 && c < width
  40.   if '|LJ'.index(input[r][c])
  41.     paths << [r, c]
  42.   end
  43. end
  44. # left
  45. r, c = start_row, start_col-1
  46. if r >= 0 && r < height && c >= 0 && c < width
  47.   if '-LF'.index(input[r][c])
  48.     paths << [r, c]
  49.   end
  50. end
  51.  
  52. p paths
  53.  
  54. # clean up map
  55. clean = [nil] * height
  56. height.times { clean[_1] = '.' * width}
  57.  
  58. visited = {}
  59.  
  60. at = paths[0]
  61. loop do
  62.   #puts "at #{at.join(', ')}"
  63.   visited[at] = true
  64.   r, c = at
  65.   clean[r][c] = input[r][c]
  66.   candidates =
  67.     case input[r][c]
  68.     when '|' then [[r-1, c], [r+1, c]]
  69.     when '-' then [[r, c-1], [r, c+1]]
  70.     when 'L' then [[r-1, c], [r, c+1]]
  71.     when 'J' then [[r-1, c], [r, c-1]]
  72.     when '7' then [[r, c-1], [r+1, c]]
  73.     when 'F' then [[r+1, c], [r, c+1]]
  74.     when 'S' then break
  75.     else
  76.       next
  77.     end
  78.   #puts "candidates: #{candidates.inspect}"
  79.   candidates.each do |pos|
  80.     r, c = pos
  81.     if r < 0 || r >= height || c < 0 || c >= width
  82.       raise "#{at.join(', ')} -> #{r}, #{c} (out of bounds)"
  83.     end
  84.     if !visited[pos] # take this path
  85.       #puts "taking [#{r}][#{c}] = #{input[r][c]}"
  86.       at = pos
  87.       break
  88.     end
  89.   end
  90.   #puts "\nat #{at}"
  91.   #pp clean
  92. end
  93.  
  94. puts
  95. clean.each {puts _1}
  96.  
  97. # now, follow both paths simultaneously until
  98. counts = [nil] * height
  99. height.times { counts[_1] = '.' * width}
  100. counts[start_row][start_col] = '0'
  101.  
  102. visited = {[start_row, start_col] => true}
  103. n = 1
  104. max = 0
  105. loop do
  106.   puts "at #{paths[0].join(', ')} | #{paths[1].join(', ')}"
  107.   nxt = paths.map do |at|
  108.     visited[at] = true
  109.     r, c = at
  110.     counts[r][c] = (n % 10).to_s
  111.     max = n if n > max
  112.     candidates =
  113.       case input[r][c]
  114.       when '|' then [[r-1, c], [r+1, c]]
  115.       when '-' then [[r, c-1], [r, c+1]]
  116.       when 'L' then [[r-1, c], [r, c+1]]
  117.       when 'J' then [[r-1, c], [r, c-1]]
  118.       when '7' then [[r, c-1], [r+1, c]]
  119.       when 'F' then [[r+1, c], [r, c+1]]
  120.       when 'S' then [] # XXX at end! what do?
  121.       else []
  122.       end
  123.     take = nil
  124.     candidates.each do |pos|
  125.       r, c = pos
  126.       if r < 0 || r >= height || c < 0 || c >= width
  127.         raise "#{at.join(', ')} -> #{r}, #{c} (out of bounds)"
  128.       end
  129.       if !visited[pos] # take this path
  130.         puts "taking [#{r}][#{c}] = #{input[r][c]}"
  131.         take = pos
  132.         break
  133.       end
  134.     end
  135.     if !take
  136.       puts "no path to take! end?"
  137.     end
  138.     puts "at #{at} -> #{take}"
  139.     take
  140.   end
  141.   print "nxt: "
  142.   p nxt
  143.  
  144.   break if nxt.all?{_1 == nil}
  145.   paths = nxt
  146.   n += 1
  147. end
  148.  
  149. puts
  150. counts.each {puts _1}
  151. puts
  152. puts "max step = #{max}"
  153.  
Tags: aoc 2023
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement