Finish day 3 2019

main
Petra 2023-12-01 22:45:02 +13:00
parent c95fc40a31
commit b3961f52db
4 changed files with 62 additions and 8 deletions

View File

@ -0,0 +1,2 @@
R8,U5,L5,D3
U7,R6,D4,L4

View File

@ -0,0 +1,2 @@
R75,D30,R83,U83,L12,D49,R71,U7,L72
U62,R66,U55,R34,D71,R55,D58,R83

View File

@ -0,0 +1,2 @@
R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51
U98,R91,D20,R16,D67,R40,U7,R15,U6,R7

View File

@ -6,7 +6,11 @@ program day3
character(path_length) :: fname
integer :: n_arguments
character(max_chars), allocatable :: data_strs(:)
!integer :: i
integer, allocatable :: line1(:,:)
integer, allocatable :: line2(:,:)
integer :: l1, l2, line1_len, line2_len
integer :: manhattan, min_manhattan
integer :: siglen, min_siglen
n_arguments = command_argument_count()
if (n_arguments .eq. 1) then
@ -17,10 +21,50 @@ program day3
stop
end if
data_strs = read_instruction_strings(fname)
print *, data_strs
print *, data_strs(1)
print *, data_strs(1)(5:)
line1 = all_occupied_points(data_strs(1))
line2 = all_occupied_points(data_strs(2))
line1_len = size(line1, 2)
line2_len = size(line2, 2)
min_manhattan = huge(min_manhattan)
min_siglen = huge(min_siglen)
print *, "Lengths: ", line1_len, line2_len
do l1 = 1, line1_len
do l2 = 1, line2_len
if ((line1(1, l1) .eq. line2(1, l2)) .and. (line1(2, l1) .eq. line2(2, l2))) then
manhattan = abs(line1(1, l1)) + abs(line1(2, l1))
siglen = l1 + l2
if ((manhattan .lt. min_manhattan) .and. (siglen .lt. min_siglen)) then
write (*, 23) l1, l2, line1(1, l1), line1(2, l1), manhattan, siglen
min_manhattan = manhattan
min_siglen = siglen
else if (siglen .lt. min_siglen) then
write (*, 22) l1, l2, line1(1, l1), line1(2, l1), manhattan, siglen
min_siglen = siglen
else if (manhattan .lt. min_manhattan) then
write (*, 21) l1, l2, line1(1, l1), line1(2, l1), manhattan, siglen
min_manhattan = manhattan
else
write (*, 20) l1, l2, line1(1, l1), line1(2, l1), manhattan, siglen
end if
end if
end do
end do
20 format(' ', i7, ',', i7, ' (', i5, ',', i5, '): ', i8, i8)
21 format('* ', i7, ',', i7, ' (', i5, ',', i5, '): ', i8, i8)
22 format('> ', i7, ',', i7, ' (', i5, ',', i5, '): ', i8, i8)
23 format('+ ', i7, ',', i7, ' (', i5, ',', i5, '): ', i8, i8)
print *
print *, "Minimum manhattan: ", min_manhattan
print *, "Minimum signal length: ", min_siglen
deallocate(line1)
deallocate(line2)
deallocate(data_strs)
contains
@ -41,8 +85,8 @@ program day3
function all_occupied_points(istring) result(occupied)
implicit none
integer, parameter :: imax = 6
character(max_chars), allocatable :: istring
integer :: n_cmds = 1
character(max_chars) :: istring
integer :: n_cmds
integer :: i_chars, i1, i2, i_s
integer :: t_strlen
integer :: t_mvmnt, s_mvmnt
@ -50,8 +94,10 @@ program day3
character(imax), allocatable :: tcmds(:)
character(1), allocatable :: dirs(:)
integer, allocatable :: mvmnts(:)
logical, allocatable :: occupied(:,:)
integer, allocatable :: occupied(:,:)
integer :: c_tot
n_cmds = 1
t_strlen = len_trim(istring)
do i_chars=1,t_strlen
if (istring(i_chars:i_chars) .eq. ',') then
@ -77,6 +123,7 @@ program day3
c_x = 0
c_y = 0
i_s = 1
c_tot = 0
do i1=1,n_cmds
if (dirs(i1) .eq. 'U') then
d_y = 1
@ -92,12 +139,13 @@ program day3
d_x = 1
end if
t_mvmnt = mvmnts(i1)
do i2 = i, t_mvmnt
do i2 = 1, t_mvmnt
c_x = c_x + d_x
c_y = c_y + d_y
occupied(1, i_s) = c_x
occupied(2, i_s) = c_y
i_s = i_s + 1
c_tot = c_tot + 1
end do
end do
deallocate(tcmds)