Advent of code 2017 - Day 1
Update - If you want to follow along but don't know how to run the python code in order to get the answer so you can read the part 2 of the challenge (which reveals itself only after you successfully complete part 1), I wrote a quick log that you can check out
Alright. Day 1 challenge. Begin!
Day 1 challenge description - http://adventofcode.com/2017/day/1
December 14th 2017
15:07
So the first thing I do is to read the puzzle given and kind of draw up a flowchart of what the solution might look like
15:15
Once that's done I like to start writing tests. In a case like this where most of the work is iterating over the numbers and doing some fairly simple equal checking I can't see anything complex to test. Therefore I'll start by writing tests for each of the given examples in the puzzle.
(Pausing for a bit here. This was basically my lunch break for the day :D. Gotta get back to work).
December 15th 2017
11:00
Picking up where I left off. Starting the tests.
11:11
And we have our first working test for the value of 1122 where the sum is 3. Again, I won't be pasting the description of the problem here. It's all in the link above (respecting the author's requirements)
- Overall I feel like the current solution is pretty verbose. But I'm not going to be spending too much time on it. The harder problems are up ahead.
def solve_captcha(captcha_string):
length_of_captcha_string = len(captcha_string)
values_to_add=[]
i = 0
while i<len(captcha_string):
if i==(len(captcha_string)-1):
current_value = captcha_string[i]
next_value = captcha_string[0]
else:
current_value = captcha_string[i]
next_value = captcha_string[i+1]
if current_value == next_value:
values_to_add.append(int(current_value))
i+=1
return sum(values_to_add)
11:13
Time to test the rest of the test cases!
11:16
Confirmed! All tests are go :). Time to run it on my input. I'll need to write some extra code to read the input but nothing else needs to change.
11:23
Christmas begins! My first star has been registered :)
11:25
Commit is up
11:27
Solving part 2
- As is standard for Advent of Code, part 2 is a variant of part 1 and in this case, the input is the same. I'll be writing a new method called
solve_captcha_part_two
to work this out. - Wrote a log to help people run code so they can get their answer to part 1 to follow along with part 2.
12:00
Drew up a quick flow for me to work with
12:07
And the first test worked for 1212 giving an output of 6! This is still all fairly straightforward so I expect the tests for the next values should work just as well.
def solve_captcha_part_two(captcha_string):
halfway_length = len(captcha_string)//2
input_length = len(captcha_string)
values_to_add = []
i = 0
while i<input_length:
current_value = captcha_string[i]
if i+halfway_length>=input_length:
next_value = captcha_string[i + halfway_length - input_length]
else:
next_value = captcha_string[i + halfway_length]
if current_value == next_value:
values_to_add.append(int(current_value))
i+=1
return sum(values_to_add)
12:12
Done deal
- Time to run against the final output!
- Done!
➜ advent-of-code-2017 git:(master) ✗ python3 -m python.day1solution
=== Part 1 ===
1150
=== Part 2 ===
1064
12:16
: Completed and pushed to Github
Posted on December 14 2017 by Adnan Issadeen