Wednesday, December 26, 2018

Advent of Code 2015 - Day 7: Some Assembly Required

Part 1

Description

Solution

Today's problem is quite difficult for me. But it only made me more challenged. At first, I was thinking of using VBA code to automate the whole things. But, running the code takes forever, maybe because I don't know how to break the loop. But, then I realize, that this puzzle can be done also with matching and sorting. So, in my Excel file below, I split the input into 5 parts: two variables, one operator, the -> and variable 3 (the result).

The column G is for sorting those five columns. First I put this formula in the column (row 1 to 342, respectively):
G4: =SUM(IF(B4="",1,0),IF(C4="",1,0),ISNUMBER(D4))
This formula basically checks if the first variable and the operator is empty and the second is a number. Those meeting these conditions (signal in variable 3 is provided by variable 2) will give the sum of three. By sorting the columns (B-G) with value of G from largest to smallest, I can put those with numeric variable 2 in the top row. Then, I put this array formula in G6 (which has value less than 2):
G6: =SUM(IF($B6="",1,0),IF($D6=$F$4:INDIRECT(ADDRESS(ROW(G$5),6)),1,0))+SUM(IF(ISNUMBER($B6),1,0),IF($D6=$F$4:INDIRECT(ADDRESS(ROW(G$5),6)),1,0))+SUM(IF(ISNUMBER($D6),1,0),IF($B6=$F$4:INDIRECT(ADDRESS(ROW(G$5),6)),1,0))
The formula again checks if any variable 1 and variable 2 matches those already declared variable 3 (c and b respectively). Only if both match, or one variable match and another one is a number, that it will give values of one (true). Again, by sorting the columns (B-G) with value of G from largest to smallest, I can put those meeting the condition in the cells below the already declared variables. I repeat the process (with the help of a Macro) until the last row (G342). And, as I already guess, there is the desired output (signal provided to a) in the last row.

Now, to give value to variables 3, I made a custom function, called Operate:

Function Operate(a, b, c) As Double
If c = "AND" Then
    Operate = WorksheetFunction.Bitand(a, b)
ElseIf c = "OR" Then
    Operate = WorksheetFunction.Bitor(a, b)
ElseIf c = "LSHIFT" Then
    Operate = WorksheetFunction.Bitlshift(a, b)
ElseIf c = "RSHIFT" Then
    Operate = WorksheetFunction.Bitrshift(a, b)
ElseIf c = "NOT" Then
    Operate = 65535 - b
ElseIf c = "" Then
    Operate = b
End If
End Function

This function, basically put together all bitwise function needed (BITAND, BITOR, BITLSHIFT, and BITRSHIFT), with addition of BITNOT (there is no such Excel function) and direct signal. Therefore, I just need to put this formula in column H:
H6: =Operate(IFERROR(INDEX($H$4:$H5,MATCH(B6,$F$4:$F$342,0)),B6),IFERROR(INDEX($H$4:$H5,MATCH(D6,$F$4:$F$342,0)),D6),C6)
The function IFERROR is needed because sometimes the variable (1 or 2) doesn't match with variable 3 and the formula returns an error value. The output is what I get in the last row.

Part 2

Description

Solution

For the part 2, I put the value of  b from the part 1 in the place of 44430 and calculate a using the same formula as above.
Share:

0 komentar:

Recent Posts

Categories

Blog Archive