My yearly advent-of-code solutions
at main 1.6 kB view raw
1program day_06 2 implicit none 3 character(len=130) :: line, lines(130) 4 integer:: io, x,y, xd,yd, i, ct 5 6 open(newunit=io, file='./day_06_input.txt', status='old', action='read') 7 read(io, '(a)') lines 8 9 ct = 0 10 ! find starting position 11 do i = 1, size(lines) 12 if (index(lines(i), '^') > 0) then 13 y = i 14 x = index(lines(i), '^') 15 xd = 0 16 yd = -1 17 exit 18 end if 19 end do 20 21 ! move until we leave the bounds when we hit a valid spot mark it with an X and increase count 22 do 23 ! are we going out of bounds? 24 if (x+xd < 1 .or. x+xd > len(lines(y)) .or. y+yd < 1 .or. y+yd > size(lines)) then 25 ct = ct + 1 26 lines(y)(x:x) = 'X' 27 exit 28 end if 29 30 ! try to move 31 select case(lines(y+yd)(x+xd:x+xd)) 32 case('.') 33 ! valid move 34 ct = ct + 1 35 x = x + xd 36 y = y + yd 37 lines(y)(x:x) = 'X' 38 case('X') 39 ! valid move but we've been here before 40 x = x + xd 41 y = y + yd 42 case('#') 43 ! we are blocked lets rotate 90 degrees 44 if(xd == 0 .and. yd == -1) then 45 xd = 1 46 yd = 0 47 else if (xd == 1 .and. yd == 0) then 48 xd = 0 49 yd = 1 50 else if (xd == 0 .and. yd == 1) then 51 xd = -1 52 yd = 0 53 else if (xd == -1 .and. yd == 0) then 54 xd = 0 55 yd = -1 56 end if 57 case default 58 exit 59 end select 60 end do 61 print*, lines 62 print*, "distinct: ", ct 63end program day_06 64