Added 2019 day 04
This commit is contained in:
parent
d30f09f3ba
commit
70475738ee
34
2019/04/README.md
Normal file
34
2019/04/README.md
Normal file
@ -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?
|
62
2019/04/code.py
Normal file
62
2019/04/code.py
Normal file
@ -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)}")
|
1
2019/04/input.txt
Normal file
1
2019/04/input.txt
Normal file
@ -0,0 +1 @@
|
||||
307237-769058
|
2
2019/04/solution.txt
Normal file
2
2019/04/solution.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Part 1: 889
|
||||
Part 2: 589
|
Loading…
x
Reference in New Issue
Block a user