Skip to content

Commit

Permalink
2020 day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Spacerulerwill committed Jan 10, 2025
1 parent 8f1cf62 commit 29af753
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions puzzles/Y2020/D2/p1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import re
from typing import Any


def puzzle(input: str) -> Any:
lines = [line.strip() for line in input.splitlines()]
valid_passwords = 0
for line in lines:
if match := re.match(r"(\d+)-(\d+) ([a-z]): ([a-z]+)", line):
min = int(match.group(1))
max = int(match.group(2))
letter = match.group(3)
password = match.group(4)

if password.count(letter) in range(min, max + 1):
valid_passwords += 1
return valid_passwords
21 changes: 21 additions & 0 deletions puzzles/Y2020/D2/p2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import re
from typing import Any


def puzzle(input: str) -> Any:
lines = [line.strip() for line in input.splitlines()]

valid_passwords = 0
for line in lines:
if match := re.match(r"(\d+)-(\d+) ([a-z]): ([a-z]+)", line):
min = int(match.group(1))
max = int(match.group(2))
letter = match.group(3)
password = match.group(4)

first_is_correct = password[min - 1] == letter
second_is_correct = password[max - 1] == letter

if first_is_correct ^ second_is_correct:
valid_passwords += 1
return valid_passwords

0 comments on commit 29af753

Please sign in to comment.