aoc-2021-rust

Advent of Code 2021 Solutions in Rust
git clone https://git.sinitax.com/sinitax/aoc-2021-rust
Log | Files | Refs | README | sfeed.txt

part1 (4349B)


      1--- Day 24: Arithmetic Logic Unit ---
      2
      3Magic smoke starts leaking from the submarine's arithmetic logic unit (ALU). Without the ability to
      4perform basic arithmetic and logic functions, the submarine can't produce cool patterns with its
      5Christmas lights!
      6
      7It also can't navigate. Or run the oxygen system.
      8
      9Don't worry, though - you probably have enough oxygen left to give you enough time to build a new
     10ALU.
     11
     12The ALU is a four-dimensional processing unit: it has integer variables w, x, y, and z. These
     13variables all start with the value 0. The ALU also supports six instructions:
     14
     15
     16 - inp a - Read an input value and write it to variable a.
     17
     18 - add a b - Add the value of a to the value of b, then store the result in variable a.
     19
     20 - mul a b - Multiply the value of a by the value of b, then store the result in variable a.
     21
     22 - div a b - Divide the value of a by the value of b, truncate the result to an integer, then store
     23the result in variable a. (Here, "truncate" means to round the value toward zero.)
     24
     25 - mod a b - Divide the value of a by the value of b, then store the remainder in variable a. (This
     26is also called the modulo operation.)
     27
     28 - eql a b - If the value of a and b are equal, then store the value 1 in variable a. Otherwise,
     29store the value 0 in variable a.
     30
     31
     32In all of these instructions, a and b are placeholders; a will always be the variable where the
     33result of the operation is stored (one of w, x, y, or z), while b can be either a variable or a
     34number. Numbers can be positive or negative, but will always be integers.
     35
     36The ALU has no jump instructions; in an ALU program, every instruction is run exactly once in order
     37from top to bottom. The program halts after the last instruction has finished executing.
     38
     39(Program authors should be especially cautious; attempting to execute div with b=0 or attempting to
     40execute mod with a<0 or b<=0  will cause the program to crash and might even damage the ALU. These
     41operations are never intended in any serious ALU program.)
     42
     43For example, here is an ALU program which takes an input number, negates it, and stores it in x:
     44
     45inp x
     46mul x -1
     47
     48Here is an ALU program which takes two input numbers, then sets z to 1 if the second input number is
     49three times larger than the first input number, or sets z to 0 otherwise:
     50
     51inp z
     52inp x
     53mul z 3
     54eql z x
     55
     56Here is an ALU program which takes a non-negative integer as input, converts it into binary, and
     57stores the lowest (1's) bit in z, the second-lowest (2's) bit in y, the third-lowest (4's) bit in x,
     58and the fourth-lowest (8's) bit in w:
     59
     60inp w
     61add z w
     62mod z 2
     63div w 2
     64add y w
     65mod y 2
     66div w 2
     67add x w
     68mod x 2
     69div w 2
     70mod w 2
     71
     72Once you have built a replacement ALU, you can install it in the submarine, which will immediately
     73resume what it was doing when the ALU failed: validating the submarine's model number. To do this,
     74the ALU will run the MOdel Number Automatic Detector program (MONAD, your puzzle input).
     75
     76Submarine model numbers are always fourteen-digit numbers consisting only of digits 1 through 9. The
     77digit 0 cannot appear in a model number.
     78
     79When MONAD checks a hypothetical fourteen-digit model number, it uses fourteen separate inp
     80instructions, each expecting a single digit of the model number in order of most to least
     81significant. (So, to check the model number 13579246899999, you would give 1 to the first inp
     82instruction, 3 to the second inp instruction, 5 to the third inp instruction, and so on.) This means
     83that when operating MONAD, each input instruction should only ever be given an integer value of at
     84least 1 and at most 9.
     85
     86Then, after MONAD has finished running all of its instructions, it will indicate that the model
     87number was valid by leaving a 0 in variable z. However, if the model number was
     88invalid, it will leave some other non-zero value in z.
     89
     90MONAD imposes additional, mysterious restrictions on model numbers, and legend says the last copy of
     91the MONAD documentation was eaten by a tanuki. You'll need to figure out what MONAD does some other
     92way.
     93
     94To enable as many submarine features as possible, find the largest valid fourteen-digit model number
     95that contains no 0 digits. What is the largest model number accepted by MONAD?
     96
     97