Unable to load image

Advent of Code 2022: Day 8

Post solutions here :marseysad: :marseyill:

25
Jump in the discussion.

No email address required.

You can make things somewhat easier for yourself by using 2d index vectors and

def addv2(v1, v2):
    return v1[0] + v2[0], v1[1] + v2[1]

It didn't get rid of all the bullshit, but of enough of it imo:

def problem8(data, second):
    _data = split_data('''30373
25512
65332
33549
35390''')
    h = len(data)
    w = len(data[0])
    assert all(len(s) == w for s in data)

    vis = set()

    def dix(pos):
        return data[pos[0]][pos[1]]

    def go(pos, dir, cnt):
        tallest = None
        for _ in range(cnt):
            h = dix(pos)
            if not tallest or tallest < h:
                vis.add(pos)
                tallest = h
            pos = addv2(pos, dir)

    def go2(pos, dir):
        myh = dix(pos)
        for i in range(1000):
            pos = addv2(pos, dir)
            if not (0 <= pos[0] < h) or not (0 <= pos[1] < w):
                return i
            if myh <= dix(pos):
                return i + 1
        assert False

    for i in range(h):
        go((i, 0), (0, 1), w)
        go((i, w - 1), (0, -1), w)

    for i in range(w):
        go((0, i), (1, 0), h)
        go((h - 1, i), (-1, 0), h)

    if not second:
        return len(vis)

    scores = []
    for i in range(1, h - 1):
        for j in range(1, w - 1):
            pos = (i, j)
            scores.append(
                go2(pos, (0, 1)) *
                go2(pos, (0, -1)) *
                go2(pos, (1, 0)) *
                go2(pos, (-1, 0)))

    return max(scores)
Jump in the discussion.

No email address required.

>for i in range(1000):

:#marseykneel:

Jump in the discussion.

No email address required.

You can make things somewhat easier for yourself by using 2d index vectors and

this but also by making a complexity monster :gigachad:. I hope the input's dimension won't get too high in the next days

![](/images/1670495109403427.webp)

Jump in the discussion.

No email address required.

>c arrays in c++

:marseybeansick:

Jump in the discussion.

No email address required.

das rite, any problems with dat, white boi?

:#chadblack:

Jump in the discussion.

No email address required.

Yes i have , Use std::array :marseybeandefiant:

Jump in the discussion.

No email address required.

Jump in the discussion.

No email address required.

Coming from the guy using iostream

:chadcopecapy::chaddilatecapy::chadsneedcapy:

https://yosefk.com/c++fqa/io.html#fqa-15.1

Jump in the discussion.

No email address required.

Yes.

:#chadyes:

the FQA is correct, that doesn't meant that std::istream & co aren't convenient for basic tasks

Jump in the discussion.

No email address required.

true king , let's hope "std::print" will in the future replace "ostream" though.

Jump in the discussion.

No email address required.

you can tell how smart someone is from their solution lol, makes sense you're from motte

an even better solution is just numpy tho:

import numpy as np
p = np.array([[int(b) for b in a] for a in open('/tmp/test').read().split('\n')[:-1]])
ac0 = lambda f, i, j: f(p[i,j], p[:i,j][::-1],p[i+1:,j],p[i,:j][::-1],p[i,j+1:])
acc = lambda f: np.apply_along_axis(lambda ii: ac0(f, ii[0], ii[1]), 0, np.indices(p.shape))
a1 = acc(lambda a,*b: any(all(x < a for x in B) for B in b)).sum()
def r(a,b):
  i = [B >= a for B in b]
  return 1+i.index(True) if True in i else len(b)
a2 = acc(lambda a,*b: np.prod([r(a,B) for B in b])).max()
Jump in the discussion.

No email address required.

Jesse what the frick are you talking about??

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.