Skip to content

Instantly share code, notes, and snippets.

@mayerrobert
Last active December 31, 2022 15:29
Show Gist options
  • Save mayerrobert/881370a1ff6b509e3eb1cc9ce1172aa3 to your computer and use it in GitHub Desktop.
Save mayerrobert/881370a1ff6b509e3eb1cc9ce1172aa3 to your computer and use it in GitHub Desktop.
AOC 2022 day 10 in Common Lisp, code golf style
;;;; AOC 2022 day 10 in Common Lisp,
;;;; based on https://www.reddit.com/r/adventofcode/comments/zhjfo4/2022_day_10_solutions/izmspl7/
#| (mostly) original Python:
from itertools import accumulate
f = lambda x: int(x) if x[-1].isdigit() else 0
xs = [*map(f, open('day10.txt').read().split())]
part1, part2 = 0, '\n'
for i, x in enumerate(accumulate([1]+xs), 1):
part1 += i*x if i%40==20 else 0
part2 += '#' if ((i-1)%40)-x in [-1,0,1] else ' '
if i%40 == 0:
part2 += '\n'
print(part1, part2)
|#
(require "uiop")
(let* ((tokens (uiop:split-string (uiop:read-file-string "day10.txt") :separator '(#\Newline #\ )))
(tokens (remove "" tokens :test 'equal))
(xs (map 'list (lambda (x) (setq x (read-from-string x)) (if (numberp x) x 0)) tokens))
(part1 0)
(part2 (make-array 0 :element-type 'character :adjustable t :fill-pointer t))
(i 1)
(x 0))
(vector-push-extend #\Newline part2)
(push 1 xs)
(dolist (n xs)
(incf x n)
(when (= (rem i 40) 20) (incf part1 (* i x)))
(vector-push-extend (case (- (rem (- i 1) 40) x)
((-1 0 1) #\#)
(t #\ ))
part2)
(when (zerop (rem i 40))
(vector-push-extend #\Newline part2))
(incf i))
(princ "AOC 2022 day10 part1: ") (princ part1) (terpri)
(princ "AOC 2022 day10 part2: ") (princ part2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment