-
-
Save pirj/385245 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 | |
whale: big black water animal | |
penguin: black white ice beak | |
piano: keyboard black white wire | |
jackboot: leather heel black | |
train: rail wheel black | |
rose: red green thorn | |
4 | |
whale penguin piano jackboot train | |
penguin piano | |
jackboot rose | |
piano train |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env escript | |
-module(rec). | |
-export([main/1]). | |
recombine(_, []) -> nil; | |
recombine([], Common) -> Common; | |
recombine([Head|Tail], Common) -> recombine(Tail, [X || X <- Head, Y <- Common, X == Y]). | |
recombine([]) -> nil; | |
recombine([Head|Tail]) -> recombine(Tail, Head). | |
remove_newline(S) -> lists:sublist(S, 1, length(S) - 1). | |
read_n_lines(_, Lines, 0) -> Lines; | |
read_n_lines(File, Lines, N) -> | |
read_n_lines(File, [remove_newline(io:get_line(File, nil)) | Lines], N - 1). | |
print_solutions(Defs, Comb) -> | |
M = [Props || Term1 <- Comb, {Term, Props} <- Defs, Term1 == Term], | |
R = recombine(M), | |
io:fwrite("Intersection: ~p~n", [R]). | |
main([FileName]) -> | |
{ok, File} = file:open(FileName, [read]), | |
TermDefs = read_n_lines(File, [], list_to_integer(remove_newline(io:get_line(File, nil)))), | |
Defs0 = [string:tokens(Def, ":") || Def <- TermDefs], | |
Defs = [{list_to_atom(H1), [list_to_atom(S) || S <- string:tokens(H2, " ")]} || [H1|[H2|_]] <- Defs0], | |
TermCombs = read_n_lines(File, [], list_to_integer(remove_newline(io:get_line(File, nil)))), | |
Combs = [[list_to_atom(S) || S <- string:tokens(Comb, " ")] || Comb <- TermCombs], | |
[print_solutions(Defs, Comb) || Comb <- Combs]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Intersection: [black] | |
Intersection: nil | |
Intersection: [black,white] | |
Intersection: [black] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment