[Blogging Intensifies]

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

  • About
  • Privacy Policy

Advent of Code 2020 – Day 15

December 15, 2020

Hey look, Day 15 is another one that took a super long time to calculate, though it was one of the easiest to code. It’s a fancy memory game, for Elves who apparently have computer brains. You start with a series of numbers, then the next number you say, is the difference between when the previous number was said, the last two times.

For example if you start saying this sequence, with 0,1,2,3 , the next number is 0, because 3 has only been said once, after that it’s 4, because 0 was said as the 5th number and the first number (5-1). After that it’s 0 again, because 4 has not been said before, then it’s 2, because 0 is at 7 and 5 (7-5), following that is 5, because 2 is at position 8 and 5 (8-5), and so on.

Both parts were the same problem, just with a different number of iterations, 2020 and 30,000,000. Calculating the 2020th number was quick, calculating the 30 millionth number, not so much.

Anyway, here is my code:


values = [0,8,15,2,12,1,4]

current = 0


while current < 30000000-7:
  check = values[-1]
  last = len(values) - values[::-1].index(check)
  if check in values[:last-1]:  
    temp=values[:last-1]
    temp.reverse()
    #print len(values)
    sec_last = len(values) - temp.index(check)-1
    next = last-sec_last
  else:
    next = 0
  values.append(next)
  #print next

  #print len(values)
  current+=1

print values[:-1]
print len(values)

I had some extra prints in there for the 2020, but printing everything for 30 million just slows it down. The final length print is just to verify that it’s giving me the correct iteration value. The main this this could use for clean up is to replace the while loop “-7” subtraction with subtracting a variable equal to the length of the initial values set, so that you could potentially feed it a value set that is larger or smaller.

The trickiest part here was figuring out the positions of the last two occurrences of a number, which meant counting backwards through the array of values. For the second number, I ended up using a reverse copy of the array, because I kept getting screwball answers trying to do it directly like I had for the first number. I would have liked to make it work with the more elegant solution but I didn’t have time to keep working on it.

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