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

87
2019/day07/day07.lisp Normal file
View file

@ -0,0 +1,87 @@
(ql:quickload :alexandria)
(defparameter *input-file* #P"input.txt")
(defvar *input*)
(defvar *program*)
(defvar *input-fn*)
(defvar *output-fn*)
(defun opcode (pc)
(mod (aref *program* pc) 100))
(defun parameter-mode (pc index)
(mod (truncate (aref *program* pc) (expt 10 (+ 1 index))) 10))
(defun parameter (pc index)
(ecase (parameter-mode pc index)
(1 (aref *program* (+ index pc)))
(0 (aref *program* (aref *program* (+ pc index))))))
(defun (setf parameter) (new-value pc index)
(ecase (parameter-mode pc index)
(0 (setf (aref *program* (aref *program* (+ pc index))) new-value))
(1 (error "Cannot write with a parameter in immediate mode"))))
(defun execute-instruction (pc)
(ecase (opcode pc)
(99 (return-from execute-instruction))
(1 (setf (parameter pc 3) (+ (parameter pc 1) (parameter pc 2)))
(execute-instruction (+ pc 4)))
(2 (setf (parameter pc 3) (* (parameter pc 1) (parameter pc 2)))
(execute-instruction (+ pc 4)))
(3 (setf (parameter pc 1) (funcall *input-fn*))
(execute-instruction (+ pc 2)))
(4 (funcall *output-fn* (parameter pc 1))
(execute-instruction (+ pc 2)))
(5 (if (/= 0 (parameter pc 1))
(execute-instruction (parameter pc 2))
(execute-instruction (+ pc 3))))
(6 (if (= 0 (parameter pc 1))
(execute-instruction (parameter pc 2))
(execute-instruction (+ pc 3))))
(7 (if (< (parameter pc 1) (parameter pc 2))
(setf (parameter pc 3) 1)
(setf (parameter pc 3) 0))
(execute-instruction (+ pc 4)))
(8 (if (= (parameter pc 1) (parameter pc 2))
(setf (parameter pc 3) 1)
(setf (parameter pc 3) 0))
(execute-instruction (+ pc 4)))))
(defun execute (program &key (input 'input) (output 'output))
(let ((*program* program)
(*input-fn* input)
(*output-fn* output))
(execute-instruction 0)))
(defun parse-program (program-string)
(map 'vector #'parse-integer (uiop:split-string program-string :separator ",")))
(defun amplify (phases)
(let ((amp-input 0))
(loop for phase in phases
do (let ((program (alexandria:copy-array *program*))
(inputs (list phase amp-input))
(output nil))
(execute program :input (lambda () (pop inputs)) :output (lambda (x) (setf output x)))
(setf amp-input output)))
amp-input))
(defun max-thrust ()
(let ((res nil))
(alexandria:map-permutations (lambda (x) (push (amplify x) res)) '(0 1 2 3 4))
(apply #'max res)))
(assert (= 43210 (let ((*program* (parse-program "3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0")))
(max-thrust))))
(assert (= 54321 (let ((*program* (parse-program "3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0")))
(max-thrust))))
(assert (= 65210 (let ((*program* (parse-program "3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0")))
(max-thrust))))
(defun part1 ()
(let ((*program* (parse-program (uiop:read-file-string *input-file*))))
(max-thrust)))

51
2019/day07/day07.rkt Normal file
View file

@ -0,0 +1,51 @@
#lang racket/base
(require "../intcode.rkt"
racket/list)
(module+ test
(require rackunit))
(define (part1 program)
(apply max
(for/list ([phases (in-permutations (range 5))])
(for/fold ([signal 0])
([phase (in-list phases)])
(define vm (execute (start-machine program (list phase signal))))
(car (machine-outputs vm))))))
(module+ test
(check-equal? (part1 (parse "3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0")) 43210)
(check-equal? (part1 (parse "3,23,3,24,1002,24,10,24,1002,23,-1,23,
101,5,23,23,1,24,23,23,4,23,99,0,0")) 54321)
(check-equal? (part1 (parse "3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,
1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0")) 65210)
(check-equal? (part1 (parse-file "input.txt")) 298586))
(define (part2 program)
(define signals
(for/list ([phases (in-permutations (range 5 10))])
(define amplifiers
(for/list ([phase (in-list phases)])
(define vm (execute (start-machine program (list phase))))
vm))
(let loop ([signal 0]
[amplifiers amplifiers])
(if (empty? amplifiers)
signal
(let* ([amp (machine-program (car amplifiers))]
[pc (machine-pc (car amplifiers))]
[new-vm (execute (machine amp (list signal) pc 0 #f '()))]
[new-amplifiers (if (machine-terminated new-vm)
(cdr amplifiers)
(append (cdr amplifiers) (list new-vm)))])
(loop (car (machine-outputs new-vm)) new-amplifiers))))))
(apply max signals))
(module+ test
(check-equal? (part2 (parse "3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,
27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5")) 139629729)
(check-equal? (part2 (parse "3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54,
-5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4,
53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10")) 18216)
(check-equal? (part2 (parse-file "input.txt")) 9246095))

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

@ -0,0 +1 @@
3,8,1001,8,10,8,105,1,0,0,21,42,67,84,109,126,207,288,369,450,99999,3,9,102,4,9,9,1001,9,4,9,102,2,9,9,101,2,9,9,4,9,99,3,9,1001,9,5,9,1002,9,5,9,1001,9,5,9,1002,9,5,9,101,5,9,9,4,9,99,3,9,101,5,9,9,1002,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,2,9,102,4,9,9,101,2,9,9,102,4,9,9,1001,9,2,9,4,9,99,3,9,102,2,9,9,101,5,9,9,1002,9,2,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,99