aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/10/input
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2023-12-15 20:10:17 +0100
committerOscar Najera <hi@oscarnajera.com>2023-12-15 20:10:17 +0100
commit435987a86bfe924f0e9e504074aa8637a8b02b1c (patch)
tree1502ceb6d9853993de6de4d9c643a29cd9e99868 /AoC2022/10/input
parent2d8ad6ddcce3421b3a94805e568c02b644989927 (diff)
downloadscratch-435987a86bfe924f0e9e504074aa8637a8b02b1c.tar.gz
scratch-435987a86bfe924f0e9e504074aa8637a8b02b1c.tar.bz2
scratch-435987a86bfe924f0e9e504074aa8637a8b02b1c.zip
solve part 2 with cache
Diffstat (limited to 'AoC2022/10/input')
0 files changed, 0 insertions, 0 deletions
.highlight .gs { color: #FF5370 } /* Generic.Strong */ .highlight .gu { color: #89DDFF } /* Generic.Subheading */ .highlight .gt { color: #FF5370 } /* Generic.Traceback */ .highlight .kc { color: #89DDFF } /* Keyword.Constant */ .highlight .kd { color: #BB80B3 } /* Keyword.Declaration */ .highlight .kn { color: #89DDFF; font-style: italic } /* Keyword.Namespace */ .highlight .kp { color: #89DDFF } /* Keyword.Pseudo */ .highlight .kr { color: #BB80B3 } /* Keyword.Reserved */ .highlight .kt { color: #BB80B3 } /* Keyword.Type */ .highlight .ld { color: #C3E88D } /* Literal.Date */ .highlight .m { color: #F78C6C } /* Literal.Number */ .highlight .s { color: #C3E88D } /* Literal.String */ .highlight .na { color: #BB80B3 } /* Name.Attribute */ .highlight .nb { color: #82AAFF } /* Name.Builtin */ .highlight .nc { color: #FFCB6B } /* Name.Class */ .highlight .no { color: #EEFFFF } /* Name.Constant */ .highlight .nd { color: #82AAFF } /* Name.Decorator */ .highlight .ni { color: #89DDFF } /* Name.Entity */ .highlight .ne { color: #FFCB6B } /* Name.Exception */ .highlight .nf { color: #82AAFF } /* Name.Function */ .highlight .nl { color: #82AAFF } /* Name.Label */ .highlight .nn { color: #FFCB6B } /* Name.Namespace */ .highlight .nx { color: #EEFFFF } /* Name.Other */ .highlight .py { color: #FFCB6B } /* Name.Property */ .highlight .nt { color: #FF5370 } /* Name.Tag */ .highlight .nv { color: #89DDFF } /* Name.Variable */ .highlight .ow { color: #89DDFF; font-style: italic } /* Operator.Word */ .highlight .pm { color: #89DDFF } /* Punctuation.Marker */ .highlight .w { color: #EEFFFF } /* Text.Whitespace */ .highlight .mb { color: #F78C6C } /* Literal.Number.Bin */ .highlight .mf { color: #F78C6C } /* Literal.Number.Float */ .highlight .mh { color: #F78C6C } /* Literal.Number.Hex */ .highlight .mi { color: #F78C6C } /* Literal.Number.Integer */ .highlight .mo { color: #F78C6C } /* Literal.Number.Oct */ .highlight .sa { color: #BB80B3 } /* Literal.String.Affix */ .highlight .sb { color: #C3E88D } /* Literal.String.Backtick */ .highlight .sc { color: #C3E88D } /* Literal.String.Char */ .highlight .dl { color: #EEFFFF } /* Literal.String.Delimiter */ .highlight .sd { color: #546E7A; font-style: italic } /* Literal.String.Doc */ .highlight .s2 { color: #C3E88D } /* Literal.String.Double */ .highlight .se { color: #EEFFFF } /* Literal.String.Escape */ .highlight .sh { color: #C3E88D } /* Literal.String.Heredoc */ .highlight .si { color: #89DDFF } /* Literal.String.Interpol */ .highlight .sx { color: #C3E88D } /* Literal.String.Other */ .highlight .sr { color: #89DDFF } /* Literal.String.Regex */ .highlight .s1 { color: #C3E88D } /* Literal.String.Single */ .highlight .ss { color: #89DDFF } /* Literal.String.Symbol */ .highlight .bp { color: #89DDFF } /* Name.Builtin.Pseudo */ .highlight .fm { color: #82AAFF } /* Name.Function.Magic */ .highlight .vc { color: #89DDFF } /* Name.Variable.Class */ .highlight .vg { color: #89DDFF } /* Name.Variable.Global */ .highlight .vi { color: #89DDFF } /* Name.Variable.Instance */ .highlight .vm { color: #82AAFF } /* Name.Variable.Magic */ .highlight .il { color: #F78C6C } /* Literal.Number.Integer.Long */
(ql:quickload :fiveam)

(defun recover-item (bitfield-int)
  (loop for i from 0
        when (logbitp i bitfield-int) return i))

(defun priority (value)
  (if (< value 31) ;; It is a capital letter in range [1;26]
      (+ value 26)
    (- value 32)))

(defun line-priority (str)
  (let ((mid (/ (length str) 2))
        (left-pack 0) (right-pack 0))
    (loop with cross = 0
          for l across (subseq str 0 mid)
          for r across (subseq str mid)
          do (progn
               (setf left-pack (logior left-pack (ash 1 (- (char-code l) 64))))
               (setf right-pack (logior right-pack (ash 1 (- (char-code r) 64))))
               (setf cross (logand left-pack right-pack)))
          when (< 0 cross) return (priority (recover-item cross)))))


(defun prio ()
  (with-open-file (in "input")
    (loop :for l = (read-line in nil nil)
          :while l
          :sum (line-priority l))))

(defun pick-badge (str)
  (loop with stack = 0
        for i across str
        do (setf stack (logior stack (ash 1 (- (char-code i) 64))))
        finally (return stack)))

(defun badge ()
  (with-open-file (in "input")
    (loop :for b = (loop repeat 3 for l = (read-line in nil nil) while l collect (pick-badge l))
          :while b
          :sum (priority (recover-item (apply #'logand b))))))

;; (pick-badge "LdHVLDLDdHdtLMhcqCqGWcWg" )

(fiveam:test intermediate
             (fiveam:is (= 12 (recover-item 4096)))
             (fiveam:is (= 20 (line-priority
                               "gzCjffWZCtCfZZVdqVSqJdvJndSt"))))

(fiveam:test results
  (fiveam:is (= 8072 (prio)))
  (fiveam:is (= 2567 (badge))))