Compare commits
5 Commits
48c351c983
...
a0f3c07e06
Author | SHA1 | Date |
---|---|---|
|
a0f3c07e06 | |
|
ce89746131 | |
|
540a1a52c5 | |
|
455cee8c79 | |
|
0116b13088 |
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,125 @@
|
||||||
|
|
||||||
|
(list 1 2 3)
|
||||||
|
|
||||||
|
;;; input - one number per line, different intentories separated by empty new line
|
||||||
|
;; need to find what - how much does have the intventory with maximal sum
|
||||||
|
;; so, do i have a function that takes in one string?
|
||||||
|
;; i guess it would be better to read input from the separate file
|
||||||
|
|
||||||
|
;; would also probably be better to read file line a time
|
||||||
|
(defvar current-sum 0)
|
||||||
|
(defvar current-max -1)
|
||||||
|
(with-open-file
|
||||||
|
(in "day1-input.txt")
|
||||||
|
(loop
|
||||||
|
for line = (read-line in nil nil)
|
||||||
|
while line
|
||||||
|
;; (if (eq line = "") (print "got empty line"))
|
||||||
|
|
||||||
|
;; collect line
|
||||||
|
;; do (format t "got line ~A~%" line)
|
||||||
|
|
||||||
|
;;; yay, this is somewhat what i want
|
||||||
|
;; if (string-equal line "") do (format t "got empty line ~A~% in if" line)
|
||||||
|
;; else do (format t "else branch for nonempty line ~A~%" line)
|
||||||
|
if (string-equal line "") do (progn
|
||||||
|
;; current inventory ended,
|
||||||
|
;; check whether it's new max and null accum
|
||||||
|
(if (< current-max current-sum) (setq current-max current-sum) )
|
||||||
|
(setq current-sum 0))
|
||||||
|
else do (setq current-sum (+ current-sum (parse-integer line)))
|
||||||
|
))
|
||||||
|
current-max ; i got a right answer
|
||||||
|
|
||||||
|
;; yay, now i can write a stateful function that gets one line at a time
|
||||||
|
;;
|
||||||
|
;; what i could probably do is have
|
||||||
|
;; if clauses directly in the loop
|
||||||
|
;; and additional variables defined and updated in the loop as well
|
||||||
|
|
||||||
|
(setq my-int (parse-integer "10497"))
|
||||||
|
my-int
|
||||||
|
|
||||||
|
;; let's wrap this into single function, i suppose
|
||||||
|
(defun calc-max-inventory-calories (filename)
|
||||||
|
(setq current-sum 0)
|
||||||
|
(setq current-max -1)
|
||||||
|
(with-open-file
|
||||||
|
(in filename)
|
||||||
|
(loop
|
||||||
|
for line = (read-line in nil nil)
|
||||||
|
while line
|
||||||
|
if (string-equal line "")
|
||||||
|
do (progn
|
||||||
|
;; current inventory ended,
|
||||||
|
;; check whether it's new max and null accum
|
||||||
|
(if (< current-max current-sum)
|
||||||
|
(setq current-max current-sum)
|
||||||
|
)
|
||||||
|
(setq current-sum 0))
|
||||||
|
else do (setq current-sum (+ current-sum (parse-integer line)))
|
||||||
|
))
|
||||||
|
current-max)
|
||||||
|
|
||||||
|
;; (in "day1-input.txt")
|
||||||
|
(calc-max-inventory-calories "day1-input.txt")
|
||||||
|
|
||||||
|
;;; and second task - find TOP 3 inventory sums, and what is their total
|
||||||
|
;; how'd i do that?
|
||||||
|
;; would be cool to do a class, that would be able to store the top 3
|
||||||
|
;; and would have single method to check and possibly include a new number with eviction of the lovest number
|
||||||
|
;; do I go for CLOS here?
|
||||||
|
;; ugh.
|
||||||
|
|
||||||
|
(setq my-prev-max (list 1 4 5 7))
|
||||||
|
(apply #'min my-prev-max)
|
||||||
|
(set-difference my-prev-max (list 1))
|
||||||
|
my-prev-max
|
||||||
|
(set-difference my-prev-max (list (apply #'min my-prev-max)))
|
||||||
|
|
||||||
|
;; so, i could use that on the "found max"
|
||||||
|
;; could also store "cur minmal maximul"
|
||||||
|
|
||||||
|
(defun calc-3-max-inventory-calories (filename)
|
||||||
|
(setq current-sum 0)
|
||||||
|
(setq current-3-max '())
|
||||||
|
(with-open-file
|
||||||
|
(in filename)
|
||||||
|
(loop
|
||||||
|
for line = (read-line in nil nil)
|
||||||
|
while line
|
||||||
|
if (string-equal line "")
|
||||||
|
do (progn
|
||||||
|
;; current inventory ended,
|
||||||
|
;; check whether it's new max and null accum
|
||||||
|
(setq current-3-max (cons current-sum current-3-max))
|
||||||
|
(if (< 3 (length current-3-max))
|
||||||
|
(setq current-3-max
|
||||||
|
(set-difference current-3-max (list (apply #'min current-3-max))))
|
||||||
|
)
|
||||||
|
(setq current-sum 0))
|
||||||
|
else do (setq current-sum (+ current-sum (parse-integer line)))
|
||||||
|
))
|
||||||
|
(apply #'+ current-3-max))
|
||||||
|
|
||||||
|
(append (list 12 1 14) 15)
|
||||||
|
(setq my-prepend (cons 15 (list 12 1 14)))
|
||||||
|
(apply #'+ (list 1 2 3))
|
||||||
|
|
||||||
|
(calc-3-max-inventory-calories "day1-input.txt")
|
||||||
|
|
||||||
|
current-3-max
|
||||||
|
(> 3 2)
|
||||||
|
|
||||||
|
;; ok. so notes for today:
|
||||||
|
;; 1. i'm messing up (> 3 1) in conditions
|
||||||
|
;; 2. I really should start using #'LET instead of #'SETQ
|
||||||
|
;; SETQ are for defining what? global names? these are set and available outside of the function
|
||||||
|
;; even though i didn't create them as DEFVAR? Or maybe I did, for the first function
|
||||||
|
;; ok then
|
||||||
|
;; 3. looking in Common Lisp Cookbook : https://lispcookbook.github.io/cl-cookbook/iteration.html
|
||||||
|
;; for the examples of the loops and set operations
|
||||||
|
;; and another tutorial with sample of reading the file https://riptutorial.com/common-lisp/example/10284/reading-file
|
||||||
|
;; and https://www.tutorialspoint.com/lisp/lisp_file_io.htm
|
||||||
|
;; and https://stackoverflow.com/questions/44466001/read-from-file-common-lisp
|
||||||
|
;; 4. Sad that I don't really look into the efficient ways of doing things
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,174 @@
|
||||||
|
;;;; https://adventofcode.com/2022/day/2
|
||||||
|
|
||||||
|
;; input is lines [A B C] [X Y Z]
|
||||||
|
;; meaning a-b-c what opponent will play
|
||||||
|
;; and x y z what i should play in rock-paper-scissors
|
||||||
|
;;
|
||||||
|
;; score is calculated as:
|
||||||
|
;; 1 for Rock X
|
||||||
|
;; 2 for Paper Y
|
||||||
|
;; 3 for Scissors Z
|
||||||
|
;;
|
||||||
|
;; 0 if Lost
|
||||||
|
;; 3 if Tie
|
||||||
|
;; 6 if Win
|
||||||
|
;;
|
||||||
|
;; Win if
|
||||||
|
;; (A Y) (B Z) (C X)
|
||||||
|
(setq MY-WIN-COMBINATIONS '((A Y) (B Z) (C X)))
|
||||||
|
;;
|
||||||
|
;; Loss if
|
||||||
|
;; (X B) (Y C) (Z A)
|
||||||
|
(setq MY-LOSS-COMBINATIONS '((B X) (C Y) (A Z)))
|
||||||
|
;;
|
||||||
|
;; otherwise it's a tie
|
||||||
|
(setq TIE-COMBINATIONS '((A X) (B Y) (C Z)))
|
||||||
|
|
||||||
|
;; now i'd also like to get symbol from the string...
|
||||||
|
(equal (intern "a") (intern "A")) ; not exactly what i means, but yeah this is it
|
||||||
|
|
||||||
|
;; now i'd like to get list of two symbols from string of two symbols
|
||||||
|
(setq my-test-line "A Z")
|
||||||
|
|
||||||
|
;; https://stackoverflow.com/questions/59516459/split-string-by-delimiter-and-include-delimiter-common-lisp
|
||||||
|
(require 'cl-ppcre)
|
||||||
|
(cl-ppcre:split "(\\.)" "a.bc.def.com" :with-registers-p t)
|
||||||
|
(cl-ppcre:split "(\\.)" "a.bc.def.com" :with-registers-p nil)
|
||||||
|
|
||||||
|
(setq my-test-line-list (mapcar #'intern (cl-ppcre:split "(\\ )" my-test-line :with-registers-p nil)))
|
||||||
|
;; yay
|
||||||
|
|
||||||
|
;;; now write scoring function over the lists of chars, for tie-win-loose match and for "my-selection-match"
|
||||||
|
;; sum the scores of the two parts of scoring
|
||||||
|
;; and iterate over input
|
||||||
|
;; would be cool to use pattern matching...
|
||||||
|
;; that's something like CASE in my
|
||||||
|
|
||||||
|
(case 1
|
||||||
|
((:in :in2) :lala)
|
||||||
|
((2 3) :numBig)
|
||||||
|
((1 -1) :numOne))
|
||||||
|
|
||||||
|
(case (list 1 2)
|
||||||
|
((:in :in2) :just-sym)
|
||||||
|
((list 1 2) :a-list))
|
||||||
|
;; this doesn't work with lists.
|
||||||
|
|
||||||
|
;; COND is similar to CASE, except it is more general. It accepts arbitrary
|
||||||
|
(find my-test-line-list my-win-combinations :TEST #'equal)
|
||||||
|
(find my-test-line-list my-loss-combinations :TEST #'equal)
|
||||||
|
|
||||||
|
(cond
|
||||||
|
((find my-test-line-list my-win-combinations :TEST #'equal) :WIN)
|
||||||
|
((find my-test-line-list my-loss-combinations :TEST #'equal) :LOSS)
|
||||||
|
(t :TIE))
|
||||||
|
|
||||||
|
(defun score-result (hands)
|
||||||
|
(cond
|
||||||
|
((find hands my-win-combinations :TEST #'equal)
|
||||||
|
6)
|
||||||
|
((find hands my-loss-combinations :TEST #'equal)
|
||||||
|
0)
|
||||||
|
(t 3)))
|
||||||
|
|
||||||
|
(score-result '(A Z))
|
||||||
|
(score-result '(A Y))
|
||||||
|
|
||||||
|
;;; now let's get score of the hands, by only my hand
|
||||||
|
(defun score-my-hand (hands)
|
||||||
|
(let ((my-hand (second hands)))
|
||||||
|
(case my-hand
|
||||||
|
(X 1)
|
||||||
|
(Y 2)
|
||||||
|
(z 3))))
|
||||||
|
|
||||||
|
(score-my-hand '(A Z))
|
||||||
|
(score-my-hand '(A Y))
|
||||||
|
(score-my-hand '(A X))
|
||||||
|
|
||||||
|
(defun full-score-hands (hands)
|
||||||
|
(+ (score-my-hand hands) (score-result hands)))
|
||||||
|
|
||||||
|
(full-score-hands '(A Y))
|
||||||
|
(full-score-hands '(B X))
|
||||||
|
(full-score-hands '(C Z))
|
||||||
|
|
||||||
|
;;; and now, iterate over file, converting strings into symbols and accumulating the score...
|
||||||
|
(require 'cl-ppcre)
|
||||||
|
(setq running-sum 0)
|
||||||
|
(let ((running-sum 0))
|
||||||
|
(with-open-file (in "~/Documents/personal/advent-of-code/2022/day2-input.txt")
|
||||||
|
(loop
|
||||||
|
for line = (read-line in nil nil)
|
||||||
|
while line
|
||||||
|
;; do (format t "line ~A~%" line)
|
||||||
|
do (let* ((line-word-list (cl-ppcre:split "(\\ )" line :with-registers-p nil))
|
||||||
|
(hands (mapcar #'intern line-word-list)))
|
||||||
|
(incf running-sum (full-score-hands hands)))
|
||||||
|
finally (return running-sum)
|
||||||
|
)))
|
||||||
|
|
||||||
|
;; (setq my-test-line-list (mapcar #'intern (cl-ppcre:split "(\\ )" my-test-line :with-registers-p nil)))
|
||||||
|
;; 12156 is the right answer!
|
||||||
|
;; let's wrap this into a function
|
||||||
|
(defun count-plays-score (filename)
|
||||||
|
(require 'cl-ppcre)
|
||||||
|
(let ((running-sum 0))
|
||||||
|
(with-open-file (in filename)
|
||||||
|
(loop
|
||||||
|
for line = (read-line in nil nil)
|
||||||
|
while line
|
||||||
|
do (let* ((line-word-list (cl-ppcre:split "(\\ )" line :with-registers-p nil))
|
||||||
|
(hands (mapcar #'intern line-word-list)))
|
||||||
|
(incf running-sum (full-score-hands hands)))
|
||||||
|
finally (return running-sum)
|
||||||
|
))))
|
||||||
|
(count-plays-score "~/Documents/personal/advent-of-code/2022/day2-input.txt")
|
||||||
|
|
||||||
|
;;; now for the Second part of the game
|
||||||
|
;; second part means something else!
|
||||||
|
;; X - need to loose
|
||||||
|
;; Y - need to draw
|
||||||
|
;; Z - need to win
|
||||||
|
;;
|
||||||
|
;; so, scoring is the same, but I need to calculate what is my hand
|
||||||
|
;; C Z means they play (SCISSORS) and i need to WIN, so I need to get ROCK
|
||||||
|
;; that goes into the calculation of the score
|
||||||
|
;; i would be able to reuse the scoring function,
|
||||||
|
;; but! i need a funtion that returns my hand
|
||||||
|
;; let's put it off for a bit?
|
||||||
|
;; or no. so. this would be best with what? ugh.
|
||||||
|
;; so Z - find hand from loose-hands that starts with required hand
|
||||||
|
;; Y - find from draw, Z - find from win
|
||||||
|
;; could use #'FIND with cusom :TEST - to compare only first item
|
||||||
|
|
||||||
|
(defun get-hand-set-by-result (hands)
|
||||||
|
(case (second hands)
|
||||||
|
(X my-loss-combinations)
|
||||||
|
(Y tie-combinations)
|
||||||
|
(Z my-win-combinations)))
|
||||||
|
|
||||||
|
(get-hand-set-by-result '(A Y))
|
||||||
|
(get-hand-set-by-result '(B X))
|
||||||
|
(get-hand-set-by-result '(C Z))
|
||||||
|
|
||||||
|
;; ok, somewhat like that
|
||||||
|
(find (first '(A Y))
|
||||||
|
(get-hand-set-by-result '(A Y)) :KEY (lambda (hands) (first hands)) )
|
||||||
|
|
||||||
|
(defun count-plays-score-2 (filename)
|
||||||
|
(require 'cl-ppcre)
|
||||||
|
(let ((running-sum 0))
|
||||||
|
(with-open-file (in filename)
|
||||||
|
(loop
|
||||||
|
for line = (read-line in nil nil)
|
||||||
|
while line
|
||||||
|
do (let* ((line-word-list (cl-ppcre:split "(\\ )" line :with-registers-p nil))
|
||||||
|
(hand-and-result (mapcar #'intern line-word-list))
|
||||||
|
(handset (get-hand-set-by-result hand-and-result))
|
||||||
|
(hands (find (first hand-and-result) handset :KEY (lambda (hands) (first hands)) )))
|
||||||
|
(incf running-sum (full-score-hands hands)))
|
||||||
|
finally (return running-sum)
|
||||||
|
))))
|
||||||
|
(count-plays-score-2 "~/Documents/personal/advent-of-code/2022/day2-input.txt")
|
||||||
|
;; 10835 is the right answer!
|
|
@ -0,0 +1,300 @@
|
||||||
|
GwrhJPDJCZFRcwfZWV
|
||||||
|
LjnQlqNpjjmpmQlLlqNfZRvQcTWcTSTTZcSQcZ
|
||||||
|
nNqjdspspngnmjmslqmjjjCDGrHPHMGddGCMCGPPPJWC
|
||||||
|
GwmVZmPWWFFmBbVbZVwmbPsTCnlgQgnQfhlffffZnlQh
|
||||||
|
DqVDSqqSMzLLDDNSHHLdqSdSllCQjsTlClhlflnTlhjgfgfM
|
||||||
|
VHJztNLHGtcbvvPG
|
||||||
|
bjrPrNCtNrjdcCPpptfpTVspDtfTtB
|
||||||
|
JGQJMJQMmmmZMnnLpLBTpHCD
|
||||||
|
WJJqWRgWlCJZhZRCQZwdPScdrPNbvzPzwvqz
|
||||||
|
QNSQNBWQNLjZBNNhLhSNRsTcsrTTVzcwZZZsfrrbwb
|
||||||
|
tCFtHpppppMldpvpqnMFmMVGrbPcrwbzswrzcccfvTfw
|
||||||
|
pdmCpgqCdmHHdJVWgSRNJDRVVj
|
||||||
|
sNrFnvNSzrjQtQjQTj
|
||||||
|
lcPmcJDLdPDbJPVLljdGGBBThBQTGwTtBw
|
||||||
|
PDLqmJmpJQfFqfqsCM
|
||||||
|
BnhctqdnqnRcBnslCJJCMrJrsG
|
||||||
|
wNDMZpbQwMpCvCGVjlss
|
||||||
|
WfzNwZFbwZzZmFZbLzNwzzzzcdqgRMTTPdHPTTPMRdcWgRPt
|
||||||
|
grsrVSFSSdFSDFVFjZZWwpWpZWZplgZZ
|
||||||
|
mcBPPPBLBfNdLlvvWljWJC
|
||||||
|
dMcmcRdbRzdVhFthSsTShM
|
||||||
|
bzvJZMTzTZSHLCCdDzmDcc
|
||||||
|
hqBqWPFssvshWvvssNqtsHftmfpHfdcdDGHmcpfctL
|
||||||
|
WvBQgNNNhghTJbJQlJTZlT
|
||||||
|
chcdwNwdbCbQctCjnnQGHsQspMHMjG
|
||||||
|
nSSSJqJZzJgWWRfZDJSnqvTTsVvvHVPpHVfpjHMTjP
|
||||||
|
BZRDRmmrDWSrZWWzWSRNhdnCFwChclFtwbNdtr
|
||||||
|
lNgmssCtqLwqCCtfsCLHPFhhhmMhVzBDbVzMDMVz
|
||||||
|
ZnRlQTlJzFQFQFVV
|
||||||
|
npZJvRRGZSnWvSvrSLglsClfpfcLgNgpHf
|
||||||
|
tVtqcVqFVtZhcfFtqlgSpmpDSDNPzSzZmNpw
|
||||||
|
LRGTHqbrHdnGHrTCSSwNDzMDwPMzNwbp
|
||||||
|
TqWGJrGHCHnTWnhsWcFthFjtfQch
|
||||||
|
qNnTbwtctvffFcqfrHjMrFjVHRjSjZDZ
|
||||||
|
dLLzWWPmCmCzGdsLgBLGGBDRMVMHRlrrrZDDZsNMrNNS
|
||||||
|
PJQWggCzWNWJzGWfchvfTbJvfnnwtf
|
||||||
|
ghzdgzzdQsdqzzhMNqQzvhgQnRRBWTjWWGTRGWwGTZhwGnBT
|
||||||
|
fsrfJHbFfDFLVLVFHrWCWrBRZZTGCCjwWZ
|
||||||
|
HLLllcDP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MPFcNNtMZDMW
|
||||||
|
VTGbzGGhTbTGHwVPvvFnfpvjgHnfjppp
|
||||||
|
JJwHqvlvDjljDwJFlZjZDwHNNsMqhNpphNpmNVzpsnsnRV
|
||||||
|
mTLgrLLcLSTTTdmPPfrrrnssNhRNWhgngzMWzgzVnM
|
||||||
|
SmTfdSBbBJbtjJvljl
|
||||||
|
bPNLwTCLLQQqtJsf
|
||||||
|
zdnnZVlWWGGRWGWdgdSStQMqJSMRptftbsMf
|
||||||
|
FWbvgvZZZZgnTmwrrhrFPCrP
|
||||||
|
HcGzzszFGllHWHbZspHbHGsHTwwrTrLLCNjSZwNjNjjCCNLj
|
||||||
|
PBJMJQJDDDnDggRhMdRSLmjTmTwwVjVQSvvwvC
|
||||||
|
RqfdhgDPDJDqJJnBdfzWWHcstslcbtStfHzl
|
||||||
|
zvRRlCqrdNdZcZpjBpVwjsmjsm
|
||||||
|
fgbTDqbhGfDnLDnLLqLhFmsHpTPHjHppppBwpwws
|
||||||
|
nhnnnDDngDtDbfSbDnGhhgRlNvQdQqNvQvtcQQNJRNJN
|
||||||
|
cZbCcbbScCbcmPGjPfSBQQSq
|
||||||
|
lnMnnVsMVvmzzGMDzPDf
|
||||||
|
LhrTsTTglrnsrrWWVvlwTnNtcpZRCmhtbCZFdttZbRCp
|
||||||
|
NWrFPZVWNVrvvrhtnNdddtpldmjm
|
||||||
|
DcBQBDsJbCwQnbtdzmjjjljbpjbz
|
||||||
|
qCDcGsDJGCcBDBcswJnBJQDfWfqgvZSvgZPfrVSWvPvZZZ
|
||||||
|
vcsdHdGtHtMHMFtVsddsWCcbppZwjScLpWhbjRWR
|
||||||
|
NTwrnzJrgTPrDwnlphRpjSpWbJJLLZWj
|
||||||
|
TlDPfPnzzlzTBzzvQFFBHMtVtqBqqw
|
||||||
|
NHnqqfZvZBNHHvgfrSlJrJCSllJRVrCn
|
||||||
|
TDTdhLMWjFcddMJPSSPJRmlCPz
|
||||||
|
bljWFdLLTDLtdFtLlwZvqfbg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GlhFtPPCqCCBFqZPQttlqFBq
|
||||||
|
wQRlwtBJBDwttJdGvLfBvHLLfTLz
|
||||||
|
MMmNZcMrcMFnRHzfjjvvHfvc
|
||||||
|
FggpbFnhrNNrrMrMbMbnhQVJVhstJwqWCVCRsQJQ
|
||||||
|
DQbCGblQlpQFQlHjCbjwDQQMggNmJmgnnpRBngfZmNgJMf
|
||||||
|
zvhWccWVdWBchdssPrrWZZZfmsmmmgsnZZJRsRTf
|
||||||
|
zBdtqPccWPHFCqCCqljq
|
||||||
|
ttrbRMmgtHgfmHSfBpLfnBBZBppB
|
||||||
|
CVTJDCCNPwCPDwcqzmddQZdTQdnLBQThWp
|
||||||
|
zwFDjwDJJPzjzVNcVJwCcbRHGmbbMrFHgHvrsgbblG
|
||||||
|
gZjjwHqHCzrMZVVR
|
||||||
|
hhzcdTzPrVhVCGMb
|
||||||
|
fPcmLPNffsccJDdNDjBnpwzmHqgWjHwwvg
|
||||||
|
SJQFSvQBlzbSCgdPPddPPPSN
|
||||||
|
pcrjcWLwwcHcgPNgTPLMNTCB
|
||||||
|
pRsjsWRnrpHRmrBrHrjlbJFvvzQFnzQblQDDbJ
|
||||||
|
VjQVMQPVMfVPPbGPHHbGJD
|
||||||
|
pcqSttltsbDGddsCJG
|
||||||
|
TSchqLtTLFhgQbMMQMrr
|
||||||
|
trqzMRwNTtDzLPJQgWmjmjrf
|
||||||
|
lbBQdpZbsmhGmZhmmG
|
||||||
|
llVbpCplvvHBBHpnRDcDRRqnRRQnFRzT
|
||||||
|
SLSSFFmzLShsVSSHnLnrJdbnRdZZbrRw
|
||||||
|
qCfWBftpNWNNlqvTpwrRbGGCnwGmgRJGZn
|
||||||
|
NcTBNpvWvBWpMftNffpqWlTpmzPDQPSzFVMsFQVhHsjHszss
|
||||||
|
VtJtNBRBGDpdpNbC
|
||||||
|
QgLncnttvFcwwhLvFjSGsSbmmQCSDdpCmpdG
|
||||||
|
vLgjLhhrctMvLFFjLtMTLMgfPZqBZPZzJBBfW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jHQRmHTgSH
|
||||||
|
FnBMBNZwZNcnDZMcnZlZgwgdQTTHjVJjHHVRQHJj
|
||||||
|
DnZrFCMZMNffrLPbLsfW
|
||||||
|
rJvmnBgnrCrGRSGNQR
|
||||||
|
hthjNfhwctwpjTLtVLjTGSpldSCGSPdlPSRzSqSz
|
||||||
|
TVcTfHNFcwtjMhTvgbHZsBbWmmZbnH
|
||||||
|
WsQgstQmvQJnssWsWPzhRzhBjZBSBRZSnj
|
||||||
|
qwCNqFwDrrlDrFPvRhTSPPzLRz
|
||||||
|
bppqwppCddlvfbDNVgmMmtMfVVmfmVWW
|
|
@ -0,0 +1,108 @@
|
||||||
|
;; rucksack has 2 compartments
|
||||||
|
;; items have types
|
||||||
|
;; for each type there's 1 compartment where the item must go
|
||||||
|
;; elf put exactly 1 item into wrong compartment for each rucksack
|
||||||
|
;; item type is identified by letters a .. z A .. Z case sensitive
|
||||||
|
;; first half exactly is items in first compartment, second half of chars - items in second compartment
|
||||||
|
|
||||||
|
;;; find item type that appears in both compartments for each rucksack
|
||||||
|
;; translate then item type into priority a .. z -> 1 .. 26 ; A .. Z -> 27 .. 52
|
||||||
|
;; return sum of all priorities
|
||||||
|
|
||||||
|
;;; ok. how to start solving that?
|
||||||
|
|
||||||
|
(setq test-line "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL")
|
||||||
|
;; i need to find char that appears in the first half and in the second half.
|
||||||
|
;; i could split the line in two, convert to list and use set diff function
|
||||||
|
(setq half-length (/ (length test-line) 2))
|
||||||
|
(intersection
|
||||||
|
(coerce (subseq test-line 0 half-length) 'list)
|
||||||
|
(coerce (subseq test-line half-length) 'list))
|
||||||
|
|
||||||
|
;; and now char to int,
|
||||||
|
(- (char-int #\a) (- (char-int #\a) 1))
|
||||||
|
(- (char-int #\z) (- (char-int #\a) 1))
|
||||||
|
(- (char-int #\A) (- (char-int #\A) 27))
|
||||||
|
(- (char-int #\Z) (- (char-int #\A) 27))
|
||||||
|
(lower-case-p #\z)
|
||||||
|
(lower-case-p #\A)
|
||||||
|
|
||||||
|
(defun get-char-priority (ch)
|
||||||
|
(if (lower-case-p ch) (- (char-int ch) (- (char-int #\a) 1))
|
||||||
|
(- (char-int ch) (- (char-int #\A) 27))))
|
||||||
|
(get-char-priority #\a)
|
||||||
|
(get-char-priority #\z)
|
||||||
|
(get-char-priority #\A)
|
||||||
|
(get-char-priority #\L)
|
||||||
|
(get-char-priority #\Z)
|
||||||
|
|
||||||
|
(let* ((test-line "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL")
|
||||||
|
(first-compartment (coerce (subseq test-line 0 half-length) 'list))
|
||||||
|
(second-compartment (coerce (subseq test-line half-length) 'list))
|
||||||
|
(the-misplaced-item-type (intersection first-compartment second-compartment)))
|
||||||
|
(get-char-priority (car the-misplaced-item-type))) ; in real life I'd need more defensiveness here, CAR woudn't be guaranteed
|
||||||
|
|
||||||
|
(defun get-rucksack-misplaced-item-priority (rucksack-as-string)
|
||||||
|
(let* ((test-line rucksack-as-string)
|
||||||
|
(half-length (/ (length test-line) 2))
|
||||||
|
(first-compartment (coerce (subseq test-line 0 half-length)
|
||||||
|
'list))
|
||||||
|
(second-compartment (coerce (subseq test-line half-length)
|
||||||
|
'list))
|
||||||
|
(the-misplaced-item-type (intersection first-compartment second-compartment)))
|
||||||
|
(get-char-priority (car the-misplaced-item-type)))
|
||||||
|
; in real life I'd need more defensiveness here, CAR woudn't be guaranteed
|
||||||
|
)
|
||||||
|
|
||||||
|
(get-rucksack-misplaced-item-priority "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL")
|
||||||
|
(get-rucksack-misplaced-item-priority "PmmdzqPrVvPwwTWBwg")
|
||||||
|
;; again I'm making mistakes by referring to the globally visible symbols inside of the functions. so bad.
|
||||||
|
;; maybe i'd want to use something like "make-symbol" as recommended in the part on macro?
|
||||||
|
|
||||||
|
;; now the funciton seems to work, need to iterate over input file, call it for each line and sum
|
||||||
|
(defun count-priories-in-file (filename)
|
||||||
|
(let ((running-sum 0))
|
||||||
|
(with-open-file (in filename)
|
||||||
|
(loop
|
||||||
|
for line = (read-line in nil nil)
|
||||||
|
while line
|
||||||
|
do (incf running-sum
|
||||||
|
(get-rucksack-misplaced-item-priority line))
|
||||||
|
finally (return running-sum)))))
|
||||||
|
(count-priories-in-file "day3-input.txt")
|
||||||
|
(count-priories-in-file "day3-test-input.txt")
|
||||||
|
|
||||||
|
;;; so, now different task for same input:
|
||||||
|
;; considering lines in groups of 3, what is their common char (group identification badge)
|
||||||
|
;; then map to priorities and sum
|
||||||
|
;; that should be a very similar program.
|
||||||
|
;; but how can i configure loop to give me 3 lines at a time?
|
||||||
|
|
||||||
|
(defun get-three-rucksacks-id-badge (r1 r2 r3)
|
||||||
|
(let* ((r1 (coerce r1 'list))
|
||||||
|
(r2 (coerce r2 'list))
|
||||||
|
(r3 (coerce r3 'list))
|
||||||
|
(badge-type-char (intersection (intersection r1 r2) r3)))
|
||||||
|
(get-char-priority (car badge-type-char)))
|
||||||
|
; in real life I'd need more defensiveness here, CAR woudn't be guaranteed
|
||||||
|
)
|
||||||
|
|
||||||
|
(get-three-rucksacks-id-badge "vJrwpWtwJgWrhcsFMMfFFhFp" "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL" "PmmdzqPrVvPwwTWBwg")
|
||||||
|
|
||||||
|
(defun count-id-badges-in-file (filename)
|
||||||
|
(let ((running-sum 0))
|
||||||
|
(with-open-file (in filename)
|
||||||
|
(loop
|
||||||
|
for line1 = (read-line in nil nil)
|
||||||
|
for line2 = (read-line in nil nil)
|
||||||
|
for line3 = (read-line in nil nil)
|
||||||
|
while line1
|
||||||
|
do (incf running-sum
|
||||||
|
(get-three-rucksacks-id-badge line1 line2 line3))
|
||||||
|
finally (return running-sum)))))
|
||||||
|
(count-id-badges-in-file "day3-test-input.txt")
|
||||||
|
(count-id-badges-in-file "day3-input.txt")
|
||||||
|
|
||||||
|
;; surely there's a better way to use loop?
|
||||||
|
;; or maybe a good introduction into how to use it?
|
||||||
|
;; the documentation site doesn't give examples =C
|
|
@ -0,0 +1,6 @@
|
||||||
|
vJrwpWtwJgWrhcsFMMfFFhFp
|
||||||
|
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
|
||||||
|
PmmdzqPrVvPwwTWBwg
|
||||||
|
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
|
||||||
|
ttgJtRGJQctTZtZT
|
||||||
|
CrZsJsPPZsGzwwsLwLmpwMDw
|
|
@ -0,0 +1,71 @@
|
||||||
|
;;; https://adventofcode.com/2022/day/4
|
||||||
|
|
||||||
|
;; so, just read two intervals.
|
||||||
|
;; intervals , separated, two numbers - separated.
|
||||||
|
;; so. ugh. have a parsing function. maybe ask how to better parse
|
||||||
|
;; then to calculate "total inclusion" - can go both ways, so 4 checks
|
||||||
|
|
||||||
|
(setq test-line "2-4,6-8")
|
||||||
|
(setq test-line "2-8,3-7")
|
||||||
|
|
||||||
|
(require 'cl-ppcre)
|
||||||
|
;; (cl-ppcre:split "(\\ )" line :with-registers-p nil)
|
||||||
|
(setq intervals (mapcar #'parse-integer (cl-ppcre:split "(,|-)" test-line)))
|
||||||
|
|
||||||
|
(defun line-to-nums (line)
|
||||||
|
(mapcar #'parse-integer (cl-ppcre:split "(,|-)" line)))
|
||||||
|
|
||||||
|
(defun first-includes-second (l1 r1 l2 r2)
|
||||||
|
(and (<= l1 l2) (>= r1 r2)))
|
||||||
|
|
||||||
|
(first-includes-second 2 8 3 7)
|
||||||
|
(first-includes-second 3 8 3 7)
|
||||||
|
(first-includes-second 4 8 3 7)
|
||||||
|
|
||||||
|
(defun first-or-last-includes-another (l1 r1 l2 r2)
|
||||||
|
(or (first-includes-second l1 r1 l2 r2)
|
||||||
|
(first-includes-second l2 r2 l1 r1)))
|
||||||
|
|
||||||
|
(with-open-file (in-file "day4-test-input.txt")
|
||||||
|
(let ((running-count 0))
|
||||||
|
(loop
|
||||||
|
for line = (read-line in-file nil nil)
|
||||||
|
while line
|
||||||
|
do (if (apply #'first-or-last-includes-another (line-to-nums line))
|
||||||
|
(incf running-count 1))
|
||||||
|
finally (return running-count))))
|
||||||
|
|
||||||
|
(defun count-full-interlaps (filename)
|
||||||
|
(with-open-file (in-file filename)
|
||||||
|
(let ((running-count 0))
|
||||||
|
(loop
|
||||||
|
for line = (read-line in-file nil nil)
|
||||||
|
while line
|
||||||
|
do (if (apply #'first-or-last-includes-another (line-to-nums line))
|
||||||
|
(incf running-count 1))
|
||||||
|
finally (return running-count)))))
|
||||||
|
|
||||||
|
(count-full-interlaps "day4-test-input.txt")
|
||||||
|
(count-full-interlaps "day4-input.txt")
|
||||||
|
|
||||||
|
;; next part - count intervals that "overlap at all", even if by single point
|
||||||
|
;; could count 'not overlapping' - should be very easy
|
||||||
|
;; and count total, and deduct
|
||||||
|
|
||||||
|
(defun count-any-overlap (filename)
|
||||||
|
(with-open-file (in-file filename)
|
||||||
|
(let ((running-count-non-overlap 0)
|
||||||
|
(total-count 0))
|
||||||
|
(loop
|
||||||
|
for line = (read-line in-file nil nil)
|
||||||
|
while line
|
||||||
|
do (let ((nums (line-to-nums line)))
|
||||||
|
(if (or (< (second nums) (third nums))
|
||||||
|
(< (fourth nums) (first nums)))
|
||||||
|
(incf running-count-non-overlap 1))
|
||||||
|
(incf total-count 1)
|
||||||
|
)
|
||||||
|
finally (return (- total-count running-count-non-overlap))))))
|
||||||
|
|
||||||
|
(count-any-overlap "day4-test-input.txt")
|
||||||
|
(count-any-overlap "day4-input.txt")
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,6 @@
|
||||||
|
2-4,6-8
|
||||||
|
2-3,4-5
|
||||||
|
5-7,7-9
|
||||||
|
2-8,3-7
|
||||||
|
6-6,4-6
|
||||||
|
2-6,4-8
|
Loading…
Reference in New Issue