This commit is contained in:
cupcakearmy 2020-12-04 06:40:52 +01:00
parent 67af2ffd9d
commit 603010eb60
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
5 changed files with 1104 additions and 0 deletions

View File

@ -2,4 +2,6 @@
Here are my solutions for the advent of code 2020 🎄🎅
Note that the exact solutions are different for some people.
`/solutions/:day/*`

14
solutions/4/README.md Normal file
View File

@ -0,0 +1,14 @@
# 4
This one was a lot of parsing, but nothing regexp can't do.
The first is quite straight forward, just check that all but `cid` are present.
The second was a bit of validation for each field, but again some simple regexp and number checking and the job is done 🙂
<details>
<summary>Solutions</summary>
<ol>
<li>206</li>
<li>123</li>
</ol>
</details>

1023
solutions/4/data.txt Normal file

File diff suppressed because it is too large Load Diff

52
solutions/4/main.py Normal file
View File

@ -0,0 +1,52 @@
from os.path import join, dirname
import re
def validate_chunk(chunk, extended=False):
parts = re.split(' |\n', chunk.strip())
password = dict(map(lambda p: p.split(":"), parts))
required = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']
if not all(item in password.keys() for item in required):
return False
if not extended:
return True
if not 1920 <= int(password['byr']) <= 2002:
return False
if not 2010 <= int(password['iyr']) <= 2020:
return False
if not 2020 <= int(password['eyr']) <= 2030:
return False
tmp = password['hgt']
hgt = int(tmp[:-2])
unit = tmp[-2:]
if not unit in ['cm', 'in']:
return False
if unit == 'cm' and not 150 <= hgt <= 193:
return False
if unit == 'in' and not 59 <= hgt <= 76:
return False
if not re.match(r'^#[\dabcdef]{6}$', password['hcl']):
return False
if not re.match(r'^\d{9}$', password['pid']):
return False
if password['ecl'] not in ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']:
return False
return True
data = join(dirname(__file__), 'data.txt')
with open(data) as f:
chunks = re.split('\n\n+', f.read().strip())
total_simple = 0
total_extended = 0
for chunk in chunks:
total_simple += int(validate_chunk(chunk))
total_extended += int(validate_chunk(chunk, extended=True))
print(f'Simple Validation:\t{total_simple}')
print(f'Extended Validation:\t{total_extended}')

13
solutions/4/test.txt Normal file
View File

@ -0,0 +1,13 @@
ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
hcl:#cfa07d byr:1929
hcl:#ae17e1 iyr:2013
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm
hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in