From 70475738ee45b6efb784a774d6943029a4913cb7 Mon Sep 17 00:00:00 2001 From: Akumatic Date: Wed, 4 Dec 2019 08:58:26 +0100 Subject: [PATCH] Added 2019 day 04 --- 2019/04/README.md | 34 ++++++++++++++++++++++++ 2019/04/code.py | 62 ++++++++++++++++++++++++++++++++++++++++++++ 2019/04/input.txt | 1 + 2019/04/solution.txt | 2 ++ 4 files changed, 99 insertions(+) create mode 100644 2019/04/README.md create mode 100644 2019/04/code.py create mode 100644 2019/04/input.txt create mode 100644 2019/04/solution.txt diff --git a/2019/04/README.md b/2019/04/README.md new file mode 100644 index 0000000..5d31168 --- /dev/null +++ b/2019/04/README.md @@ -0,0 +1,34 @@ +# 2019 Day 4: Secure Container +Copyright (c) Eric Wastl +#### [Direct Link](https://adventofcode.com/2019/day/4) + +## Part 1 + +You arrive at the Venus fuel depot only to discover it's protected by a password. The Elves had written the password on a sticky note, but someone threw it out. + +However, they do remember a few key facts about the password: + +- It is a six-digit number. +- The value is within the range given in your puzzle input. +- Two adjacent digits are the same (like `22` in `122345`). +- Going from left to right, the digits **never decrease**; they only ever increase or stay the same (like `111123` or `135679`). + +Other than the range rule, the following are true: + +- `111111` meets these criteria (double `11`, never decreases). +- `223450` does not meet these criteria (decreasing pair of digits `50`). +- `123789` does not meet these criteria (no double). + +**How many different passwords** within the range given in your puzzle input meet these criteria? + +## Part 2 + +An Elf just remembered one more important detail: the two adjacent matching digits **are not part of a larger group of matching digits**. + +Given this additional criterion, but still ignoring the range rule, the following are now true: + +- `112233` meets these criteria because the digits never decrease and all repeated digits are exactly two digits long. +- `123444` no longer meets the criteria (the repeated `44` is part of a larger group of `444`). +- `111122` meets the criteria (even though `1` is repeated more than twice, it still contains a double `22`). + +**How many different passwords** within the range given in your puzzle input meet all of the criteria? \ No newline at end of file diff --git a/2019/04/code.py b/2019/04/code.py new file mode 100644 index 0000000..1452be2 --- /dev/null +++ b/2019/04/code.py @@ -0,0 +1,62 @@ +""" https://adventofcode.com/2019/day/4 """ + +def readFile(): + with open(f"{__file__.rstrip('code.py')}input.txt", "r") as f: + return [int(vals) for vals in f.readline().split("-")] + +def getNumbers(min, max): + result = set() + for i in range(min, max+1): + nums = [i // 100000, (i // 10000) % 10, (i // 1000) % 10, + (i // 100) % 10, (i // 10) % 10, i % 10] + if isNotDecreasing(nums) and hasDoubleAdjacentValue(nums): + result.add(i) + return result + +def isNotDecreasing(nums): + for x in range(1, 6): + if nums[x] < nums[x - 1]: + return False + return True + +def hasDoubleAdjacentValue(nums): + for x in range(1, 5): + if nums[x] == nums[x - 1] or nums[x] == nums[x + 1]: + return True + return False + +def isNotPartOfBiggerGroup(nums): + for x in range(1, 6): + if nums[x] == nums[x - 1] and nums.count(nums[x]) == 2: + return True + return False + +def part1(vals : list): + return len(getNumbers(vals[0], vals[1])) + +def part2(vals : list): + result = set() + numbers = getNumbers(vals[0], vals[1] + 1) + for i in numbers: + nums = [i // 100000, (i // 10000) % 10, (i // 1000) % 10, + (i // 100) % 10, (i // 10) % 10, i % 10] + if isNotPartOfBiggerGroup(nums): + result.add(i) + return len(result) + +def test(): + assert(isNotDecreasing([1, 1, 1, 1, 1, 1]) == True) + assert(isNotDecreasing([2, 2, 3, 4, 5, 0]) == False) + assert(isNotDecreasing([1, 2, 3, 7, 8, 9]) == True) + assert(hasDoubleAdjacentValue([1, 1, 1, 1, 1, 1]) == True) + assert(hasDoubleAdjacentValue([2, 2, 3, 4, 5, 0]) == True) + assert(hasDoubleAdjacentValue([1, 2, 3, 7, 8, 9]) == False) + assert(isNotPartOfBiggerGroup([1,1,2,2,3,3]) == True) + assert(isNotPartOfBiggerGroup([1,2,3,4,4,4]) == False) + assert(isNotPartOfBiggerGroup([1,1,1,1,2,2]) == True) + +if __name__ == "__main__": + test() + vals = readFile() + print(f"Part 1: {part1(vals)}") + print(f"Part 2: {part2(vals)}") \ No newline at end of file diff --git a/2019/04/input.txt b/2019/04/input.txt new file mode 100644 index 0000000..cc2802f --- /dev/null +++ b/2019/04/input.txt @@ -0,0 +1 @@ +307237-769058 \ No newline at end of file diff --git a/2019/04/solution.txt b/2019/04/solution.txt new file mode 100644 index 0000000..dc57bb4 --- /dev/null +++ b/2019/04/solution.txt @@ -0,0 +1,2 @@ +Part 1: 889 +Part 2: 589 \ No newline at end of file