HACKvent 2015: Day 6
Challenge We are given the following string:
1 |
HR7DYQ3ON4TC6U2AFAZDGJK3J44TYXZUNRCTATK2GEZDGJR5JJUC6ULUFQZEI5L6HY====== |
Solution We can deduce by the amount of padding that this is Base32 encoded string. We use an online decoder and get:
1 |
<~<Cno&/S@(23%[O9<_4lE0MZ123&=Jh/Qt,2Du~> |
Then we try various other things to decode the above string. I will admit I failed to find a proper solution until I received…read more.
HACKvent 2015: Day 5
Challenge You are given the following file: Download PDF File Solution So we open the PDF file to find 1 QR image in a ball. We scan it and we get the text Oooops ! . We then use an online tool to extract all images from the PDF (link). I do this because PDF files store…read more.
Advent Of Code: Day 7
Challenge
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 |
# Day 7 - Part 1 and Part 2 # # To find Part 1 answer: python day7.py | grep ' a = ' # To find Part 2 answer: Edit value of b in input.txt to result from part 1 and rerun script import re from z3 import * def z3VarsAddIfMissing(wire): if wire not in z3Vars: z3Vars[wire] = BitVec(wire, 32) #32 bit integers z3Vars = {} s = Solver() with open('input.txt') as f: lines = f.readlines() for line in lines: # Get command -> store variables res = re.search(r'(.*)\s*->\s*(.*)', line) command, store_var = res.group(1).strip(), res.group(2) # Store var z3VarsAddIfMissing(store_var) # Add logical constraints to solver # Store other variables in commands if they don't exit on the way # Bitwise AND res = re.search(r'(.*?)\s*AND\s*(.*)', command) if res and res.group(1) != '' and res.group(2) != '': z3VarsAddIfMissing(res.group(2)) # If first component is an integer if res.group(1).isdigit(): s.add(z3Vars[store_var] == int(res.group(1)) & z3Vars[res.group(2)]) else: z3VarsAddIfMissing(res.group(1)) s.add(z3Vars[store_var] == z3Vars[res.group(1)] & z3Vars[res.group(2)]) continue # Bitwise OR res = re.search(r'(.*?)\s*OR\s*(.*)', command) if res and res.group(1) != '' and res.group(2) != '': z3VarsAddIfMissing(res.group(1)) z3VarsAddIfMissing(res.group(2)) s.add(z3Vars[store_var] == z3Vars[res.group(1)] | z3Vars[res.group(2)]) continue # Bit LSHIFT res = re.search(r'(.*?)\s*LSHIFT\s*(\d*)', command) if res and res.group(1) != '' and res.group(2) != '': z3VarsAddIfMissing(res.group(1)) s.add(z3Vars[store_var] == z3Vars[res.group(1)] << int(res.group(2))) continue # Bit RSHIFT res = re.search(r'(.*?)\s*RSHIFT\s*(\d*)', command) if res and res.group(1) != '' and res.group(2) != '': z3VarsAddIfMissing(res.group(1)) s.add(z3Vars[store_var] == z3Vars[res.group(1)] >> int(res.group(2))) continue # Bitwise NOT res = re.search(r'NOT\s*(.*)', command) if res and res.group(1) != '': z3VarsAddIfMissing(res.group(1)) s.add(z3Vars[store_var] == (~z3Vars[res.group(1)]) & 0xFFFF ) # 0xFFFF bitwise and to constrain value to [0, 2^16) continue # Assign operation (variable or integer) == res = re.search(r'^(.*)$', command) if res and res.group(1) != '': if res.group(1).isdigit(): s.add(z3Vars[store_var] == res.group(1)) else: z3VarsAddIfMissing(res.group(1)) s.add(z3Vars[store_var] == z3Vars[res.group(1)]) continue # Print answers set_option(max_args=10000000, max_lines=1000000, max_depth=10000000, max_visited=1000000) print(s.check()) print(s.model()) |
Solution Wow this one was fun! I decided to use Z3Py, a theorem prover module for python. My solution essentially parses each command using regexp and adds the various boolean rules to a solver object. Finally, the theorem solver checks all possible values and prints out the results of every ‘wire’. Determining the answer is…read more.
HACKvent 2015: Day 7
Challenge
1 2 3 |
Clue: Imagine your quick response for today. 0x1fc137f82a7a0dd05d76ebbcbb74815d82c720ff555fc018801f78baaf93c051d55e46346fd16dd457f54451df65fcec3a493768ffc00948aff4154e090627753ffebafa7ddd568860a87a3fd88eb |
Solution This challenge was very easy to solve as I had completed the teaser earlier. First I convert the hexadecimal number provided (as it starts with 0x) to binary. The result is 625 bits (which does not divide nicely by 8 so it is probably not an ASCII message). However, its likely to be a…read more.
HACKvent 2015: Day 4
Challenge
1 2 3 |
Clue: Crypto - a classic / simple one HOlAfOVWOqVd1o6q7u5Vj8Mv----- |
Solution At first I was convinced this was a Vigenere cipher but this is not the case. After a clue from M from the hacking-lab IRC channel I was able to figure out the cipher used is the Scytale cipher. This is somewhat obvious as the - characters from the nugget are all grouped at the end…read more.
HACKvent 2015: Day 3
Challenge This image was provided: Solution We clearly have to either scan every QR code or find the correct QR code. So we simply use an online tool that splits up GIF files based on their frames (link). We download all the frames (29 images exist so its likely that each frame corresponds to one…read more.
HACKvent 2015: Day 2
Challenge
1 2 3 |
Clue: I wonder if this is the native language of one of you! pagh wa'vatlh netlh wa'maH wa'maH wa' SaD wa' SaD wa'vatlh wa' wa'maH wa'maH wa'vatlh wa'maH wa' wa'maH wa'maH netlh pagh wa'maH wa' wa'vatlh wa' wa'vatlh SaD SaD wa' wa'vatlh netlh wa'maH wa' wa'maH wa'maH wa'vatlh wa' wa'maH wa'maH wa' wa' wa'vatlh SaD wa' wa'maH wa'maH wa'maH wa'vatlh wa'maH SaD wa'maH wa' wa'maH wa'maH wa' wa' wa' wa'maH wa'vatlh wa' wa'vatlh wa' SaD wa' SaD wa'maH wa' wa' wa'maH wa' SaD wa'maH wa' wa'maH wa'maH wa'vatlh wa'maH wa' wa'vatlh wa' wa'maH wa' wa' wa'maH wa' netlh wa'maH wa'vatlh wa' wa' wa' wa'vatlh wa'maH wa' wa'maH wa'maH SaD wa' wa'vatlh wa'maH SaD wa'vatlh wa' wa'maH SaD wa' wa'maH SaD |
Solution After some Googling I realise this is the Klingon language from Star Trek. I try some online translators (including Bing!) and it is clear that the message is a series of numbers that contain 0 or 1 only. This is most likely binary that represents ASCII text. I write a small script to convert each component…read more.
Advent Of Code: Day 6
The Challenge
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 |
--- Day 6: Probably a Fire Hazard --- Because your neighbors keep defeating you in the holiday house decorating contest year after year, you've decided to deploy one million lights in a 1000x1000 grid. Furthermore, because you've been especially nice this year, Santa has mailed you instructions on how to display the ideal lighting configuration. Lights in your grid are numbered from 0 to 999 in each direction; the lights at each corner are at 0,0, 0,999, 999,999, and 999,0. The instructions include whether to turn on, turn off, or toggle various inclusive ranges given as coordinate pairs. Each coordinate pair represents opposite corners of a rectangle, inclusive; a coordinate pair like 0,0 through 2,2 therefore refers to 9 lights in a 3x3 square. The lights all start turned off. To defeat your neighbors this year, all you have to do is set up your lights by doing the instructions Santa sent you in order. For example: turn on 0,0 through 999,999 would turn on (or leave on) every light. toggle 0,0 through 999,0 would toggle the first line of 1000 lights, turning off the ones that were on, and turning on the ones that were off. turn off 499,499 through 500,500 would turn off (or leave off) the middle four lights. After following the instructions, how many lights are lit? Your puzzle answer was 569999. --- Part Two --- You just finish implementing your winning light pattern when you realize you mistranslated Santa's message from Ancient Nordic Elvish. The light grid you bought actually has individual brightness controls; each light can have a brightness of zero or more. The lights all start at zero. The phrase turn on actually means that you should increase the brightness of those lights by 1. The phrase turn off actually means that you should decrease the brightness of those lights by 1, to a minimum of zero. The phrase toggle actually means that you should increase the brightness of those lights by 2. What is the total brightness of all lights combined after following Santa's instructions? For example: turn on 0,0 through 0,0 would increase the total brightness by 1. toggle 0,0 through 999,999 would increase the total brightness by 2000000. Your puzzle answer was 17836115. |
Solution I split up the solutions for part 1 and part 2. Was almost tempted to go for a numpy and scipy sparse matrix solution but a 1000×1000 matrix is small enough for a dense matrix. Part 1
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 |
# Day 6 - Part 1 import re lightMatrix = [[False for x in range(1000)] for x in range(1000)] lightOnCounter = 0 with open('input.txt') as f: for line in f: # Match command res = re.search(r'(turn on|turn off|toggle)\s*(\d*?),(\d*?)\s*through\s(\d*?),(\d*)', line) command, lower_X, lower_Y, upper_X, upper_Y = res.group(1), int(res.group(2)), int(res.group(3)), int(res.group(4)), int(res.group(5)) # Perform actions toggle = False if command == 'turn off': data = False elif command == 'turn on': data = True elif command == 'toggle': toggle = True # Apply action for x_val in xrange (lower_X, upper_X + 1): for y_val in xrange (lower_Y, upper_Y + 1): if toggle: lightMatrix[x_val][y_val] = not lightMatrix[x_val][y_val] else: lightMatrix[x_val][y_val] = data # Get all true lights for i in lightMatrix: for isLightOn in i: lightOnCounter += isLightOn #bool to int implicitly # answer print "Total number of lights that are on:", lightOnCounter |
Part 2
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 |
# Day 6 - Part 2 import re lightMatrix = [[0 for x in range(1000)] for x in range(1000)] brightnessCounter = 0 with open('input.txt') as f: for line in f: # Match command res = re.search(r'(turn on|turn off|toggle)\s*(\d*?),(\d*?)\s*through\s(\d*?),(\d*)', line) command, lower_X, lower_Y, upper_X, upper_Y = res.group(1), int(res.group(2)), int(res.group(3)), int(res.group(4)), int(res.group(5)) # Perform actions if command == 'turn off': data = -1 elif command == 'turn on': data = 1 elif command == 'toggle': data = 2 # Apply action for x_val in xrange (lower_X, upper_X + 1): for y_val in xrange (lower_Y, upper_Y + 1): lightMatrix[x_val][y_val] += data if lightMatrix[x_val][y_val] + data >= 0 else 0 # Get brightness level for i in lightMatrix: for brightness in i: brightnessCounter += brightness # answer print "Total brightness level is:", brightnessCounter |
Modifying the Peggle Deluxe Game
So some time ago I modified a the video game Peggle Deluxe which is a PC game by Popcap Games. My little baby brother had really gotten into the game and I wanted to add him as a character! I found a baby photo of him and thought I’d simply have to replace one image in…read more.