go Subroutine

subroutine go(case, filename, parta)

solve the case

Arguments

Type IntentOptional 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


Calls

proc~~go~5~~CallsGraph proc~go~5 problem_23::go proc~build_graph problem_23::build_graph proc~go~5->proc~build_graph proc~count_adjacent problem_23::count_adjacent proc~go~5->proc~count_adjacent proc~dijkstra problem_23::dijkstra proc~go~5->proc~dijkstra proc~read_file_to_char_array aoc_utilities::read_file_to_char_array proc~go~5->proc~read_file_to_char_array proc~traverse~2 problem_23::traverse proc~go~5->proc~traverse~2 proc~build_graph->proc~build_graph proc~add_edge~2 problem_23::add_edge proc~build_graph->proc~add_edge~2 proc~get_cell problem_23::get_cell proc~build_graph->proc~get_cell proc~node_number problem_23::node_number proc~build_graph->proc~node_number proc~not_tree problem_23::not_tree proc~count_adjacent->proc~not_tree proc~number_of_lines_in_file aoc_utilities::number_of_lines_in_file proc~read_file_to_char_array->proc~number_of_lines_in_file proc~read_line aoc_utilities::read_line proc~read_file_to_char_array->proc~read_line proc~traverse~2->proc~traverse~2

Called by

proc~~go~5~~CalledByGraph proc~go~5 problem_23::go program~problem_23 problem_23 program~problem_23->proc~go~5

Source Code

    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