Advertisement
Guest User

Day 10 Advent Of Code

a guest
Dec 10th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 2.41 KB | None | 0 0
  1. function day10(stop_on_hit_200 = true)
  2.     # PART 1
  3.     f = open("input10.txt", "r")
  4.     asteroid_field = hcat( (readlines(f) .|> x -> parse.(Bool, split(x, "")))...)
  5.     close(f)
  6.  
  7.     nx, ny = size(asteroid_field)
  8.    
  9.     all_rays = [(x,y) for x in -nx:nx, y in -ny:ny] |>
  10.                         y -> filter(x -> gcd(x...) == 1, y) |>
  11.                         y -> sort(y, by = x-> -atan(x...)) .|>
  12.                         CartesianIndex
  13.    
  14.     view_counts = zeros(Int64, size(asteroid_field))
  15.  
  16.     for station_position in CartesianIndices(asteroid_field)  
  17.         #Exclude non-asteroids from starting positions
  18.         if asteroid_field[station_position] != 1
  19.             continue
  20.         end
  21.  
  22.         for ray in all_rays
  23.             asteroid_hit = false
  24.             current_position = station_position + ray
  25.             while checkbounds(Bool, asteroid_field, current_position) && !asteroid_hit
  26.                 # If we hit one, we can stop propagating
  27.                 if asteroid_field[current_position] == 1
  28.                     asteroid_hit = true
  29.                     view_counts[station_position] += 1
  30.                 end
  31.                 # Propagate ray further
  32.                 current_position += ray
  33.             end
  34.         end
  35.     end
  36.    
  37.     println(findmax(view_counts))
  38.    
  39.     laser_location = findmax(view_counts)[2]
  40.    
  41.     # PART 2
  42.    
  43.     total_asteroids = sum(asteroid_field)
  44.     total_asteroids_hit = 0
  45.  
  46.     while total_asteroids_hit < total_asteroids - 1
  47.         for ray in all_rays
  48.  
  49.             asteroid_hit = false
  50.             current_position = laser_location + ray
  51.  
  52.             while checkbounds(Bool, asteroid_field, current_position) && !asteroid_hit
  53.                  # If we hit one, we can stop propagating    
  54.                 if asteroid_field[current_position] == 1
  55.                     asteroid_hit = true
  56.                     asteroid_field[current_position] = 0
  57.                     total_asteroids_hit += 1                    
  58.                     if total_asteroids_hit == 200 && stop_on_hit_200
  59.                         println("200th is ", current_position)
  60.                         # Break out of loop when we reach 200
  61.                         @goto end_iteration
  62.                     end
  63.                 end
  64.                 # Propagate ray further
  65.                 current_position += ray
  66.             end
  67.         end
  68.     end
  69.     @label end_iteration
  70. end
  71. day10()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement