Jess Mitchell

Advent of Code 2017: Day 1 Solutions πŸŽ„

December 01, 2017

q: β€œThe captcha requires you to review a sequence of digits (your puzzle input) and find the sum of all digits that match the next digit in the list. The list is circular, so the digit after the last digit is the first digit in the list.

For example: 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit.

What is the solution to your captcha?”

a:

input = "818275977931166178424892653779931342156567268946849597948944469863818248114327524824136924486891794739281668741616818614613222585132742386168687517939432911753846817997473555693821316918473474459788714917665794336753628836231159578734813485687247273288926216976992516314415836985611354682821892793983922755395577592859959966574329787693934242233159947846757279523939217844194346599494858459582798326799512571365294673978955928416955127211624234143497546729348687844317864243859238665326784414349618985832259224761857371389133635711819476969854584123589566163491796442167815899539788237118339218699137497532932492226948892362554937381497389469981346971998271644362944839883953967698665427314592438958181697639594631142991156327257413186621923369632466918836951277519421695264986942261781256412377711245825379412978876134267384793694756732246799739464721215446477972737883445615664755923441441781128933369585655925615257548499628878242122434979197969569971961379367756499884537433839217835728263798431874654317137955175565253555735968376115749641527957935691487965161211853476747758982854811367422656321836839326818976668191525884763294465366151349347633968321457954152621175837754723675485348339261288195865348545793575843874731785852718281311481217515834822185477982342271937155479432673815629144664144538221768992733498856934255518875381672342521819499939835919827166318715849161715775427981485233467222586764392783699273452228728667175488552924399518855743923659815483988899924199449721321589476864161778841352853573584489497263216627369841455165476954483715112127465311353411346132671561568444626828453687183385215975319858714144975174516356117245993696521941589168394574287785233685284294357548156487538175462176268162852746996633977948755296869616778577327951858348313582783675149343562362974553976147259225311183729415381527435926224781181987111454447371894645359797229493458443522549386769845742557644349554641538488252581267341635761715674381775778868374988451463624332123361576518411234438681171864923916896987836734129295354684962897616358722633724198278552339794629939574841672355699222747886785616814449297817352118452284785694551841431869545321438468118"
arr = input.split("").collect{ |s| s.to_i }
counter = 0
total = 0

while counter < arr.length do
  if arr[counter] == arr[counter + 1]
    total = total + arr[counter]
  end
  counter = counter + 1
end

if arr[0] == arr[-1]
  total = total + arr[0]
end

puts total

solution: 1097


part deux: β€œNow, instead of considering the next digit, it wants you to consider the digit halfway around the circular list. That is, if your list contains 10 items, only include a digit in your sum if the digit 10/2 = 5 steps forward matches it. Fortunately, your list has an even number of elements.”

a:

arr = input.split("").collect{ |s| s.to_i }
total = 0
counter = 0
steps_ahead = arr.length / 2
first_half = arr.take(steps_ahead)
new_arr = arr + first_half

while counter < new_arr.length do
    if new_arr[counter] == new_arr[counter + steps_ahead]
        total = total + new_arr[counter]
    end
    counter = counter + 1
end

puts total

solution: 1188

i decided to try this in ruby instead of javascript since i’m less comfortable in ruby. check out the original questions here. happy coding!


Jess

Hi, I'm Jess. I write about software development, health, and how the two can relate. 🌿 Follow me on Twitter