Skip to content

Commit 84cc6e8

Browse files
Initial checkin of advent source
0 parents  commit 84cc6e8

File tree

7 files changed

+2165
-0
lines changed

7 files changed

+2165
-0
lines changed

day1.el

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(defun fuel-required (mass)
2+
(- (floor (/ mass 3)) 2))
3+
4+
(defconst masses '(132709 102150 126463 85035 77219 86458 119251
5+
121098 118730 122505 127964 68004 55833 77664 142865 124503
6+
115892 87236 122743 127096 94893 62129 56520 117000 81519 121719
7+
96291 96556 79006 137122 124340 125151 51603 50132 67568 132599
8+
149009 60997 99382 96506 57269 118133 115119 126208 101098 60514
9+
146171 70314 76473 51209 99190 57647 126985 142055 99615 146442
10+
129520 145334 57799 87148 118362 80407 106449 57146 129035 60156
11+
120016 147383 68819 83868 81021 131594 137692 86537 110709 127678
12+
106849 137640 108482 131412 70331 90118 117557 117347 84688
13+
108869 145359 127024 100976 90419 53362 106100 129474 56101 99975
14+
79211 99865 121099 74511 123172))
15+
16+
(apply '+ (mapcar 'fuel-required masses))
17+
; => 3393938
18+
19+
(defun fuel-required2 (mass)
20+
(let ((fuel (- (floor (/ mass 3)) 2)))
21+
(if (> fuel 0)
22+
(+ fuel (fuel-required2 fuel))
23+
0)))
24+
25+
(apply '+ (mapcar 'fuel-required2 masses))
26+
; => 5088037

day2.el

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
(defun program-to-list (program-string)
3+
(mapcar 'string-to-number (split-string program-string ",")))
4+
5+
(defvar program-input "1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,6,19,23,1,10,23,27,2,27,13,31,1,31,6,35,2,6,35,39,1,39,5,43,1,6,43,47,2,6,47,51,1,51,5,55,2,55,9,59,1,6,59,63,1,9,63,67,1,67,10,71,2,9,71,75,1,6,75,79,1,5,79,83,2,83,10,87,1,87,5,91,1,91,9,95,1,6,95,99,2,99,10,103,1,103,5,107,2,107,6,111,1,111,5,115,1,9,115,119,2,119,10,123,1,6,123,127,2,13,127,131,1,131,6,135,1,135,10,139,1,13,139,143,1,143,13,147,1,5,147,151,1,151,2,155,1,155,5,0,99,2,0,14,0")
6+
7+
(defun execute-program (program)
8+
(defun op (program pc f)
9+
(let ((out-pos (nth (+ pc 3) program)))
10+
(setf (nth out-pos program)
11+
(apply f (list (nth (nth (+ pc 1) program) program)
12+
(nth (nth (+ pc 2) program) program))))
13+
program))
14+
(defun ex (program pc)
15+
(let ((inst (nth pc program)))
16+
(cond ((= inst 1)
17+
(ex (op program pc '+) (+ 4 pc)))
18+
((= inst 2)
19+
(ex (op program pc '*) (+ 4 pc)))
20+
((= inst 99)
21+
program))))
22+
(ex program 0))
23+
24+
; (execute-program (program-to-list "1,0,0,0,99"))
25+
; (execute-program (program-to-list "2,3,0,3,99"))
26+
; (execute-program (program-to-list "1,1,1,4,99,5,6,0,99"))
27+
28+
29+
(execute-program
30+
(let ((program-list (program-to-list program-input)))
31+
(setf (nth 1 program-list) 12)
32+
(setf (nth 2 program-list) 2)
33+
program-list))
34+
; => (6327510 12 2 2 1 1 2 3 1 3 4 3 ...)
35+
36+
37+
(defun test-run (noun verb)
38+
(nth 0
39+
(execute-program
40+
(let ((program-list (program-to-list program-input)))
41+
(setf (nth 1 program-list) noun)
42+
(setf (nth 2 program-list) verb)
43+
program-list))))
44+
45+
(defconst target 19690720)
46+
47+
; couldn't get cl-loop to work :(
48+
49+
; (cl-loop for noun from 0 to 5
50+
; for result = (cl-loop for verb from 0 to 5
51+
; until (= (test-run noun verb) target)
52+
; finally return verb)
53+
; until (not (= verb 100))
54+
; finally return (list noun verb))
55+
56+
(defvar noun)
57+
(defvar verb)
58+
59+
;; apparently no TRO in elsip, so am using global state
60+
61+
(defun test-run-while ()
62+
(while (not (= (test-run noun verb) target))
63+
(setq noun (+ noun 1))
64+
(cond ((> noun 99)
65+
(setq noun 0)
66+
(setq verb (+ verb 1))))
67+
(if (> verb 99)
68+
"no solution found"))
69+
(+ (* noun 100) verb))
70+
71+
(progn
72+
(setq noun 0)
73+
(setq verb 0)
74+
(test-run-while))
75+
; => 4112

day3.el

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
;;; -*- lexical-binding: t -*-
2+
3+
(defun wire-to-list (program-string)
4+
(mapcar '(lambda (segment)
5+
(cons (aref segment 0)
6+
(string-to-number (substring segment 1))))
7+
(split-string program-string ",")))
8+
9+
(defconst wire1 (wire-to-list "R75,D30,R83,U83,L12,D49,R71,U7,L72"))
10+
(defconst wire2 (wire-to-list "U62,R66,U55,R34,D71,R55,D58,R83"))
11+
12+
;(defconst wire1 (wire-to-list "R8,U5,L5,D3"))
13+
;(defconst wire2 (wire-to-list "U7,R6,D4,L4"))
14+
15+
;;; alternative approach -- store each position as (x . y)
16+
17+
(defun create-segments (wire segment-map)
18+
(defun create-points (x y new-x new-y)
19+
(if (= new-x x)
20+
(mapcar #'(lambda (y) (cons x y))
21+
(number-sequence y new-y (if (< y new-y) 1 -1)))
22+
(mapcar #'(lambda (x) (cons x y))
23+
(number-sequence x new-x (if (< x new-x) 1 -1)))))
24+
(defun node-to-segment (node)
25+
(let* ((op (car node))
26+
(val (cdr node))
27+
(new-x (+ global-x (* (cond
28+
((= op ?L) -1)
29+
((= op ?R) 1)
30+
(t 0))
31+
val)))
32+
(new-y (+ global-y (* (cond
33+
((= op ?D) -1)
34+
((= op ?U) 1)
35+
(t 0))
36+
val))))
37+
(setq firstp t)
38+
(mapc #'(lambda (point)
39+
(if firstp
40+
;; don't count the first node in a wire segment
41+
(setq firstp nil)
42+
(progn
43+
(setq global-step (1+ global-step))
44+
(if (not (gethash point segment-map))
45+
(puthash point global-step segment-map)))))
46+
(create-points global-x global-y new-x new-y))
47+
(setq global-x new-x)
48+
(setq global-y new-y)))
49+
(setq global-x 0)
50+
(setq global-y 0)
51+
(setq global-step 0)
52+
(mapc 'node-to-segment wire))
53+
54+
(defconst map1 (make-hash-table :test 'equal))
55+
(defconst map2 (make-hash-table :test 'equal))
56+
57+
(create-segments (wire-to-list "R8,U5,L5,D3") map1)
58+
(create-segments (wire-to-list "U7,R6,D4,L4") map2)
59+
60+
(defun wire-steps (wire1 wire2)
61+
(let ((map1 (make-hash-table :test 'equal))
62+
(map2 (make-hash-table :test 'equal)))
63+
(create-segments wire1 map1)
64+
(create-segments wire2 map2)
65+
(sort (loop
66+
for node being the hash-keys of map1
67+
if (gethash node map2)
68+
collect (+ (gethash node map1) (gethash node map2)) into found
69+
finally return found)
70+
'<)))
71+
72+
(wire-steps (wire-to-list "R8,U5,L5,D3") (wire-to-list "U7,R6,D4,L4"))
73+
(wire-steps (wire-to-list "R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51") (wire-to-list "U98,R91,D20,R16,D67,R40,U7,R15,U6,R7"))
74+
;; => 410
75+
(wire-steps (wire-to-list "R75,D30,R83,U83,L12,D49,R71,U7,L72") (wire-to-list "U62,R66,U55,R34,D71,R55,D58,R83"))
76+
77+
;; big wires..
78+
79+
(defconst big-wire1 "R1009,U34,L600,U800,R387,D247,R76,U797,R79,D582,L325,D236,R287,U799,R760,U2,L261,D965,R854,D901,R527,D998,R247,U835,L29,U525,L10,D351,L599,D653,L39,D112,R579,D650,L539,D974,R290,U729,L117,D112,L926,U270,L158,D800,L291,U710,L28,D211,R700,U691,L488,D307,R448,U527,L9,D950,L535,D281,L683,U576,L372,U849,R485,D237,L691,U453,L667,U856,R832,U956,L47,D951,R171,U484,R651,D731,L768,D44,R292,U107,L237,U731,L795,D460,R781,U77,L316,U873,L994,D322,L479,U121,R754,U68,L454,D162,L308,D986,L893,D808,R929,D328,L591,D718,R616,U139,R221,U124,R477,U614,L439,D329,R217,D157,L65,D460,R523,U955,R512,D458,L823,D975,R506,D870,R176,U558,R935,U319,L281,D470,L285,U639,L974,U186,L874,U487,L979,D95,R988,U398,R776,D637,R75,U331,R746,D603,R102,U978,R702,U89,L48,D757,L173,D422,L394,U800,R955,U644,R911,D327,R471,D313,L982,D93,R998,U549,R210,D640,R332,U566,R736,U302,L69,U677,L137,U674,R204,D720,R868,U143,L635,D177,L277,D749,R180,D432,R451,D426,R559,U964,L35,U452,L848,D707,R758,D41,R889,D966,R460,U11,R819,D30,L953,U150,L621,U915,R400,D723,R299,D93,L987,D790,L541,U864,R711,D968,L2,D963,L996,D260,L824,D765,L617,U257,R175,U786,L873,D118,L433,U246,R821,D308,L468,U53,R859,U806,L197,D663,R540,D84,L398,D945,L999,U114,L731,D676,L538,U680,R519,U313,R699,U746,R471,D393,L902,U697,R542,D385,R183,U463,R276,U990,R111,U709,R726,D996,L728,D215,R726,D911,L199,D484,R282,U129,L329,U309,L270,U990,L813,U242,L353,D741,R447,D253,L556,U487,L102,D747,L965,D743,R768,U589,R657,D910,L760,D981,L982,D292,R730,U236,L831")
80+
81+
(defconst big-wire2 "L1000,U720,R111,D390,L400,U931,R961,D366,L172,D434,R514,D185,L555,D91,R644,U693,L902,U833,L28,U136,R204,D897,L18,D601,R855,U409,R567,U57,L561,D598,R399,D238,R37,U478,R792,D587,R740,D647,L593,U576,L662,U389,R540,U359,R547,D449,R518,D747,L887,U421,R153,D365,L357,U495,L374,D27,L338,D57,R431,U796,L487,D480,L273,U662,R874,D29,R596,D166,R167,D788,R175,D395,L739,U180,R145,U824,L156,D387,R427,U167,R268,D653,L371,D467,L216,U23,L930,D494,L76,U338,R813,U373,R237,D1,R706,U37,R202,D187,L905,D431,R787,D391,R576,D370,R320,U225,L901,D921,R656,U517,R782,D965,L849,U241,L160,U792,L587,U408,L750,U21,R317,U919,L449,D691,L895,D853,L547,D178,R793,D921,L873,D962,L232,U690,L815,U309,R455,U156,L200,U34,L761,U402,R278,U952,L294,D183,R475,U770,L375,D117,R58,D905,L580,U240,R263,U549,R771,U512,L20,D996,L265,U619,L742,U754,L68,D824,R694,D678,R412,D321,R611,U325,L874,U776,L907,U39,R568,D485,R528,D197,R487,D920,R879,D935,R107,U897,L263,D979,L420,U498,L757,D348,L279,U266,R699,D729,R65,U672,L945,U780,L339,U324,R927,U357,R324,U435,R602,D245,L456,D161,L537,U740,R454,U211,L952,D356,L317,U456,L6,D718,L389,D554,L366,D141,R543,U756,R334,U209,L207,U726,R375,U59,L238,D118,L514,D390,R212,U272,L350,U898,L105,U514,L591,U839,L767,U651,R298,U726,L429,U350,L53,U789,R9,D295,L558,U9,L515,D177,L430,U158,L959,U601,L994,U635,L252,D159,R155,U601,L809,D5,R47,U567,R328,U559,R149,U43,L612,U428,R694,D568,L80,U80,R983,D143,R612,U735,L10,D697,L640,D788,R714,U555,L139,U396,L830,D825,R928,D25,L889,U973,L343")
82+
83+
84+
(wire-steps (wire-to-list big-wire1) (wire-to-list big-wire2))
85+
;; => (15612 16068 16184 16470 16848 16848 16958 18442 18442 21716 21936 24932 ...)

day4.el

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
;;; -*- lexical-binding: t -*-
2+
3+
(defun make-pass (number)
4+
(apply 'vector (mapcar 'string-to-number (split-string (number-to-string number) "" t))))
5+
6+
(defun valid-pass (number)
7+
(defun at-least-one-duplicate (pass)
8+
(let ((end (- (length pass) 2)))
9+
(loop
10+
for i from 0 to end
11+
if (and (= (elt pass i) (elt pass (1+ i)))
12+
;; not after
13+
(or (= i end)
14+
(not (= (elt pass i) (elt pass (+ i 2)))))
15+
;; not before
16+
(or (= i 0)
17+
(not (= (elt pass i) (elt pass (1- i))))))
18+
return t
19+
finally return nil)))
20+
(defun in-sequential-order (pass)
21+
(loop
22+
for i from 0 to (- (length pass) 2)
23+
if (> (elt pass i) (elt pass (1+ i))) return nil
24+
finally return t))
25+
(let ((pass (make-pass number)))
26+
(and (in-sequential-order pass)
27+
(at-least-one-duplicate pass))))
28+
29+
(defun count-pass (low high)
30+
(loop
31+
for pass from low to high
32+
count (valid-pass pass)))
33+
34+
(count-pass 271973 785961)
35+
;; part2 607
36+
37+
;; part1 925

day5.el

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
(defun program-to-list (program-string)
3+
(mapcar 'string-to-number (split-string program-string ",")))
4+
5+
(defvar program-input "1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,6,19,23,1,10,23,27,2,27,13,31,1,31,6,35,2,6,35,39,1,39,5,43,1,6,43,47,2,6,47,51,1,51,5,55,2,55,9,59,1,6,59,63,1,9,63,67,1,67,10,71,2,9,71,75,1,6,75,79,1,5,79,83,2,83,10,87,1,87,5,91,1,91,9,95,1,6,95,99,2,99,10,103,1,103,5,107,2,107,6,111,1,111,5,115,1,9,115,119,2,119,10,123,1,6,123,127,2,13,127,131,1,131,6,135,1,135,10,139,1,13,139,143,1,143,13,147,1,5,147,151,1,151,2,155,1,155,5,0,99,2,0,14,0")
6+
7+
(defconst program2 "3,225,1,225,6,6,1100,1,238,225,104,0,1001,152,55,224,1001,224,-68,224,4,224,1002,223,8,223,1001,224,4,224,1,224,223,223,1101,62,41,225,1101,83,71,225,102,59,147,224,101,-944,224,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,2,40,139,224,1001,224,-3905,224,4,224,1002,223,8,223,101,7,224,224,1,223,224,223,1101,6,94,224,101,-100,224,224,4,224,1002,223,8,223,101,6,224,224,1,224,223,223,1102,75,30,225,1102,70,44,224,101,-3080,224,224,4,224,1002,223,8,223,1001,224,4,224,1,223,224,223,1101,55,20,225,1102,55,16,225,1102,13,94,225,1102,16,55,225,1102,13,13,225,1,109,143,224,101,-88,224,224,4,224,1002,223,8,223,1001,224,2,224,1,223,224,223,1002,136,57,224,101,-1140,224,224,4,224,1002,223,8,223,101,6,224,224,1,223,224,223,101,76,35,224,1001,224,-138,224,4,224,1002,223,8,223,101,5,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,677,224,1002,223,2,223,1006,224,329,1001,223,1,223,8,677,226,224,102,2,223,223,1006,224,344,101,1,223,223,1107,226,226,224,1002,223,2,223,1006,224,359,1001,223,1,223,1108,677,226,224,102,2,223,223,1005,224,374,1001,223,1,223,1007,226,226,224,102,2,223,223,1006,224,389,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,404,1001,223,1,223,1007,677,677,224,102,2,223,223,1005,224,419,1001,223,1,223,8,226,677,224,102,2,223,223,1005,224,434,101,1,223,223,1008,677,226,224,102,2,223,223,1006,224,449,1001,223,1,223,7,677,677,224,102,2,223,223,1006,224,464,1001,223,1,223,8,226,226,224,1002,223,2,223,1005,224,479,1001,223,1,223,7,226,677,224,102,2,223,223,1006,224,494,1001,223,1,223,7,677,226,224,1002,223,2,223,1005,224,509,101,1,223,223,107,677,677,224,102,2,223,223,1006,224,524,101,1,223,223,1007,677,226,224,102,2,223,223,1006,224,539,101,1,223,223,107,226,226,224,1002,223,2,223,1006,224,554,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,569,1001,223,1,223,1107,677,226,224,1002,223,2,223,1005,224,584,101,1,223,223,1107,226,677,224,102,2,223,223,1005,224,599,101,1,223,223,1108,226,677,224,102,2,223,223,1005,224,614,101,1,223,223,108,677,226,224,102,2,223,223,1005,224,629,101,1,223,223,107,226,677,224,102,2,223,223,1006,224,644,1001,223,1,223,1108,226,226,224,1002,223,2,223,1006,224,659,101,1,223,223,108,226,226,224,102,2,223,223,1005,224,674,101,1,223,223,4,223,99,226")
8+
9+
(defun decode-inst (inst-raw)
10+
(let ((inst (mod inst-raw 100))
11+
(op1 (mod (/ inst-raw 100) 10))
12+
(op2 (mod (/ inst-raw 1000) 10))
13+
(op3 (mod (/ inst-raw 10000) 10))
14+
)
15+
(list inst op1 op2 op3)))
16+
17+
(defun get-operand (val mode program)
18+
(if (and (numberp val) (= mode 0))
19+
(nth val program)
20+
val))
21+
22+
(defun prompt-user ()
23+
(interactive)
24+
(read-number "Enter code: "))
25+
26+
(defun execute-program (program)
27+
(defun op (program pc f arg1 arg2)
28+
(let ((out-pos (nth (+ pc 3) program)))
29+
(setf (nth out-pos program)
30+
(apply f (list arg1 arg2)))
31+
program))
32+
(defun op-test (program pc f arg1 arg2)
33+
(let ((out-pos (nth (+ pc 3) program)))
34+
(setf (nth out-pos program)
35+
(if (apply f (list arg1 arg2))
36+
1
37+
0))
38+
program))
39+
(defun op-input (program pc)
40+
(let ((out-pos (nth (+ pc 1) program))
41+
(val (prompt-user)))
42+
(setf (nth out-pos program) val)
43+
program))
44+
(defun op-print (program pc arg1)
45+
(print arg1)
46+
program)
47+
(defun ex (program pc)
48+
(let* ((inst-parsed (decode-inst (nth pc program)))
49+
(inst (car inst-parsed))
50+
(arg1 (get-operand (nth (+ pc 1) program) (cadr inst-parsed) program))
51+
(arg2 (get-operand (nth (+ pc 2) program) (caddr inst-parsed) program))
52+
)
53+
(cond ((= inst 1)
54+
(ex (op program pc '+ arg1 arg2) (+ 4 pc)))
55+
((= inst 2)
56+
(ex (op program pc '* arg1 arg2) (+ 4 pc)))
57+
((= inst 3)
58+
(ex (op-input program pc) (+ 2 pc)))
59+
((= inst 4)
60+
(ex (op-print program pc arg1) (+ 2 pc)))
61+
((= inst 5)
62+
(ex program
63+
(if (not (= 0 arg1))
64+
arg2
65+
(+ 3 pc))))
66+
((= inst 6)
67+
(ex program
68+
(if (= 0 arg1)
69+
arg2
70+
(+ 3 pc))))
71+
((= inst 7)
72+
(ex (op-test program pc '< arg1 arg2) (+ 4 pc)))
73+
((= inst 8)
74+
(ex (op-test program pc '= arg1 arg2) (+ 4 pc)))
75+
((= inst 99)
76+
program)
77+
(t (concat "Unknown instruction: " (number-to-string inst) " at pc " (number-to-string pc))))))
78+
(ex (program-to-list program) 0))
79+
80+
(execute-program program2)
81+
;; 1409363
82+
;; => (314 225 1 225 6 6 1105 1 238 225 104 0 ...)
83+
84+
;; 9431221
85+
(3 225 1 225 6 6 1101 1 238 225 104 0 ...)
86+
87+
(execute-program "104,1234,99")
88+
(execute-program "1002,4,3,4,33,99")
89+
(execute-program "1101,100,-1,4,0,99")
90+
(execute-program "3,0,4,0,99")
91+
;; part2
92+
(execute-program "3,9,8,9,10,9,4,9,99,-1,8")
93+
94+
(execute-program "3,9,7,9,10,9,4,9,99,-1,8")
95+
(execute-program "3,3,1108,-1,8,3,4,3,99")
96+
(execute-program "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9")
97+
98+
(execute-program "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99")

0 commit comments

Comments
 (0)