[Blogging Intensifies]

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

  • About
  • Privacy Policy

Advent of Code 2020 – Day 2

December 2, 2020

Day two is a little more complicated than Day 1 was. Today’s challenge is to take a blob of passwords, and verify if they are acceptable or not, per the “company standard” at the time of the creation of each password.

For example:

1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc

For part 1, on line 1, the letter a should appear at least 1 time and at most 3 times. For line 2, the letter b should appear at least 1 time and at most 3 times. For line 3, the letter c should appear at least 2 times and at most 9 times.

My solution for Day 2, Part 1 is below:

number_valid = 0
toolow = 0
toohigh = 0
number_of_entries = 0

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

for x in lines:
  dashloc = x.find("-")
  spaceloc = x.find(" ")
  colloc = x.find(":")
  mincount=int(x[0:dashloc])
  maxcount=int(x[int(dashloc)+1:spaceloc])
  limitchar=x[int(spaceloc+1):int(colloc)]
  password = x[int(colloc+2):]
  checkvalue = int(password.count(limitchar))
  if(mincount <= checkvalue <= maxcount):
    number_valid=number_valid+1
  if(checkvalue<mincount):
    toolow+=1
  if(checkvalue>maxcount):
    toohigh+=1
  if(mincount>=maxcount):
    print "Problem?"

print number_valid
#print toolow
#print toohigh
#print number_valid+toolow+toohigh

On a note, I’ve got some debugging bits still in there, that are commented out. After reading the file in, I’ve initialized some variables I’ll be using in the code. Then I start looping through the data.

This is where my janky code really gets to shine, as I am sure there is a better way to handle this. First step is to locate the special characters separating the data, from the exampe, “1-3 a:abcde”, these are a dash, a space and a colon. With these located, I’ve extracted the min and max values, the letter I’m tracking, and the password itself.

After getting these core values, it’s simple enough to count the occurrences of the letter, then verify it against the min/max values and count how many are valid.

I had a bit of trouble at first because I had used “password = x[int(colloc+2):-1]” instead of “password = x[int(colloc+2):]”. I’m not a master of a lot of programming languages, but I am familiar with enough of them that I get the syntax confused a lot (I keep forgetting the colon on ifs and loops in Python). I forget which language uses -1 for “go to tot he end of a range”, but I am pretty sure there is one, because I did this a lot at first.

Part 2 changes things up a bit. Instead of the first numbers representing a range of how many, it demands that the special letter appears at one of those two positions, but not both.

My solution for part 2 is below:

number_valid = 0
number_of_entries = 0

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

for x in lines:
  first=0
  second=0
  dashloc = x.find("-")
  spaceloc = x.find(" ")
  colloc = x.find(":")
  mincount=int(x[0:dashloc])-1
  maxcount=int(x[int(dashloc)+1:spaceloc])-1
  limitchar=x[int(spaceloc+1):int(colloc)]
  password = x[int(colloc+2):]
  if(password[mincount]==limitchar):
    first=1
  if(password[maxcount]==limitchar):
    second=1
  if ((first==1) or (second==1)):
    if(first !=second):
      number_valid+=1

print number_valid
#print toolow
#print toohigh
#print number_valid+toolow+toohigh

More sloppy code, i didn’t even change the variable names. Now, instead of counting and comparing, I’m checking for the special character in each position, and toggling a variable if it’s there or not, then making sure it’s nor present in both. This is where the new variables “first” and “second” come in.

This biggest challenge on Day 2 was that it required more manipulation of the input data.

Github Repository of my Solutions

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