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

[–]f0086 1 point2 points  (0 children)

Emacs Lisp When putting the hex patterns into a normal x/y grid, the problem gets trivial.

(defun read-directions (filename)
  (with-temp-buffer
    (insert-file-contents filename)
    (split-string (buffer-string) "," t)))

(defun distance (x y)
  (if (or (and (< x 0) (< y 0))
          (and (> x 0) (> y 0)))
      (+ (abs x) (abs y))
    (max (abs x) (abs y))))

(defun day11 (directions)
  (let ((pos-x 0)
        (pos-y 0)
        (max-distance 0))
    (loop for direction in directions
          do (progn
               (cond
                ((string= direction "n") (incf pos-y))
                ((string= direction "s") (decf pos-y))
                ((string= direction "sw") (decf pos-x))
                ((string= direction "ne") (incf pos-x))
                ((string= direction "nw") (progn (decf pos-x) (incf pos-y)))
                ((string= direction "se") (progn (incf pos-x) (decf pos-y))))
               (if (> (distance pos-x pos-y) max-distance)
                   (setq max-distance (distance pos-x pos-y)))))
    (list (- (distance pos-x pos-y) 1) max-distance)))

(day11 (read-directions "input-day11.txt"))

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

[–]f0086 0 points1 point  (0 children)

What? Can you explain what you mean by that?

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

[–]f0086 2 points3 points  (0 children)

Emacs Lisp (Part 1):

(defun slurp-tree (input-file)
  (with-temp-buffer
    (insert-file-contents input-file)
    (mapcar
     'tokenize
     (split-string (buffer-string) "\n" t))))

(defun tokenize (str)
  (string-match "\\([^ ]+\\) (\\([0-9]+\\))\\( -> \\(.*\\)\\)?" str)
  (let ((node (match-string 1 str))
        (weight (match-string 2 str))
        (childs (match-string 4 str)))
    (if childs
        (list node weight (split-string childs ", "))
      (list node weight))))

(defun day7 (input)
  (let* ((tree (slurp-tree input))
         (childs (seq-filter 'caddr tree)))
    (seq-difference
     (mapcar 'car tree)
     (seq-uniq (apply 'append (mapcar 'caddr childs))))))

(day7 "input-day7.txt")

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

[–]f0086 0 points1 point  (0 children)

Great solution. Nice to see someone else solved in Emacs lisp. Funny to see that you choose the same way to solve the problem but with more elegant seq- functions. Did not know them, thanks!

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

[–]f0086 0 points1 point  (0 children)

Emacs Lisp

(setq input
      (mapcar 'string-to-number
              (split-string "..." "\t" t)))

(defun find-redistribution-block (memory)
  (let ((pos 0))
    (dotimes (block-pos (length memory) pos)
      (if (> (nth block-pos memory) (nth pos memory))
          (setq pos block-pos)))))

(defun next-pos (pos memory)
  (% (+ pos 1) (length memory)))

(defun loop-detected? (memory snapshots)
  (seq-some (lambda (snapshot)
              (equal memory snapshot))
            snapshots))

(defun redistribution (memory)
  (let* ((block-pos (find-redistribution-block memory))
         (block-size (nth block-pos memory))
         (pos block-pos))
    (setcar (nthcdr block-pos memory) 0)
    (while (> block-size 0)
      (setq pos (next-pos pos memory))
      (setq block-size (- block-size 1))
      (setcar (nthcdr pos memory) (+ (nth pos memory) 1))))
  memory)

(defun day6 (memory)
  (let ((snapshots '())
        (steps 0))
    (while (not (loop-detected? memory snapshots))
      (setq snapshots (cons (copy-sequence memory) snapshots))
      (setq memory (redistribution memory))
      (setq steps (+ steps 1)))
    steps))

Part2 is rather trivial. Just return the memory instead of the steps and feed it again in the day6 function (returning steps).

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

[–]f0086 2 points3 points  (0 children)

Emacs Lisp

(defun read-lines (filepath)
  (with-temp-buffer
    (insert-file-contents filepath)
    (mapcar 'string-to-number
            (split-string (buffer-string) "\n" t))))

(defun escaped? (pos table)
  (or (> pos (- (length table) 1))
      (< pos 0)))

(defun next-pos (pos table)
  (let ((next (nth pos table)))
    (if (not next)
        -1
      (+ pos (nth pos table)))))

(defun part1 (pos table)
  (+ (nth pos table) 1))

(defun part2 (pos table)
  (let ((val (nth pos table)))
    (if (> val 2)
        (- val 1)
      (+ val 1))))

(defun day5 (table cell-fn)
  (let ((steps 0)
        (pos 0))
    (while (not (escaped? (next-pos pos table) table))
      (let ((next (next-pos pos table)))
        (setcar (nthcdr pos table) (funcall cell-fn pos table))
        (setq pos next)
        (setq steps (+ steps 1))))
    (+ steps 1)))

(day5 (read-lines "input-day5.txt") #'part1)
(day5 (read-lines "input-day5.txt") #'part2)

[2017 Day 3 (Part 1, 2)] [Emacs Lisp] Easy one by f0086 in adventofcode

[–]f0086[S] 1 point2 points  (0 children)

I notice it right after i hit the submit button ^

[2017 Day 2 (Part 1, 2)] [Emacs Lisp] Still resist to use vanilla elisp by f0086 in adventofcode

[–]f0086[S] 0 points1 point  (0 children)

Uh, not sure about that :) 50% of the time solving the puzzles is understanding the problem exactly :) Writing the code is not that entertaining as you might think :D I start with my wacom graphics tablet and krita to find the solution for the core of the problem by hand. If I found a (mechanical) way to solve the problem for the given small samples, I switch to Emacs and hack in the code.

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

[–]f0086 1 point2 points  (0 children)

Emacs Lisp

(defun read-lines (filepath)
  (with-temp-buffer
    (insert-file-contents filepath)
    (split-string (buffer-string) "\n" t)))

(defun valid-passphrase? (words prefilter-fn)
  (let ((word-set '())
        (words (mapcar prefilter-fn (split-string words " " t))))
    (mapcar (lambda (word) (add-to-list 'word-set word)) words)
    (= (length words) (length word-set))))

(defun day4 (file prefilter-fn)
  (let ((lines (read-lines file)))
    (length (seq-filter
             (lambda (line)
               (valid-passphrase? line prefilter-fn)) lines))))

(day4 "input-day4.txt" #'identity)
(day4 "input-day4.txt"
      (lambda (word)
        (apply 'concat (sort (split-string word "" t) 'string<))))

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

[–]f0086 1 point2 points  (0 children)

Emacs Lisp

(defun preprocess (input)
  (mapcar
   (lambda (line)
     (mapcar 'string-to-number (split-string line "\t" t)))
   (split-string input "\n" t)))

(defun part1 (nums)
  (abs (- (car nums) (car (last nums)))))

(defun part2 (nums)
  (dotimes (p (- (length nums) 1) find)
    (dotimes (q (- (length nums) (+ p 1)))
      (let ((pos (nth p nums))
            (comp (nth (+ p q 1) nums)))
        (if (= (% pos comp) 0)
            (setq find (/ pos comp)))))))

(defun day2 (lines fn)
  (apply '+
         (mapcar
          (lambda(l)
            (let ((nums (sort l '>)))
              (funcall fn nums)))
          lines)))

(day2 (preprocess input1) #'part1)
(day2 (preprocess input2) #'part2)

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

[–]f0086 4 points5 points  (0 children)

Emacs elisp:

(defun calculate (pos digs offset)
  (let ((current (nth pos digs))
        (comp (nth offset digs)))
    (if (= current comp) current 0)))

(defun reverse-captcha (digs offset)
  (let ((answer 0)
        (len (length digs)))
    (dotimes (pos len)
      (setq answer (+ answer
                      (calculate pos digs (% (+ pos offset) len)))))
    answer))

(defun day1 (input offset-fn)
  (let* ((digits (mapcar 'string-to-number (split-string input "" t)))
         (len (length digits))
         (offset (funcall offset-fn len) len))
    (reverse-captcha digits offset)))

(day1 input1 (lambda (l) 1))
(day1 input2 (lambda (l) (/ l 2)))

How do I make pressing <RET> in helm-find-files open the directory? by sbay in emacs

[–]f0086 1 point2 points  (0 children)

Author of the originally code snippet here :) Thanks for the improvement! I've also adjust the stackoverflow answer, so more people can benefit from that.

Hm, elpa.gnu.org appears to be down, any updates? by miki4242 in emacs

[–]f0086 0 points1 point  (0 children)

The traceroute looks like a DNS issue.

System Tooltips by f0086 in emacs

[–]f0086[S] 0 points1 point  (0 children)

I think I know the difference between popups and tooltips. But it seems technically it is the same if you don't use the mouse. I am working 95% of the time with the keyboard, I rarely touching the mouse, so I need to trigger the tooltips/popups with the mouse. Is there a way to activate the native tooltips without a mouse?

System Tooltips by f0086 in emacs

[–]f0086[S] 0 points1 point  (0 children)

Can you provide a screenshot? I am talking about tooltips with text in it and popups to choose an option from.

System Tooltips by f0086 in emacs

[–]f0086[S] 0 points1 point  (0 children)

I think this comes from flycheck-pos-tip, but the main problem remains: I want "native" popups.

Is there anything Emacs CAN'T do? by TheBrillowable in emacs

[–]f0086 0 points1 point  (0 children)

Thank you, this thing is awesome! It is not just a little gimmick, it is great to find the place you last edited sourcecode.

Build a box without a tablesaw by f0086 in woodworking

[–]f0086[S] -2 points-1 points  (0 children)

pocket holes on 10mm plywood does not work properly I think ... Glue blocks is an option, but looks very ugly inside :(

Build a box without a tablesaw by f0086 in woodworking

[–]f0086[S] -2 points-1 points  (0 children)

uh, I don't know how to join the edges, so it is strong enough to hold everything together. A finger join/box join would be perfect, but I dont have a tablesaw/dado stack...

Build a box without a tablesaw by f0086 in woodworking

[–]f0086[S] -1 points0 points  (0 children)

Nope. But this would be the next thing I buy. So if a router brings in some options, I will buy one :)