Sfoglia il codice sorgente

Day 2 solutions in Python

master
Noëlle Anthony 5 anni fa
parent
commit
3c886c76ee
2 ha cambiato i file con 73 aggiunte e 0 eliminazioni
  1. 41
    0
      02a.py
  2. 32
    0
      02b.py

+ 41
- 0
02a.py Vedi File

@@ -0,0 +1,41 @@
""" Advent of Code 2018

December 02, puzzle 1
"""

def main(lines):
""" For each line, count characters.

If any character appears exactly twice, increment x2.
If any character appears exactly three times, increment x3.
Ignore any doubles or triples past the first set.
At the end, multiply x2 by x3 to get the checksum.
"""
x2, x3 = 0, 0
for line in lines:
chars = {}
gota2, gota3 = False, False
for char in line:
if char in chars.keys():
chars[char] += 1
else:
chars[char] = 1
for k,v in chars.items():
if v == 2 and not gota2:
x2 += 1
gota2 = True
if v == 3 and not gota3:
x3 += 1
gota3 = True
if gota2 and gota3:
break
print("Totals: x2 {}, x3 {}".format(x2, x3))
print("Checksum: {}".format(x2 * x3))


if __name__ == "__main__":
lines = []
with open("02in.txt","r") as f:
for line in f:
lines.append(line.strip())
main(lines)

+ 32
- 0
02b.py Vedi File

@@ -0,0 +1,32 @@
""" Advent of Code 2018

December 02, puzzle 2
"""

def main(lines):
""" For each line, check to see if another line varies by only one character.

There's an easy but expensive way to do this. I'll start with that
and see if I can refine it later.
"""
for i, line in enumerate(lines):
if i + 1 != len(lines):
for word in lines[(i+1):]:
diffs = 0
idx = 0
for j in range(len(line)):
if line[j] != word[j]:
diffs += 1
idx = j
if diffs == 1:
print("Found a match: {} and {}".format(line, word))
print("Matched value: {}".format("".join([word[:idx],word[(idx+1):]])))
return

if __name__ == "__main__":
lines = []
with open("02in.txt","r") as f:
for line in f:
lines.append(line.strip())
main(lines)

Loading…
Annulla
Salva