-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.p
148 lines (117 loc) · 6.52 KB
/
day2.p
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
--- Day 2: 1202 Program Alarm ---
On the way to your gravity assist around the Moon, your ship computer beeps angrily about a "1202 program alarm". On the radio, an Elf is already explaining how to handle the situation: "Don't worry, that's perfectly norma--" The ship computer bursts into flames.
You notify the Elves that the computer's magic smoke seems to have escaped. "That computer ran Intcode programs like the gravity assist program it was working on; surely there are enough spare parts up there to build a new Intcode computer!"
An Intcode program is a list of integers separated by commas (like 1,0,0,3,99). To run one, start by looking at the first integer (called position 0). Here, you will find an opcode - either 1, 2, or 99. The opcode indicates what to do; for example, 99 means that the program is finished and should immediately halt. Encountering an unknown opcode means something went wrong.
Opcode 1 adds together numbers read from two positions and stores the result in a third position. The three integers immediately after the opcode tell you these three positions - the first two indicate the positions from which you should read the input values, and the third indicates the position at which the output should be stored.
For example, if your Intcode computer encounters 1,10,20,30, it should read the values at positions 10 and 20, add those values, and then overwrite the value at position 30 with their sum.
Opcode 2 works exactly like opcode 1, except it multiplies the two inputs instead of adding them. Again, the three integers after the opcode indicate where the inputs and outputs are, not their values.
Once you're done processing an opcode, move to the next one by stepping forward 4 positions.
For example, suppose you have the following program:
1,9,10,3,2,3,11,0,99,30,40,50
For the purposes of illustration, here is the same program split into multiple lines:
1,9,10,3,
2,3,11,0,
99,
30,40,50
The first four integers, 1,9,10,3, are at positions 0, 1, 2, and 3. Together, they represent the first opcode (1, addition), the positions of the two inputs (9 and 10), and the position of the output (3). To handle this opcode, you first need to get the values at the input positions: position 9 contains 30, and position 10 contains 40. Add these numbers together to get 70. Then, store this value at the output position; here, the output position (3) is at position 3, so it overwrites itself. Afterward, the program looks like this:
1,9,10,70,
2,3,11,0,
99,
30,40,50
Step forward 4 positions to reach the next opcode, 2. This opcode works just like the previous, but it multiplies instead of adding. The inputs are at positions 3 and 11; these positions contain 70 and 50 respectively. Multiplying these produces 3500; this is stored at position 0:
3500,9,10,70,
2,3,11,0,
99,
30,40,50
Stepping forward 4 more positions arrives at opcode 99, halting the program.
Here are the initial and final states of a few more small programs:
1,0,0,0,99 becomes 2,0,0,0,99 (1 + 1 = 2).
2,3,0,3,99 becomes 2,3,0,6,99 (3 * 2 = 6).
2,4,4,5,99,0 becomes 2,4,4,5,99,9801 (99 * 99 = 9801).
1,1,1,4,99,5,6,0,99 becomes 30,1,1,4,2,5,6,0,99.
Once you have a working computer, the first step is to restore the gravity assist program (your puzzle input) to the "1202 program alarm" state it had just before the last computer caught fire. To do this, before running the program, replace position 1 with the value 12 and replace position 2 with the value 2. What value is left at position 0 after the program halts?
*/
DEFINE TEMP-TABLE ttRAM
FIELD iAddress AS INTEGER
FIELD iValue AS INTEGER
INDEX ix IS PRIMARY UNIQUE iAddress.
DEFINE BUFFER ttOpcode FOR ttRam.
DEFINE BUFFER ttValue1 FOR ttRam.
DEFINE BUFFER ttValue2 FOR ttRam.
DEFINE BUFFER ttAddress FOR ttRam.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE cMemory AS CHARACTER NO-UNDO.
DEFINE VARIABLE iPC AS INTEGER NO-UNDO.
DEFINE VARIABLE iMaxAddress AS INTEGER NO-UNDO.
INPUT FROM C:\User\JCCARDOT\Perso\Travail\aoc\aoc2019\day2.txt.
IMPORT cMemory.
INPUT CLOSE.
iMaxAddress = NUM-ENTRIES(cMemory) - 1.
DO i = iMaxAddress + 1 TO 1 BY -1:
CREATE ttRAM.
ASSIGN
ttRam.iAddress = i - 1
ttRam.iValue = INTEGER(ENTRY(i, cMemory)).
END.
RELEASE ttRam.
/* replace values */
FIND ttRam WHERE ttRam.iAddress = 1. ttRam.iValue = 12.
FIND ttRam WHERE ttRam.iAddress = 2. ttRam.iValue = 2.
/* run the program */
iPC = 0.
blkProgram:
DO WHILE TRUE:
FIND ttOpcode WHERE ttOpcode.iAddress = iPC.
CASE ttOpcode.iValue:
WHEN 1 THEN DO:
FIND ttRam WHERE ttRam.iAddress = iPC + 1.
FIND ttValue1 WHERE ttValue1.iAddress = ttRam.iValue.
FIND ttRam WHERE ttRam.iAddress = iPC + 2.
FIND ttValue2 WHERE ttValue2.iAddress = ttRam.iValue.
FIND ttAddress WHERE ttAddress.iAddress = iPC + 3.
FIND ttRam WHERE ttRam.iAddress = ttAddress.iValue.
ttRam.iValue = ttValue1.iValue + ttValue2.iValue.
iPC = iPC + 4.
END.
WHEN 2 THEN DO:
FIND ttRam WHERE ttRam.iAddress = iPC + 1.
FIND ttValue1 WHERE ttValue1.iAddress = ttRam.iValue.
FIND ttRam WHERE ttRam.iAddress = iPC + 2.
FIND ttValue2 WHERE ttValue2.iAddress = ttRam.iValue.
FIND ttAddress WHERE ttAddress.iAddress = iPC + 3.
FIND ttRam WHERE ttRam.iAddress = ttAddress.iValue.
ttRam.iValue = ttValue1.iValue * ttValue2.iValue.
iPC = iPC + 4.
END.
WHEN 99 THEN
LEAVE blkProgram.
OTHERWISE DO:
MESSAGE SUBSTITUTE("Invalid opcode &2 at address &1", ttRam.iAddress, ttRam.iValue)
VIEW-AS ALERT-BOX INFO BUTTONS OK.
QUIT.
END.
END CASE.
/*
DEFINE VARIABLE cRam AS CHARACTER NO-UNDO.
cRam = "".
DO i = iMaxAddress TO 0 BY -1:
FIND ttRam WHERE ttRam.iAddress = i.
cRam = STRING(ttRam.iValue) + (IF iPC = i THEN "*" ELSE "") + "," + cRam.
END.
MESSAGE "PC:" iPC SKIP "RAM:" cRam
VIEW-AS ALERT-BOX INFO BUTTONS OK.
*/
END.
FIND ttRam WHERE ttRam.iAddress = 0.
MESSAGE ttRam.iValue
VIEW-AS ALERT-BOX INFO BUTTONS OK.
/*
---------------------------
Information (Press HELP to view stack trace)
---------------------------
4945026
---------------------------
Aceptar Ayuda
---------------------------
*/