[Blogging Intensifies]

Technology, Projects, Linux, Coding, Internet of Things, Music, Books, Life...

  • About
  • Privacy Policy

Advent of Code 2020 – Day 8

December 8, 2020

Day 8 may be the first real trouble I have had so far. Part 1 wasn’t too bad, but Part 2 has had me stuck for a bit.

The job is to troubleshoot bad code for a handheld device. It gets stuck in an infinite loop. The input is a string of commands that either increment an accumulator, or jump forward or back in the code. Sometimes there is no action.

Part 1 was to execute the code without ever repeating a command.

with open('day8data.txt') as f:
    lines = [line.rstrip() for line in f]


accumulator=0
location=0
commands_executed=[]

while(location<=len(lines)):
  command = lines[location][:3]
  amount = int(lines[location][4:])

  #print command
  #print amount
  
  if command == 'acc':
    accumulator+=amount
    location+=1
  if command == 'jmp':
    location+=amount
  if command == 'nop':
    location+=1
  
  if location not in commands_executed:
    commands_executed.append(location)
  else:
    break
  #print commands_executed



print accumulator

Basically, run each command, tracking which have been executed, then, when you reach a repeat, which will cause a loop, you exit.

Part 2 had me for a bit, but mostly because I missed what it was looking for. The object is to change one of the jump or no operation commands, to cause the loop to break and the code to complete. My original approach and thought was, that at some point in the loop, the code would swoop down to the bottom of the commands, and then loop away. So I was checking for if the code was close to the end, then changing the jump or no-operation there.

What I needed to be doing was changing each one, and then testing it through the solve loop to see if it would complete or not (no loops).

Basically, run the code, anytime there is a jump or no-op, flip it and check for a loop. If not, keep going on the original path.

def solver(accumulator, location, commands_executed, switcher):

  while(True):
    if lines[location] == "":
      print accumulator
      return False

    command = lines[location][:3]
    amount = int(lines[location][4:])

    #print command
    #print amount
  
    if switcher==1 and command == 'jmp':
      switcher = 0
      command = 'nop'
    if switcher==1 and command == 'nop':
      switcher = 0
      command = 'jmp'


    if command == 'acc':
      accumulator+=amount
      location+=1
    if command == 'jmp':
      location+=amount
    if command == 'nop':
      location+=1

    if location not in commands_executed:
      commands_executed.append(location)
    else:
      break
    #print commands_executed

  return True


with open('day8data.txt') as f:
    lines = [line.rstrip() for line in f]


accumulator=0
location=0
breaker = True
commands_executed=[]

while(breaker):
  while(True):
    command = lines[location][:3]
    amount = int(lines[location][4:])

    #print command
    #print amount
    if command == "":
      print accumulator

    if command == 'acc':
      accumulator+=amount
      location+=1
    if command == 'jmp':
      breaker = solver(accumulator,location, commands_executed, 1)
      location+=amount
    if command == 'nop':
      breaker = solver(accumulator,location, commands_executed, 1)
      location+=1

    if location not in commands_executed:
      commands_executed.append(location)
    else:
      break
    #print commands_executed

I tried to get some recursion going here but I couldn’t work it out so I just did it with repeated code blocks. Because this is why I dislike recursion. If I need to pass around and check for changing conditionals, I may as well put them in a regular loop.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Reddit (Opens in new window)

Related

Posted in: Advent of Code 2020 Tagged: Advent of Code 2020, Coding, Python
Mastodon LinkedIn email
Instagram Instagram GitHub
JoshMiller.net
Lameazoid.com

Categories

  • collapsCat options: Array ( [title] => Categories [showPostCount] => 1 [inExclude] => exclude [inExcludeCats] => [showPosts] => 0 [showPages] => 0 [linkToCat] => 1 [olderThan] => 0 [excludeAll] => 0 [catSortOrder] => ASC [catSort] => catName [postSortOrder] => ASC [postSort] => postTitle [expand] => 0 [defaultExpand] => [debug] => 1 [postTitleLength] => 0 [catfeed] => none [taxonomy] => category [post_type] => post [postDateAppend] => after [postDateFormat] => m/d [showPostDate] => 1 [useCookies] => 1 [postsBeforeCats] => 1 [expandCatPost] => 1 [showEmptyCat] => 1 [showTopLevel] => 1 [useAjax] => 0 [customExpand] => [customCollapse] => [style] => kubrick [accordion] => 1 [title_link] => [addMisc] => 1 [addMiscTitle] => [number] => 3 [includeCatArray] => Array ( ) [expandSym] => ► [collapseSym] => ▼ ) postsToExclude: Array ( ) CATEGORY QUERY RESULTS Array ( [0] => WP_Term Object ( [term_id] => 486 [name] => Advent of Code 2020 [slug] => advent-of-code-2020 [term_group] => 0 [term_taxonomy_id] => 486 [taxonomy] => category [description] => [parent] => 172 [count] => 12 [filter] => raw ) [1] => WP_Term Object ( [term_id] => 156 [name] => Android [slug] => android [term_group] => 0 [term_taxonomy_id] => 156 [taxonomy] => category [description] => [parent] => 155 [count] => 3 [filter] => raw ) [2] => WP_Term Object ( [term_id] => 135 [name] => Arduino [slug] => arduino [term_group] => 0 [term_taxonomy_id] => 135 [taxonomy] => category [description] => [parent] => 153 [count] => 8 [filter] => raw ) [3] => WP_Term Object ( [term_id] => 438 [name] => Books [slug] => books [term_group] => 0 [term_taxonomy_id] => 438 [taxonomy] => category [description] => [parent] => 436 [count] => 4 [filter] => raw ) [4] => WP_Term Object ( [term_id] => 368 [name] => CHIP [slug] => chip [term_group] => 0 [term_taxonomy_id] => 368 [taxonomy] => category [description] => [parent] => 153 [count] => 5 [filter] => raw ) [5] => WP_Term Object ( [term_id] => 172 [name] => Coding [slug] => programming [term_group] => 0 [term_taxonomy_id] => 172 [taxonomy] => category [description] => [parent] => 153 [count] => 13 [filter] => raw ) [6] => WP_Term Object ( [term_id] => 247 [name] => Copyright and You [slug] => copyright-and-you [term_group] => 0 [term_taxonomy_id] => 247 [taxonomy] => category [description] => [parent] => 154 [count] => 3 [filter] => raw ) [7] => WP_Term Object ( [term_id] => 155 [name] => Devices [slug] => devices [term_group] => 0 [term_taxonomy_id] => 155 [taxonomy] => category [description] => [parent] => 0 [count] => 5 [filter] => raw ) [8] => WP_Term Object ( [term_id] => 281 [name] => Drafts [slug] => drafts [term_group] => 0 [term_taxonomy_id] => 281 [taxonomy] => category [description] => [parent] => 0 [count] => 0 [filter] => raw ) [9] => WP_Term Object ( [term_id] => 366 [name] => Hardware [slug] => hardware [term_group] => 0 [term_taxonomy_id] => 366 [taxonomy] => category [description] => [parent] => 153 [count] => 1 [filter] => raw ) [10] => WP_Term Object ( [term_id] => 373 [name] => Hardware [slug] => hardware-what-i-use [term_group] => 0 [term_taxonomy_id] => 373 [taxonomy] => category [description] => [parent] => 159 [count] => 4 [filter] => raw ) [11] => WP_Term Object ( [term_id] => 243 [name] => Home Security [slug] => home-security [term_group] => 0 [term_taxonomy_id] => 243 [taxonomy] => category [description] => [parent] => 153 [count] => 2 [filter] => raw ) [12] => WP_Term Object ( [term_id] => 446 [name] => Language [slug] => language [term_group] => 0 [term_taxonomy_id] => 446 [taxonomy] => category [description] => [parent] => 436 [count] => 1 [filter] => raw ) [13] => WP_Term Object ( [term_id] => 436 [name] => Lifestyle [slug] => lifestyle [term_group] => 0 [term_taxonomy_id] => 436 [taxonomy] => category [description] => [parent] => 0 [count] => 2 [filter] => raw ) [14] => WP_Term Object ( [term_id] => 224 [name] => Linux & Open Source [slug] => linux [term_group] => 0 [term_taxonomy_id] => 224 [taxonomy] => category [description] => [parent] => 279 [count] => 2 [filter] => raw ) [15] => WP_Term Object ( [term_id] => 153 [name] => Maker [slug] => maker [term_group] => 0 [term_taxonomy_id] => 153 [taxonomy] => category [description] => [parent] => 0 [count] => 1 [filter] => raw ) [16] => WP_Term Object ( [term_id] => 437 [name] => Music [slug] => music [term_group] => 0 [term_taxonomy_id] => 437 [taxonomy] => category [description] => [parent] => 436 [count] => 14 [filter] => raw ) [17] => WP_Term Object ( [term_id] => 395 [name] => My DIY Projects [slug] => my-diy-projects [term_group] => 0 [term_taxonomy_id] => 395 [taxonomy] => category [description] => [parent] => 153 [count] => 3 [filter] => raw ) [18] => WP_Term Object ( [term_id] => 367 [name] => Non-Tech [slug] => non-tech [term_group] => 0 [term_taxonomy_id] => 367 [taxonomy] => category [description] => [parent] => 153 [count] => 3 [filter] => raw ) [19] => WP_Term Object ( [term_id] => 154 [name] => Opinion/Editorial [slug] => articles [term_group] => 0 [term_taxonomy_id] => 154 [taxonomy] => category [description] => [parent] => 0 [count] => 3 [filter] => raw ) [20] => WP_Term Object ( [term_id] => 491 [name] => Organizing [slug] => organizing [term_group] => 0 [term_taxonomy_id] => 491 [taxonomy] => category [description] => [parent] => 436 [count] => 5 [filter] => raw ) [21] => WP_Term Object ( [term_id] => 279 [name] => OS [slug] => os [term_group] => 0 [term_taxonomy_id] => 279 [taxonomy] => category [description] => [parent] => 0 [count] => 0 [filter] => raw ) [22] => WP_Term Object ( [term_id] => 242 [name] => PCs [slug] => pcs [term_group] => 0 [term_taxonomy_id] => 242 [taxonomy] => category [description] => [parent] => 155 [count] => 4 [filter] => raw ) [23] => WP_Term Object ( [term_id] => 384 [name] => Photography [slug] => photography [term_group] => 0 [term_taxonomy_id] => 384 [taxonomy] => category [description] => [parent] => 159 [count] => 2 [filter] => raw ) [24] => WP_Term Object ( [term_id] => 146 [name] => Privacy [slug] => privacy [term_group] => 0 [term_taxonomy_id] => 146 [taxonomy] => category [description] => [parent] => 154 [count] => 3 [filter] => raw ) [25] => WP_Term Object ( [term_id] => 142 [name] => Raspberry Pi [slug] => raspberry-pi [term_group] => 0 [term_taxonomy_id] => 142 [taxonomy] => category [description] => [parent] => 153 [count] => 9 [filter] => raw ) [26] => WP_Term Object ( [term_id] => 407 [name] => Security [slug] => security [term_group] => 0 [term_taxonomy_id] => 407 [taxonomy] => category [description] => [parent] => 166 [count] => 1 [filter] => raw ) [27] => WP_Term Object ( [term_id] => 133 [name] => Site News [slug] => site-news [term_group] => 0 [term_taxonomy_id] => 133 [taxonomy] => category [description] => [parent] => 0 [count] => 0 [filter] => raw ) [28] => WP_Term Object ( [term_id] => 136 [name] => Social Media [slug] => social-media [term_group] => 0 [term_taxonomy_id] => 136 [taxonomy] => category [description] => [parent] => 154 [count] => 2 [filter] => raw ) [29] => WP_Term Object ( [term_id] => 160 [name] => Software [slug] => software [term_group] => 0 [term_taxonomy_id] => 160 [taxonomy] => category [description] => [parent] => 159 [count] => 5 [filter] => raw ) [30] => WP_Term Object ( [term_id] => 241 [name] => Synology NAS [slug] => synology-nas [term_group] => 0 [term_taxonomy_id] => 241 [taxonomy] => category [description] => [parent] => 155 [count] => 4 [filter] => raw ) [31] => WP_Term Object ( [term_id] => 166 [name] => Technology [slug] => technology [term_group] => 0 [term_taxonomy_id] => 166 [taxonomy] => category [description] => [parent] => 0 [count] => 7 [filter] => raw ) [32] => WP_Term Object ( [term_id] => 424 [name] => The Basement [slug] => the-basement [term_group] => 0 [term_taxonomy_id] => 424 [taxonomy] => category [description] => [parent] => 153 [count] => 6 [filter] => raw ) [33] => WP_Term Object ( [term_id] => 413 [name] => The Cloud [slug] => the-cloud [term_group] => 0 [term_taxonomy_id] => 413 [taxonomy] => category [description] => [parent] => 153 [count] => 3 [filter] => raw ) [34] => WP_Term Object ( [term_id] => 1 [name] => Uncategorized [slug] => uncategorized [term_group] => 0 [term_taxonomy_id] => 1 [taxonomy] => category [description] => [parent] => 0 [count] => 0 [filter] => raw ) [35] => WP_Term Object ( [term_id] => 159 [name] => What I Use [slug] => what-i-use [term_group] => 0 [term_taxonomy_id] => 159 [taxonomy] => category [description] => [parent] => 0 [count] => 0 [filter] => raw ) [36] => WP_Term Object ( [term_id] => 280 [name] => Windows [slug] => windows [term_group] => 0 [term_taxonomy_id] => 280 [taxonomy] => category [description] => [parent] => 279 [count] => 2 [filter] => raw ) [37] => WP_Term Object ( [term_id] => 207 [name] => Windows Phone [slug] => windows-phone [term_group] => 0 [term_taxonomy_id] => 207 [taxonomy] => category [description] => [parent] => 155 [count] => 3 [filter] => raw ) ) POST QUERY: POST QUERY RESULTS
  • ►Devices (19)
    • Android (3)
    • PCs (4)
    • Synology NAS (4)
    • Windows Phone (3)
  • ►Drafts (0)
  • ►Lifestyle (26)
    • Books (4)
    • Language (1)
    • Music (14)
    • Organizing (5)
  • ▼Maker (66)
    • Arduino (8)
    • CHIP (5)
    • ▼Coding (25)
      • Advent of Code 2020 (12)
    • Hardware (1)
    • Home Security (2)
    • My DIY Projects (3)
    • Non-Tech (3)
    • Raspberry Pi (9)
    • The Basement (6)
    • The Cloud (3)
  • ►Opinion/Editorial (11)
    • Copyright and You (3)
    • Privacy (3)
    • Social Media (2)
  • ►OS (4)
    • Linux & Open Source (2)
    • Windows (2)
  • ►Site News (0)
  • ►Technology (8)
    • Security (1)
  • ►Uncategorized (0)
  • ►What I Use (11)
    • Hardware (4)
    • Photography (2)
    • Software (5)

Hosted on…


Help support hosting with our referral link!

Copyright © 2022 [Blogging Intensifies].

Me WordPress Theme by themehall.com