DEV Community

Viper
Viper

Posted on • Updated on

Advent of Code 2021 Python Solution: Day 3

Again, I will be using helper function from part 1 to read my data.

Part 1

from collections import Counter
import numpy as np
data,data1 = get_data(day=3)

def part1(inp):
    cs = len(inp[0])
    dt = [int(d) for dt in inp for d in dt]
    dt = np.array(dt).reshape(-1, cs)

    print(dt[0])

    # sorted(x.items(), key=lambda item: item[1])
    counts = [sorted(dict(Counter(dt[:, i])).items(), key=lambda item: item[1]) for i in range(len(dt[0]))]
    counts = np.array(counts).reshape(-1,2)

#     print(counts)

    minidx = np.arange(0, len(counts), 2)
    maxidx = np.arange(1, len(counts), 2)

    minv = int("".join(list(map(str, counts[minidx, 0]))), 2)
    maxv = int("".join(list(map(str, counts[maxidx, 0]))), 2)
    print(minv, maxv)
    print(minv*maxv)
part1(data)
Enter fullscreen mode Exit fullscreen mode

The output of above code will be:

[0 0 1 0 0]
9 22
198
Enter fullscreen mode Exit fullscreen mode

198 is a test output. The real output will be 4103154.

Part 2

def o2(dt):
    ndt = dt.copy()
    #print(len(ndt[0]))

    curr_c = 0
    while curr_c<len(dt[0]):
        print(f"Current Col: {curr_c}")
        counts = [sorted(dict(Counter(ndt[:, curr_c])).items(), key=lambda item: item[1])]
        counts = np.array(counts).reshape(-1,2)
        if len(counts)>1:
            if counts[0, 1]==counts[1, 1]:
                ndt = ndt[ndt[:,curr_c]==1]
            else:
                ndt = ndt[ndt[:,curr_c]==counts[1][0]]
        else:
            ndt = ndt[ndt[:,curr_c]==counts[1][0]]
        print(f"Current Col: {curr_c} Rows: {len(ndt)}")
#         print(counts)
#         print(ndt[ndt[:,curr_c]==counts[1][0]])



        curr_c+=1
    res = int("".join(list(map(str, ndt[0]))), 2)
    print(res)
    return res

def co2(dt):
    ndt = dt.copy()
    #print(len(ndt[0]))

    curr_c = 0
    while curr_c<len(dt[0]):

        counts = [sorted(dict(Counter(ndt[:, curr_c])).items(), key=lambda item: item[1])]
        counts = np.array(counts).reshape(-1,2)
        if len(counts)>1:
            if counts[0, 1]== counts[1, 1]:
                ndt = ndt[ndt[:,curr_c]==0]
            else:
                ndt = ndt[ndt[:,curr_c]==counts[0][0]]
        else:
            ndt = ndt[ndt[:,curr_c]==counts[0][0]]

#         print(ndt)
        print(f"Current Col: {curr_c} Rows: {len(ndt)}")
        curr_c+=1
    return int("".join(list(map(str, ndt[0]))), 2)

def part2(inp):
    cs = len(inp[0])
    dt = [int(d) for dt in inp for d in dt]
    dt = np.array(dt).reshape(-1, cs)

    print("O2")
    o2v = o2(dt)
    co2v = co2(dt)
    print(o2v, co2v)
    print(o2v*co2v)
part2(data1)
Enter fullscreen mode Exit fullscreen mode

The output will be:

O2
Current Col: 0
Current Col: 0 Rows: 511
Current Col: 1
Current Col: 1 Rows: 263
Current Col: 2
Current Col: 2 Rows: 134
Current Col: 3
Current Col: 3 Rows: 71
Current Col: 4
Current Col: 4 Rows: 36
Current Col: 5
Current Col: 5 Rows: 20
Current Col: 6
Current Col: 6 Rows: 12
Current Col: 7
Current Col: 7 Rows: 8
Current Col: 8
Current Col: 8 Rows: 5
Current Col: 9
Current Col: 9 Rows: 3
Current Col: 10
Current Col: 10 Rows: 2
Current Col: 11
Current Col: 11 Rows: 1
3399
Current Col: 0 Rows: 489
Current Col: 1 Rows: 235
Current Col: 2 Rows: 109
Current Col: 3 Rows: 54
Current Col: 4 Rows: 24
Current Col: 5 Rows: 11
Current Col: 6 Rows: 4
Current Col: 7 Rows: 2
Current Col: 8 Rows: 1
Current Col: 9 Rows: 1
Current Col: 10 Rows: 1
Current Col: 11 Rows: 1
3399 1249
4245351
Enter fullscreen mode Exit fullscreen mode

All of my codes are available in GitHub as Jupyter Notebook.

Why not read more?

Discussion (0)