Day 2

So here we're basically just trying to compute the maximum minus the minimum for each row of this data set. I think this looks like a good opportunity to try and use the pandas module

In [1]:
import pandas as pd
import numpy as np

# Ez mode:
test = pd.read_csv("H:/python_jiggering/logs/aoc_2017/d2_input.txt", header = None, sep = '\t')
test.head()
Out[1]:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 5048 177 5280 5058 4504 3805 5735 220 4362 1809 1521 230 772 1088 178 1794
1 6629 3839 258 4473 5961 6539 6870 4140 4638 387 7464 229 4173 5706 185 271
2 5149 2892 5854 2000 256 3995 5250 249 3916 184 2497 210 4601 3955 1110 5340
3 153 468 550 126 495 142 385 144 165 188 609 182 439 545 608 319
4 1123 104 567 1098 286 665 1261 107 227 942 1222 128 1001 122 69 139

We wanna figure out how to compute the difference between min and max

In [2]:
def check(x):
    return(np.max(x) - np.min(x))

Apply across rows:

In [3]:
ranges = test.apply(func = check, axis = 1)
checksum = sum(ranges)
checksum
Out[3]:
58975

Part 2

Okay for the second part we have to find which pair of numbers even divides the other. I think my style is again to just write to do it once and see if we can get the indices. Probably two for loops will do this simply enough...

In [4]:
def check2(x):
    for i in range(len(x)):
        for j in range(i+1, len(x)):
            if x[i] % x[j] == 0:
                return(x[i] / x[j])
            if x[j] % x[i] == 0:
                return(x[j] / x[i])
    
quotients = test.apply(func = check2, axis = 1)
checksum2 = sum(quotients)
checksum2
Out[4]:
308.0