problem_21 Program

Uses

  • program~~problem_21~~UsesGraph program~problem_21 problem_21 iso_fortran_env iso_fortran_env program~problem_21->iso_fortran_env module~aoc_utilities aoc_utilities program~problem_21->module~aoc_utilities module~aoc_utilities->iso_fortran_env

Calls

program~~problem_21~~CallsGraph program~problem_21 problem_21 proc~clock_end aoc_utilities::clock%clock_end program~problem_21->proc~clock_end proc~clock_start aoc_utilities::clock%clock_start program~problem_21->proc~clock_start proc~read_file_to_char_array aoc_utilities::read_file_to_char_array program~problem_21->proc~read_file_to_char_array proc~step problem_21::step program~problem_21->proc~step 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

Variables

Type Attributes Name Initial
character(len=1), dimension(:,:), allocatable :: array
integer :: nrows
integer :: ncols
integer :: i
integer :: j
integer, dimension(2) :: iloc
integer, dimension(:,:), allocatable :: icount

Subroutines

recursive subroutine step(array)

take all valid steps from any # elements in array

Arguments

Type IntentOptional Attributes Name
character(len=1), intent(inout), dimension(:,:), allocatable :: array

Source Code

program problem_21

    use aoc_utilities
    use iso_fortran_env

    implicit none

    character(len=1),dimension(:,:),allocatable :: array
    integer :: nrows, ncols, i, j
    integer,dimension(2) :: iloc
    integer,dimension(:,:),allocatable :: icount

    call clk%tic()

    ! array = read_file_to_char_array('inputs/day21_test.txt', border='#')
    array = read_file_to_char_array('inputs/day21.txt', border='.')
    nrows = size(array,1)
    ncols = size(array,2)
    allocate(icount(nrows, ncols)); icount = 0

    iloc = findloc(array, 'S') ! find starting point
    i = iloc(1)
    j = iloc(2)
    array(i,j) = 'O' ! starting here

    do i = 1, 64
        call step(array)
    end do
    write(*,*) '21a: ', count(array == 'O')

    call clk%toc('21')

    contains

    recursive subroutine step(array)
        !! take all valid steps from any # elements in array
        character(len=1),dimension(:,:),intent(inout),allocatable :: array
        integer :: i,j
        logical :: steped
        character(len=1),dimension(:,:),allocatable :: tmp
        tmp = array
        do i = 1, nrows
            do j = 1, ncols
                if (array(i,j)=='O') then
                    steped = .false.
                    ! try a step in each direction:
                    if (array(i-1,j)=='.') then
                        tmp(i-1,j)='O'; steped = .true.
                    end if
                    if (array(i+1,j)=='.') then
                        tmp(i+1,j)='O'; steped = .true.
                    end if
                    if (array(i,j-1)=='.') then
                        tmp(i,j-1)='O'; steped = .true.
                    end if
                    if (array(i,j+1)=='.') then
                        tmp(i,j+1)='O'; steped = .true.
                    end if
                    if (steped) tmp(i,j) = '.'
                end if
            end do
        end do
        call move_alloc(tmp,array)
    end subroutine step

end program problem_21