@@ -97,7 +97,6 @@ subroutine guess_init(guess, library)
9797 class(guess_t), intent (inout ) :: guess
9898 type (library_t), target , intent (in ) :: library
9999 integer :: n_rooms, room_id, door_id, cnt, room_in
100- integer , allocatable :: rooms(:)
101100
102101 if (guess% inited) return
103102 guess% inited = .true.
@@ -107,7 +106,6 @@ subroutine guess_init(guess, library)
107106 guess% max_length = size (guess% library% plans(1 )% steps)
108107 allocate (guess% guess(6 * n_rooms), source = - 1 )
109108 allocate (guess% mask(6 * n_rooms), source = .false. )
110- allocate (rooms(n_rooms), source = 6 )
111109
112110 cnt = 1
113111 do room_id = 1 , n_rooms
@@ -116,22 +114,12 @@ subroutine guess_init(guess, library)
116114 if (room_in /= - 1 ) then
117115 guess% guess(cnt) = room_in
118116 guess% mask(cnt) = .true.
119- rooms(room_in) = rooms(room_in) - 1
120117 end if
121118 cnt = cnt + 1
122119 end do
123120 end do
124121
125- room_id = 1
126- do cnt = 1 , size (guess% guess)
127- if (rooms(room_id) == 0 ) room_id = room_id + 1
128- if (.not. guess% mask(cnt)) then
129- guess% guess(cnt) = room_id
130- rooms(room_id) = rooms(room_id) - 1
131- end if
132- end do
133-
134- call shuffle(guess% guess, size (guess% guess), guess% mask)
122+ call guess% next()
135123
136124 end subroutine guess_init
137125 subroutine eval (guess )
@@ -149,9 +137,65 @@ subroutine eval(guess)
149137 guess% max_length = max_length
150138 end subroutine eval
151139 subroutine next (guess )
152- use random_mod, only: shuffle
140+ use random_mod, only: shuffle, rand_int
153141 class(guess_t), intent (inout ) :: guess
154- call shuffle(guess% guess, size (guess% guess), guess% mask)
142+ integer , allocatable :: rooms(:)
143+ integer :: door_id, room_id, n_rooms, cnt
144+ integer :: idx, i1, i2
145+ integer :: room_in, curr_room
146+ logical :: found
147+
148+ n_rooms = size (guess% library% rooms)
149+ allocate (rooms(n_rooms), source = 6 )
150+
151+ do cnt = 1 , size (guess% guess)
152+ if (.not. guess% mask(cnt)) then
153+ guess% guess(cnt) = 0
154+ else
155+ room_in = guess% guess(cnt)
156+ rooms(room_in) = rooms(room_in) - 1
157+ end if
158+ end do
159+
160+ do cnt = 1 , size (guess% guess)
161+ if (guess% guess(cnt) /= 0 ) cycle
162+ curr_room = (cnt - 1 ) / 6 + 1
163+ do
164+ room_id = rand_int(1 , n_rooms)
165+ if (rooms(room_id) > 0 ) then
166+ rooms(room_id) = rooms(room_id) - 1
167+ exit
168+ end if
169+ end do
170+ guess% guess(cnt) = - room_id
171+ if (room_id == curr_room) cycle
172+ i1 = (room_id - 1 ) * 6 + 1
173+ i2 = i1 + 5
174+ found = .false.
175+ do idx = i1, i2
176+ if (guess% mask(idx) .and. guess% guess(idx) == curr_room) then
177+ guess% guess(idx) = - guess% guess(idx)
178+ found = .true.
179+ exit
180+ end if
181+ end do
182+ if (.not. found) then
183+ do idx = i1, i2
184+ if (guess% guess(idx) == 0 ) then
185+ guess% guess(idx) = - curr_room
186+ exit
187+ end if
188+ end do
189+ end if
190+ end do
191+
192+ guess% guess = abs (guess% guess)
193+
194+ do room_id = 1 , n_rooms
195+ i1 = (room_id - 1 ) * 6 + 1
196+ i2 = i1 + 5
197+ call shuffle(guess% guess(i1:i2), 6 , guess% mask(i1:i2))
198+ end do
155199 end subroutine next
156200 subroutine set_solution (guess , library )
157201 class(guess_t), intent (in ) :: guess
0 commit comments