(defpackage #:rucksacks
(:use :cl))
(in-package #:rucksacks)
(defvar *rucksacks* ())
(defun chars (s) (loop for c across s collect c))
(with-open-file (s "./list.txt")
(setf *rucksacks*
(loop for line = (read-line s nil)
while line collect
(let ((half (floor (length line) 2)))
(let ((s1 (subseq line 0 half)) (s2 (subseq line half)))
(list (chars s1) (chars s2)))))))
(defun items-in-both (x y)
(loop for e in x
when (member e y) collect e))
(defun char-to-priority (c)
(let ((x (char-code c)))
(cond ((and (>= x (char-code #\a)) (<= x (char-code #\z))) (+ 1 (- x (char-code #\a))))
((and (>= x (char-code #\A)) (<= x (char-code #\Z))) (+ 27 (- x (char-code #\A))))
(t 0))))
(let ((duplicates (mapcar #'(lambda (x) (items-in-both (car x) (car (cdr x)))) *rucksacks*)))
(let ((tmp (loop for e in duplicates collect (mapcar #'char-to-priority e))))
(print (reduce #'+ (loop for e in tmp collect (reduce #'+ (remove-duplicates e)))))))