Browse Source

Day 9 completed.

master
noirscape 1 year ago
parent
commit
e5aada4969
4 changed files with 2119 additions and 0 deletions
  1. +1000
    -0
      9/puzzle 1/input.txt
  2. +54
    -0
      9/puzzle 1/main.py
  3. +1000
    -0
      9/puzzle 2/input.txt
  4. +65
    -0
      9/puzzle 2/main.py

+ 1000
- 0
9/puzzle 1/input.txt
File diff suppressed because it is too large
View File


+ 54
- 0
9/puzzle 1/main.py View File

@ -0,0 +1,54 @@
handle = open("input.txt", "r")
lines = [line.rstrip() for line in handle.readlines()]
preamble = 25
test_lines = [
"35",
"20",
"15",
"25",
"47",
"40",
"62",
"55",
"65",
"95",
"102",
"117",
"150",
"182",
"127",
"219",
"299",
"277",
"309",
"576",
]
test_preamble = 5
def get_first_invalid_cipher_number(lines, preamble):
lines = [int(line) for line in lines]
previous_numbers = []
for idx, value in enumerate(lines):
value = int(value)
if idx < preamble:
previous_numbers.append(value)
continue
if idx != preamble:
previous_numbers = previous_numbers[1:] # Slice off first value -> quicker than remove()
possibilities = []
for number_one in previous_numbers:
for number_two in previous_numbers:
if number_one != number_two:
possibilities.append(number_one + number_two)
print("VAL: ", value, "PREV: ", previous_numbers, " POSS: ", possibilities)
if value not in possibilities:
return value
previous_numbers.append(value)
def main(lines, preamble):
invalid_value = get_first_invalid_cipher_number(lines, preamble)
print(f"{invalid_value=}")
main(lines, preamble)
handle.close()

+ 1000
- 0
9/puzzle 2/input.txt
File diff suppressed because it is too large
View File


+ 65
- 0
9/puzzle 2/main.py View File

@ -0,0 +1,65 @@
handle = open("input.txt", "r")
lines = [line.rstrip() for line in handle.readlines()]
preamble = 25
test_lines = [
"35",
"20",
"15",
"25",
"47",
"40",
"62",
"55",
"65",
"95",
"102",
"117",
"150",
"182",
"127",
"219",
"299",
"277",
"309",
"576",
]
test_preamble = 5
def get_first_invalid_cipher_number(lines, preamble):
lines = [int(line) for line in lines]
previous_numbers = []
for idx, value in enumerate(lines):
value = int(value)
if idx < preamble:
previous_numbers.append(value)
continue
if idx != preamble:
previous_numbers = previous_numbers[1:] # Slice off first value -> quicker than remove()
possibilities = []
for number_one in previous_numbers:
for number_two in previous_numbers:
if number_one != number_two:
possibilities.append(number_one + number_two)
if value not in possibilities:
return value
previous_numbers.append(value)
def get_encryption_weakness(lines, invalid_value):
lines = [int(line) for line in lines]
for idx, first_step in enumerate(lines):
if first_step == invalid_value:
continue
options = [first_step]
for line in lines[idx+1:]:
options.append(line)
if sum(options) == invalid_value:
return min(options) + max(options)
def main(lines, preamble):
invalid_value = get_first_invalid_cipher_number(lines, preamble)
weakness = get_encryption_weakness(lines, invalid_value)
print(f"{invalid_value=} {weakness=}")
main(lines, preamble)
handle.close()

Loading…
Cancel
Save