Skip to content

Instantly share code, notes, and snippets.

@4Kaylum
Last active December 4, 2017 11:54
Show Gist options
  • Save 4Kaylum/fac4e8f4626720b6cbf326c1f9e87fac to your computer and use it in GitHub Desktop.
Save 4Kaylum/fac4e8f4626720b6cbf326c1f9e87fac to your computer and use it in GitHub Desktop.
def get_value(captcha:int) -> int:
'''
Gets the captcha value for the given integer
Advent of Code day 1
A captcha value is determined by the value of n being added to a total if
n+1 is the same number as n. This wraps round back to the start, so the end
value can be equal to the first.
Parameters:
captcha: int
The integer input of what needs to be worked out
Returns:
int
The value that has been worked out according to the day 1 spec
'''
captcha = str(captcha)
val = 0
for i, o in enumerate(captcha):
try:
if captcha[i+1] == o:
val += int(o)
except IndexError:
if captcha[0] == o:
val += int(o)
return val
def get_value_part_2(captcha:int) -> int:
'''
Gets the captcha value for the given integer
Advent of Code day 1 pt 2
Parameters:
captcha: int
The integer input of what needs to be worked out
Returns:
int
The value that has been worked out according to the day 1 spec
'''
captcha = str(captcha)
x = int(len(captcha) / 2)
captcha_list = [captcha[:x], captcha[x:]]
val = 0
for i, o in enumerate(captcha_list[0]):
if captcha_list[1][i] == o:
val += (int(o) * 2)
return val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment