···
1
+
program day_06_part_2
3
+
character(len=130) :: line, lines(130), temp_lines(130)
4
+
integer:: io, x,y, xd,yd, i, j, ct, o_ct, init_x, init_y, init_xd, init_yd
6
+
open(newunit=io, file='./day_06_input.txt', status='old', action='read')
7
+
read(io, '(a)') lines
10
+
! find starting position
11
+
do i = 1, size(lines)
12
+
if (index(lines(i), '^') > 0) then
14
+
x = index(lines(i), '^')
15
+
lines(i)(x:x) = '.' ! replace with a dot to not confuse this part
26
+
do i = 1, size(lines)
27
+
do j = 1, len(lines(i))
34
+
temp_lines = lines ! fresh board every run
35
+
if(temp_lines(i)(j:j) == '.') then
36
+
temp_lines(i)(j:j) = 'O' ! place obstacle to test
37
+
! move until we leave the bounds when we hit a valid spot mark it with an X and increase count
39
+
! are we going out of bounds?
40
+
if (x+xd < 1 .or. x+xd > len(temp_lines(y)) .or. y+yd < 1 .or. y+yd > size(temp_lines)) then
45
+
select case(temp_lines(y+yd)(x+xd:x+xd))
51
+
! we are blocked lets rotate 90 degrees and count obstacle
53
+
if(xd == 0 .and. yd == -1) then
56
+
else if (xd == 1 .and. yd == 0) then
59
+
else if (xd == 0 .and. yd == 1) then
62
+
else if (xd == -1 .and. yd == 0) then
67
+
! we are blocked lets rotate 90 degrees
68
+
! change this to an O to try and detect when stuck
69
+
temp_lines(y+yd)(x+xd:x+xd) = 'O'
70
+
if(xd == 0 .and. yd == -1) then
73
+
else if (xd == 1 .and. yd == 0) then
76
+
else if (xd == 0 .and. yd == 1) then
79
+
else if (xd == -1 .and. yd == 0) then
87
+
exit ! probably stuck
91
+
if(o_ct > 100) then ! we got stuck probably 100 is so arbitrary
96
+
print*, "block points: ", ct
97
+
end program day_06_part_2