119 lines
3.6 KiB
Fortran
119 lines
3.6 KiB
Fortran
program day
|
|
implicit none
|
|
integer, parameter :: max_chars = 300
|
|
integer, parameter :: i_kind = selected_int_kind(15)
|
|
integer, parameter :: max_rows = 300
|
|
integer, parameter :: max_galaxies = 500
|
|
character(200) :: fname
|
|
character(max_chars) :: fline
|
|
integer :: n_arguments
|
|
integer :: istat
|
|
character(len=max_chars) :: map(1:max_rows)
|
|
integer :: galaxy_y(1:max_galaxies), galaxy_x(1:max_galaxies)
|
|
logical :: row_empty(1:max_rows), col_empty(1:max_chars)
|
|
integer :: n_cols, n_rows, n_galaxies, temp, t2
|
|
integer :: i, i2, i3, strlen
|
|
logical :: is_empty
|
|
integer :: dist, total_distance, x1, x2, y1, y2
|
|
integer(kind=i_kind) :: dist_2, total_distance_2
|
|
|
|
n_arguments = command_argument_count()
|
|
if (n_arguments .eq. 1) then
|
|
call get_command_argument(1, fname)
|
|
print *, "File: ", trim(fname)
|
|
print *
|
|
else
|
|
print *, "Wrong number of arguments: ", n_arguments
|
|
stop
|
|
end if
|
|
n_rows = 0
|
|
n_galaxies = 0
|
|
open(10, file=fname)
|
|
row_empty = .false.
|
|
do i=1,max_rows
|
|
read(10, "(a)", iostat=istat) fline
|
|
strlen = len_trim(fline)
|
|
if ((strlen .eq. 0) .or. (is_iostat_end(istat))) then
|
|
exit
|
|
end if
|
|
temp = scan(fline(1:strlen), '#')
|
|
if (temp .eq. 0) then
|
|
row_empty(i) = .true.
|
|
print *, "Empty row: ", i
|
|
else
|
|
t2 = temp
|
|
do
|
|
if (t2 .eq. 0) then
|
|
exit
|
|
else
|
|
n_galaxies = n_galaxies + 1
|
|
galaxy_y(n_galaxies) = i
|
|
galaxy_x(n_galaxies) = temp
|
|
print *, "Galaxy ", n_galaxies, ": ", i, temp
|
|
end if
|
|
if (temp .ge. strlen) then
|
|
exit
|
|
end if
|
|
t2 = scan(fline((temp+1):strlen), '#')
|
|
temp = t2 + temp
|
|
end do
|
|
end if
|
|
map(i) = fline
|
|
n_rows = n_rows + 1
|
|
end do
|
|
close(10)
|
|
n_cols = len_trim(map(1))
|
|
col_empty = .true.
|
|
do i = 1, n_cols
|
|
is_empty = .true.
|
|
do i2 = 1, n_rows
|
|
if (map(i2)(i:i) .eq. '#') then
|
|
col_empty(i) = .false.
|
|
is_empty = .false.
|
|
exit
|
|
end if
|
|
end do
|
|
if (is_empty) then
|
|
print *, "Empty column: ", i
|
|
end if
|
|
end do
|
|
print *, "Columns: ", n_cols
|
|
print *, "Rows: ", n_rows
|
|
print *, "Galaxies: ", n_galaxies
|
|
print *
|
|
total_distance = 0
|
|
total_distance_2 = 0_i_kind
|
|
do i=1,(n_galaxies-1)
|
|
do i2=(i+1), n_galaxies
|
|
y1 = galaxy_y(i)
|
|
y2 = galaxy_y(i2)
|
|
x1 = galaxy_x(i)
|
|
x2 = galaxy_x(i2)
|
|
dist = abs(y1 - y2) + abs(x1 - x2)
|
|
dist_2 = dist
|
|
if (y1 .ne. y2) then
|
|
do i3=min(y1,y2), max(y1,y2)
|
|
if (row_empty(i3)) then
|
|
dist = dist + 1
|
|
dist_2 = dist_2 + 999999
|
|
end if
|
|
end do
|
|
end if
|
|
if (x1 .ne. x2) then
|
|
do i3=min(x1,x2), max(x1, x2)
|
|
if (col_empty(i3)) then
|
|
dist = dist + 1
|
|
dist_2 = dist_2 + 999999
|
|
end if
|
|
end do
|
|
end if
|
|
total_distance = total_distance + dist
|
|
total_distance_2 = total_distance_2 + dist_2
|
|
end do
|
|
end do
|
|
print *
|
|
print *, "Total distance: ", total_distance
|
|
print *, "Total distance (Expanded): ", total_distance_2
|
|
end program day
|
|
|