From 1fa48259cfbb493ea5f45e43301f9ed89531d8f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer=20=28Macbook=29?= Date: Mon, 9 Dec 2019 14:39:03 +0100 Subject: [PATCH] aoc 6+7+8+9 --- www/statics/aoc/2019/06_challenge.txt | 106 ++ www/statics/aoc/2019/06_input.txt | 1860 +++++++++++++++++++++++++ www/statics/aoc/2019/06_solution-1.ts | 71 + www/statics/aoc/2019/06_solution-2.ts | 102 ++ www/statics/aoc/2019/07_challenge.txt | 83 ++ www/statics/aoc/2019/07_input.txt | 1 + www/statics/aoc/2019/07_solution-1.ts | 211 +++ www/statics/aoc/2019/07_solution-2.ts | 299 ++++ www/statics/aoc/2019/08_challenge.txt | 57 + www/statics/aoc/2019/08_input.txt | 1 + www/statics/aoc/2019/08_solution-1.ts | 31 + www/statics/aoc/2019/08_solution-2.ts | 43 + www/statics/aoc/2019/09_challenge.txt | 48 + www/statics/aoc/2019/09_input.txt | 1 + www/statics/aoc/2019/09_solution-1.ts | 213 +++ www/statics/aoc/2019/09_solution-2.ts | 213 +++ www/statics/aoc/__all.php | 5 +- 17 files changed, 3344 insertions(+), 1 deletion(-) create mode 100644 www/statics/aoc/2019/06_challenge.txt create mode 100644 www/statics/aoc/2019/06_input.txt create mode 100644 www/statics/aoc/2019/06_solution-1.ts create mode 100644 www/statics/aoc/2019/06_solution-2.ts create mode 100644 www/statics/aoc/2019/07_challenge.txt create mode 100644 www/statics/aoc/2019/07_input.txt create mode 100644 www/statics/aoc/2019/07_solution-1.ts create mode 100644 www/statics/aoc/2019/07_solution-2.ts create mode 100644 www/statics/aoc/2019/08_challenge.txt create mode 100644 www/statics/aoc/2019/08_input.txt create mode 100644 www/statics/aoc/2019/08_solution-1.ts create mode 100644 www/statics/aoc/2019/08_solution-2.ts create mode 100644 www/statics/aoc/2019/09_challenge.txt create mode 100644 www/statics/aoc/2019/09_input.txt create mode 100644 www/statics/aoc/2019/09_solution-1.ts create mode 100644 www/statics/aoc/2019/09_solution-2.ts diff --git a/www/statics/aoc/2019/06_challenge.txt b/www/statics/aoc/2019/06_challenge.txt new file mode 100644 index 0000000..fda794e --- /dev/null +++ b/www/statics/aoc/2019/06_challenge.txt @@ -0,0 +1,106 @@ +--- Day 6: Universal Orbit Map --- + +You've landed at the Universal Orbit Map facility on Mercury. Because navigation in space often involves transferring between orbits, the orbit maps here are useful for finding efficient routes between, for example, you and Santa. You download a map of the local orbits (your puzzle input). + +Except for the universal Center of Mass (COM), every object in space is in orbit around exactly one other object. An orbit looks roughly like this: + + \ + \ + | + | +AAA--> o o <--BBB + | + | + / + / + +In this diagram, the object BBB is in orbit around AAA. The path that BBB takes around AAA (drawn with lines) is only partly shown. In the map data, this orbital relationship is written AAA)BBB, which means "BBB is in orbit around AAA". + +Before you use your map data to plot a course, you need to make sure it wasn't corrupted during the download. To verify maps, the Universal Orbit Map facility uses orbit count checksums - the total number of direct orbits (like the one shown above) and indirect orbits. + +Whenever A orbits B and B orbits C, then A indirectly orbits C. This chain can be any number of objects long: if A orbits B, B orbits C, and C orbits D, then A indirectly orbits D. + +For example, suppose you have the following map: + +COM)B +B)C +C)D +D)E +E)F +B)G +G)H +D)I +E)J +J)K +K)L + +Visually, the above map of orbits looks like this: + + G - H J - K - L + / / +COM - B - C - D - E - F + \ + I + +In this visual representation, when two objects are connected by a line, the one on the right directly orbits the one on the left. + +Here, we can count the total number of orbits as follows: + + D directly orbits C and indirectly orbits B and COM, a total of 3 orbits. + L directly orbits K and indirectly orbits J, E, D, C, B, and COM, a total of 7 orbits. + COM orbits nothing. + +The total number of direct and indirect orbits in this example is 42. + +What is the total number of direct and indirect orbits in your map data? + +--- Part Two --- + +Now, you just need to figure out how many orbital transfers you (YOU) need to take to get to Santa (SAN). + +You start at the object YOU are orbiting; your destination is the object SAN is orbiting. An orbital transfer lets you move from any object to an object orbiting or orbited by that object. + +For example, suppose you have the following map: + +COM)B +B)C +C)D +D)E +E)F +B)G +G)H +D)I +E)J +J)K +K)L +K)YOU +I)SAN + +Visually, the above map of orbits looks like this: + + YOU + / + G - H J - K - L + / / +COM - B - C - D - E - F + \ + I - SAN + +In this example, YOU are in orbit around K, and SAN is in orbit around I. To move from K to I, a minimum of 4 orbital transfers are required: + + K to J + J to E + E to D + D to I + +Afterward, the map of orbits looks like this: + + G - H J - K - L + / / +COM - B - C - D - E - F + \ + I - SAN + \ + YOU + +What is the minimum number of orbital transfers required to move from the object YOU are orbiting to the object SAN is orbiting? (Between the objects they are orbiting - not between YOU and SAN.) diff --git a/www/statics/aoc/2019/06_input.txt b/www/statics/aoc/2019/06_input.txt new file mode 100644 index 0000000..9279ed3 --- /dev/null +++ b/www/statics/aoc/2019/06_input.txt @@ -0,0 +1,1860 @@ +CYP)BC6 +FPL)G1W +6MM)5MX +CXK)W4F +TW9)KG3 +7LR)RVF +9LK)MJF +FZT)G7N +4F5)YJH +PJF)DJV +JYT)3WQ +839)Q21 +8CW)NGC +564)8T3 +M86)3NM +XSL)814 +MH7)LDS +DKB)742 +FWR)NDK +RFZ)B5P +MLZ)YZQ +3RK)FZB +X46)NQ1 +Y64)VDS +H79)LPF +G4Q)HSZ +76X)37N +5X1)7PF +YT3)8RH +QPY)32L +3M3)XY4 +Z9Z)ZP1 +MZ1)H8X +2RY)VW7 +8CB)L95 +RQF)1NR +JRX)2N9 +YV3)4R2 +XKB)MFH +13H)1BW +WQS)XBR +WQ9)GQP +HGX)7G5 +ZT8)7C6 +DHD)HSD +TCX)7P7 +8NF)KTM +FG4)V8D +C49)DJK +KBZ)MFF +9SG)DBC +YWQ)HF1 +Y7T)7RN +6YM)BV6 +B41)P1W +PSF)NLC +9VN)W8W +YS5)6RK +NLH)ZH9 +S8D)2BJ +923)9RJ +VGK)D33 +SL1)7LT +ZM4)S6L +G1P)QFG +RLW)6TK +NDK)DXV +DFN)7GM +B6Z)FL7 +4JP)JCR +DQF)8CN +7PB)F3V +7QM)116 +1WM)WS8 +LLC)1YP +GP2)M37 +DX1)V6G +V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1M4 +HNT)4MP +PBL)K84 +T97)DT7 +65J)CNR +7GZ)PCZ +GHT)ZDF +L7R)W9Y +NHT)HX7 +SDZ)MJP +BV6)374 +13H)M5L +YXN)XFS +DCP)1NY +SV3)33T +415)B34 +8NL)DS8 +RM5)796 +G1W)RK1 +5CJ)SMH +JBL)974 +HPL)M3M +XNB)MB7 +H9D)DQ9 +4JB)6JX +XR5)B22 +BMK)RR5 +KD3)D7H +76X)CTC +TQD)56X +WZY)84Y +HSZ)663 +SJZ)NHV +RSR)7MF +WB3)537 +62Y)LHR +H69)26P +811)167 +K65)7VL +Q62)3PG +YZ2)C1C +56X)VFS +6WQ)8NF +DP3)PJF +62D)NHM +T44)F8B +NDB)K1T +LVH)HFP +96D)YSP +2N9)4BM +89B)246 +T3D)KH1 +Y4V)2CX +6DP)TQ1 +5CM)9TB +PHX)5NT +6LW)ZQQ +7LT)K1R +HBJ)9L9 +XW6)NNM +BCX)KB5 +8WG)ZXM diff --git a/www/statics/aoc/2019/06_solution-1.ts b/www/statics/aoc/2019/06_solution-1.ts new file mode 100644 index 0000000..a117845 --- /dev/null +++ b/www/statics/aoc/2019/06_solution-1.ts @@ -0,0 +1,71 @@ +namespace AdventOfCode2019_06_1 +{ + const DAY = 6; + const PROBLEM = 1; + + class Body + { + name: string; + children: Body[] = []; + parent: Body|null = null; + + constructor(n: string) { this.name = n; } + } + + class StellarSystem + { + com: Body; + bodies: { [key:string]:Body } = {}; + + constructor() { this.com = new Body("COM"); this.bodies["COM"] = this.com; } + + private getOrCreate(name: string): Body + { + if (name in this.bodies) return this.bodies[name]; + return this.bodies[name] = new Body(name); + } + + public add(mastername: string, slavename: string) + { + const master = this.getOrCreate(mastername); + const slave = this.getOrCreate(slavename); + + if (slave.parent !== null) throw "slave already has master"; + slave.parent = master; + + master.children.push(slave); + } + + public getChecksum(): number + { + return this.calcChecksum(this.com, 0); + } + + private calcChecksum(master: Body, depth: number): number + { + let r = depth; + for (const body of master.children) + { + r += this.calcChecksum(body, depth+1); + } + return r; + } + } + + export async function run() + { + let input = await AdventOfCode.getInput(DAY); + if (input == null) return; + + const data = input + .split(new RegExp('\r?\n')) + .filter(p => p.trim().length > 0) + .map(p => p.split(')')); + + let sys = new StellarSystem(); + + for(const dat of data) sys.add(dat[0], dat[1]); + + AdventOfCode.output(DAY, PROBLEM, sys.getChecksum().toString()); + } +} diff --git a/www/statics/aoc/2019/06_solution-2.ts b/www/statics/aoc/2019/06_solution-2.ts new file mode 100644 index 0000000..17a0c93 --- /dev/null +++ b/www/statics/aoc/2019/06_solution-2.ts @@ -0,0 +1,102 @@ +namespace AdventOfCode2019_06_2 +{ + const DAY = 6; + const PROBLEM = 2; + + class Body + { + name: string; + children: Body[] = []; + parent: Body|null = null; + + constructor(n: string) { this.name = n; } + } + + class StellarSystem + { + com: Body; + bodies: { [key:string]:Body } = {}; + + constructor() { this.com = new Body("COM"); this.bodies["COM"] = this.com; } + + private getOrCreate(name: string): Body + { + if (name in this.bodies) return this.bodies[name]; + return this.bodies[name] = new Body(name); + } + + public add(mastername: string, slavename: string) + { + const master = this.getOrCreate(mastername); + const slave = this.getOrCreate(slavename); + + if (slave.parent !== null) throw "slave already has master"; + slave.parent = master; + + master.children.push(slave); + } + + public getChecksum(): number + { + return this.calcChecksum(this.com, 0); + } + + private calcChecksum(master: Body, depth: number): number + { + let r = depth; + for (const body of master.children) + { + r += this.calcChecksum(body, depth+1); + } + return r; + } + + public getTransferDistance(ba: string, bb: string) + { + let dist: { [key:string]:number } = {}; + + // From [BA] upwards + { + let obj = this.bodies[ba].parent; + let dctr = 0; + while(obj !== null) + { + dist[obj.name] = dctr; + dctr++; + obj = obj.parent; + } + } + + // From [BB] upwards + { + let obj = this.bodies[bb].parent; + let dctr = 0; + while(obj !== null) + { + if (obj.name in dist) return dctr + dist[obj.name]; + dctr++; + obj = obj.parent; + } + } + + return -1; // no path; + } + } + + export async function run() + { + let input = await AdventOfCode.getInput(DAY); + if (input == null) return; + + const data = input + .split(new RegExp('\r?\n')) + .filter(p => p.trim().length > 0) + .map(p => p.split(')')); + + let sys = new StellarSystem(); + + for(const dat of data) sys.add(dat[0], dat[1]); + + AdventOfCode.output(DAY, PROBLEM, sys.getTransferDistance("YOU", "SAN").toString()); + } +} diff --git a/www/statics/aoc/2019/07_challenge.txt b/www/statics/aoc/2019/07_challenge.txt new file mode 100644 index 0000000..b393dad --- /dev/null +++ b/www/statics/aoc/2019/07_challenge.txt @@ -0,0 +1,83 @@ +--- Day 7: Amplification Circuit --- + +Based on the navigational maps, you're going to need to send more power to your ship's thrusters to reach Santa in time. To do this, you'll need to configure a series of amplifiers already installed on the ship. + +There are five amplifiers connected in series; each one receives an input signal and produces an output signal. They are connected such that the first amplifier's output leads to the second amplifier's input, the second amplifier's output leads to the third amplifier's input, and so on. The first amplifier's input value is 0, and the last amplifier's output leads to your ship's thrusters. + + O-------O O-------O O-------O O-------O O-------O +0 ->| Amp A |->| Amp B |->| Amp C |->| Amp D |->| Amp E |-> (to thrusters) + O-------O O-------O O-------O O-------O O-------O + +The Elves have sent you some Amplifier Controller Software (your puzzle input), a program that should run on your existing Intcode computer. Each amplifier will need to run a copy of the program. + +When a copy of the program starts running on an amplifier, it will first use an input instruction to ask the amplifier for its current phase setting (an integer from 0 to 4). Each phase setting is used exactly once, but the Elves can't remember which amplifier needs which phase setting. + +The program will then call another input instruction to get the amplifier's input signal, compute the correct output signal, and supply it back to the amplifier with an output instruction. (If the amplifier has not yet received an input signal, it waits until one arrives.) + +Your job is to find the largest output signal that can be sent to the thrusters by trying every possible combination of phase settings on the amplifiers. Make sure that memory is not shared or reused between copies of the program. + +For example, suppose you want to try the phase setting sequence 3,1,2,4,0, which would mean setting amplifier A to phase setting 3, amplifier B to setting 1, C to 2, D to 4, and E to 0. Then, you could determine the output signal that gets sent from amplifier E to the thrusters with the following steps: + + Start the copy of the amplifier controller software that will run on amplifier A. At its first input instruction, provide it the amplifier's phase setting, 3. At its second input instruction, provide it the input signal, 0. After some calculations, it will use an output instruction to indicate the amplifier's output signal. + Start the software for amplifier B. Provide it the phase setting (1) and then whatever output signal was produced from amplifier A. It will then produce a new output signal destined for amplifier C. + Start the software for amplifier C, provide the phase setting (2) and the value from amplifier B, then collect its output signal. + Run amplifier D's software, provide the phase setting (4) and input value, and collect its output signal. + Run amplifier E's software, provide the phase setting (0) and input value, and collect its output signal. + +The final output signal from amplifier E would be sent to the thrusters. However, this phase setting sequence may not have been the best one; another sequence might have sent a higher signal to the thrusters. + +Here are some example programs: + + Max thruster signal 43210 (from phase setting sequence 4,3,2,1,0): + + 3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0 + + Max thruster signal 54321 (from phase setting sequence 0,1,2,3,4): + + 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 thruster signal 65210 (from phase setting sequence 1,0,4,3,2): + + 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 + +Try every combination of phase settings on the amplifiers. What is the highest signal that can be sent to the thrusters? + +--- Part Two --- + +It's no good - in this configuration, the amplifiers can't generate a large enough output signal to produce the thrust you'll need. The Elves quickly talk you through rewiring the amplifiers into a feedback loop: + + O-------O O-------O O-------O O-------O O-------O +0 -+->| Amp A |->| Amp B |->| Amp C |->| Amp D |->| Amp E |-. + | O-------O O-------O O-------O O-------O O-------O | + | | + '--------------------------------------------------------+ + | + v + (to thrusters) + +Most of the amplifiers are connected as they were before; amplifier A's output is connected to amplifier B's input, and so on. However, the output from amplifier E is now connected into amplifier A's input. This creates the feedback loop: the signal will be sent through the amplifiers many times. + +In feedback loop mode, the amplifiers need totally different phase settings: integers from 5 to 9, again each used exactly once. These settings will cause the Amplifier Controller Software to repeatedly take input and produce output many times before halting. Provide each amplifier its phase setting at its first input instruction; all further input/output instructions are for signals. + +Don't restart the Amplifier Controller Software on any amplifier during this process. Each one should continue receiving and sending signals until it halts. + +All signals sent or received in this process will be between pairs of amplifiers except the very first signal and the very last signal. To start the process, a 0 signal is sent to amplifier A's input exactly once. + +Eventually, the software on the amplifiers will halt after they have processed the final loop. When this happens, the last output signal from amplifier E is sent to the thrusters. Your job is to find the largest output signal that can be sent to the thrusters using the new phase settings and feedback loop arrangement. + +Here are some example programs: + + Max thruster signal 139629729 (from phase setting sequence 9,8,7,6,5): + + 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 + + Max thruster signal 18216 (from phase setting sequence 9,7,8,5,6): + + 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 + +Try every combination of the new phase settings on the amplifier feedback loop. What is the highest signal that can be sent to the thrusters? diff --git a/www/statics/aoc/2019/07_input.txt b/www/statics/aoc/2019/07_input.txt new file mode 100644 index 0000000..0dbf16c --- /dev/null +++ b/www/statics/aoc/2019/07_input.txt @@ -0,0 +1 @@ +3,8,1001,8,10,8,105,1,0,0,21,46,67,88,101,126,207,288,369,450,99999,3,9,1001,9,5,9,1002,9,5,9,1001,9,5,9,102,3,9,9,101,2,9,9,4,9,99,3,9,102,4,9,9,101,5,9,9,102,5,9,9,101,3,9,9,4,9,99,3,9,1001,9,3,9,102,2,9,9,1001,9,5,9,102,4,9,9,4,9,99,3,9,102,3,9,9,1001,9,4,9,4,9,99,3,9,102,3,9,9,1001,9,3,9,1002,9,2,9,101,4,9,9,102,3,9,9,4,9,99,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,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,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,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,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,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,1,9,4,9,99,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,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,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,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,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,99 diff --git a/www/statics/aoc/2019/07_solution-1.ts b/www/statics/aoc/2019/07_solution-1.ts new file mode 100644 index 0000000..a0e812e --- /dev/null +++ b/www/statics/aoc/2019/07_solution-1.ts @@ -0,0 +1,211 @@ +namespace AdventOfCode2019_07_1 +{ + const DAY = 7; + const PROBLEM = 1; + + export async function run() + { + let input = await AdventOfCode.getInput(DAY); + if (input == null) return; + + let code = input.split(",").map(p => parseInt(p.trim())); + + let outputs = []; + + for(let i=0; i < 5*5*5*5*5; i++) + { + let v = i; + let a1 = v%5; v = Math.floor(v/5); + let a2 = v%5; v = Math.floor(v/5); + let a3 = v%5; v = Math.floor(v/5); + let a4 = v%5; v = Math.floor(v/5); + let a5 = v%5; v = Math.floor(v/5); + + if ([a1,a2,a3,a4,a5].sort((a, b) => a - b).filter((v,i,s) => s.indexOf(v)===i).length !== 5) continue; + + const runner1 = new Interpreter(code, [a1, 0]); + runner1.run(); + + const runner2 = new Interpreter(code, [a2, runner1.output[0]]); + runner2.run(); + + const runner3 = new Interpreter(code, [a3, runner2.output[0]]); + runner3.run(); + + const runner4 = new Interpreter(code, [a4, runner3.output[0]]); + runner4.run(); + + const runner5 = new Interpreter(code, [a5, runner4.output[0]]); + runner5.run(); + + outputs.push(runner5.output[0]); + console.log([a1,a2,a3,a4,a5].toString() + " --> " + runner1.output[0] + " |> " + runner2.output[0] + " |> " + runner3.output[0] + " |> " + runner4.output[0] + " |> " + runner5.output[0]); + } + + const max = outputs.sort((a, b) => a - b).reverse()[0]; + + AdventOfCode.output(DAY, PROBLEM, max.toString()); + } + + class Interpreter + { + program: number[]; + input: number[]; + instructionpointer: number; + inputpointer: number; + output: number[]; + + constructor(prog: number[], input: number[]) + { + this.program = prog; + this.input = input; + this.instructionpointer = 0; + this.inputpointer = 0; + this.output = []; + } + + run() + { + while(true) + { + const cmd = new Op(this.program[this.instructionpointer]); + let ipinc = true; + + if (cmd.opcode == OpCode.ADD) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 + p1; + cmd.setParameter(this, 2, pv); + } + else if (cmd.opcode == OpCode.MUL) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 * p1; + cmd.setParameter(this, 2, pv); + } + else if (cmd.opcode == OpCode.HALT) + { + return; + } + else if (cmd.opcode == OpCode.IN) + { + const pv = this.input[this.inputpointer]; + cmd.setParameter(this, 0, pv); + this.inputpointer++; + } + else if (cmd.opcode == OpCode.OUT) + { + const p0 = cmd.getParameter(this, 0); + this.output.push(p0); + //console.log("# " + p0); + } + else if (cmd.opcode == OpCode.TJMP) + { + const p0 = cmd.getParameter(this, 0); + if (p0 != 0) { this.instructionpointer = cmd.getParameter(this, 1); ipinc=false; } + } + else if (cmd.opcode == OpCode.FJMP) + { + const p0 = cmd.getParameter(this, 0); + if (p0 == 0) { this.instructionpointer = cmd.getParameter(this, 1); ipinc=false; } + } + else if (cmd.opcode == OpCode.LT) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 < p1 ? 1 : 0; + cmd.setParameter(this, 2, pv); + } + else if (cmd.opcode == OpCode.EQ) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 == p1 ? 1 : 0; + cmd.setParameter(this, 2, pv); + } + else throw "Unknown Op: " + cmd.opcode + " @ " + this.instructionpointer; + + if (ipinc) this.instructionpointer += 1 + cmd.parametercount; + } + } + } + + enum OpCode + { + ADD = 1, + MUL = 2, + IN = 3, + OUT = 4, + TJMP = 5, + FJMP = 6, + LT = 7, + EQ = 8, + HALT = 99, + } + + enum ParamMode + { + POSITION_MODE = 0, + IMMEDIATE_MODE = 1, + } + + class Op + { + opcode: OpCode; + modes: ParamMode[]; + + name: string; + parametercount: number; + + constructor(v: number) + { + this.opcode = v%100; + v = Math.floor(v/100); + this.modes = []; + for(let i=0; i<4; i++) + { + this.modes.push(v%10); + v = Math.floor(v/10); + } + + if (this.opcode == OpCode.ADD) { this.name="ADD"; this.parametercount=3; } + else if (this.opcode == OpCode.MUL) { this.name="MUL"; this.parametercount=3; } + else if (this.opcode == OpCode.HALT) { this.name="HALT"; this.parametercount=0; } + else if (this.opcode == OpCode.IN) { this.name="IN"; this.parametercount=1; } + else if (this.opcode == OpCode.OUT) { this.name="OUT"; this.parametercount=1; } + else if (this.opcode == OpCode.TJMP) { this.name="TJMP"; this.parametercount=2; } + else if (this.opcode == OpCode.FJMP) { this.name="FJMP"; this.parametercount=2; } + else if (this.opcode == OpCode.LT) { this.name="LT"; this.parametercount=3; } + else if (this.opcode == OpCode.EQ) { this.name="EQ"; this.parametercount=3; } + else throw "Unknown opcode: "+this.opcode; + } + + getParameter(proc: Interpreter, index: number): number + { + const prog = proc.program; + const ip = proc.instructionpointer; + + let p = prog[ip+1+index]; + + if (this.modes[index] == ParamMode.POSITION_MODE) p = prog[p]; + else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) p = p; + else throw "Unknown ParamMode: "+this.modes[index]; + + return p; + } + + setParameter(proc: Interpreter, index: number, value: number): void + { + const prog = proc.program; + const ip = proc.instructionpointer; + + let p = prog[ip+1+index]; + + if (this.modes[index] == ParamMode.POSITION_MODE) prog[p] = value; + else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) throw "Immediate mode not allowed in write"; + else throw "Unknown ParamMpde: "+this.modes[index]; + } + } +} diff --git a/www/statics/aoc/2019/07_solution-2.ts b/www/statics/aoc/2019/07_solution-2.ts new file mode 100644 index 0000000..0ed7929 --- /dev/null +++ b/www/statics/aoc/2019/07_solution-2.ts @@ -0,0 +1,299 @@ +namespace AdventOfCode2019_07_2 +{ + const DAY = 7; + const PROBLEM = 2; + + export async function run() + { + let input = await AdventOfCode.getInput(DAY); + if (input == null) return; + + let code = input.split(",").map(p => parseInt(p.trim())); + + let outputs: number[] = []; + + for(let i=0; i < 5*5*5*5*5; i++) + { + let v = i; + let a1 = v%5 + 5; v = Math.floor(v/5); + let a2 = v%5 + 5; v = Math.floor(v/5); + let a3 = v%5 + 5; v = Math.floor(v/5); + let a4 = v%5 + 5; v = Math.floor(v/5); + let a5 = v%5 + 5; v = Math.floor(v/5); + + if ([a1,a2,a3,a4,a5].sort((a, b) => a - b).filter((v,i,s) => s.indexOf(v)===i).length !== 5) continue; + + const r = evalConfiguration(code, a1,a2,a3,a4,a5) + + outputs.push(r); + console.log([a1,a2,a3,a4,a5].toString() + " --> " + r); + } + + const max = outputs.sort((a, b) => a - b).reverse()[0]; + + AdventOfCode.output(DAY, PROBLEM, max.toString()); + } + + function evalConfiguration(code: number[], a1 : number, a2 : number, a3 : number, a4 : number, a5 : number) : number + { + const runner1 = new Interpreter(code, [a1, 0]); + const runner2 = new Interpreter(code, [a2]); + const runner3 = new Interpreter(code, [a3]); + const runner4 = new Interpreter(code, [a4]); + const runner5 = new Interpreter(code, [a5]); + + let optr1 = 0; + let optr2 = 0; + let optr3 = 0; + let optr4 = 0; + let optr5 = 0; + + while (!runner5.is_halted) + { + runner1.singleStep(); + if (runner1.output.length > optr1) + { + runner2.inputqueue.push(runner1.output[optr1]); + optr1++; + } + + runner2.singleStep(); + if (runner2.output.length > optr2) + { + runner3.inputqueue.push(runner2.output[optr2]); + optr2++; + } + + runner3.singleStep(); + if (runner3.output.length > optr3) + { + runner4.inputqueue.push(runner3.output[optr3]); + optr3++; + } + + runner4.singleStep(); + if (runner4.output.length > optr4) + { + runner5.inputqueue.push(runner4.output[optr4]); + optr4++; + } + + runner5.singleStep(); + if (runner5.output.length > optr5) + { + runner1.inputqueue.push(runner5.output[optr5]); + optr5++; + } + } + + return runner5.output.reverse()[0]; + } + + class Interpreter + { + program: number[]; + inputqueue: number[]; + instructionpointer: number; + output: number[]; + + is_halted: boolean = false; + + constructor(prog: number[], input: number[]) + { + this.program = Object.assign([], prog); + this.inputqueue = Object.assign([], input); + this.instructionpointer = 0; + this.output = []; + } + + fullRun() : number[] + { + while(!this.is_halted) + { + const r = this.singleStep(); + + if (r === StepResult.EXECUTED) continue; + if (r === StepResult.HALTED) return this.output; + if (r === StepResult.WAITING_FOR_IN) throw "not enough input"; + + throw "unknown output of singleStep"; + } + + return this.output; + } + + singleStep() : StepResult + { + const cmd = new Op(this.program[this.instructionpointer]); + + if (cmd.opcode == OpCode.ADD) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 + p1; + cmd.setParameter(this, 2, pv); + + this.incInstrPtr(cmd); + + return StepResult.EXECUTED; + } + else if (cmd.opcode == OpCode.MUL) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 * p1; + cmd.setParameter(this, 2, pv); + + this.incInstrPtr(cmd); + + return StepResult.EXECUTED; + } + else if (cmd.opcode == OpCode.HALT) + { + this.is_halted = true; + return StepResult.HALTED; + } + else if (cmd.opcode == OpCode.IN) + { + if (this.inputqueue.length == 0) return StepResult.WAITING_FOR_IN; + + const pv = this.inputqueue[0]; + cmd.setParameter(this, 0, pv); + this.inputqueue = this.inputqueue.slice(1); + + this.incInstrPtr(cmd); + return StepResult.EXECUTED; + } + else if (cmd.opcode == OpCode.OUT) + { + const p0 = cmd.getParameter(this, 0); + this.output.push(p0); + + this.incInstrPtr(cmd); + + return StepResult.EXECUTED; + } + else if (cmd.opcode == OpCode.TJMP) + { + const p0 = cmd.getParameter(this, 0); + if (p0 != 0) this.instructionpointer = cmd.getParameter(this, 1); + else this.incInstrPtr(cmd); + + return StepResult.EXECUTED; + } + else if (cmd.opcode == OpCode.FJMP) + { + const p0 = cmd.getParameter(this, 0); + if (p0 == 0) this.instructionpointer = cmd.getParameter(this, 1); + else this.incInstrPtr(cmd); + + return StepResult.EXECUTED; + } + else if (cmd.opcode == OpCode.LT) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 < p1 ? 1 : 0; + cmd.setParameter(this, 2, pv); + + this.incInstrPtr(cmd); + return StepResult.EXECUTED; + } + else if (cmd.opcode == OpCode.EQ) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 == p1 ? 1 : 0; + cmd.setParameter(this, 2, pv); + + this.incInstrPtr(cmd); + return StepResult.EXECUTED; + } + else throw "Unknown Op: " + cmd.opcode + " @ " + this.instructionpointer; + } + + private incInstrPtr(cmd: Op) + { + this.instructionpointer += 1 + cmd.parametercount; + } + } + + enum StepResult { EXECUTED, HALTED, WAITING_FOR_IN } + + enum OpCode + { + ADD = 1, + MUL = 2, + IN = 3, + OUT = 4, + TJMP = 5, + FJMP = 6, + LT = 7, + EQ = 8, + HALT = 99, + } + + enum ParamMode + { + POSITION_MODE = 0, + IMMEDIATE_MODE = 1, + } + + class Op + { + opcode: OpCode; + modes: ParamMode[]; + + name: string; + parametercount: number; + + constructor(v: number) + { + this.opcode = v%100; + v = Math.floor(v/100); + this.modes = []; + for(let i=0; i<4; i++) + { + this.modes.push(v%10); + v = Math.floor(v/10); + } + + if (this.opcode == OpCode.ADD) { this.name="ADD"; this.parametercount=3; } + else if (this.opcode == OpCode.MUL) { this.name="MUL"; this.parametercount=3; } + else if (this.opcode == OpCode.HALT) { this.name="HALT"; this.parametercount=0; } + else if (this.opcode == OpCode.IN) { this.name="IN"; this.parametercount=1; } + else if (this.opcode == OpCode.OUT) { this.name="OUT"; this.parametercount=1; } + else if (this.opcode == OpCode.TJMP) { this.name="TJMP"; this.parametercount=2; } + else if (this.opcode == OpCode.FJMP) { this.name="FJMP"; this.parametercount=2; } + else if (this.opcode == OpCode.LT) { this.name="LT"; this.parametercount=3; } + else if (this.opcode == OpCode.EQ) { this.name="EQ"; this.parametercount=3; } + else throw "Unknown opcode: "+this.opcode; + } + + getParameter(proc: Interpreter, index: number): number + { + const prog = proc.program; + const ip = proc.instructionpointer; + + let p = prog[ip+1+index]; + + if (this.modes[index] == ParamMode.POSITION_MODE) p = prog[p]; + else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) p = p; + else throw "Unknown ParamMode: "+this.modes[index]; + + return p; + } + + setParameter(proc: Interpreter, index: number, value: number): void + { + const prog = proc.program; + const ip = proc.instructionpointer; + + let p = prog[ip+1+index]; + + if (this.modes[index] == ParamMode.POSITION_MODE) prog[p] = value; + else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) throw "Immediate mode not allowed in write"; + else throw "Unknown ParamMpde: "+this.modes[index]; + } + } +} diff --git a/www/statics/aoc/2019/08_challenge.txt b/www/statics/aoc/2019/08_challenge.txt new file mode 100644 index 0000000..f740a28 --- /dev/null +++ b/www/statics/aoc/2019/08_challenge.txt @@ -0,0 +1,57 @@ +--- Day 8: Space Image Format --- + +The Elves' spirits are lifted when they realize you have an opportunity to reboot one of their Mars rovers, and so they are curious if you would spend a brief sojourn on Mars. You land your ship near the rover. + +When you reach the rover, you discover that it's already in the process of rebooting! It's just waiting for someone to enter a BIOS password. The Elf responsible for the rover takes a picture of the password (your puzzle input) and sends it to you via the Digital Sending Network. + +Unfortunately, images sent via the Digital Sending Network aren't encoded with any normal encoding; instead, they're encoded in a special Space Image Format. None of the Elves seem to remember why this is the case. They send you the instructions to decode it. + +Images are sent as a series of digits that each represent the color of a single pixel. The digits fill each row of the image left-to-right, then move downward to the next row, filling rows top-to-bottom until every pixel of the image is filled. + +Each image actually consists of a series of identically-sized layers that are filled in this way. So, the first digit corresponds to the top-left pixel of the first layer, the second digit corresponds to the pixel to the right of that on the same layer, and so on until the last digit, which corresponds to the bottom-right pixel of the last layer. + +For example, given an image 3 pixels wide and 2 pixels tall, the image data 123456789012 corresponds to the following image layers: + +Layer 1: 123 + 456 + +Layer 2: 789 + 012 + +The image you received is 25 pixels wide and 6 pixels tall. + +To make sure the image wasn't corrupted during transmission, the Elves would like you to find the layer that contains the fewest 0 digits. On that layer, what is the number of 1 digits multiplied by the number of 2 digits? + +--- Part Two --- + +Now you're ready to decode the image. The image is rendered by stacking the layers and aligning the pixels with the same positions in each layer. The digits indicate the color of the corresponding pixel: 0 is black, 1 is white, and 2 is transparent. + +The layers are rendered with the first layer in front and the last layer in back. So, if a given position has a transparent pixel in the first and second layers, a black pixel in the third layer, and a white pixel in the fourth layer, the final image would have a black pixel at that position. + +For example, given an image 2 pixels wide and 2 pixels tall, the image data 0222112222120000 corresponds to the following image layers: + +Layer 1: 02 + 22 + +Layer 2: 11 + 22 + +Layer 3: 22 + 12 + +Layer 4: 00 + 00 + +Then, the full image can be found by determining the top visible pixel in each position: + + The top-left pixel is black because the top layer is 0. + The top-right pixel is white because the top layer is 2 (transparent), but the second layer is 1. + The bottom-left pixel is white because the top two layers are 2, but the third layer is 1. + The bottom-right pixel is black because the only visible pixel in that position is 0 (from layer 4). + +So, the final image looks like this: + +01 +10 + +What message is produced after decoding your image? diff --git a/www/statics/aoc/2019/08_input.txt b/www/statics/aoc/2019/08_input.txt new file mode 100644 index 0000000..6bb3aee --- /dev/null +++ b/www/statics/aoc/2019/08_input.txt @@ -0,0 +1 @@ +222222022022222222202112210222222222221222202222222222202022222222222222222222022222202222222202110022212122102222222002122220222222222222221222212222222222122122222222222102210222222222222222202222222222202122222222222222222222122222212222222212011222212022002222222012122220222222222222220222202222222222022022222222222202220222222222220222222222222222202122222222222222222222022222202222222212002222212022202222222122222220222222222222220222222222222222122222222222212012210212222222221222222222222222202022222222222222222222022222212202222202200022212022202222222202222221222222222222220222222222222222022122222222202102200202222222221222222222222222222022222222222222222222122222212222222202220022222122002222222102022220222222222222222222212222222222122122222222212102200202222222221222002222222222222222222222222222222222122222222202222212110122202222122222222122022221222222222122221222212222222222222022222222202210202212222222222222002222222222202222222222222222222222222222212212220222002222222122022222222102022221222222222022220222202222222222122022222222212010222202222222222222012222222222212022222222222222222222222222202222220212220222222122022222222022022222222222222122222222212222222222022122222222202221200222222222220222202222222222222122222222222222222222222222202212221212101022222122012222222112122222222222202222220222222222222222222122222222212102222222222222222222212222222222202120222222222222222222022222222212220222022222222222102222222212022222222201222222222222222222222222222122222222222020201202222222222022212222222222222122222222222222222222122222212212221222110122202022012222222002222221222210202222221222222222222222122122222222212201220222222222220022212222222222222020222222222222222222222222222222221202221022202222022222222112222220222202222022221222222222222222222222212222222110221202222222221022102222222222212120222222222222222222022022202212221212220022220122212222222222122221222222202222220222202222222222222222202222222001200222222222222222112122222222212121222222222222222222022222222202220202011122210122112222222012022222222210222222222222202222222222022122122222222121222222222222220222102222222222222221222222222222222222122122222222220222120222222122012222222202222220222212212122222222202222222222222022122222212200222222022222221222012122222222212020222222222222222222122222202202220222010022221022102222222212122220222210212222220222222222222222022222122222202212220202122222222222112022222222222020222222222222222222022222222212220212011022221122122222222212022221222201202022222222212222222222222022102222212221222202222222222022202122222222222020222222222222222222022022222222222212112022211222102222222022222222222222212222221222222222222222122222222222212222210212122222222122222222122222212221222222222222222222222222202212220202121222222122212222222122122221222220222222222222212222222222022222002222202211200202022222221222202122222222212222222222222222222222222022202202222202120022221122202222222102022221222202202122221222202222222222022222012222212102200202122222220022222022022222222122222222222222222222222122222222222212020222221122122222222222222220222200222222220222212222222222222022012222212100222202222222220122122222122222212220222222222222222222222122212220221212102122210022112222222022022222222210222222220222202222222022222022222222202102220212022222220122102122122222222121222222222222222222222122222222221222010222200122122222222112022220222210222122221222222222222222222122102222212200200220222222220022002122122222212020222222222222222222122122222211222222021022201022112222222012122222222202202222220222202222222022022122202222212010202211222222221022002222222222202222222222222222222222122122222212220211012222200222111222222002222221222222202022220222202222222122122022212222222222220222002222221022102022022222202121222222222222222222222222212220222222122022221022200222222002122220222212202222222222212222222222122122222222212101220210122222220022012222022222202021222222222222222222222022212211221200211222220122121222222212022222222200212122222222202222222122222222012222212101220201002222222122102122222222202020222222222222222220022122222200221210202222221022021222222022222221222202212022222222202222222122022022112222202210212211202222222122212122122222212221222222222222222220022022202202221211111022210222121222222102022220222210202022222222222222222222122022212222202001211210102222222022122022222222222022222222222222222221122122212202221201110022202022002222222122222220222200212022220222212222222022222112202222202220211212112222220122222122122222202021222222222222222222222222212222220210112122222022212222222222222222222212222222220221202222222022122212202222202020212222222222221022022222022222222120222222222222222221122222222221221201102122210222212222222122022220222201222022221222222222222122212022012222202102210222202222220222202122122222202120222222222222222221022222202210222222111122210222020222222122122220222220202022221222212222222122122222012222222020222210222222222222112222222222212022222222222222222221022022222212221222001122201122011222222122222221222220222222221222202222222122002212022222212012202222012222220122002222222222212122022222222222222220222122222202221222202222201022001222222112022222222221212022221221022220222222122202202222222012202221012222222122022022022222202120122222222222222221022222202202220211010122211022201222222112222221222220202222221220122222222222222012212222202102212212202222221122222222022222212021122222222222222222122222212210221212012122212122110222222112122222222222212022222221022222222022202222012222212002212202112222221122222222122222222022022222222212222221222121202202222222221022210222020222222222222222212210222222221221112221222122212122102222222000200222102222221022212122122222212022022222222202222220222020222202220211001122220022000222222212222222202220212022221220212222222022222022212222222011221201212222222022212120122222202220122222222222222220022122222212221201222022201222112222222212222221212221212222221220212222222222222202022220222122210222122222222122122120122222202222122222222212222220222222212200221222202122212020022222222222022222212221202122222222112221222222002012102222211210220212122222222022102020122222212120222222222222222220022022212220222202012022201220002222222002222222212212222022221220222222222122012202002220221100211222122222222022202022022222222020022222222222222222022220222220220201000022002221012222222212222221222201222022221221122222222022012112202221212001201201212222222222112121022222222120222222222212222221022121202212221202110122121021212222222012022222212211212022222221212221222222002222202221221022222201122222220122222220222222202021022222222212222221122122202221222201121122101222200222222022122220202222222222221222202220022122121102110220220202200221012222222122102222222222202220022222222222222220222221212201222211110122202122202222222022222021202220212022221220202220222122121202211222222122212201202220222022212220222222202020222222222212222222122120222221220222201022122021021222022112022122212211222022220221122220222222101022002222220221220202102221222222010022222222212221122222222212222221222220222202222201220222112121012022122222122120202201212122222222122220222222112022010221222010200210012221221022000122222222212221122222222222222221022120202220221211102022011221020122222112122022212211212222221221022221022222211112112222220200221222212222220222120121122222212222222222222202222220222022212210220210201222011222111022122012022022222221222122201220212220022222221012011222200212220202012222221122221221022222202222122222202212222220222221202221222210101022120221220022122222122122222210222222202221022220122022000012022222200000202202012222222122110122222222212222122222222212222221022220212200222212202022111221022022122212222202202200212122200222212220122122200012001221221022222210102221220022221020022222202022022222212222222221022021202220221212110222210220112222022022222200222210212222202220012221221122102002120220210202200210112222222122000221122222202221022222212212222220122220202201222220100122010022022122122212122210222201212122222220202221120022200022010220201000212221122220220022122020122222212222022222202212222222122122212212222200102022212020002122022012020012222221222022212220202220021222111122110222210220201200212221221122212100222222212222022222212212222220022202222201220202102222010222222122022102021121222201212222221222102222221222122112202220221002211221122221220122100102222222212020022222212222222221122020202221220210202022202020001222222012021012202210222022201221122222121222010102221220211021222222212221222222202112222222212020122222222222222221022221222210221220100222022222001022022112221022112200202222222221112221120122222022120221220121202221002220222222221212222222222222222222202222222222122002222202222211111122122221002122222002021022112200212022220221222220022222222112022221201211202212122222220222001022122222222022222222202212222222120102222221222211001022012022002122122102120201102210212222201221202220121122222122202222200220221201012220222122111202222222202122022222202222222220120102202201220200111022211120201122022112221021022201222222210220012221020122110002102221211011220212122222222122200220222222212120022222212202222220221221222212221220002022120122010222220102021110012202202022220221202220021122110102211221220201221212122220222122212101022222222022122222202202222202020122202211222211212022020220120122021202222100102201212022200222202220221222122022121221210201202211122220222222100000122222222020222222212222222212021021202201222212010222021221212122220122021200122202202022202222122220120122122112001221201020212200212220222222112211222222222221222222202212222210221120212222220212220122012020201122021012022112112211202222201221112221120122002122211220202000201200022220222022122000022222212121122222202212222221220112222220220011110222020021220002021112220122112221222122210222112220122022002012101222210012222220212222220022111212122222202221022222212202222201022011212201220211012222201221000122022222220002202211212122212220102221222222001102000221222101210220202220220022222110122222002221222222212222222222220100222221221211220022112221012202021022222211222211222122220222012222121022002202001221200120221212002220200122120020122222222120022222212212222222222021212221220212001222012021120012221002121120012201212022212222112222222022101122121222211010201211112221221222202122022222212121022222212222222201222110202220221101210222012021010102022102221011122220202022201220012220120222211212200222220102210202002220221022212001122222202022022222222222222220220122202210220012211022212121021022120102221111112221212122212221022221021122001222110221212222220211102222212022202120122222012022022222222222222210120212202210220221021212220121222102120012122020112201222122222221022222221222001102102220200210220220112221222222120000022222102120222222222212222201102100212210222021000122111120121002020222221000212211202122201220002020220002102222100222221100222201012221221222101020122222212020022222212202222220021020222201221122100112110020220012120022122022012221222122201222222021021112020202121220201012221212122222210222110111122222002121022222212212222202120101202200221012002102221120122112020022020020002221202222201222202221221002122002100221212022201210202222211022212022122222002122122222222202222101011022212220221110200022012021020212022202220200112221112122201221102122222202212102122221211010222220112220210222200220022222002221122022222222222201222211202212220101012102100022100122222022121100022212222122212222122120120002010002101221200201212211102220222222221020022222212121022222202202222100002121202222220221102102000121010212221122121201212201122022201222112022020122201222221220220201202212222222211022021111022222102222222222212212222102002102222211220011021212110121202212020222020212222220222022221222022020021012121002220222201121210210022221211222001220222222222220122222202212222010222000212210222002000012220220102212121110122112222210212022210222212021020002202002010221222211212210022221210222122011122222102120122122222222222200221102212200221110212102102122202102220210020000012211002222212120012121220112112022202221210211202211202220202022222121222222212020222222212202222101000000202220220002211210120120101002220202120001102211012022222121102221220202112112112221210020221221122121202022100012022022022221022022222212222010120210222211220100011201100100212202222220122002102211212022210221200120221212121122020220220112211201002122212022011001222122202012122022222222222201100110212220221100211101222112012200122220122110102220002022221222011121222002020112012222211221210200022021202122110220022122122020022222222202222202102200212210222122202120120222110210220011221210202220222022220120201022122122211022221221212202202210112221200222001212122222012200022022222222222000112021202210222201011200112122201200020022020211112222112122222020002121212122112002210220222221012222112021220122012122222022122000022122212212222002122001212222222020222202011100210120221211102121212200102122111010122020201222021022201220211210011201102122200222020011022022212100222222222202222221021101222211221101001002210002221002222221022022202220212222222212112122221002000012100221200100210201222122210022120222022122002021222222212202222101200200202200221122011211010201222012011200000001102211212122102221011121000122022222202220222001000211222120001122020112022122102021122122212222222122012222222211221200221221011110220210112222211222212211202222200211101122220002112022020222211020200220122220010022200021202222212202122122202212222001221100202222220222212200002110012220112102021222112222202222011001220021000012110112121222220111212220102022220022011001002222222220222022212212222012222101022202220202010110011101102022021012002222122222202122101002000221221002111022021221202121100201212022111222121201002122122121022222202002222021021111202202221122202001112112022102201102200000012212002022210202020021110022111002002221202111211200112221200122021111022222002121022002202022222002021202012210200122021021121112020001112011012110022202012122111211201022021012120102110222212101021211002221111222001202112022102120222012222102212022000221212222221002002110110112122022222020111012212221122222002102211222011002111202220201201011111212012222222022000012002122212002122222212122222212010222222200221022201022112101220112020011221020002222100122201122221022021202122212121202221102212200012221110022022102122222102201021201212222202102102201122212212220212202211220220202222220121220002221001022102122220020101012101012120211201211121210002020001022102220222022022100121121212122222120011220202221221012202010001001212100021100101200112212222122211000001021110200001102201221211111211202222200111222101112022222202220022100212222212111011011212210221121000001100220111122100111221100012201201122100100121020000020121001012121021022002022100212101210020001001100010200101110000210000011111221011020100210122100002120102101100221022202200012021111021211101001 diff --git a/www/statics/aoc/2019/08_solution-1.ts b/www/statics/aoc/2019/08_solution-1.ts new file mode 100644 index 0000000..112f560 --- /dev/null +++ b/www/statics/aoc/2019/08_solution-1.ts @@ -0,0 +1,31 @@ +namespace AdventOfCode2019_08_1 +{ + const DAY = 8; + const PROBLEM = 1; + + export async function run() + { + let input = await AdventOfCode.getInput(DAY); + if (input == null) return; + + let data = input.trim().split("").map(p => parseInt(p)); + + const layers = []; + + while (data.length>0) { layers.push(data.slice(0, 25*6)); data = data.slice(25*6); } + + let zc = 9999999999; + let vv = -1; + + for(let i = 0; i < layers.length; i++) + { + const _zc = layers[i].filter(p => p==0).length; + const _vv = layers[i].filter(p => p==1).length * layers[i].filter(p => p==2).length; + + console.log("["+i+"] => "+_zc+" ("+_vv+")"); + if (_zc < zc) { zc=_zc; vv = _vv; } + } + + AdventOfCode.output(DAY, PROBLEM, vv.toString()); + } +} diff --git a/www/statics/aoc/2019/08_solution-2.ts b/www/statics/aoc/2019/08_solution-2.ts new file mode 100644 index 0000000..d65bc49 --- /dev/null +++ b/www/statics/aoc/2019/08_solution-2.ts @@ -0,0 +1,43 @@ +namespace AdventOfCode2019_08_2 +{ + const DAY = 8; + const PROBLEM = 2; + + export async function run() + { + let input = await AdventOfCode.getInput(DAY); + if (input == null) return; + + let data = input.trim().split("").map(p => parseInt(p)); + + const layers = []; + + while (data.length>0) { layers.push(data.slice(0, 25*6)); data = data.slice(25*6); } + + let r = Array(25*6); + for(let i=0; i<25*6;i++) r[i]=2; + + for(let il = layers.length-1; il >= 0 ; il--) + { + for(let i=0; i<25*6;i++) + { + if (layers[il][i] == 2) continue; + r[i] = layers[il][i]; + } + } + + let str = ""; + for(let i=0; i<25*6;i++) + { + if (i>0 && i%25==0)str+="\n"; + + if (r[i]===0) str += " "; + if (r[i]===1) str += "#"; + if (r[i]===2) str += "."; + } + + console.log(str); + + AdventOfCode.output(DAY, PROBLEM, "CJZLP"); // OCR -.- + } +} diff --git a/www/statics/aoc/2019/09_challenge.txt b/www/statics/aoc/2019/09_challenge.txt new file mode 100644 index 0000000..35f9afc --- /dev/null +++ b/www/statics/aoc/2019/09_challenge.txt @@ -0,0 +1,48 @@ +--- Day 9: Sensor Boost --- + +You've just said goodbye to the rebooted rover and left Mars when you receive a faint distress signal coming from the asteroid belt. It must be the Ceres monitoring station! + +In order to lock on to the signal, you'll need to boost your sensors. The Elves send up the latest BOOST program - Basic Operation Of System Test. + +While BOOST (your puzzle input) is capable of boosting your sensors, for tenuous safety reasons, it refuses to do so until the computer it runs on passes some checks to demonstrate it is a complete Intcode computer. + +Your existing Intcode computer is missing one key feature: it needs support for parameters in relative mode. + +Parameters in mode 2, relative mode, behave very similarly to parameters in position mode: the parameter is interpreted as a position. Like position mode, parameters in relative mode can be read from or written to. + +The important difference is that relative mode parameters don't count from address 0. Instead, they count from a value called the relative base. The relative base starts at 0. + +The address a relative mode parameter refers to is itself plus the current relative base. When the relative base is 0, relative mode parameters and position mode parameters with the same value refer to the same address. + +For example, given a relative base of 50, a relative mode parameter of -7 refers to memory address 50 + -7 = 43. + +The relative base is modified with the relative base offset instruction: + + Opcode 9 adjusts the relative base by the value of its only parameter. The relative base increases (or decreases, if the value is negative) by the value of the parameter. + +For example, if the relative base is 2000, then after the instruction 109,19, the relative base would be 2019. If the next instruction were 204,-34, then the value at address 1985 would be output. + +Your Intcode computer will also need a few other capabilities: + + The computer's available memory should be much larger than the initial program. Memory beyond the initial program starts with the value 0 and can be read or written like any other memory. (It is invalid to try to access memory at a negative address, though.) + The computer should have support for large numbers. Some instructions near the beginning of the BOOST program will verify this capability. + +Here are some example programs that use these features: + + 109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99 takes no input and produces a copy of itself as output. + 1102,34915192,34915192,7,4,7,99,0 should output a 16-digit number. + 104,1125899906842624,99 should output the large number in the middle. + +The BOOST program will ask for a single input; run it in test mode by providing it the value 1. It will perform a series of checks on each opcode, output any opcodes (and the associated parameter modes) that seem to be functioning incorrectly, and finally output a BOOST keycode. + +Once your Intcode computer is fully functional, the BOOST program should report no malfunctioning opcodes when run in test mode; it should only output a single value, the BOOST keycode. What BOOST keycode does it produce? + +--- Part Two --- + +You now have a complete Intcode computer. + +Finally, you can lock on to the Ceres distress signal! You just need to boost your sensors using the BOOST program. + +The program runs in sensor boost mode by providing the input instruction the value 2. Once run, it will boost the sensors automatically, but it might take a few seconds to complete the operation on slower hardware. In sensor boost mode, the program will output a single value: the coordinates of the distress signal. + +Run the BOOST program in sensor boost mode. What are the coordinates of the distress signal? diff --git a/www/statics/aoc/2019/09_input.txt b/www/statics/aoc/2019/09_input.txt new file mode 100644 index 0000000..18c74f7 --- /dev/null +++ b/www/statics/aoc/2019/09_input.txt @@ -0,0 +1 @@ +1102,34463338,34463338,63,1007,63,34463338,63,1005,63,53,1102,3,1,1000,109,988,209,12,9,1000,209,6,209,3,203,0,1008,1000,1,63,1005,63,65,1008,1000,2,63,1005,63,904,1008,1000,0,63,1005,63,58,4,25,104,0,99,4,0,104,0,99,4,17,104,0,99,0,0,1102,1,21,1008,1101,427,0,1028,1102,23,1,1012,1101,32,0,1009,1101,37,0,1007,1102,1,892,1023,1102,27,1,1004,1102,1,38,1013,1102,1,20,1005,1101,0,29,1001,1101,0,22,1015,1102,1,35,1003,1101,0,39,1016,1102,34,1,1011,1101,899,0,1022,1102,195,1,1024,1101,36,0,1014,1101,0,24,1000,1102,1,31,1006,1101,0,28,1017,1101,422,0,1029,1102,1,33,1019,1102,1,26,1018,1102,1,0,1020,1102,25,1,1002,1102,712,1,1027,1101,0,190,1025,1101,0,715,1026,1102,1,1,1021,1101,30,0,1010,109,30,2105,1,-6,4,187,1106,0,199,1001,64,1,64,1002,64,2,64,109,-19,1206,10,211,1106,0,217,4,205,1001,64,1,64,1002,64,2,64,109,-13,1202,8,1,63,1008,63,28,63,1005,63,241,1001,64,1,64,1106,0,243,4,223,1002,64,2,64,109,8,1201,-2,0,63,1008,63,29,63,1005,63,263,1105,1,269,4,249,1001,64,1,64,1002,64,2,64,109,-9,2101,0,3,63,1008,63,24,63,1005,63,295,4,275,1001,64,1,64,1106,0,295,1002,64,2,64,109,12,2107,31,0,63,1005,63,317,4,301,1001,64,1,64,1106,0,317,1002,64,2,64,109,7,21101,40,0,0,1008,1016,43,63,1005,63,341,1001,64,1,64,1106,0,343,4,323,1002,64,2,64,109,-14,1208,-1,31,63,1005,63,363,1001,64,1,64,1106,0,365,4,349,1002,64,2,64,109,9,1208,-6,20,63,1005,63,387,4,371,1001,64,1,64,1105,1,387,1002,64,2,64,109,2,2102,1,-7,63,1008,63,31,63,1005,63,413,4,393,1001,64,1,64,1106,0,413,1002,64,2,64,109,21,2106,0,-6,4,419,1106,0,431,1001,64,1,64,1002,64,2,64,109,-25,2108,35,-6,63,1005,63,449,4,437,1106,0,453,1001,64,1,64,1002,64,2,64,109,3,21107,41,42,0,1005,1012,471,4,459,1105,1,475,1001,64,1,64,1002,64,2,64,109,7,21108,42,39,-2,1005,1017,495,1001,64,1,64,1105,1,497,4,481,1002,64,2,64,109,-8,1206,9,515,4,503,1001,64,1,64,1106,0,515,1002,64,2,64,109,4,1205,6,529,4,521,1105,1,533,1001,64,1,64,1002,64,2,64,109,-8,2107,26,-5,63,1005,63,553,1001,64,1,64,1106,0,555,4,539,1002,64,2,64,109,-6,2102,1,1,63,1008,63,26,63,1005,63,575,1105,1,581,4,561,1001,64,1,64,1002,64,2,64,109,10,2101,0,-8,63,1008,63,37,63,1005,63,601,1105,1,607,4,587,1001,64,1,64,1002,64,2,64,109,-19,1207,8,23,63,1005,63,627,1001,64,1,64,1106,0,629,4,613,1002,64,2,64,109,18,21101,43,0,3,1008,1013,43,63,1005,63,655,4,635,1001,64,1,64,1106,0,655,1002,64,2,64,109,-16,1207,6,25,63,1005,63,677,4,661,1001,64,1,64,1106,0,677,1002,64,2,64,109,25,21102,44,1,-4,1008,1015,44,63,1005,63,703,4,683,1001,64,1,64,1106,0,703,1002,64,2,64,109,17,2106,0,-9,1106,0,721,4,709,1001,64,1,64,1002,64,2,64,109,-16,1205,0,737,1001,64,1,64,1105,1,739,4,727,1002,64,2,64,109,-12,21107,45,44,5,1005,1013,759,1001,64,1,64,1106,0,761,4,745,1002,64,2,64,109,4,1201,-8,0,63,1008,63,27,63,1005,63,783,4,767,1106,0,787,1001,64,1,64,1002,64,2,64,109,-16,2108,25,4,63,1005,63,803,1105,1,809,4,793,1001,64,1,64,1002,64,2,64,109,27,21102,46,1,-5,1008,1018,43,63,1005,63,829,1106,0,835,4,815,1001,64,1,64,1002,64,2,64,109,-27,1202,8,1,63,1008,63,27,63,1005,63,857,4,841,1105,1,861,1001,64,1,64,1002,64,2,64,109,23,21108,47,47,-2,1005,1017,883,4,867,1001,64,1,64,1106,0,883,1002,64,2,64,109,-1,2105,1,5,1001,64,1,64,1106,0,901,4,889,4,64,99,21102,1,27,1,21102,915,1,0,1105,1,922,21201,1,29589,1,204,1,99,109,3,1207,-2,3,63,1005,63,964,21201,-2,-1,1,21102,1,942,0,1106,0,922,21202,1,1,-1,21201,-2,-3,1,21102,957,1,0,1105,1,922,22201,1,-1,-2,1106,0,968,21202,-2,1,-2,109,-3,2106,0,0 diff --git a/www/statics/aoc/2019/09_solution-1.ts b/www/statics/aoc/2019/09_solution-1.ts new file mode 100644 index 0000000..102c4fe --- /dev/null +++ b/www/statics/aoc/2019/09_solution-1.ts @@ -0,0 +1,213 @@ +namespace AdventOfCode2019_09_1 +{ + const DAY = 9; + const PROBLEM = 1; + + export async function run() + { + let input = await AdventOfCode.getInput(DAY); + if (input == null) return; + + const code = input.split(",").map(p => parseInt(p.trim())); + let runner = new Interpreter(code, [1]); + + runner.run(); + + AdventOfCode.output(DAY, PROBLEM, runner.output.reverse()[0].toString()); + } + + class Interpreter + { + program: InfMem; + input: number[]; + instructionpointer: number; + inputpointer: number; + output: number[]; + relative_base: number; + + constructor(prog: number[], input: number[]) + { + this.program = new InfMem(prog); + this.input = input; + this.instructionpointer = 0; + this.inputpointer = 0; + this.output = []; + this.relative_base = 0; + } + + run() + { + while(true) + { + const cmd = new Op(this.program.r(this.instructionpointer)); + let ipinc = true; + + if (cmd.opcode == OpCode.ADD) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 + p1; + cmd.setParameter(this, 2, pv); + } + else if (cmd.opcode == OpCode.MUL) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 * p1; + cmd.setParameter(this, 2, pv); + } + else if (cmd.opcode == OpCode.HALT) + { + return; + } + else if (cmd.opcode == OpCode.IN) + { + const pv = this.input[this.inputpointer]; + cmd.setParameter(this, 0, pv); + this.inputpointer++; + } + else if (cmd.opcode == OpCode.OUT) + { + const p0 = cmd.getParameter(this, 0); + this.output.push(p0); + console.log("# " + p0); + } + else if (cmd.opcode == OpCode.TJMP) + { + const p0 = cmd.getParameter(this, 0); + if (p0 != 0) { this.instructionpointer = cmd.getParameter(this, 1); ipinc=false; } + } + else if (cmd.opcode == OpCode.FJMP) + { + const p0 = cmd.getParameter(this, 0); + if (p0 == 0) { this.instructionpointer = cmd.getParameter(this, 1); ipinc=false; } + } + else if (cmd.opcode == OpCode.LT) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 < p1 ? 1 : 0; + cmd.setParameter(this, 2, pv); + } + else if (cmd.opcode == OpCode.EQ) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 == p1 ? 1 : 0; + cmd.setParameter(this, 2, pv); + } + else if (cmd.opcode == OpCode.ARB) + { + const p0 = cmd.getParameter(this, 0); + this.relative_base = this.relative_base+p0; + } + else throw "Unknown Op: " + cmd.opcode + " @ " + this.instructionpointer; + + if (ipinc) this.instructionpointer += 1 + cmd.parametercount; + } + } + } + + enum OpCode + { + ADD = 1, + MUL = 2, + IN = 3, + OUT = 4, + TJMP = 5, + FJMP = 6, + LT = 7, + EQ = 8, + ARB = 9, + HALT = 99, + } + + enum ParamMode + { + POSITION_MODE = 0, + IMMEDIATE_MODE = 1, + RELATIVE_MODE = 2, + } + + class Op + { + opcode: OpCode; + modes: ParamMode[]; + + name: string; + parametercount: number; + + constructor(v: number) + { + this.opcode = v%100; + v = Math.floor(v/100); + this.modes = []; + for(let i=0; i<4; i++) + { + this.modes.push(v%10); + v = Math.floor(v/10); + } + + if (this.opcode == OpCode.ADD) { this.name="ADD"; this.parametercount=3; } + else if (this.opcode == OpCode.MUL) { this.name="MUL"; this.parametercount=3; } + else if (this.opcode == OpCode.HALT) { this.name="HALT"; this.parametercount=0; } + else if (this.opcode == OpCode.IN) { this.name="IN"; this.parametercount=1; } + else if (this.opcode == OpCode.OUT) { this.name="OUT"; this.parametercount=1; } + else if (this.opcode == OpCode.TJMP) { this.name="TJMP"; this.parametercount=2; } + else if (this.opcode == OpCode.FJMP) { this.name="FJMP"; this.parametercount=2; } + else if (this.opcode == OpCode.LT) { this.name="LT"; this.parametercount=3; } + else if (this.opcode == OpCode.EQ) { this.name="EQ"; this.parametercount=3; } + else if (this.opcode == OpCode.ARB) { this.name="ARB"; this.parametercount=1; } + else throw "Unknown opcode: "+this.opcode; + } + + getParameter(proc: Interpreter, index: number): number + { + const prog = proc.program; + const ip = proc.instructionpointer; + + let p = prog.r(ip+1+index); + + if (this.modes[index] == ParamMode.POSITION_MODE) p = prog.r(p); + else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) p = p; + else if (this.modes[index] == ParamMode.RELATIVE_MODE) p = prog.r(proc.relative_base+p); + else throw "Unknown ParamMode: "+this.modes[index]; + + return p; + } + + setParameter(proc: Interpreter, index: number, value: number): void + { + const prog = proc.program; + const ip = proc.instructionpointer; + + let p = prog.r(ip+1+index); + + if (this.modes[index] == ParamMode.POSITION_MODE) prog.w(p, value); + else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) throw "Immediate mode not allowed in write"; + else if (this.modes[index] == ParamMode.RELATIVE_MODE) prog.w(proc.relative_base+p, value); + else throw "Unknown ParamMode: "+this.modes[index]; + } + } + + class InfMem + { + private data: { [_:number]:number } = {}; + + constructor(v: number[]) + { + for(let i=0; i parseInt(p.trim())); + let runner = new Interpreter(code, [2]); + + runner.run(); + + AdventOfCode.output(DAY, PROBLEM, runner.output.reverse()[0].toString()); + } + + class Interpreter + { + program: InfMem; + input: number[]; + instructionpointer: number; + inputpointer: number; + output: number[]; + relative_base: number; + + constructor(prog: number[], input: number[]) + { + this.program = new InfMem(prog); + this.input = input; + this.instructionpointer = 0; + this.inputpointer = 0; + this.output = []; + this.relative_base = 0; + } + + run() + { + while(true) + { + const cmd = new Op(this.program.r(this.instructionpointer)); + let ipinc = true; + + if (cmd.opcode == OpCode.ADD) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 + p1; + cmd.setParameter(this, 2, pv); + } + else if (cmd.opcode == OpCode.MUL) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 * p1; + cmd.setParameter(this, 2, pv); + } + else if (cmd.opcode == OpCode.HALT) + { + return; + } + else if (cmd.opcode == OpCode.IN) + { + const pv = this.input[this.inputpointer]; + cmd.setParameter(this, 0, pv); + this.inputpointer++; + } + else if (cmd.opcode == OpCode.OUT) + { + const p0 = cmd.getParameter(this, 0); + this.output.push(p0); + console.log("# " + p0); + } + else if (cmd.opcode == OpCode.TJMP) + { + const p0 = cmd.getParameter(this, 0); + if (p0 != 0) { this.instructionpointer = cmd.getParameter(this, 1); ipinc=false; } + } + else if (cmd.opcode == OpCode.FJMP) + { + const p0 = cmd.getParameter(this, 0); + if (p0 == 0) { this.instructionpointer = cmd.getParameter(this, 1); ipinc=false; } + } + else if (cmd.opcode == OpCode.LT) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 < p1 ? 1 : 0; + cmd.setParameter(this, 2, pv); + } + else if (cmd.opcode == OpCode.EQ) + { + const p0 = cmd.getParameter(this, 0); + const p1 = cmd.getParameter(this, 1); + const pv = p0 == p1 ? 1 : 0; + cmd.setParameter(this, 2, pv); + } + else if (cmd.opcode == OpCode.ARB) + { + const p0 = cmd.getParameter(this, 0); + this.relative_base = this.relative_base+p0; + } + else throw "Unknown Op: " + cmd.opcode + " @ " + this.instructionpointer; + + if (ipinc) this.instructionpointer += 1 + cmd.parametercount; + } + } + } + + enum OpCode + { + ADD = 1, + MUL = 2, + IN = 3, + OUT = 4, + TJMP = 5, + FJMP = 6, + LT = 7, + EQ = 8, + ARB = 9, + HALT = 99, + } + + enum ParamMode + { + POSITION_MODE = 0, + IMMEDIATE_MODE = 1, + RELATIVE_MODE = 2, + } + + class Op + { + opcode: OpCode; + modes: ParamMode[]; + + name: string; + parametercount: number; + + constructor(v: number) + { + this.opcode = v%100; + v = Math.floor(v/100); + this.modes = []; + for(let i=0; i<4; i++) + { + this.modes.push(v%10); + v = Math.floor(v/10); + } + + if (this.opcode == OpCode.ADD) { this.name="ADD"; this.parametercount=3; } + else if (this.opcode == OpCode.MUL) { this.name="MUL"; this.parametercount=3; } + else if (this.opcode == OpCode.HALT) { this.name="HALT"; this.parametercount=0; } + else if (this.opcode == OpCode.IN) { this.name="IN"; this.parametercount=1; } + else if (this.opcode == OpCode.OUT) { this.name="OUT"; this.parametercount=1; } + else if (this.opcode == OpCode.TJMP) { this.name="TJMP"; this.parametercount=2; } + else if (this.opcode == OpCode.FJMP) { this.name="FJMP"; this.parametercount=2; } + else if (this.opcode == OpCode.LT) { this.name="LT"; this.parametercount=3; } + else if (this.opcode == OpCode.EQ) { this.name="EQ"; this.parametercount=3; } + else if (this.opcode == OpCode.ARB) { this.name="ARB"; this.parametercount=1; } + else throw "Unknown opcode: "+this.opcode; + } + + getParameter(proc: Interpreter, index: number): number + { + const prog = proc.program; + const ip = proc.instructionpointer; + + let p = prog.r(ip+1+index); + + if (this.modes[index] == ParamMode.POSITION_MODE) p = prog.r(p); + else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) p = p; + else if (this.modes[index] == ParamMode.RELATIVE_MODE) p = prog.r(proc.relative_base+p); + else throw "Unknown ParamMode: "+this.modes[index]; + + return p; + } + + setParameter(proc: Interpreter, index: number, value: number): void + { + const prog = proc.program; + const ip = proc.instructionpointer; + + let p = prog.r(ip+1+index); + + if (this.modes[index] == ParamMode.POSITION_MODE) prog.w(p, value); + else if (this.modes[index] == ParamMode.IMMEDIATE_MODE) throw "Immediate mode not allowed in write"; + else if (this.modes[index] == ParamMode.RELATIVE_MODE) prog.w(proc.relative_base+p, value); + else throw "Unknown ParamMode: "+this.modes[index]; + } + } + + class InfMem + { + private data: { [_:number]:number } = {}; + + constructor(v: number[]) + { + for(let i=0; i 3, 'parts' => 2, 'title' => 'Crossed Wires', 'language' => 'ts', 'solutions' => ['399', '15678'] ], ['day' => 4, 'parts' => 2, 'title' => 'Secure Container', 'language' => 'ts', 'solutions' => ['979', '635'] ], ['day' => 5, 'parts' => 2, 'title' => 'Sunny with a Chance of Asteroids', 'language' => 'ts', 'solutions' => ['8332629', '8805067'] ], - + ['day' => 6, 'parts' => 2, 'title' => 'Universal Orbit Map', 'language' => 'ts', 'solutions' => ['333679', '370'] ], + ['day' => 7, 'parts' => 2, 'title' => 'Amplification Circuit', 'language' => 'ts', 'solutions' => ['844468', '4215746'] ], + ['day' => 8, 'parts' => 2, 'title' => 'Space Image Format', 'language' => 'ts', 'solutions' => ['2286', 'CJZLP'] ], + ['day' => 9, 'parts' => 2, 'title' => 'Sensor Boost', 'language' => 'ts', 'solutions' => ['2738720997', '50894'] ], ], ]; \ No newline at end of file