Skip to content

Instantly share code, notes, and snippets.

@visibletrap visibletrap/ thai-fp-q5.clj Secret
Last active Aug 29, 2015

Embed
What would you like to do?
(ns pg.core
(:require [clojure.string :as s]))
(defn ->executable-patterns [pattern]
(->> pattern
(re-seq #"\(\w+\)|\w")
(map #(s/replace % #"\(|\)" ""))
(map set)))
(defn pattern-match? [pattern word]
(let [sub-patterns (->executable-patterns pattern)]
(->> word
(map list sub-patterns)
(map eval)
(every? identity))))
(defn count-match [pattern words]
(->> words
(map #(pattern-match? pattern %))
(filter identity)
count))
(defn solve [filename]
(let [in (s/split-lines (slurp filename))
[_ d _] (s/split (first in) #"\s")
end-words-pos (+ 1 (Integer/parseInt d))
words (subvec in 1 end-words-pos)
cases (subvec in end-words-pos)]
(->> cases
(map #(count-match % words))
(map-indexed vector)
(map (fn [[i out]] (str "Case #" (inc i) ": " out))))))
(prn (solve "sample.in"))
(spit "small.out" (s/join "\n" (solve "small.in")))
;; count-match tests
(assert (= 2 (count-match "(ab)(bc)(ca)" ["abc" "bca" "dac" "dbc" "cba"])))
(assert (= 1 (count-match "abc" ["abc" "bca" "dac" "dbc" "cba"])))
(assert (= 3 (count-match "(abc)(abc)(abc)" ["abc" "bca" "dac" "dbc" "cba"])))
(assert (= 0 (count-match "(zyx)bc" ["abc" "bca" "dac" "dbc" "cba"])))
;; pattern-sets tests
(assert (= [#{\a \b} #{\b \c} #{\c \a}] (->executable-patterns "(ab)(bc)(ca)")))
(assert (= [#{\a} #{\b} #{\c}] (->executable-patterns "abc")))
(assert (= [#{\z \y \x} #{\b} #{\c}] (->executable-patterns "(zyx)bc")))
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class AlienLangDecipher {
static String input = "A-small-attempt0.in";
static String output = "A-small.out";
static int langE;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("/home/visibletrap/Desktop/" + input));
langE = sc.nextInt();
int trueNo = sc.nextInt();
int setNo = sc.nextInt();
sc.nextLine();
List<String> trueList = new ArrayList();
for (int i = 0; i < trueNo; i++)
trueList.add(sc.nextLine());
Set<Character> [][] ss = new Set[setNo][langE];
for (int i = 0; i < setNo; i++)
ss[i] = parse(sc.nextLine());
List<Integer> results = new ArrayList<Integer>();
for (int i = 0; i < ss.length; i++) {
int k = 0;
for (int j = 0; j < trueList.size(); j++) {
for (int l = 0; l < langE; l++) {
if(!ss[i][l].contains(trueList.get(j).charAt(l)))break;
if (l == langE - 1) k++;
}
}
results.add(k);
}
printResult(results);
}
static void printResult(List<Integer> results) throws IOException {
FileOutputStream fos = new FileOutputStream(new File("/home/visibletrap/Desktop/" + output));
for (int i = 1; i <= results.size(); i++) {
fos.write(("Case #" +i +": " +results.get(i-1) +"\n").getBytes());
}
fos.close();
}
static Set<Character> [] parse(String line) {
Set<Character> [] sets = new Set[langE];
int i = 0;
while (line.length() > 0) {
if (line.startsWith("(")) {
sets[i] = buildSet(line.substring(1,line.indexOf(")")));
line = line.substring(line.indexOf(")") + 1);
} else {
sets[i] = new HashSet<Character>();
sets[i].add(line.charAt(0));
line = line.substring(1);
}
i++;
}
return sets;
}
private static Set<Character> buildSet(String substring) {
Set<Character> s = new HashSet<Character>();
for (int i = 0; i < substring.length(); i++) {
s.add(substring.charAt(i));
}
return s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.