Day 9

Previous: Day 08 | Next: Day 10
###AUTO-ACQUIRED DATA FOLLOWS…
archivist/day09.lisp
archivist/day09.lisp
(asdf:load-system :split-sequence)
    (asdf:load-system :iterate)
    (defpackage \#:aoc-9
      (:use \#:cl \#:split-sequence \#:iterate))
    (in-package \#:aoc-9)
    
    (defun getinput ()
        (mapcar \#&\#39;parse-integer 
          (uiop:read-file-lines "input")))
    
    (defun split (vlist index)
        (values (subseq vlist 0 index) 
                (nthcdr index vlist)))
    
    (defun seek (l val)
        (iterate (while l)
          (let ((a (pop l))) 
            (if 
              (iterate (for b in l)
                (if (eq val (+ a b))
                    (return t)))
              (return t)))))
    
    (defun part1 ()
        (let\* ((all (getinput)))
          (multiple-value-bind 
            (preamble remain) 
            (split all 25)
            (iterate (while remain)
              (let\* ((val (pop remain))
                    (s (remove-if (lambda (x)
                                    (> x val)) 
                                  (sort (copy-seq preamble) \#&\#39;<))))
                (if (not (seek s val))
                    (return val))
                (pop preamble)
                (nconc preamble (list val)))))))
    
    (defun part2 ()
        (let\* ((all-in (getinput))
               (all (make-array (length all-in)
                                      :initial-contents
                                      all-in))
               (val (part1)))
          (iterate (for i from 0 to (1- (length all)))
            (let ((ret 
                (iterate (for j from (1+ i) to (1- (length all)))
                  (let ((s (subseq all i (1+ j))))
                     (if (eq val
                            (reduce \#&\#39;+
                                    s))
                        (progn 
                          (return (+ (reduce \#&\#39;min s)
                                     (reduce \#&\#39;max s)))))))))
              (if ret (return ret))))))
    
day-9/julia-day09.jl
day-9/julia-day09.jl
using DataStructures: CircularBuffer, Queue
    using DelimitedFiles: readdlm
    
    function main()
        data = readdlm("day9input", &\#39;\n&\#39;, Int)
        silver = gold = 0
        preamble = CircularBuffer{Int}(25); append!(preamble, data[1:25])
        for toconsider in Iterators.rest(data, 26)
            if !any(x->((toconsider-x  preamble) && (2x != toconsider)), preamble)
                silver = toconsider
                break
            end
            push!(preamble, toconsider)
        end
    
        subsumlist = Queue{Int}()
        subsum = 0
        for input in data
            enqueue!(subsumlist, input)
            subsum += input
            while (subsum > silver)
                subsum -= dequeue!(subsumlist)
            end
            if (subsum == silver) && (length(subsumlist) > 1)
                gold = maximum(subsumlist) + minimum(subsumlist)
                break
            end
        end
        @show (silver, gold)
    end
    
    @time main()
    
javanon/day09.js
javanon/day09.js
dataset = document.querySelector("body > pre");
    numbers = dataset.innerText.trim().split("\n").map(val => Number(val));
    
    function day9\_partA (input, preamble) {
        var values = [];
    
        for (var i = preamble; i < input.length; i++) {
            if (!input.slice(i - preamble, i).some( (item, idx, arr) => arr.includes(input[i] - item))) {
                values.push(input[i]);
            }
        }
        
        return values;
    }
    
    function day9\_partB (input, value) {
        var currentSum = ceil = floor = 0;
        while (value - currentSum) {
            if (currentSum < value) {
                currentSum += input[ceil++];
            } else if (currentSum > value) {
                currentSum -= input[floor++];
            }
        }
        var valueRange = input.slice(floor, ceil);
        return (Math.max(...valueRange) + Math.min(...valueRange));
    }
    
steveklabnik/steveklabnik-day09.rs
steveklabnik/steveklabnik-day09.rs
fn solve(input: &str) -> (u64, u64) {
      let numbers: Vec<\_> = input.lines().map(|s| s.parse().unwrap()).collect();
    
      let a = numbers
          .windows(26)
          .map(|w| (&w[..25], w[25]))
          .find(|&(preamble, target)| {
              let mut iter = preamble.iter().copied();
    
              while let Some(a) = iter.next() {
                  if iter.clone().any(|b| a + b == target) {
                      return false;
                  }
              }
    
              true
          })
          .map(|(\_, n)| n)
          .unwrap();
    
      let mut stack = VecDeque::new();
      let mut sum = 0;
    
      numbers
          .iter()
          .find(|&&n| {
              stack.push\_back(n);
              sum += n;
    
              while sum > a {
                  sum -= stack.pop\_front().unwrap();
              }
    
              sum == a
          })
          .unwrap();
    
      let b = stack
          .iter()
          .fold((!0, 0), |(a, b), &n| (a.min(n), b.max(n)));
    
      (a, b.0 + b.1)
    }
    

Tags: