<jupyter_start><jupyter_text>Advent of Code 2020[Website](https://adventofcode.com/2020/)<jupyter_code># import modules
import operator
import re<jupyter_output><empty_output><jupyter_text>Day 1Find the two entries that sum to 2020; what do you get if you multiply them together?<jupyter_code>with open('day01-input.txt') as input_file_01:
    input_01 = input_file_01.readlines()
    while len(input_01) > 2:
        a = int(input_01.pop(1))
        for x in range(1, len(input_01)):
            b = int(input_01[x])
            if (a + b == 2020):
                prod_ab = a * b
                print(prod_ab)
                break<jupyter_output>1020099<jupyter_text>What is the product of the three entries that sum to 2020?<jupyter_code>with open('day01-input.txt') as input_file_01:
    input_01 = input_file_01.readlines()
    while len(input_01) > 2:
        a = int(input_01.pop(1))
        for x in range(1, len(input_01)):
            b = int(input_01[x])
            for y in range(x, len(input_01)):
                c  = int(input_01[y])
                if (a + b + c == 2020):
                    prod_abc = a * b * c
                    print(prod_abc)
                    break<jupyter_output>49214880<jupyter_text>Day 2 How many passwords are valid?<jupyter_code>valid = 0

with open('day02-input.txt') as input_file_02:
    for line in input_file_02:
        pw = re.split(r'-|:? ', str.strip(line))
        matched = re.findall(pw[2], pw[3])
        if len(matched) >= int(pw[0]) and len(matched) <= int(pw[1]):
            valid = valid + 1

print(valid)<jupyter_output>591<jupyter_text>New validation rules<jupyter_code>valid_pos = 0

with open('day02-input.txt') as input_file_02:
    for line in input_file_02:
        pw = re.split(r'-|:? ', str.strip(line))
        pos0 = int(pw[0]) - 1
        pos1 = int(pw[1]) - 1
        ltr = pw[2]
        pw_str = pw[3]
        if operator.xor(pw_str[pos0] == ltr, pw_str[pos1] == ltr):
            valid_pos = valid_pos + 1

print(valid_pos)<jupyter_output>335<jupyter_text>Day 3With a slope of three right and one down, how many trees do I encounter on the map?<jupyter_code># First we repeat the map rightward to at least three times its length.

# Map of the area
a = []
b = []
with open('day03-input.txt') as input_file_03:
    for line in input_file_03:
        x = len(line)
        a.append(line.strip())

# How many rows down
y = len(a)

# How much we need to repeat the map
r = (4 * y) // x + 1

# Repeat each row to the right r times
for row in a:
    row *= r
    b.append(row)

# Now move across and down the map.

# Trees found
t = 0

# Position of toboggan
p = [0, 0]
while p[0] < x * r-1 and p[1] < y-1:
    p[0] = p[0] + 3
    p[1] = p[1] + 1
    if b[p[1]][p[0]] == '#':
        t = t + 1

print(t)<jupyter_output>254<jupyter_text>The same, with different slope combinations.<jupyter_code># Repeat the map again.

# How much we need to repeat the map
r = (8 * y) // x + 1

# Repeat each row to the right r times
b =  []
for row in a:
    row *= r
    b.append(row)

# Four more slope combinations.
sl = {0: [1, 1], 1: [3, 1], 2: [5, 1], 3: [7, 1], 4: [1, 2]}

# Record of trees hit by each slope.
tr = {}

# Final product
pr = 1

for i in range(0, 5):
    p = [0, 0]
    t = 0
    while p[0] < x * r-1 and p[1] < y-1:
        p[0] = p[0] + sl[i][0]
        p[1] = p[1] + sl[i][1]
        if b[p[1]][p[0]] == '#':
            t = t + 1
    tr[i] = t
    pr = pr * t

print(tr)
print(pr)<jupyter_output>{0: 63, 1: 254, 2: 62, 3: 56, 4: 30}
1666768320<jupyter_text>Day 4Find all valid passports/credentials -- all fields other than `cid` are required.<jupyter_code># Passport fields.
fld_ps = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid', 'cid']
fld_cr = set(fld_ps[0:-1])
present = 0
ps_present = []

with open('day04-input.txt') as input_file_04:
    input_04 = input_file_04.read()
    ps = input_04.split('\n\n')

    for line in ps:
        pp = line.split()
        pe = {}
        for entry in pp:
            ent = re.split(r':', entry, maxsplit=1)
            pe[ent[0]] = ent[1]
        fld_e = set(list(pe))
        if len(fld_cr.difference(fld_e)) == 0:
            ps_present.append(pe)
            present = present + 1

print(present)<jupyter_output>202<jupyter_text>Now validate the documents.<jupyter_code># Documents with all fields present are in the list ps_present.
valid = 0
ecl_valid = set(['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'])

for ent_pr in ps_present:
    if int(ent_pr['byr']) >= 1920 and int(ent_pr['byr']) <= 2002 and int(ent_pr['iyr']) >= 2010 and int(ent_pr['iyr']) <= 2020 and int(ent_pr['eyr']) >= 2020 and int(ent_pr['eyr']) <= 2030 and re.match(r'^\#[a-f0-9]{6}$', ent_pr['hcl']) and ent_pr['ecl'] in ecl_valid and len(ent_pr['pid']) == 9:
        ent_hgt = {}
        if re.search(r'(cm)$', ent_pr['hgt']):
            ent_hgt = {'cm': re.search(r'[0-9]+', ent_pr['hgt']).group()}
            if int(ent_hgt['cm']) >= 150 and int(ent_hgt['cm']) <= 193:
                valid = valid + 1
        elif re.search(r'(in)$', ent_pr['hgt']):
            ent_hgt = {'in': re.search(r'[0-9]+', ent_pr['hgt']).group()}
            if int(ent_hgt['in']) >= 59 and int(ent_hgt['in']) <= 76:
                valid = valid + 1

print(valid)<jupyter_output>137<jupyter_text>Day 5What is the highest seat ID on a boarding pass?<jupyter_code>seats = []

# Assign seat IDs.
with open('day05-input.txt') as input_file_05:
    for line in input_file_05:
        # They're just binary numbers.
        row_s = line[0:7]
        row_i = int(row_s.replace('F', '0').replace('B', '1'), base=2)
        col_s = line[7:10]
        col_i = int(col_s.replace('L', '0').replace('R', '1'), base=2)
        seats.append(row_i * 8 + col_i)

# Find the highest seat ID.
print(max(seats))<jupyter_output>813<jupyter_text>Find the missing seat ID.<jupyter_code>seats.sort()
for i in seats:
    if seats[i+1] > seats[i] + 1:
        print(seats[i], seats[i+1])
        break<jupyter_output>611 613<jupyter_text>Day 6Sum of count of unique answers in each group.<jupyter_code>import string

counts = 0

with open('day06-input.txt') as input_file_06:
    input_06 = input_file_06.read()
    groups = input_06.split('\n\n')
    
    for group in groups:
        i = 0
        for l in string.ascii_lowercase:
            if l in group:
                i = i + 1
        counts = counts + i
        
print(counts)<jupyter_output>6703<jupyter_text>Sum of count of questions in each group to which everyone answered yes.<jupyter_code>countt = 0

for group in groups:
    allyes = set(string.ascii_lowercase)
    answers = group.split('\n')
    for a in answers:
        allyes.intersection_update(set(a))
    countt = countt + len(allyes)

print(countt)<jupyter_output>3430
{'fraction_non_alphanumeric': 0.15206147480419685, 'fraction_numerical': 0.04920939855179548, 'mean_word_length': 2.724821133736929, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 50, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '190295', 'license': 'MIT', 'n_tokens_mistral': 2819, 'n_tokens_neox': 2586, 'n_words': 682, 'path': 'aoc2020.ipynb', 'repo_name': 'claudinec/advent-of-code-2020', 'size': '6767'}