-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.py
More file actions
46 lines (37 loc) · 1.3 KB
/
day2.py
File metadata and controls
46 lines (37 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
"""
Advent of Code 2017 - Day 2 (http://adventofcode.com/2017/day/2)
Assumptions made:
* Since the puzzle data provided is formatted with tabs, it is assumed that the puzzle input will see cells
seperated by \t, and rows seperated by /n.
* Puzzle input will be ready from a file
"""
def read_input(target_file):
try:
with open(target_file, 'r') as openfile:
contents = openfile.read()
return contents.strip().split("\n")
except FileNotFoundError:
print("File not found!")
exit(1)
def solve_a():
input_val = read_input("data/day2_input.val")
result = 0
for row in input_val:
nums = [int(x) for x in row.split("\t")]
low = min(nums)
high = max(nums)
result += (int(high) - int(low))
return result
def solve_b():
input_val = read_input("data/day2_input.val")
result = 0
for row in input_val:
nums = [int(x) for x in row.split("\t")]
for num in nums:
compatible = [x for x in nums if num % x == 0 and x != num]
if len(compatible) == 1:
result += max([num, compatible[0]])/min([num, compatible[0]])
return result
print("Solution A: {answer}".format(answer=solve_a()))
print("Solution B: {answer}".format(answer=solve_b()))