day2
This commit is contained in:
parent
6348ae58a3
commit
5f1e68270b
27
day02.py
Normal file
27
day02.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import aoclib
|
||||||
|
import re
|
||||||
|
|
||||||
|
if 'TEST' not in globals():
|
||||||
|
TEST = False
|
||||||
|
|
||||||
|
my_input = aoclib.getInputLineAsArray(day=2, test=TEST)
|
||||||
|
splitter = re.compile(r'([0-9]+)-([0-9]+) ([a-z]): (.*)')
|
||||||
|
|
||||||
|
|
||||||
|
def getCharacterCount(password, character):
|
||||||
|
count = 0
|
||||||
|
for char in password:
|
||||||
|
if char == character:
|
||||||
|
count = count + 1
|
||||||
|
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
|
valid_count = 0
|
||||||
|
for line in my_input:
|
||||||
|
match = re.match(splitter, line)
|
||||||
|
(min_count, max_count, character, password) = match.group(1, 2, 3, 4)
|
||||||
|
if int(min_count) <= getCharacterCount(password, character) <= int(max_count):
|
||||||
|
valid_count = valid_count + 1
|
||||||
|
|
||||||
|
aoclib.printSolution(2, 1, valid_count, TEST)
|
||||||
19
day02_2.py
Normal file
19
day02_2.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import aoclib
|
||||||
|
import re
|
||||||
|
|
||||||
|
if 'TEST' not in globals():
|
||||||
|
TEST = False
|
||||||
|
|
||||||
|
my_input = aoclib.getInputLineAsArray(day=2, test=TEST)
|
||||||
|
splitter = re.compile(r'([0-9]+)-([0-9]+) ([a-z]): (.*)')
|
||||||
|
|
||||||
|
|
||||||
|
valid_count = 0
|
||||||
|
for line in my_input:
|
||||||
|
match = re.match(splitter, line)
|
||||||
|
(min_count, max_count, character, password) = match.group(1, 2, 3, 4)
|
||||||
|
if (password[int(min_count) - 1] == character or password[int(max_count) - 1] == character) \
|
||||||
|
and password[int(min_count) - 1] != password[int(max_count) - 1]:
|
||||||
|
valid_count = valid_count + 1
|
||||||
|
|
||||||
|
aoclib.printSolution(2, 1, valid_count, TEST)
|
||||||
3
inputs/2_test
Normal file
3
inputs/2_test
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
1-3 a: abcde
|
||||||
|
1-3 b: cdefg
|
||||||
|
2-9 c: ccccccccc
|
||||||
Loading…
Reference in New Issue
Block a user