Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function day10(stop_on_hit_200 = true)
- # PART 1
- f = open("input10.txt", "r")
- asteroid_field = hcat( (readlines(f) .|> x -> parse.(Bool, split(x, "")))...)
- close(f)
- nx, ny = size(asteroid_field)
- all_rays = [(x,y) for x in -nx:nx, y in -ny:ny] |>
- y -> filter(x -> gcd(x...) == 1, y) |>
- y -> sort(y, by = x-> -atan(x...)) .|>
- CartesianIndex
- view_counts = zeros(Int64, size(asteroid_field))
- for station_position in CartesianIndices(asteroid_field)
- #Exclude non-asteroids from starting positions
- if asteroid_field[station_position] != 1
- continue
- end
- for ray in all_rays
- asteroid_hit = false
- current_position = station_position + ray
- while checkbounds(Bool, asteroid_field, current_position) && !asteroid_hit
- # If we hit one, we can stop propagating
- if asteroid_field[current_position] == 1
- asteroid_hit = true
- view_counts[station_position] += 1
- end
- # Propagate ray further
- current_position += ray
- end
- end
- end
- println(findmax(view_counts))
- laser_location = findmax(view_counts)[2]
- # PART 2
- total_asteroids = sum(asteroid_field)
- total_asteroids_hit = 0
- while total_asteroids_hit < total_asteroids - 1
- for ray in all_rays
- asteroid_hit = false
- current_position = laser_location + ray
- while checkbounds(Bool, asteroid_field, current_position) && !asteroid_hit
- # If we hit one, we can stop propagating
- if asteroid_field[current_position] == 1
- asteroid_hit = true
- asteroid_field[current_position] = 0
- total_asteroids_hit += 1
- if total_asteroids_hit == 200 && stop_on_hit_200
- println("200th is ", current_position)
- # Break out of loop when we reach 200
- @goto end_iteration
- end
- end
- # Propagate ray further
- current_position += ray
- end
- end
- end
- @label end_iteration
- end
- day10()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement