Skip to content

Instantly share code, notes, and snippets.

@tommy-mor
Created December 11, 2017 22:51
Show Gist options
  • Save tommy-mor/6cd7028d4cb45dd71290c065073e1d36 to your computer and use it in GitHub Desktop.
Save tommy-mor/6cd7028d4cb45dd71290c065073e1d36 to your computer and use it in GitHub Desktop.
(ns lisp.core
(:gen-class)
(:require [clojure.string :as s]))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "Hello, World!"))
(map inc (range 10))
;; => (1 2 3 4 5 6 7 8 9 10)
(def f
(as-> (slurp "/Users/tommy/programming/pscsta/student_data/fezr.in") x
(s/split x #"\s+")
(rest x)
(map read-string x)))
(s/join ", " f)
;; => "1, 258, 304, 2, 1014, 1019, 1017, 1024, 4, 3, 7, 6, 9, 502, 502, 3512, 3519, 2, 2384, 2407, 1756, 1766, 3, 684, 702, 702, 721, 784, 789"
(defn read [[head & rest]]
(if head
(cons (take (* 2 head) rest)
(read (drop (* 2 head) rest)))
nil))
(defn printn [s]
(for [x s]
(println x)))
(println "---")
(range 1 5)
(printn) (for [x (read f)]
(count
(distinct
(flatten
(map (fn [[a b]] (range a (inc b)))
(partition 2 x))))))
;; => (47 11 16 35 44)
@Shamrock-Frost
Copy link

Shamrock-Frost commented Dec 12, 2017

#lang racket

(require threading)

(define path (string-append "student_data/" "fezr.in"))

(define (pair-to-neighbours ls)
  (if (null? ls) null
      (cons (cons (car ls) (cadr ls))
            (pair-to-neighbours (cddr ls)))))

(define (read-input lines)
  (if (null? lines) null
      (let ([n (* 2 (car lines))])
        (cons (pair-to-neighbours (take (cdr lines) n))
              (read-input (drop (cdr lines) n))))))

(define inputs (~> path file->list cdr read-input))

(for ([item inputs])
  (~>> item
       (map (λ [x] (range (car x) (add1 (cdr x)))))
       flatten
       remove-duplicates
       length
       println))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment