Create a gist now

Instantly share code, notes, and snippets.

@mattsan /README.md Secret
Last active Oct 23, 2015

What would you like to do?
% run:
% $ brew install swipl
% $ swipl -s ramen.prolog -g 'main, halt.'
rr([1, 0, 0, 0, 0, 0, 0, 0], 0, [1, 0, 0, 0, 0, 0, 0, 0]). rr([1, 0, 0, 0, 0, 0, 0, 0], 1, [0, 1, 0, 0, 0, 0, 0, 0]).
rr([1, 0, 0, 0, 0, 0, 0, 0], 2, [0, 0, 1, 0, 0, 0, 0, 0]). rr([1, 0, 0, 0, 0, 0, 0, 0], 3, [0, 0, 0, 1, 0, 0, 0, 0]).
rr([1, 0, 0, 0, 0, 0, 0, 0], 4, [0, 0, 0, 0, 1, 0, 0, 0]). rr([1, 0, 0, 0, 0, 0, 0, 0], 5, [0, 0, 0, 0, 0, 1, 0, 0]).
rr([1, 0, 0, 0, 0, 0, 0, 0], 6, [0, 0, 0, 0, 0, 0, 1, 0]). rr([1, 0, 0, 0, 0, 0, 0, 0], 7, [0, 0, 0, 0, 0, 0, 0, 1]).
rr([1, 1, 0, 0, 0, 0, 0, 0], 0, [1, 1, 0, 0, 0, 0, 0, 0]). rr([1, 1, 0, 0, 0, 0, 0, 0], 1, [0, 1, 1, 0, 0, 0, 0, 0]).
rr([1, 1, 0, 0, 0, 0, 0, 0], 2, [0, 0, 1, 1, 0, 0, 0, 0]). rr([1, 1, 0, 0, 0, 0, 0, 0], 3, [0, 0, 0, 1, 1, 0, 0, 0]).
rr([1, 1, 0, 0, 0, 0, 0, 0], 4, [0, 0, 0, 0, 1, 1, 0, 0]). rr([1, 1, 0, 0, 0, 0, 0, 0], 5, [0, 0, 0, 0, 0, 1, 1, 0]).
rr([1, 1, 0, 0, 0, 0, 0, 0], 6, [0, 0, 0, 0, 0, 0, 1, 1]). rr([1, 1, 0, 0, 0, 0, 0, 0], 7, [1, 0, 0, 0, 0, 0, 0, 1]).
rr([1, 1, 1, 0, 0, 0, 0, 0], 0, [1, 1, 1, 0, 0, 0, 0, 0]). r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 1, 1, 1, 1, 0, 1, 1]). rr([1, 1, 1, 1, 1, 1, 1, 0], 7, [1, 1, 1, 1, 1, 1, 0, 1]).
rr([1, 1, 1, 1, 1, 1, 1, 1], 0, [1, 1, 1, 1, 1, 1, 1, 1]). rr([1, 1, 1, 1, 1, 1, 1, 1], 1, [1, 1, 1, 1, 1, 1, 1, 1]).
rr([1, 1, 1, 1, 1, 1, 1, 1], 2, [1, 1, 1, 1, 1, 1, 1, 1]). rr([1, 1, 1, 1, 1, 1, 1, 1], 3, [1, 1, 1, 1, 1, 1, 1, 1]).
rr([1, 1, 1, 1, 1, 1, 1, 1], 4, [1, 1, 1, 1, 1, 1, 1, 1]). rr([1, 1, 1, 1, 1, 1, 1, 1], 5, [1, 1, 1, 1, 1, 1, 1, 1]).
rr([1, 1, 1, 1, 1, 1, 1, 1], 6, [1, 1, 1, 1, 1, 1, 1, 1]). rr([1, 1, 1, 1, 1, 1, 1, 1], 7, [1, 1, 1, 1, 1, 1, 1, 1]).
and([], [], []).
and([1|Op1], [1|Op2], [1|Op3]) :- and(Op1, Op2, Op3).
and([0|Op1], [1|Op2], [0|Op3]) :- and(Op1, Op2, Op3).
and([1|Op1], [0|Op2], [0|Op3]) :- and(Op1, Op2, Op3).
and([0|Op1], [0|Op2], [0|Op3]) :- and(Op1, Op2, Op3).
or([], [], []).
or([1|Op1], [1|Op2], [1|Op3]) :- or(Op1, Op2, Op3).
or([0|Op1], [1|Op2], [1|Op3]) :- or(Op1, Op2, Op3).
or([1|Op1], [0|Op2], [1|Op3]) :- or(Op1, Op2, Op3).
or([0|Op1], [0|Op2], [0|Op3]) :- or(Op1, Op2, Op3).
zero([0, 0, 0, 0, 0, 0, 0, 0]).
group('1', [1, 0, 0, 0, 0, 0, 0, 0]).
group('2', [1, 1, 0, 0, 0, 0, 0, 0]).
group('3', [1, 1, 1, 0, 0, 0, 0, 0]).
group('4', [1, 1, 1, 1, 0, 0, 0, 0]).
group('5', [1, 1, 1, 1, 1, 0, 0, 0]).
group('6', [1, 1, 1, 1, 1, 1, 0, 0]).
group('7', [1, 1, 1, 1, 1, 1, 1, 0]).
group('8', [1, 1, 1, 1, 1, 1, 1, 1]).
groups([], []).
groups([G|GS], [B|BS]) :-
group(G, B),
groups(GS, BS).
visit1(G, C, [W1, E1, _], [W2, W1, E1]) :-
zero(Z),
rr(G, C, W2), or(W1, E1, S1), and(S1, W2, Z);
C1 is C + 1, C1 < 8, visit1(G, C1, [W1, E1, _], [W2, W1, E1]).
visit2(G, C, [W1, _, _], [W2, Z, W1]) :-
zero(Z),
rr(G, C, W2), and(W1, W2, Z);
C1 is C + 1, C1 < 8, visit2(G, C1, [W1, _, _], [W2, Z, W1]).
visit0(G, S1, S2) :- visit1(G, 0, S1, S2); visit2(G, 0, S1, S2).
visit0(G, _, [G, Z, Z]) :- zero(Z).
visit([], [W, E, R], [W, E, R]).
visit([G|GS], [W1, E1, R1], [W, E, R]) :-
visit0(G, [W1, E1, R1], [W2, E2, R2]),
visit(GS, [W2, E2, R2], [W, E, R]).
to_chars([], []).
to_chars([1|BS], ['1'|CS]) :- to_chars(BS, CS).
to_chars([0|BS], ['0'|CS]) :- to_chars(BS, CS).
solve(Input, Actual) :-
string_chars(Input, Chars),
groups(Chars, Groups),
zero(Z),
visit(Groups, [Z, Z, Z], [W, E, R]),
or(W, E, WE),
or(WE, R, WER),
to_chars(WER, CS),
string_chars(Actual, CS).
judge(E, E, 'PASSED').
judge(_, _, 'FAILED').
test(Input, Expected) :-
solve(Input, Actual),
judge(Expected, Actual, Result),
format("~p input ~s, expected ~s, actual ~s~n", [Result, Input, Expected, Actual]).
main :-
test("3316", "11111110"),
test("3342153", "11111111"),
test("8", "11111111"),
test("232624", "11110110"),
test("1", "10000000"),
test("224673432", "11111000"),
test("123464334", "11111110"),
test("44372332", "11111111"),
test("2344", "11111111"),
test("6448476233", "11111100"),
test("4345174644", "11111111"),
test("77743", "11111110"),
test("2136524281", "10000000"),
test("344373", "11100000"),
test("434226", "11111111"),
test("7433223", "11111110"),
test("2246534", "11110111"),
test("634", "11111110"),
test("2222", "11111100"),
test("2442343238", "11111111"),
test("7243344", "11111111"),
test("26147234", "10111111"),
test("34424", "10011111"),
test("6334", "11011111"),
test("3828342", "11011110"),
test("4431", "11110000"),
test("2843212125", "11111111"),
test("333336482", "11000000"),
test("374", "11110000"),
test("4382333", "11100111").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment