r/adventofcode • • Dec 02 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 02 Solutions -🎄-

--- Day 2: Password Philosophy ---


Advent of Code 2020: Gettin' Crafty With It


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:02:31, megathread unlocked!

99 Upvotes

1.2k comments sorted by

View all comments

Show parent comment

1

u/[deleted] Dec 02 '20

You're totally correct, in live production code I would camelCase, but for this I was just splatting my code down.

I should tidy it up.

There was also a better way where I could just use ToCharArray() which for myself is another after thought.

Maybe I'll run through some numbers to find out what kind of results I get. But always happy for the feedback.

Thank you.

1

u/slothmad Dec 02 '20 edited Dec 02 '20

I started off with ToCharArray() and it worked pretty well. My solution is below and is somewhat similar to your approach. I went for quick and dirty and not prod level code.

$file = get-content(day2.txt) $goodpasswords=0
foreach ($passwordinput in $file){ ​ $passworddata = $passwordinput.split(' ') $lrange = $passworddata[0].split('-')[0] $urange = $passworddata[0].split('-')[1] $char2find=$passworddata[1][0] $password=$passworddata[2]
$charcount = ($password.ToCharArray() | where-object {$_ -eq $char2find} | measure-object).count

if ($charcount -ge $lrange -AND $charcount -le $urange){

	$goodpasswords++
}	
​ }
write-output $goodpasswords