Initial commit

This commit is contained in:
Dimitri Lozeve 2024-11-12 21:43:32 +01:00
commit f242d2b0df
420 changed files with 62521 additions and 0 deletions

5
2019/day02/day02.dyalog Normal file
View file

@ -0,0 +1,5 @@
⎕io←0
p←⍎¨','(≠⊆⊢)⊃⊃⎕nget'input.txt'1
f←{i←0 ⊣ s[1 2]←⍵ ⊣ s←p ⋄ ⊃s⊣{i+←4 ⊣ a x y z←s[i+4] ⋄ s[z]←(a-1)⌷s[x](+,×)s[y]}⍣{99=i⌷s}0}
f 12 2 ⍝ part 1
nv←1+,99 99 ⋄ +/100 1×⊃nv[(19690720=f¨nv)1] ⍝ part 2

32
2019/day02/day02.lisp Normal file
View file

@ -0,0 +1,32 @@
(ql:quickload "str")
(defparameter *input-file* #P"input.txt")
(defparameter *input* (uiop:read-file-string *input-file*))
(defun execute (program)
(loop for i from 0 by 4 until (= 99 (aref program i))
do (let ((opcode (aref program i)))
(cond ((= opcode 1)
(setf (aref program (aref program (+ 3 i)))
(+ (aref program (aref program (+ 1 i)))
(aref program (aref program (+ 2 i))))))
((= opcode 2)
(setf (aref program (aref program (+ 3 i)))
(* (aref program (aref program (+ 1 i)))
(aref program (aref program (+ 2 i)))))))))
program)
(defun execute-with-inputs (program-string noun verb)
(let ((program (map 'vector #'parse-integer (str:split "," program-string))))
(setf (aref program 1) noun)
(setf (aref program 2) verb)
(aref (execute program) 0)))
(defun part1 (program-string)
(execute-with-inputs program-string 12 2))
(defun part2 (program-string)
(dotimes (noun 99)
(dotimes (verb 99)
(when (= 19690720 (execute-with-inputs program-string noun verb))
(return-from part2 (+ (* 100 noun) verb))))))

34
2019/day02/day02.rkt Normal file
View file

@ -0,0 +1,34 @@
#lang racket/base
(require "../intcode.rkt"
racket/vector)
(module+ test
(require rackunit))
(define (part1 filename)
(define program (parse-file filename))
(vector-set! program 1 12)
(vector-set! program 2 2)
(define vm (execute (start-machine program '())))
(vector-ref (machine-program vm) 0))
(module+ test
(check-equal? (part1 "input.txt") 6627023))
(define (try-inputs program noun verb)
(define my-program (vector-copy program))
(vector-set! my-program 1 noun)
(vector-set! my-program 2 verb)
(define vm (execute (start-machine my-program '())))
(vector-ref (machine-program vm) 0))
(define (part2 filename)
(define program (parse-file filename))
(for*/first ([noun (in-range 100)]
[verb (in-range 100)]
#:when (eq? 19690720 (try-inputs program noun verb)))
(+ (* 100 noun) verb)))
(module+ test
(check-equal? (part2 "input.txt") 4019))

1
2019/day02/input.txt Normal file
View file

@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,9,1,19,1,9,19,23,1,23,5,27,2,27,10,31,1,6,31,35,1,6,35,39,2,9,39,43,1,6,43,47,1,47,5,51,1,51,13,55,1,55,13,59,1,59,5,63,2,63,6,67,1,5,67,71,1,71,13,75,1,10,75,79,2,79,6,83,2,9,83,87,1,5,87,91,1,91,5,95,2,9,95,99,1,6,99,103,1,9,103,107,2,9,107,111,1,111,6,115,2,9,115,119,1,119,6,123,1,123,9,127,2,127,13,131,1,131,9,135,1,10,135,139,2,139,10,143,1,143,5,147,2,147,6,151,1,151,5,155,1,2,155,159,1,6,159,0,99,2,0,14,0