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