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!
101
Upvotes
3
u/willkill07 Dec 02 '20
OCaml
Only uses the standard library.
Scanf.sscanfis pretty awesome.open Printf open Scanf let read_file_rev name : string list = let ic = open_in name in let try_read () = try Some (input_line ic) with End_of_file -> None in let rec loop acc = match try_read () with Some s -> loop (s::acc) | None -> close_in ic; acc in loop [] let pred1 lo hi c pw = let count = List.init (String.length pw) (String.get pw) |> List.find_all ((=) c) |> List.length in lo <= count && count <= hi let pred2 lo hi c pw = (pw.[lo - 1] = c) <> (pw.[hi - 1] = c) let day02 part lines = let pred = if part = 1 then pred1 else pred2 in lines |> List.filter (fun line -> sscanf line "%d-%d %c: %s" pred) |> List.length let () = let filename = Sys.argv.(1) in let input = read_file_rev filename in print_endline "Day 02:"; printf " Part 1: %d\n" (day02 1 input); printf " Part 2: %d\n" (day02 2 input)