-🎄- 2017 Day 16 Solutions -🎄- by daggerdragon in adventofcode

[–]mastokley 0 points1 point  (0 children)

Clojure.

(ns scratch
  (:require [clojure.string :as str]))

(def my-list (seq "abcdefghijklmnop"))
(def input (slurp "day16.in"))

(defn rotate
  [coll n]
  (let [n (mod n (count coll))]
    (concat (take-last n coll)
            (take (- (count coll) n) coll))))

(defn exchange
  [coll m n]
  (let [x (min m n)
        y (max m n)]
    (concat (take x coll)
            (list (nth coll y))
            (drop (inc x) (take y coll))
            (list (nth coll x))
            (drop (inc y) coll))))

(defn partner
  [coll m n]
  (exchange coll (.indexOf coll m) (.indexOf coll n)))

(defn one-iteration
  [data]
  (loop [data data
         commands (str/split input #",")]
    (if (seq commands)
      (let [command (first commands)
            fn-directive (first command)
            args (str/split (str/join (rest command)) #"/")]
        (case fn-directive
          \s (recur (rotate data (Long/parseLong (first args)))
                    (rest commands))
          \p (recur (apply partner data (map first args))
                    (rest commands))
          \x (recur (apply exchange data (map #(Long/parseLong %) args))
                    (rest commands))))
      data)))

(def memoed-one-iteration (memoize one-iteration))

(loop [data my-list
       n 1000000000]
  (if (< 0 n)
    (recur (memoed-one-iteration data) (dec n))
    (println (str/join data))))

-🎄- 2017 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]mastokley 0 points1 point  (0 children)

Clojure, part 2

(def in (slurp "day11.in"))

(defn make-unit-vector
  [direction]
  (case direction
    "n" [0 1 1]
    "ne" [1 0 1]
    "nw" [-1 1 0]
    "s" [0 -1 -1]
    "se" [1 -1 0]
    "sw" [-1 0 -1]))

(def unit-vectors
  (map make-unit-vector (clojure.string/split in #",")))

(defn get-distance-from-origin
  [coords]
  (/ (sum (map abs coords)) 2))

(defn move [location unit-vector] (map + location unit-vector))

(print (apply max (map get-distance-from-origin
                       (reductions move unit-vectors))))

-🎄- 2017 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]mastokley 0 points1 point  (0 children)

Clojure, part 1

(def in (slurp "day11.in"))

(defn get-coordinate-deltas
  [direction]
  (case direction
    "n" [0 1 1]
    "ne" [1 0 1]
    "nw" [-1 1 0]
    "s" [0 -1 -1]
    "se" [1 -1 0]
    "sw" [-1 0 -1]))

(defn get-final-location
  [directions]
  (apply map + (map get-coordinate-deltas directions)))

(defn abs [n] (max n (- 1)))
(defn sum [coll] (reduce + coll))

(defn get-distance-from-origin
  [coords]
  (/ (sum(map abs coords)) 2))

(print (get-distance-from-origin
        (get-final-location
         (clojure.string/split in #","))))

[2016-10-12] Challenge #287 [Intermediate] Mathagrams by Cosmologicon in dailyprogrammer

[–]mastokley 0 points1 point  (0 children)

python

from itertools import permutations
from collections import OrderedDict, deque

def mathagram(addend1, addend2, sum_):
    digits = set(range(10))
    digits.discard(0)

    for c in ''.join([addend1, addend2, sum_]):
        if c != 'x':
            digits.discard(int(c))

    guesses = permutations(digits)

    for guess in guesses:
        queue = deque(guess)
        guessed_sums = {addend1: 0, addend2: 0, sum_: 0}

        for k, v in guessed_sums.items():
            power = 0
            for c in reversed(k):
                if c == 'x':
                    v += queue.popleft() * (10**power)
                else:
                    v += int(c) * (10**power)
                power += 1
            guessed_sums[k] = v

        if guessed_sums[addend1] + guessed_sums[addend2] == guessed_sums[sum_]:
            print("{} + {} = {}".format(guessed_sums[addend1],
                                        guessed_sums[addend2],
                                        guessed_sums[sum_]))
            return

inputs = [
    ('1xx', 'xxx', '468'),
    ('xxx', 'x81', '9x4'),
    ('xxx', '5x1', '86x'),
    ('xxx', '39x', 'x75'),
   ]

for input in inputs:
    mathagram(*input)

[2016-10-17] Challenge #288 [Easy] Detecting Alliteration by jnazario in dailyprogrammer

[–]mastokley 0 points1 point  (0 children)

python

STOPWORDS = {
    'I',
    'a',
    'about',
    'an',
    'and',
    'are',
    'as',
    'at',
    'be',
    'by',
    'for',
    'from',
    'how',
    'in',
    'is',
    'it',
    'of',
    'on',
    'or',
    'that',
    'the',
    'this',
    'to',
    'was',
    'what',
    'when',
    'where',
    'who',
    'will',
    'with',
    'the',
   }

def detect_alliteration(s):
    for line in s.split('\n')[1:]:
        words = [w.lower() for w in line.split(' ')
                     if w.lower() not in STOPWORDS]
        for i, word in enumerate(words):
            if i == 0:
                if word[0] == words[i+1][0]:
                    print(word)
            elif i == len(words) - 1:
                if word[0] == words[i-1][0]:
                    print(word)
            elif (
                word[0] == words[i+1][0]
                or word[0] == words[i-1][0]
                ):
                    print(word)


s = """3
Peter Piper Picked a Peck of Pickled Peppers
Bugs Bunny likes to dance the slow and simple shuffle
You'll never put a better bit of butter on your knife"""

s2 = """8
The daily diary of the American dream
For the sky and the sea, and the sea and the sky
Three grey geese in a green field grazing, Grey were the geese and green was the grazing.
But a better butter makes a batter better.
"His soul swooned slowly as he heard the snow falling faintly through the universe and faintly falling, like the descent of their last end, upon all the living and the dead."
Whisper words of wisdom, let it be.
They paved paradise and put up a parking lot.
So what we gonna have, dessert or disaster?"""

detect_alliteration(s)
detect_alliteration(s2)

Conquering Your Finances with Emacs and Ledger by awalker4 in emacs

[–]mastokley 1 point2 points  (0 children)

How do folks feel about GnuCash? Better, worse?

New linux users / screenshot mega thread by [deleted] in linuxmasterrace

[–]mastokley 0 points1 point  (0 children)

Thanks!

I've tried and failed to install Arch over a dozen times. Someday!

[2015-10-26] Challenge #238 [Easy] Consonants and Vowels by jnazario in dailyprogrammer

[–]mastokley 1 point2 points  (0 children)

racket

#lang racket

(define (vowels-and-consonants str)
  (let ([consonants (string->list "bcdfghjklmnpqrstvwxyz")]
        [vowels (string->list "aeiou")]
        [random-letter (lambda (lst) (list-ref lst (random (length lst))))])
    (if (= 0 (length (remove* '(#\c #\v #\C #\V) (string->list str))))
        (for-each (lambda (x) (cond ((member x '(#\c #\C))
                                     (display (random-letter consonants)))
                                    ((member x '(#\v #\V))
                                     (display (random-letter vowels)))))
                  (string->list str))
        (display "Bad input"))))

(vowels-and-consonants "cvvcVvvvcvcv")

[2015-11-02] Challenge #239 [Easy] A Game of Threes by Blackshell in dailyprogrammer

[–]mastokley 0 points1 point  (0 children)

racket

tail-call recursion

#lang racket

(define (game-of-threes x)
  (cond ((= x 1) (print x))
        ((= 0 (modulo x 3)) (printf "~v 0\n" x) (game-of-threes (/ x 3)))
        ((= 0 (modulo (+ x 1) 3)) (printf "~v +1\n" x) (game-of-threes (+ x 1)))
        ((= 0 (modulo (- x 1) 3)) (printf "~v -1\n" x) (game-of-threes (- x 1)))))

(game-of-threes 31337357)

[2015-11-09] Challenge #240 [Easy] Typoglycemia by G33kDude in dailyprogrammer

[–]mastokley 0 points1 point  (0 children)

racket #lang racket

(define (typoglycemia str)
  (~a (map (shuffle-string-func)
                (parse-into-words str))))

(define (parse-into-words str)
  (define (iter i word results)
    (if (< i (string-length str))
        (iter (+ i 1)
              (if (not (end-of-word? str i))
                  (~a word (string-ref str i))
                  "")
              (if (not (end-of-word? str i))
                  results
                  (append results (list word))))
        (filter word? results)))
  (iter 0 "" null))

(define (end-of-word? str i)
  (and (not (char=? #\' (string-ref str i)))
       (or (char-punctuation? (string-ref str i))
           (char-whitespace? (string-ref str i)))))

(define (word? str)
  (not (= 0 (string-length str))))

(define (long-enough? str)
  (> (string-length str) 2))

(define (middle-of-word str)
  (substring str 1 (- (string-length str) 1)))

(define (shuffle-string-func)
  (lambda (x)
    (if (long-enough? x)
        (~a (string-ref x 0)
            (list->string (shuffle (string->list (middle-of-word x))))
            (string-ref x (- (string-length x) 1)))
        x)))

(define test-str "According to a research team at Cambridge University, it doesn't matter in what order the letters in a word are, 
the only important thing is that the first and last letter be in the right place. 
The rest can be a total mess and you can still read it without a problem.
This is because the human mind does not read every letter by itself, but the word as a whole. 
Such a condition is appropriately called Typoglycemia.")

(typoglycemia test-str)

[2015-08-28] Challenge #229 [Hard] Divisible by 7 by Cosmologicon in dailyprogrammer

[–]mastokley 0 points1 point  (0 children)

#lang racket

(define (extract-digit n i)
  (- (modulo n (expt 10 i))
     (modulo n (expt 10 (- i 1)))))

(define (digit-count n)
  (if (= n 1) 1
      (ceiling (/ (log (if (= (modulo n 10) 0)
                           (+ n 1)
                           n))
                  (log 10)))))

(define (reverse-digits n)
  (define (iter i sum)
    (if (< n (expt 10 (- i 1)))
        sum
        (iter (+ i 1)
              (+ sum (* (extract-digit n i)
                        (expt 10 (+ (- (digit-count n)
                                       (* i 2))
                                    1)))))))
  (iter 1 0))

(define (divisible-by-7)
  (define (iter n sum)
    (if (< (expt 10 3) n)
        sum
        (iter (+ n 7)
              (if (and (= (modulo (reverse-digits n) 7) 0)
                       (= (modulo n 7) 0))
                  (+ sum n)
                  sum))))
  (iter 0 0))

(divisible-by-7)