def test_read_non_empty_without_anything(self): self.assertEqual( list( read_lines( stream(encoding="ascii", text=""" foo bar baz """))), [ "foo\n", "bar\n", "baz\n", ]) self.assertEqual( list( read_lines( stream(encoding="utf-8", text=""" föø bär båz """))), [ "föø\n", "bär\n", "båz\n", ])
def test_read_non_empty_with_initial_encoding(self): self.assertEqual( list( read_lines( stream(encoding="iso-8859-1", text=""" föø bär båz """), "iso-8859-1")), [ "föø\n", "bär\n", "båz\n", ])
letter = split[1][0] password = split[2] return Password(letter, first_number, second_number, password) # Part one def is_password_valid(password): letters = re.sub(f'[^{password.letter}]', '', password.plain) length = len(letters) return length >= password.first_number and length <= password.second_number available_passwords = [] for line in read_lines('day02.txt'): password = read_password_from_string(line) available_passwords.append(password) valid_passwords = [] for password in available_passwords: if is_password_valid(password): valid_passwords.append(password) print(len(valid_passwords)) # Part two def is_password_valid(password): first_letter = password.plain[password.first_number - 1]
from file_reader import read_lines lines = read_lines('day03.txt') width = len(lines[0]) height = len(lines) def count_trees(coords): trees_amount = 0 x = 0 y = 0 while y < height: if lines[y][x] == '#': trees_amount += 1 x += coords[0] y += coords[1] if x >= width: x %= width return trees_amount # Part one print(count_trees((3, 1))) # Part two coords_list = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)] result = 1
def test_read_empty(self): self.assertEqual(list(read_lines(stream())), [])