Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program day16
- implicit none
- integer, parameter :: dim = 110
- character, dimension(dim,dim) :: contraption
- integer, dimension(dim,dim) :: visited
- integer :: i, j, ecode, solution, x, y
- integer, parameter :: in = 50
- open(in, file="day16.txt", action="read", iostat=ecode)
- if ( ecode .eq. 0 ) then
- do j = 1, dim
- read (in, '(110(a))', iostat=ecode) contraption(:,j)
- if ( ecode .ne. 0 ) exit
- end do
- close(in)
- call visit(visited, 0, 1, 0)
- solution = viscount(visited)
- write (*, '("Part 1: ", i0)') solution
- solution = 0
- do i = 1, dim
- call visit(visited, 0, i, 0)
- solution = max(solution, viscount(visited))
- call visit(visited, dim + 1, i, 3)
- solution = max(solution, viscount(visited))
- end do
- do i = 1, dim
- call visit(visited, i, dim + 1, 1)
- solution = max(solution, viscount(visited))
- call visit(visited, i, 0, 3)
- solution = max(solution, viscount(visited))
- end do
- write (*, '("Part 2: ", i0)') solution
- else
- print '(a)', "Error opening file"
- end if
- contains
- integer function viscount(visited)
- integer, dimension(dim,dim), intent(in) :: visited
- integer :: x, y
- viscount = 0
- do x = 1, dim
- do y = 1, dim
- if ( visited(x,y) .gt. 0 ) viscount = viscount + 1
- end do
- end do
- end function viscount
- subroutine visit(visited, startx, starty, startd)
- integer, dimension(dim,dim), intent(inout) :: visited
- integer, intent(in) :: startx, starty, startd
- integer, dimension(0:100) :: visitorsx, visitorsy, direction
- integer :: next_visitor, currx, curry, currd
- visited = 0; next_visitor = 0
- visitorsx(next_visitor) = startx
- visitorsy(next_visitor) = starty
- direction(next_visitor) = startd
- next_visitor = next_visitor + 1
- do while ( next_visitor .gt. 0 )
- next_visitor = next_visitor - 1
- currx = visitorsx(next_visitor)
- curry = visitorsy(next_visitor)
- currd = direction(next_visitor)
- if ( 0 .eq. currd ) then ! RIGHT
- currx = currx + 1
- else if ( 1 .eq. currd ) then ! UP
- curry = curry - 1
- else if ( 2 .eq. currd ) then ! LEFT
- currx = currx - 1
- else if ( 3 .eq. currd ) then ! DOWN
- curry = curry + 1
- else ! INVALID DIRECTION
- exit
- end if
- if ( currx .eq. 0 .or. curry .eq. 0 .or. currx .eq. (dim + 1) .or. curry .eq. (dim + 1)) cycle
- if ( iand(visited(currx,curry), ishft(1, currd)) .gt. 0 ) cycle
- visited(currx, curry) = ior(visited(currx, curry), ishft(1, currd))
- if ( contraption(currx, curry) .eq. '.' ) then
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- next_visitor = next_visitor + 1
- else if ( contraption(currx, curry) .eq. '|' ) then
- if ( currd .eq. 1 .or. currd .eq. 3 ) then ! UP or DOWN
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- next_visitor = next_visitor + 1
- else
- ! push UP
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- direction(next_visitor) = 1
- next_visitor = next_visitor + 1
- ! push DOWN
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- direction(next_visitor) = 3
- next_visitor = next_visitor + 1
- end if
- else if ( contraption(currx, curry) .eq. '-' ) then
- if ( currd .eq. 0 .or. currd .eq. 2 ) then ! LEFT or RIGHT
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- next_visitor = next_visitor + 1
- else
- ! push RIGHT
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- direction(next_visitor) = 0
- next_visitor = next_visitor + 1
- ! push LEFT
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- direction(next_visitor) = 2
- next_visitor = next_visitor + 1
- end if
- else if ( contraption(currx, curry) .eq. '/' ) then
- if ( 0 .eq. currd ) then ! RIGHT
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- direction(next_visitor) = 1
- next_visitor = next_visitor + 1
- else if ( 1 .eq. currd ) then ! UP
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- direction(next_visitor) = 0
- next_visitor = next_visitor + 1
- else if ( 2 .eq. currd ) then ! LEFT
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- direction(next_visitor) = 3
- next_visitor = next_visitor + 1
- else if ( 3 .eq. currd ) then ! DOWN
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- direction(next_visitor) = 2
- next_visitor = next_visitor + 1
- end if
- else if ( contraption(currx, curry) .eq. '\' ) then
- if ( 0 .eq. currd ) then ! RIGHT
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- direction(next_visitor) = 3
- next_visitor = next_visitor + 1
- else if ( 1 .eq. currd ) then ! UP
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- direction(next_visitor) = 2
- next_visitor = next_visitor + 1
- else if ( 2 .eq. currd ) then ! LEFT
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- direction(next_visitor) = 1
- next_visitor = next_visitor + 1
- else if ( 3 .eq. currd ) then ! DOWN
- visitorsx(next_visitor) = currx
- visitorsy(next_visitor) = curry
- direction(next_visitor) = 0
- next_visitor = next_visitor + 1
- end if
- end if
- end do
- end subroutine visit
- end program day16
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement