Skip to content

Instantly share code, notes, and snippets.

@vlandeiro
Created December 2, 2020 15:33
Show Gist options
  • Save vlandeiro/0419f926927ae9ee29ba70930c78da67 to your computer and use it in GitHub Desktop.
Save vlandeiro/0419f926927ae9ee29ba70930c78da67 to your computer and use it in GitHub Desktop.
Advent of Code 2020

Advent of code

2020

Day 1

1st problem

import numpy as np

with open("/Users/vlandeiro/Downloads/input.txt") as fd:
    input = np.array([int(line) for line in fd])

input = np.sort(input)
input

input_removed_once = 2020 - input
input_removed_once

set(2020 - input) & set(input)
456 * 1564

2nd problem

input_removed_twice = np.repeat([2020 - input], input.size,  axis=0)
input_removed_twice = (input_removed_twice - input.reshape(-1, 1)).ravel()
input_removed_twice = input_removed_twice[input_removed_twice > 0]
input_removed_twice

set(input_removed_twice) & set(input)
399 * 764 * 857

Day 2

problem 1

import numpy as np
from collections import Counter
with open("/Users/vlandeiro/Downloads/day2_input.txt") as fd:
    valid_count = 0
    for line in fd:
        parts = line.strip().split(" ")
        min_count, max_count = [int(x) for x in parts[0].split("-")]
        letter = parts[1][0]
        password = parts[2]
        letter_counts = Counter(password)
        if letter_counts[letter] >= min_count and letter_counts[letter] <= max_count:
            valid_count += 1
print(valid_count)

problem 2

import numpy as np

with open("/Users/vlandeiro/Downloads/day2_input.txt") as fd:
    valid_count = 0
    for line in fd:
        parts = line.strip().split(" ")
        pos1, pos2 = [int(x) - 1 for x in parts[0].split("-")]
        letter = parts[1][0]
        password = parts[2]
        if (password[pos1] == letter) != (password[pos2] == letter):
            valid_count += 1
print(valid_count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment