Broadcasting Practice (Advent of Code in Pytorch)

Anyone working on Advent of Code? I practicing broadcasting and array manipulation skills that we started working on earlier in the course (with the mean-shift implementation) with this competition.

I am doing the problems in APL and then trying to do them in Pytorch. My goal is to use Pytorch functionality as much as possible and avoid anything non-pytorch (loops, if statements, comprehension, non-pytorch functions, etc). My 1 exception is file reading - I will use anything to get the data into pytorch.

Here was Day 1 in Pytorch, where the only thing I couldn’t do in pytorch completely was 1 comprehension. Would love to see other people’s solutions!

import torch as t

data = t.Tensor(pd.read_csv('day1_input.txt',skip_blank_lines=False).values)
splits = t.isnan(data).squeeze().nonzero().squeeze()

sums = t.Tensor([t.nansum(o) for o in t.tensor_split(data,splits)])

part1_solution = sums.max()
part2_solution = sums.sort()[0][-3:].sum()

that’s cool @Ezno ! I’m just doing them in ordinary Python. Have you kept up with them? I’ve been wanting to actually practice broadcasting or try APL if you wanted to try pairing on one (I’m in PST.)

1 Like

I have been keeping up with completing them (barely) in APL. I have not kept up in python. I plan to try to catch up in python though!

A big problem with doing them in pytorch completely is the text stuff just doesn’t work there. Maybe numpy would be better. I decided for python I am not going to restrict myself to array programming in python though. Trying to do what APL does in python was just not working quite like I had hoped - APL is a very powerful array language so replicating that same solution in python wasn’t working well for me! Hopefully the APL solving informs some of my python solutions though!

1 Like