For Each Cell1 In Range(« D4:D200 »)
If (Cell1.Value) > 2020 Then
Debug.Print (Cell1.Value + Cell2.Value + Cell3.Value)
Debug.Print (« Exit1 »)
Exit For
End If
For Each Cell2 In Range(« D4:D200 »)
If (Cell1.Value + Cell2.Value) > 2020 Then
'Debug.Print (Cell1.Value + Cell2.Value + Cell3.Value)
'Debug.Print (« Exit2 »)
Exit For
End If
For Each Cell3 In Range(« D4:D200 »)
If (Cell1.Value + Cell2.Value + Cell3.Value) > 2020 Then
'Debug.Print (Cell1.Value + Cell2.Value + Cell3.Value)
'Debug.Print (« Exit3 »)
Exit For
End If
Debug.Print (Cell1.Value + Cell2.Value + Cell3.Value)
If (Cell1.Value + Cell2.Value + Cell3.Value) = 2020 Then
Debug.Print (Cell1.Value * Cell2.Value * Cell3.Value)
Debug.Print (« OK »)
End If
with open("input.txt", 'r') as f:
lines = [x.strip() for x in f.readlines() if x.strip()]
valid = 0
for line in lines:
split_line = line.split(" ")
password = split_line[2]
letter = split_line[1][0] # split_line 1 = a:, we only want a
r = [int(x) for x in split_line[0].split("-")]
letter_count = password.count(letter)
if r[0] <= letter_count <= r[1]:
valid += 1
print(valid, "valid passwords")
Part 2
with open("input.txt", 'r') as f:
lines = [x.strip() for x in f.readlines() if x.strip()]
valid = 0
for line in lines:
split_line = line.split(" ")
password = split_line[2]
letter = split_line[1][0] # split_line 1 = a:, we only want a
pos = [int(x) - 1 for x in split_line[0].split("-")]
if (password[pos[0]] == letter and password[pos[1]] != letter) or \
(password[pos[0]] != letter and password[pos[1]] == letter):
valid += 1
print(valid, "valid passwords")