summary refs log tree commit diff stats
path: root/day5.scm
blob: 3c35310f5ad682efc113e346399b2c59f46e56f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env gosh
(use file.util)

(define (read-input filename)
  (map string->number (string-split (file->string filename) ",")))

(define (run-opcode vec pos)
  (letrec ((instruction (vector-ref vec pos))
           (opcode (mod instruction 100))
           (mode1 (= 1 (div (mod instruction 1000) 100)))
           (mode2 (= 1 (div (mod instruction 10000) 1000)))
           (mode3 (= 1 (div instruction 10000)))
           (parameter 
            (lambda (x)
              (cond
                ((= x 1)
                  (if mode1 
                    (vector-ref vec (+ pos 1))
                    (vector-ref vec (vector-ref vec (+ pos 1)))))
                ((= x 2)
                  (if mode2
                    (vector-ref vec (+ pos 2))
                    (vector-ref vec (vector-ref vec (+ pos 2)))))
                ((= x 3)
                  (if mode3
                    (vector-ref vec (+ pos 3))
                    (vector-ref vec (vector-ref vec (+ pos 3)))))))))
    (print "opcode: " opcode)
    (print "real params: " 
      (vector-ref vec (+ pos 1)) " " 
      (vector-ref vec (+ pos 2)) " " 
      (vector-ref vec (+ pos 3)))
    (print "resolved params: "
      (parameter 1) " "
      (parameter 2) " "
      (parameter 3))
    (print vec)
    (cond
      ((= opcode 1)
        (vector-set! 
          vec
          (vector-ref vec (+ pos 3)) 
          (+ (parameter 1) 
             (parameter 2)))
        (run-opcode vec (+ pos 4)))
      ((= opcode 2)
        (vector-set! 
          vec 
          (vector-ref vec (+ pos 3))
          (* (parameter 1)
             (parameter 2)))
        (run-opcode vec (+ pos 4)))
      ((= opcode 3)
        (vector-set!
          vec
          (vector-ref vec (+ pos 1))
          (string->number (read-line)))
        (run-opcode vec (+ pos 2)))
      ((= opcode 4)
        (print (parameter 1))
        (run-opcode vec (+ pos 2)))
      ((= opcode 5) 
        (if (= (parameter 1) 0)
          (run-opcode vec (+ pos 3))
          (run-opcode vec (parameter 2))))
      ((= opcode 6)
        (if (= (parameter 1) 0)
          (run-opcode vec (parameter 2))
          (run-opcode vec (+ pos 3))))
      ((= opcode 7)
        (if (< (parameter 1) (parameter 2))
          (vector-set! vec (parameter 3) 1)
          (vector-set! vec (parameter 3) 0))
        (run-opcode vec (+ pos 4)))
      ((= opcode 8)
        (if (= (parameter 1) (parameter 2))
          (vector-set! vec (parameter 3) 1)
          (vector-set! vec (parameter 3) 0))
        (run-opcode vec (+ pos 4)))
      ((= opcode 99)
        vec)
      (else (print "this shouldn't happen")))))

(define (part1 input)
  (run-opcode input 0))

(define (main args)
  (let ((input (read-input "inputs/day5test2.txt")))
    (part1 (list->vector input))))