## Advent Of Code: Day 5

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 |
--- Day 5: Doesn't He Have Intern-Elves For This? --- Santa needs help figuring out which strings in his text file are naughty or nice. A nice string is one with all of the following properties: It contains at least three vowels (aeiou only), like aei, xazegov, or aeiouaeiouaeiou. It contains at least one letter that appears twice in a row, like xx, abcdde (dd), or aabbccdd (aa, bb, cc, or dd). It does not contain the strings ab, cd, pq, or xy, even if they are part of one of the other requirements. For example: ugknbfddgicrmopn is nice because it has at least three vowels (u...i...o...), a double letter (...dd...), and none of the disallowed substrings. aaa is nice because it has at least three vowels and a double letter, even though the letters used by different rules overlap. jchzalrnumimnmhp is naughty because it has no double letter. haegwjzuvuyypxyu is naughty because it contains the string xy. dvszwmarrgswjxmb is naughty because it contains only one vowel. How many strings are nice? --- Part Two --- Realizing the error of his ways, Santa has switched to a better model of determining whether a string is naughty or nice. None of the old rules apply, as they are all clearly ridiculous. Now, a nice string is one with all of the following properties: It contains a pair of any two letters that appears at least twice in the string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps). It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa. For example: qjhvhtzxzqqjkmpb is nice because is has a pair that appears twice (qj) and a letter that repeats with exactly one letter between them (zxz). xxyxx is nice because it has a pair that appears twice and a letter that repeats with one between, even though the letters used by each rule overlap. uurcxstgmygtbstg is naughty because it has a pair (tg) but no repeat with a single letter between them. ieodomkazucvgmuy is naughty because it has a repeating letter with one between (odo), but no pair that appears twice. How many strings are nice under these new rules? |

Solution Another quick and simple Python solution!

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 |
# Day 5 - Parts 1 and 2 import re nice_counter = 0 improved_nice_counter = 0 naughty_list = ['ab', 'cd', 'pq', 'xy'] def nice_string(str): if len(re.sub(r'[^aeiou]', '', str)) < 3: return False # Appear twice if not re.search(r'(.)\1', str): return False # No naughty words for nw in naughty_list: if nw in str: return False return True def improved_nice_string(str): # Check for double pair if not re.search(r'(.)(.).*\1\2', str): #some wild boobies appear return False # letter repeat if not re.search(r'(.).\1', str): return False return True with open('input.txt') as f: for line in f: if nice_string(line): nice_counter += 1 if improved_nice_string(line): improved_nice_counter += 1 # answer print "Total number of nice strings:", nice_counter print "Improved total number of nice strings:", improved_nice_counter |

## Advent Of Code: Day 4

The Challenge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
--- Day 4: The Ideal Stocking Stuffer --- Santa needs help mining some AdventCoins (very similar to bitcoins) to use as gifts for all the economically forward-thinking little girls and boys. To do this, he needs to find MD5 hashes which, in hexadecimal, start with at least five zeroes. The input to the MD5 hash is some secret key (your puzzle input, given below) followed by a number in decimal. To mine AdventCoins, you must find Santa the lowest positive number (no leading zeroes: 1, 2, 3, ...) that produces such a hash. For example: If your secret key is abcdef, the answer is 609043, because the MD5 hash of abcdef609043 starts with five zeroes (000001dbbfa...), and it is the lowest such number to do so. If your secret key is pqrstuv, the lowest number it combines with to make an MD5 hash starting with five zeroes is 1048970; that is, the MD5 hash of pqrstuv1048970 looks like 000006136ef.... Your puzzle answer was 346386. --- Part Two --- Now find one that starts with six zeroes. |

Solution Because speed was not an issue in this case, I simply used python again to solve the problem. The script is a little slow taking ~10 seconds to compute the answer in my case. A C/C++ solution would be orders of magnitude faster.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# Day 4 - Parts 1 and 2 import hashlib key = 'iwrupvqb' i = 0 #counter five_zero_index = six_zero_index = 0 # Loop until criteria met while True: i += 1 hash = hashlib.md5(key + str(i)).hexdigest() if not five_zero_index and hash.startswith('0' * 5): five_zero_index = i if not six_zero_index and hash.startswith('0' * 6): six_zero_index = i # exit if (five_zero_index and six_zero_index): break # Print answers print "Five zero index is:", five_zero_index print "Six zero index is:", six_zero_index |

## 8891 – Santas leak (Hackvent 2015 Teaser)

This was a fun and interesting challenge to kick off Hackvent. It took me 4 days to solve the problem! I strongly suggest you attempt the problem yourself before reading by solution. You can get the single image you need to do the challenge here (santa.png): Download Starting Image These are all the files/scripts I ended up with…read more.

## Advent Of Code: Day 3

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 |
--- Day 3: Perfectly Spherical Houses in a Vacuum --- Santa is delivering presents to an infinite two-dimensional grid of houses. He begins by delivering a present to the house at his starting location, and then an elf at the North Pole calls him via radio and tells him where to move next. Moves are always exactly one house to the north (^), south (v), east (>), or west (<). After each move, he delivers another present to the house at his new location. However, the elf back at the north pole has had a little too much eggnog, and so his directions are a little off, and Santa ends up visiting some houses more than once. How many houses receive at least one present? For example: > delivers presents to 2 houses: one at the starting location, and one to the east. ^>v< delivers presents to 4 houses in a square, including twice to the house at his starting/ending location. ^v^v^v^v^v delivers a bunch of presents to some very lucky children at only 2 houses. --- Part Two --- The next year, to speed up the process, Santa creates a robot version of himself, Robo-Santa, to deliver presents with him. Santa and Robo-Santa start at the same location (delivering two presents to the same starting house), then take turns moving based on instructions from the elf, who is eggnoggedly reading from the same script as the previous year. This year, how many houses receive at least one present? For example: ^v delivers presents to 3 houses, because Santa goes north, and then Robo-Santa goes south. ^>v< now delivers presents to 3 houses, and Santa and Robo-Santa end up back where they started. ^v^v^v^v^v now delivers presents to 11 houses, with Santa going one direction and Robo-Santa going the other. |

Solution A rather interesting challenge. I wasn’t too happy with my solution but it works. I split up parts 1 and parts 2 so the code can be read more easily. Part 1 – Santa Solo Trip

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 |
# Day 3 - Part 1 from itertools import product X = Y = 0 visited = [(0,0)] #(0,0) always visited unique_houses_visited = 1 #house at (0,0) with open('input.txt') as f: for c in f.read(): if c == '^': Y += 1 elif c == '<': X -= 1 elif c == 'v': Y -= 1 elif c == '>': X += 1 # Check if new house if (X, Y) not in visited: # New house unique_houses_visited += 1 visited.append((X, Y)) #answer print "Unique houses visited:", unique_houses_visited |

Part 2 – Santa + Robo Santa I didn’t like the use of the santasTurn boolean…read more.

## Advent Of Code: Day 2

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 |
--- Day 2: I Was Told There Would Be No Math --- The elves are running low on wrapping paper, and so they need to submit an order for more. They have a list of the dimensions (length l, width w, and height h) of each present, and only want to order exactly as much as they need. Fortunately, every present is a box (a perfect right rectangular prism), which makes calculating the required wrapping paper for each gift a little easier: find the surface area of the box, which is 2*l*w + 2*w*h + 2*h*l. The elves also need a little extra paper for each present: the area of the smallest side. For example: A present with dimensions 2x3x4 requires 2*6 + 2*12 + 2*8 = 52 square feet of wrapping paper plus 6 square feet of slack, for a total of 58 square feet. A present with dimensions 1x1x10 requires 2*1 + 2*10 + 2*10 = 42 square feet of wrapping paper plus 1 square foot of slack, for a total of 43 square feet. All numbers in the elves' list are in feet. How many total square feet of wrapping paper should they order? --- Part Two --- The elves are also running low on ribbon. Ribbon is all the same width, so they only have to worry about the length they need to order, which they would again like to be exact. The ribbon required to wrap a present is the shortest distance around its sides, or the smallest perimeter of any one face. Each present also requires a bow made out of ribbon as well; the feet of ribbon required for the perfect bow is equal to the cubic feet of volume of the present. Don't ask how they tie the bow, though; they'll never tell. For example: A present with dimensions 2x3x4 requires 2+2+3+3 = 10 feet of ribbon to wrap the present plus 2*3*4 = 24 feet of ribbon for the bow, for a total of 34 feet. A present with dimensions 1x1x10 requires 1+1+1+1 = 4 feet of ribbon to wrap the present plus 1*1*10 = 10 feet of ribbon for the bow, for a total of 14 feet. How many total feet of ribbon should they order? |

Solution This challenge was very simple. I whipped up a short python script that solves it. The use of sorted to get the second smallest integer is not pretty but insignificant with such small numbers.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Day 2 - Part 1 and 2 with open('input.txt') as f: lines = f.readlines() totalPaper = 0 totalRibbon = 0 for line in lines: l,w,h = map(int, line.split('x')) totalPaper += (2*l*w + 2*w*h + 2*h*l) + min(l*w, w*h, h*l) totalRibbon += min(l,w,h)*2 + sorted([l,w,h])[1] * 2 + (l*w*h) # answers print "Total wrapping paper needed:", totalPaper print "Total ribbon needed:", totalRibbon |

## Advent Of Code: Day 1

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 |
--- Day 1: Not Quite Lisp --- Santa was hoping for a white Christmas, but his weather machine's "snow" function is powered by stars, and he's fresh out! To save Christmas, he needs you to collect fifty stars by December 25th. Collect stars by helping Santa solve puzzles. Two puzzles will be made available on each day in the advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! Here's an easy puzzle to warm you up. Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time. An opening parenthesis, (, means he should go up one floor, and a closing parenthesis, ), means he should go down one floor. The apartment building is very tall, and the basement is very deep; he will never find the top or bottom floors. For example: (()) and ()() both result in floor 0. ((( and (()(()( both result in floor 3. ))((((( also results in floor 3. ()) and ))( both result in floor -1 (the first basement level). ))) and )())()) both result in floor -3. To what floor do the instructions take Santa? --- Part Two --- Now, given the same instructions, find the position of the first character that causes him to enter the basement (floor -1). The first character in the instructions has position 1, the second character has position 2, and so on. For example: ) causes him to enter the basement at character position 1. ()()) causes him to enter the basement at character position 5. What is the position of the character that causes Santa to first enter the basement? |

Solution Before part 2 of the problem was revealed I decided to tackle the problem using the command line. I cam up with the following command:

1 2 |
# Day 1 - Part 1 echo $((`cat input.txt | tr -d '\)' | wc -c` - `cat input.txt | tr -d '\(' | wc -c`)) |

We echo the input and remove the ) symbol which leaves only ( and we use wc to count the number of ( symbols which tells us how many…read more.

## 5020 – Password protected ZIP

To solve this challenge I simply performed a dictionary attack on the zip file until I had discovered the correct password. Linux tools available are fairly slow and may only test 1000-10000 passwords each second so I decided to use Accent Zip password recovery which is a commercial tool capable of testing upto 600000 passwords a…read more.

## Fixing Mixed Content warnings using CRONjobs

So if you, like myself, have a HTTPs only website you may have noticed that your green bar, green label or security shield (image below) disappears if your webpage fetches an image from another website over HTTP and not HTTPS. Chrome Secure Green Lock Now this isn’t an issue in itself which is why all…read more.