From ca17d18358f39f3294eb827ee5c39a30135569f8 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Fri, 15 Nov 2024 17:10:56 +0100 Subject: [PATCH] Added 2017/04 --- 2017/04/4.md | 53 +++++++++++++++++++++++++++++++++++++++++++++ 2017/04/solution.py | 40 ++++++++++++++++++++++++++++++++++ 2017/04/test2 | 5 +++++ 3 files changed, 98 insertions(+) create mode 100644 2017/04/4.md create mode 100644 2017/04/solution.py create mode 100644 2017/04/test2 diff --git a/2017/04/4.md b/2017/04/4.md new file mode 100644 index 0000000..419c8c2 --- /dev/null +++ b/2017/04/4.md @@ -0,0 +1,53 @@ +## \-\-- Day 4: High-Entropy Passphrases \-\-- + +A new system policy has been put in place that requires all accounts to +use a *passphrase* instead of simply a pass*word*. A passphrase consists +of a series of words (lowercase letters) separated by spaces. + +To ensure security, a valid passphrase must contain no duplicate words. + +For example: + +- `aa bb cc dd ee` is valid. +- `aa bb cc dd aa` is not valid - the word `aa` appears more than + once. +- `aa bb cc dd aaa` is valid - `aa` and `aaa` count as different + words. + +The system\'s full passphrase list is available as your puzzle input. +*How many passphrases are valid?* + +Your puzzle answer was `383`. + +## \-\-- Part Two \-\-- {#part2} + +For added security, [yet another system +policy]{title="Because as everyone knows, the number of rules is proportional to the level of security."} +has been put in place. Now, a valid passphrase must contain no two words +that are anagrams of each other - that is, a passphrase is invalid if +any word\'s letters can be rearranged to form any other word in the +passphrase. + +For example: + +- `abcde fghij` is a valid passphrase. +- `abcde xyz ecdab` is not valid - the letters from the third word can + be rearranged to form the first word. +- `a ab abc abd abf abj` is a valid passphrase, because *all* letters + need to be used when forming another word. +- `iiii oiii ooii oooi oooo` is valid. +- `oiii ioii iioi iiio` is not valid - any of these words can be + rearranged to form any other word. + +Under this new system policy, *how many passphrases are valid?* + +Your puzzle answer was `265`. + +Both parts of this puzzle are complete! They provide two gold stars: +\*\* + +At this point, you should [return to your Advent calendar](/2017) and +try another puzzle. + +If you still want to see it, you can [get your puzzle +input](4/input). diff --git a/2017/04/solution.py b/2017/04/solution.py new file mode 100644 index 0000000..f396773 --- /dev/null +++ b/2017/04/solution.py @@ -0,0 +1,40 @@ +#!/bin/python3 +import sys +from pprint import pprint +import numpy as np + +input_f = sys.argv[1] + +count = 0 + +######################################### +# # +# Part 1 # +# # +######################################### + +with open(input_f) as file: + for line in file: + line = line.rstrip().split() + u,c = np.unique(line,return_counts=True) + if u[c > 1].size == 0: + count += 1 + +print(count) + +######################################### +# # +# Part 2 # +# # +######################################### +count = 0 + +with open(input_f) as file: + for line in file: + line = line.rstrip().split() + for idx,x in enumerate(line): + line[idx] = ''.join(sorted(x)) + u,c = np.unique(line,return_counts=True) + if u[c > 1].size == 0: + count += 1 +print(count) diff --git a/2017/04/test2 b/2017/04/test2 new file mode 100644 index 0000000..d4acfe1 --- /dev/null +++ b/2017/04/test2 @@ -0,0 +1,5 @@ +abcde fghij +abcde xyz ecdab +a ab abc abd abf abj +iiii oiii ooii oooi oooo +oiii ioii iioi iiio