solve the case
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | case |
case name for printing |
||
| character(len=*), | intent(in) | :: | filename |
input file to read |
||
| logical, | intent(in) | :: | parta |
if this is part a, then consider the slopes |
subroutine go(case, filename, parta) !! solve the case character(len=*),intent(in) :: case !! case name for printing character(len=*),intent(in) :: filename !! input file to read logical,intent(in) :: parta !! if this is part a, then consider the slopes integer(ip),dimension(1) :: iloc integer(ip) :: istart, iend, idist, i, j logical,parameter :: use_dijkstra = .false. !.... doesn't work yet .... don't know why ....... ! initialize: max_dist = 0 slopes = parta if (allocated(nodes)) deallocate(nodes) if (allocated(visited)) deallocate(visited) if (allocated(nodes_visited)) deallocate(nodes_visited) if (allocated(inodes)) deallocate(inodes) if (allocated(jnodes)) deallocate(jnodes) if (allocated(node_dist)) deallocate(node_dist) if (allocated(node_prev)) deallocate(node_prev) ! read the data file: array = read_file_to_char_array(filename) nrows = size(array,1) ncols = size(array,2) iloc = findloc(array(1,:), '.'); istart = iloc(1) ! get start and end columns iloc = findloc(array(nrows,:), '.'); iend = iloc(1) ! ! identify the coordinates of all the nodes: inodes = [1] ! start node jnodes = [istart] do i = 1, nrows do j = 1, ncols if (count_adjacent(i,j)>=3) then inodes = [inodes, i] jnodes = [jnodes, j] end if end do end do inodes = [inodes,nrows] ! end node jnodes = [jnodes,iend] total_nodes = size(inodes) ! for each node, find the other nodes they are ! connected to and the distances between them (the edges) allocate(nodes(total_nodes)) allocate(visited(nrows, ncols)) do i = 1, total_nodes visited = .false. idist = 0 call build_graph(i,inodes(i),jnodes(i),idist,visited) end do if (use_dijkstra) then !write(*,*) 'hello use_dijkstra' ! ... something wrong here... don't get the right answer ! based on AOC 2021, Problem 15 allocate(node_dist(total_nodes)); node_dist = -1; node_dist(1) = 0 allocate(node_prev(total_nodes)); node_prev = -1 allocate(nodes_visited(total_nodes)); nodes_visited = .false. do iloc = maxloc(node_dist, mask=.not. nodes_visited) i = iloc(1) nodes_visited(i) = .true. if (i==total_nodes) exit ! we are done! !write(*,*) 'visited ', i if (allocated(nodes(i)%inext)) then do j = 1, size(nodes(i)%inext) ! adjacent nodes to this one call dijkstra(i, j) end do end if !if (all(nodes_visited)) exit ! done end do !write(*,*) case, node_dist(size(node_dist)) ! result for this case (last node) !write(*,*) nodes_visited !write(*,*) node_dist else ! start at first, and find the longest that gets to the last. ! recursively traverse the graph. visited = .false. allocate(nodes_visited(total_nodes)); nodes_visited = .false. call traverse(1_ip, 0_ip, nodes_visited) write(*,*) case, max_dist ! result for this case end if end subroutine go