Skip to content

Instantly share code, notes, and snippets.

@wakemaster39
Created December 1, 2018 21:46
Show Gist options
  • Save wakemaster39/e3bf23439154383a744d248946efb7cf to your computer and use it in GitHub Desktop.
Save wakemaster39/e3bf23439154383a744d248946efb7cf to your computer and use it in GitHub Desktop.
import requests
import os
dir = os.path.dirname(__file__)
f = open(os.path.join(dir, "data.txt"))
inputs = [int(x) for x in f]
# Question 1 answer
print(sum(inputs))
seen = set()
current = 0
count = 0
while True:
for x in inputs:
current += x
if current in seen:
# Question 2 answer
print(current)
break
seen.add(current)
else:
print(count)
count += 1
continue
break
@zero-code-hero
Copy link

My sol:

from typing import List, Dict, Generator
import os
import time

DIR_PATH = os.path.dirname(os.path.realpath(__file__))

with open(f"{DIR_PATH}/../resources/2018_day_1/input") as file:
    input_data: str = file.read()
input_data: List[str] = input_data.strip().split("\n")
input_data: List[int] = list(map(lambda x: int(x), input_data))


def get_frequency_totals(input_data: List[int]) -> Generator[int, None, None]:
    frequency: int = 0
    while True:
        for k, v in enumerate(input_data):
            yield frequency
            frequency += input_data[k]


def find_first_duplicate(input_data: List[int]) -> int:

    # Dict of a number and it's position in the list
    history: List[int] = []
    gen = get_frequency_totals(input_data)
    for k in gen:
        # Number has been seen before.
        if k in history:
            return k
        else:
            history.append(k)


def get_answer() -> int:
    # 56752
    return find_first_duplicate(input_data)

@harryvgiunta
Copy link

harryvgiunta commented Dec 2, 2018

My solution:

import pandas as p

file = p.read_excel('C:/AdventOfCode/DayOne.xlsx')
data = []

for num in file['numbers']:
    data.append(num)
print("part one answer:", sum(data))

cf = 0
previousfrequencies = [0]
duplicatefound = False

while duplicatefound is False:
    for num in data:
        cf = num + cf
        if cf in previousfrequencies:
            print("part two answer:", cf)
            duplicatefound = True
            break
        previousfrequencies.append(cf)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment