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

61
2019/day05/day05.lisp Normal file
View file

@ -0,0 +1,61 @@
(defparameter *input-file* #P"input.txt")
(defparameter *input* (uiop:read-file-string *input-file*))
(defvar *program*)
(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 (inputs)
(defun execute-instruction (pc inputs outputs)
(ecase (opcode pc)
(99 (return-from execute-instruction outputs))
(1 (setf (parameter pc 3) (+ (parameter pc 1) (parameter pc 2)))
(execute-instruction (+ pc 4) inputs outputs))
(2 (setf (parameter pc 3) (* (parameter pc 1) (parameter pc 2)))
(execute-instruction (+ pc 4) inputs outputs))
(3 (setf (parameter pc 1) (car inputs))
(execute-instruction (+ pc 2) (cdr inputs) outputs))
(4 (execute-instruction (+ pc 2) inputs (cons (parameter pc 1) outputs)))
(5 (if (/= 0 (parameter pc 1))
(execute-instruction (parameter pc 2) inputs outputs)
(execute-instruction (+ pc 3) inputs outputs)))
(6 (if (= 0 (parameter pc 1))
(execute-instruction (parameter pc 2) inputs outputs)
(execute-instruction (+ pc 3) inputs outputs)))
(7 (if (< (parameter pc 1) (parameter pc 2))
(setf (parameter pc 3) 1)
(setf (parameter pc 3) 0))
(execute-instruction (+ pc 4) inputs outputs))
(8 (if (= (parameter pc 1) (parameter pc 2))
(setf (parameter pc 3) 1)
(setf (parameter pc 3) 0))
(execute-instruction (+ pc 4) inputs outputs))))
(execute-instruction 0 inputs nil))
(defun parse-program (program-string)
(map 'vector #'parse-integer (uiop:split-string program-string :separator ",")))
(defun part1 ()
(let ((*program* (parse-program *input*)))
(car (execute '(1)))))
(defun part2 ()
(let ((*program* (parse-program *input*)))
(car (execute '(5)))))
(assert (= (part1) 12896948))
(assert (= (part2) 7704130))

22
2019/day05/day05.rkt Normal file
View file

@ -0,0 +1,22 @@
#lang racket/base
(require "../intcode.rkt")
(module+ test
(require rackunit))
(define (part1 filename)
(define program (parse-file filename))
(define vm (execute (start-machine program '(1))))
(car (machine-outputs vm)))
(module+ test
(check-equal? (part1 "input.txt") 12896948))
(define (part2 filename)
(define program (parse-file filename))
(define vm (execute (start-machine program '(5))))
(car (machine-outputs vm)))
(module+ test
(check-equal? (part2 "input.txt") 7704130))

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

@ -0,0 +1 @@
3,225,1,225,6,6,1100,1,238,225,104,0,1102,46,47,225,2,122,130,224,101,-1998,224,224,4,224,1002,223,8,223,1001,224,6,224,1,224,223,223,1102,61,51,225,102,32,92,224,101,-800,224,224,4,224,1002,223,8,223,1001,224,1,224,1,223,224,223,1101,61,64,225,1001,118,25,224,101,-106,224,224,4,224,1002,223,8,223,101,1,224,224,1,224,223,223,1102,33,25,225,1102,73,67,224,101,-4891,224,224,4,224,1002,223,8,223,1001,224,4,224,1,224,223,223,1101,14,81,225,1102,17,74,225,1102,52,67,225,1101,94,27,225,101,71,39,224,101,-132,224,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,1002,14,38,224,101,-1786,224,224,4,224,102,8,223,223,1001,224,2,224,1,223,224,223,1,65,126,224,1001,224,-128,224,4,224,1002,223,8,223,101,6,224,224,1,224,223,223,1101,81,40,224,1001,224,-121,224,4,224,102,8,223,223,101,4,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1008,677,226,224,1002,223,2,223,1005,224,329,1001,223,1,223,107,677,677,224,102,2,223,223,1005,224,344,101,1,223,223,1107,677,677,224,102,2,223,223,1005,224,359,1001,223,1,223,1108,226,226,224,1002,223,2,223,1006,224,374,101,1,223,223,107,226,226,224,1002,223,2,223,1005,224,389,1001,223,1,223,108,226,226,224,1002,223,2,223,1005,224,404,1001,223,1,223,1008,677,677,224,1002,223,2,223,1006,224,419,1001,223,1,223,1107,677,226,224,102,2,223,223,1005,224,434,1001,223,1,223,108,226,677,224,102,2,223,223,1006,224,449,1001,223,1,223,8,677,226,224,102,2,223,223,1006,224,464,1001,223,1,223,1007,677,226,224,1002,223,2,223,1006,224,479,1001,223,1,223,1007,677,677,224,1002,223,2,223,1005,224,494,1001,223,1,223,1107,226,677,224,1002,223,2,223,1006,224,509,101,1,223,223,1108,226,677,224,102,2,223,223,1005,224,524,1001,223,1,223,7,226,226,224,102,2,223,223,1005,224,539,1001,223,1,223,8,677,677,224,1002,223,2,223,1005,224,554,101,1,223,223,107,677,226,224,102,2,223,223,1006,224,569,1001,223,1,223,7,226,677,224,1002,223,2,223,1005,224,584,1001,223,1,223,1008,226,226,224,1002,223,2,223,1006,224,599,101,1,223,223,1108,677,226,224,102,2,223,223,1006,224,614,101,1,223,223,7,677,226,224,102,2,223,223,1005,224,629,1001,223,1,223,8,226,677,224,1002,223,2,223,1006,224,644,101,1,223,223,1007,226,226,224,102,2,223,223,1005,224,659,101,1,223,223,108,677,677,224,1002,223,2,223,1006,224,674,1001,223,1,223,4,223,99,226