Skip to content

Instantly share code, notes, and snippets.

@takeouchida
Last active December 4, 2016 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takeouchida/3be55d527307c57e7784bc0655276504 to your computer and use it in GitHub Desktop.
Save takeouchida/3be55d527307c57e7784bc0655276504 to your computer and use it in GitHub Desktop.
A solution of the programming problem (Japanese) http://nabetani.sakura.ne.jp/hena/orde09_penwa/
link(0, 0, 2, 0, 0, 2). link(0, 1, 3, 0, 0, 3). link(0, 2, 1, -1, -1, 4). link(0, 3, 3, 0, -1, 0). link(0, 4, 1, 0, 0, 1).
link(1, 0, 2, 0, 0, 3). link(1, 1, 0, 0, 0, 4). link(1, 2, 2, 0, -1, 0). link(1, 3, 3, 1, 0, 1). link(1, 4, 0, 1, 1, 2).
link(2, 0, 1, 0, 1, 2). link(2, 1, 3, 0, 0, 4). link(2, 2, 0, 0, 0, 0). link(2, 3, 1, 0, 0, 0). link(2, 4, 3, 1, 1, 2).
link(3, 0, 0, 0, 1, 3). link(3, 1, 1, -1, 0, 3). link(3, 2, 2, -1, -1, 4). link(3, 3, 0, 0, 0, 1). link(3, 4, 2, 0, 0, 1).
move((C1, X1, Y1, D1), [], (C2, X2, Y2, D2)) :- link(C1, D1, C2, DX, DY, D2), X2 is X1 + DX, Y2 is Y1 + DY.
move((C, X, Y, D1), [cw|Cs], S) :- D2 is (D1 + 4) mod 5, move((C, X, Y, D2), Cs, S).
move((C, X, Y, D1), [ccw|Cs], S) :- D2 is (D1 + 1) mod 5, move((C, X, Y, D2), Cs, S).
next_command([fw|Cs], [], Cs).
next_command([cw|Cs1], [cw|Cs3], Cs2) :- next_command(Cs1, Cs3, Cs2).
next_command([ccw|Cs1], [ccw|Cs3], Cs2) :- next_command(Cs1, Cs3, Cs2).
same_node((C, X, Y, _), (C, X, Y, _)).
is_answer_2(S1, N, S2, _, N) :- same_node(S1, S2).
is_answer_2(S1, N2, S2, Cs2, N4) :- next_command(Cs2, A, Cs3), move(S2, A, S3), N3 is N2 + 1, is_answer_2(S1, N3, S3, Cs3, N4).
is_answer_1(S1, Cs1, N1, N1, N3) :- next_command(Cs1, A, Cs2), move(S1, A, S2), N2 is N1 + 1, is_answer_2(S1, N2, S2, Cs2, N3).
is_answer_1(S1, Cs1, N1, N3, N4) :- next_command(Cs1, A, Cs2), move(S1, A, S2), N2 is N1 + 1, is_answer_1(S2, Cs2, N2, N3, N4).
is_answer(Cs, M, N) :- is_answer_1((0, 0, 0, 0), Cs, 0, M, N).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment