Skip to content

Commit

Permalink
first day + some init
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecRosenbaum committed Dec 1, 2018
1 parent 6504997 commit cf7e2c8
Show file tree
Hide file tree
Showing 9 changed files with 1,152 additions and 0 deletions.
996 changes: 996 additions & 0 deletions 1/input.txt

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions 1/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
Day 1 challenge
"""
from functools import reduce


def solution_part_one(arg):
return reduce(
lambda agg, x: agg + (int(x[1:]) if x[0] == "+" else -1 * int(x[1:])),
arg.split(),
0,
)


def solution_part_two(arg):
curr_freq = 0
seen_frequencies = set()
idx = 0
args = arg.split()

while curr_freq not in seen_frequencies:
seen_frequencies.add(curr_freq)
curr_freq += (
int(args[idx % len(args)][1:])
if args[idx % len(args)][0] == "+"
else -1 * int(args[idx % len(args)][1:])
)
idx += 1

return curr_freq

if __name__ == "__main__":
with open("input.txt", "r") as fin:
problem_input = fin.read()
print("Part 1:", solution_part_one(problem_input))
print("Part 1:", solution_part_two(problem_input))
39 changes: 39 additions & 0 deletions 1/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest

from solution import solution_part_one, solution_part_two


class TestPartOne(unittest.TestCase):
def test_one(self):
problem_input = "\n".join(["+1", "+1", "+1"])
self.assertEqual(3, solution_part_one(problem_input))

def test_two(self):
problem_input = "\n".join(["+1", "+1", "-2"])
self.assertEqual(0, solution_part_one(problem_input))

def test_three(self):
problem_input = "\n".join(["-1", "-2", "-3"])
self.assertEqual(-6, solution_part_one(problem_input))


class TestPartTwo(unittest.TestCase):
def test_one(self):
problem_input = "\n".join(["+1", "-1"])
self.assertEqual(0, solution_part_two(problem_input))

def test_two(self):
problem_input = "\n".join(["+3", "+3", "+4", "-2", "-4"])
self.assertEqual(10, solution_part_two(problem_input))

def test_three(self):
problem_input = "\n".join(["-6", "+3", "+8", "+5", "-6"])
self.assertEqual(5, solution_part_two(problem_input))

def test_four(self):
problem_input = "\n".join(["+7", "+7", "-2", "-7", "-4"])
self.assertEqual(14, solution_part_two(problem_input))


if __name__ == "__main__":
unittest.main()
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM python:3.7
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3'
services:
shell:
build: .
volumes:
- .:/code
Empty file added example/input.txt
Empty file.
53 changes: 53 additions & 0 deletions example/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Day 17 challenge
"""
import re

import attr


def solution_part_one(arg):
buff = [0]
idx = 0

for i in range(1, 2018):
idx = (idx + arg) % len(buff)
buff.insert((idx % len(buff)) + 1, i)
idx += 1


return buff[(idx + 1) % len(buff)]


def solution_part_two(arg):
idx = 0
buff_len = 1
pos_zero = 0
after_zero = None


for i in range(1, 50000001):
# cycle the index
idx = (idx + arg) % buff_len

if idx % buff_len < pos_zero:
pos_zero += 1
elif idx % buff_len == pos_zero:
after_zero = i
else:
# insert idx > 0, do nothing
pass

# bump the index
idx += 1

# bump the size of the list
buff_len += 1

return after_zero


if __name__ == "__main__":
problem_input = 367
print(solution_part_one(problem_input))
print(solution_part_two(problem_input))
17 changes: 17 additions & 0 deletions example/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest

from solution import solution_part_one, solution_part_two


class TestPartOne(unittest.TestCase):
def test_one(self):
self.assertEqual(solution_part_one(3), 638)


class TestPartTwo(unittest.TestCase):
def test_one(self):
self.assertEqual(solution_part_two(3), 1222153)


if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
attrs==18.1.0

0 comments on commit cf7e2c8

Please sign in to comment.