Wednesday, December 26, 2018

Advent of Code 2015 - Day 8: Matchsticks

Part 1

Description

Solution

Unfortunately, (as far as I know) Excel or VBA doesn't support escape string. So, I have to do it with substitute. 

I put this formula:
B3: =IFERROR(SUBSTITUTE(A3,MID(A3,SEARCH("\x??",A3),4),CHAR(HEX2DEC(MID(MID(A3,SEARCH("\x??",A3),4),3,2)))),A3)
This formula search for hexadecimal notation and replace it with corresponding character. I have to run it several times (column B to E) because each notation is unique. Next, I put the following:
F3: =SUBSTITUTE(SUBSTITUTE(E3,"\"&CHAR(34),CHAR(34)),"\\","\")
The formula replace any "\\" with "\" and "\" with doublequote. Then, I calculate the length of these strings (the original and the modified one). For the modified one, I have to subtract it by 2 for opening and closing double quotes.

From calculating this length of stings, I can also know whether I still need another column for running the first formula or not. Subtracting the length of original string and the modified one and summing them give me the desired output.

Part 2

Description

Solution

For the part 2, I just need to modify the formula, count the length of the strings, subtract them from the length of the original ones, and sum up them. The formula looks like this.

H3: =SUBSTITUTE(SUBSTITUTE(A3,"\","\\"),CHAR(34),"\"&CHAR(34))

Share:

0 komentar:

Recent Posts

Categories

Blog Archive