-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6504997
commit cf7e2c8
Showing
9 changed files
with
1,152 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
version: '3' | ||
services: | ||
shell: | ||
build: . | ||
volumes: | ||
- .:/code |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
attrs==18.1.0 |