11module solution_mod
22 implicit none
33 private
4+
5+ ! > @brief save connection (room, door) -> (room, door)
46 type :: connection_t
5- integer :: room_out = - 1
6- integer :: door_out = - 1
7- integer :: room_in = - 1
8- integer :: door_in = - 1
7+ integer :: room_out = - 1 ! < exit room
8+ integer :: door_out = - 1 ! < exit door
9+ integer :: room_in = - 1 ! < enter room
10+ integer :: door_in = - 1 ! < enter door
911 contains
1012 procedure :: to_json = > connection_t_to_json
1113 procedure :: connection_t_assignment
@@ -16,16 +18,29 @@ module solution_mod
1618 generic :: operator (==) = > connection_t_equal
1719 generic :: operator (/= ) = > connection_t_not_equal
1820 end type connection_t
21+
22+ ! > @brief final solution to submit
1923 type :: solution_t
20- logical (1 ) :: inited = .false.
21- type (connection_t), allocatable :: connections(:)
22- integer :: n_conns
24+ logical (1 ) :: inited = .false. ! < was initialised
25+ type (connection_t), allocatable :: connections(:) ! < list of all connections
26+ integer :: n_conns ! < number of connections
2327 contains
2428 procedure :: init
2529 procedure :: to_json = > solution_t_to_json
2630 end type solution_t
31+
2732 public :: solution_t
2833contains
34+
35+ ! >
36+ ! > @brief serialises connection_t to JSON
37+ ! >
38+ ! > @param[in] connection - connection_t object
39+ ! > @return - serialised JSON string
40+ ! >
41+ ! > @author foxtran
42+ ! > @date Sep 8, 2025
43+ ! >
2944 function connection_t_to_json (connection ) result(json)
3045 class(connection_t), intent (in ) :: connection
3146 character (len= :), allocatable :: json
@@ -36,6 +51,16 @@ function connection_t_to_json(connection) result(json)
3651 ' "door": ' , connection% door_in, ' } }'
3752 json = trim (json_tmp)
3853 end function connection_t_to_json
54+
55+ ! >
56+ ! > @brief generate connection with exchanged input-output
57+ ! >
58+ ! > @param[in] connection - connection_t object
59+ ! > @return - swapped in<->out room-door pair
60+ ! >
61+ ! > @author foxtran
62+ ! > @date Sep 8, 2025
63+ ! >
3964 function gen_swap (connection ) result(new_connection)
4065 class(connection_t), intent (in ) :: connection
4166 type (connection_t) :: new_connection
@@ -44,6 +69,16 @@ function gen_swap(connection) result(new_connection)
4469 new_connection% room_in = connection% room_out
4570 new_connection% door_in = connection% door_out
4671 end function gen_swap
72+
73+ ! >
74+ ! > @brief allows assignment without compiler bugs
75+ ! >
76+ ! > @param[out] lhs - connection_t object for initialisation
77+ ! > @param[in] rhs - connection_t object with data
78+ ! >
79+ ! > @author foxtran
80+ ! > @date Sep 8, 2025
81+ ! >
4782 subroutine connection_t_assignment (lhs , rhs )
4883 class(connection_t), intent (out ) :: lhs
4984 class(connection_t), intent (in ) :: rhs
@@ -52,6 +87,17 @@ subroutine connection_t_assignment(lhs, rhs)
5287 lhs% room_in = rhs% room_in
5388 lhs% door_in = rhs% door_in
5489 end subroutine connection_t_assignment
90+
91+ ! >
92+ ! > @brief checks if two connections are equal
93+ ! >
94+ ! > @param[in] lhs - connection_t object
95+ ! > @param[in] rhs - connection_t object
96+ ! > @return - .true. if connection are equal
97+ ! >
98+ ! > @author foxtran
99+ ! > @date Sep 8, 2025
100+ ! >
55101 logical function connection_t_equal (lhs , rhs ) result(equal)
56102 class(connection_t), intent (in ) :: lhs
57103 class(connection_t), intent (in ) :: rhs
@@ -61,11 +107,32 @@ logical function connection_t_equal(lhs, rhs) result(equal)
61107 (lhs% room_in == rhs% room_in) .and. &
62108 (lhs% door_in == rhs% door_in)
63109 end function connection_t_equal
110+
111+ ! >
112+ ! > @brief checks if two connections are not equal
113+ ! >
114+ ! > @param[in] lhs - connection_t object
115+ ! > @param[in] rhs - connection_t object
116+ ! > @return - .true. if connection are not equal
117+ ! >
118+ ! > @author foxtran
119+ ! > @date Sep 8, 2025
120+ ! >
64121 logical function connection_t_not_equal (lhs , rhs ) result(equal)
65122 class(connection_t), intent (in ) :: lhs
66123 class(connection_t), intent (in ) :: rhs
67124 equal = .not. (lhs == rhs)
68125 end function connection_t_not_equal
126+
127+ ! >
128+ ! > @brief serialises solution_t to JSON
129+ ! >
130+ ! > @param[in] solution - solution_t object
131+ ! > @return - serialised JSON string
132+ ! >
133+ ! > @author foxtran
134+ ! > @date Sep 8, 2025
135+ ! >
69136 function solution_t_to_json (solution ) result(json)
70137 class(solution_t), intent (in ) :: solution
71138 character (len= :), allocatable :: json
@@ -84,19 +151,33 @@ function solution_t_to_json(solution) result(json)
84151 end do
85152 json = json // ' ] }'
86153 end function solution_t_to_json
154+
155+ ! >
156+ ! > @brief initialise solution_t
157+ ! >
158+ ! > @details resolves (room_out, door_out) -> (room_in) to (room_out, door_out) <-> (room_in, door_in)
159+ ! >
160+ ! > @param[in,out] solution - solution_t object
161+ ! > @param[in] library - labyrinth
162+ ! >
163+ ! > @author foxtran
164+ ! > @date Sep 8, 2025
165+ ! >
87166 subroutine init (solution , library )
88167 use library_mod, only: library_t
89168 class(solution_t), intent (inout ) :: solution
90169 type (library_t), intent (in ) :: library
91170 type (library_t) :: libint
92171 integer :: room_id, door_id, conn_id
93172 integer :: n_rooms
173+
94174 if (solution% inited) return
95175 solution% inited = .true.
96176
97177 libint = library
98178 call libint% refine()
99179 n_rooms = size (libint% rooms)
180+
100181 ! validate
101182 do room_id = 1 , n_rooms
102183 do door_id = lbound (libint% rooms(room_id)% doors, 1 ), ubound (libint% rooms(room_id)% doors, 1 )
@@ -107,18 +188,23 @@ subroutine init(solution, library)
107188 end if
108189 end do
109190 end do
191+
110192 allocate (solution% connections(6 * n_rooms))
193+
111194 conn_id = 1
112195 do room_id = 1 , n_rooms
113196 do door_id = lbound (libint% rooms(room_id)% doors, 1 ), ubound (libint% rooms(room_id)% doors, 1 )
114197 if (libint% rooms(room_id)% doors(door_id)% room == - 2 ) cycle
198+
115199 block
116200 type (connection_t) :: conn1, conn2
117201 integer :: door_id_in
118202 logical :: found
203+
119204 conn1% room_out = room_id
120205 conn1% door_out = door_id
121206 conn1% room_in = libint% rooms(room_id)% doors(door_id)% room
207+
122208 found = .false.
123209 do door_id_in = lbound (libint% rooms(conn1% room_in)% doors, 1 ), ubound (libint% rooms(conn1% room_in)% doors, 1 )
124210 if (libint% rooms(conn1% room_in)% doors(door_id_in)% room == conn1% room_out) then
0 commit comments