r/adventofcode • u/daggerdragon • Dec 02 '20
SOLUTION MEGATHREAD -🎄- 2020 Day 02 Solutions -🎄-
--- Day 2: Password Philosophy ---
Advent of Code 2020: Gettin' Crafty With It
- T-4 days until unlock!
- Full details and rules are in the Submissions Megathread
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
View all comments
2
u/nilreference Dec 02 '20 edited Dec 02 '20
F# script (fsx)
First
open System.IOopen System.Text.RegularExpressionslet read (filePath:string) = seq {use sr = new StreamReader (filePath)while not sr.EndOfStream doyield sr.ReadLine ()}type passwordRecord = {min:int;max:int;mustContain:char;password:string}let regex = new Regex(@"(?<min>[0-9]*)-(?<max>[0-9]*)\s(?<char>[a-z]?):\s(?<pwd>[a-z0-9]*)")let toPwdRecord txt : passwordRecord=let r = regex.Match txt{min = (int r.Groups.["min"].Value); max= (int r.Groups.["max"].Value); mustContain = char r.Groups.["char"].Value; password = r.Groups.["pwd"].Value}let validatePassword record =record.password|> Seq.toList|> Seq.filter (fun f -> f = record.mustContain)|> Seq.length|> fun l -> (>=) l record.min && (<=) l record.max"input.txt"|> read|> Seq.map toPwdRecord|> Seq.filter validatePassword|> Seq.length|> printfn "Valid passwords: %i"Second
open System.IOopen System.Text.RegularExpressionslet read (filePath:string) = seq {use sr = new StreamReader (filePath)while not sr.EndOfStream doyield sr.ReadLine ()}type passwordRecord = {posOne:int;posTwo:int;mustContain:char;password:string}let regex = new Regex(@"(?<min>[0-9]*)-(?<max>[0-9]*)\s(?<char>[a-z]?):\s(?<pwd>[a-z0-9]*)")let toPwdRecord txt : passwordRecord=let r = regex.Match txt{posOne = (int r.Groups.["min"].Value);posTwo = (int r.Groups.["max"].Value);mustContain = char r.Groups.["char"].Value;password = r.Groups.["pwd"].Value}let validatePassword record =(record.password.[record.posOne-1] = record.mustContain) <> (record.password.[record.posTwo-1] = record.mustContain)"input.txt"|> read|> Seq.map toPwdRecord|> Seq.filter validatePassword|> Seq.length|> printfn "Valid passwords: %i"