Skip to content

Instantly share code, notes, and snippets.

@pqr
Forked from bernik/gist:5f7c5c52cde8bff71564
Last active August 29, 2015 14:26
Show Gist options
  • Save pqr/921aa05195920dc03ec2 to your computer and use it in GitHub Desktop.
Save pqr/921aa05195920dc03ec2 to your computer and use it in GitHub Desktop.
(require '[leiningen.exec :as le])
(ns foo
(:require [clojure.string :as str]))
(def source-text "https://gist.githubusercontent.com/pqr/0cf155de6b8f95c3c400/raw/56fc9aa6c06bf3e55a2a02d3e4a857580feff2bc/haskell_vs_php_example.txt")
(defn get-lines [text] (map str/trim (str/split-lines text)))
(defn drop-header-line [lines]
(if (re-find #"Container No\\." (first lines))
(rest lines)
lines))
(defn drop-flag-line [lines]
(cond
(<= (count lines) 20) lines
(re-find #"Flag\\:" (nth lines 20)) (concat (take 20 lines) (drop 21 lines))
:else lines))
(defn lines-without-header-and-flag [lines] (-> lines drop-header-line drop-flag-line))
(defn split-per-block
([lines]
(split-per-block [] lines))
([blocks lines]
(cond
(< (count lines) 20) blocks
(>= (count (lines-without-header-and-flag lines)) 20) (let [first-20-lines (take 20 (lines-without-header-and-flag lines))
other-lines (drop 21 (lines-without-header-and-flag lines))
new-found-blocks (concat blocks first-20-lines)]
(split-per-block new-found-blocks other-lines))
:else blocks)))
(defn parse [text] (-> text
get-lines
split-per-block))
(println "Count: " (count (parse (slurp source-text))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment