wer2

joined 2 years ago
[–] wer2@lemm.ee 4 points 8 months ago

Lisp

Brute forced part 2, but got a lot of reuse from part 1.

Part 1 and 2


(defvar *part1* "inputs/day06-part1")
(defvar *part1-test* "inputs/day06-part1-test")

(defstruct move x y direction)

(defstruct guard direction x y (moves (make-hash-table :test 'equalp)))

(defun convert-direction (g)
  (case g
    (^ 'up)
    (> 'right)
    (< 'left)
    (v 'down)))


(defun find-guard (map)
  (destructuring-bind (rows cols) (array-dimensions map)
    (loop for j from 0 below rows
          do (loop for i from 0 below cols
                   for v = (aref  map j i)
                   when (not (or (eql '|.| v) (eql '|#| v)))
                     do (return-from find-guard (make-guard :direction (convert-direction v) :x i :y j ))))))

(defun turn-guard (guard)
  (case (guard-direction guard)
    (UP (setf (guard-direction guard) 'RIGHT))
    (DOWN (setf (guard-direction guard) 'LEFT))
    (LEFT (setf (guard-direction guard) 'UP))
    (RIGHT (setf (guard-direction guard) 'DOWN))))

(defun on-map (map x y)
  (destructuring-bind (rows cols) (array-dimensions map)
    (and (>= x 0) (>= y 0)
         (< y rows) (< x cols))))

(defun mark-guard (map guard)
  (setf (aref map (guard-y guard) (guard-x guard)) 'X))

(defun next-pos (guard)
  (case (guard-direction guard)
    (UP (list (guard-x guard) (1- (guard-y guard))))
    (DOWN (list (guard-x guard) (1+ (guard-y guard))))
    (LEFT (list (1- (guard-x guard)) (guard-y guard)))
    (RIGHT (list (1+ (guard-x guard)) (guard-y guard)))))

(defun move-guard (map guard)
  (destructuring-bind (x y) (next-pos guard)
    (if (on-map map x y)
        (if (eql '|#| (aref map y x))
            (turn-guard guard)
            (progn (setf (guard-x guard) x)
                   (setf (guard-y guard) y))) 
        (setf (guard-direction guard) nil))))

(defun run-p1 (file) 
  (let* ((map (list-to-2d-array (read-file file #'to-symbols)))
         (guard (find-guard map)))
    (mark-guard map guard)
    (loop while (guard-direction guard)
          do (mark-guard map guard)
          do (move-guard map guard))
    (destructuring-bind (rows cols) (array-dimensions map)
      (loop for y from 0 below rows sum (loop for x from 0 below cols count (eql (aref map y x) 'X))))))

(defun save-move (guard move)
  (setf (gethash move (guard-moves guard)) t))

(defun reset-moves (guard)
  (setf (guard-moves guard) nil))

(defun is-loop (x y map original-guard)
  ;; can only set new blocks in blank spaces
  (unless (eql '|.| (aref map y x)) (return-from is-loop nil))
  (let ((guard (copy-guard original-guard)))
    ;; save the initial guard position
    (save-move guard (make-move :x (guard-x guard) :y (guard-y guard) :direction (guard-direction guard)))
    ;; set the "new" block
    (setf (aref map y x) '|#|)
    ;; loop and check for guard loops
    (let ((result
            (loop
              while (move-guard map guard)
              for move = (make-move :x (guard-x guard) :y (guard-y guard) :direction (guard-direction guard))
              ;; if we have seen the move before, then it is a loop
              if (gethash move (guard-moves guard))
                return t
              else
                do (save-move guard move)
              finally
                 (return nil))))

      ;; reset initial position
      (setf (aref map y x) '|.|)
      (clrhash (guard-moves guard))
      result)))


(defun run-p2 (file) 
  (let* ((map (list-to-2d-array (read-file file #'to-symbols)))
         (guard (find-guard map)))
    
    (destructuring-bind (rows cols) (array-dimensions map)
      (loop for y from 0 below rows
            sum (loop for x from 0 below cols
                      count (is-loop x y map guard)))
      )))

[–] wer2@lemm.ee 2 points 8 months ago

Lisp

Part 1

(defun p1-process-line (line)
  (mapcar #'parse-integer (str:words line)))

(defun line-direction-p (line)
  "make sure the line always goes in the same direction"
  (loop for x in line
        for y in (cdr line)
        count (> x y) into dec
        count (< x y) into inc
        when (and (> dec 0 ) (> inc 0)) return nil
        when (= x y) return nil
        finally (return t)))

(defun line-in-range-p (line)
  "makes sure the delta is within 3"
  (loop for x in line
        for y in (cdr line)
        for delta = (abs (- x y))
        when (or (> delta 3) )
          return nil 
        finally (return t)))

(defun test-line-p (line)
  (and (line-in-range-p line) (line-direction-p line)))

(defun run-p1 (file) 
  (let ((data (read-file file #'p1-process-line)))
    (apply #'+ (mapcar (lambda (line) (if (test-line-p line) 1 0)) data))))

Part 2

(defun test-line-p2 (line)
  (or (test-line-p (cdr line))
      (test-line-p (cdr (reverse line)))
  (loop for back on line
        collect (car back) into front
        when (test-line-p (concatenate 'list front (cddr back)))
          return t
        finally (return nil)
  )))

(defun run-p2 (file) 
  (let ((data (read-file file #'p1-process-line)))
    (loop for line in data
          count (test-line-p2 line))))

[–] wer2@lemm.ee 6 points 9 months ago

I use syncthings.

[–] wer2@lemm.ee 3 points 11 months ago (1 children)

Hashing is more about obscuring the password if the database gets compromised. I guess they could send 2^256 or 2^512 passwords guesses, but at that point you probably have bigger issues.

[–] wer2@lemm.ee 15 points 11 months ago (4 children)

It doesn't matter the input size, it hashes down to the same length. It does increase the CPU time, but not the storage space. If the hashing is done on the client side (pre-transmission), then the server has no extra cost.

For example, the hash of a Linux ISO isn't 10 pages long. If you SHA-256 something, it always results in 256 bits of output.

On the other hand, base 64-ing something does get longer as the input grows.

[–] wer2@lemm.ee 5 points 11 months ago (1 children)
[–] wer2@lemm.ee 3 points 1 year ago (1 children)

As an emacs user, have you considered org mode, with org-roam enabled? You can use source control to back it up or, use something like syncthings to move the files around.

[–] wer2@lemm.ee 27 points 1 year ago (6 children)

As someone stuck in DTW, I feel the pain.

[–] wer2@lemm.ee 3 points 1 year ago

I just sleep in full plate, because keeping track of the AC difference is too hard (because I am lazy).

[–] wer2@lemm.ee 5 points 1 year ago (1 children)

My opinion of Alien 3 went up a lot after watching all the movies that came after it.

[–] wer2@lemm.ee 6 points 1 year ago

Probably for tax purposes.

[–] wer2@lemm.ee 1 points 1 year ago

The beauty of Linux at home, you get to choose what works best for you.

view more: β€Ή prev next β€Ί