Advent of Code Solutions Thread: Day Two

Only shitty solutions allowed

38
Jump in the discussion.

No email address required.

The shitty version, managed to knock it down to half this length afterward by actually using modulo and just passing numbers around instead of characters.

Also, which one of y'all is #986674?

class Day02:
	def _score(self, a, b):
		la = ['A', 'B', 'C']
		lb = ['X', 'Y', 'Z']
		ia = la.index(a)
		ib = lb.index(b)

		score = (ib + 1) 

		if ia == ib:
			score += 3
		elif ((a == 'A' and b == 'Y')
				or (a == 'B' and b == 'Z')
				or (a == 'C' and b == 'X')):
			score += 6

		return score
	def run_a(self, data):
		acc = 0
		for l in data:
			acc += self._score(l[0], l[2])
		return acc
	def run_b(self, data):
		la = ['A', 'B', 'C']
		lb = ['X', 'Y', 'Z']
		acc = 0
		for l in data:
			x, y = l[0], l[2]
			ix = la.index(x)
			if y == 'Y':
				s = lb[ix]
			elif y == 'Z':
				s = lb[(ix + 1) % 3]
			elif y == 'X':
				s = lb[ix - 1]
			acc += self._score(x, s)
		return acc
Jump in the discussion.

No email address required.

too much nesting, make subfunctions :marseyautism:

Jump in the discussion.

No email address required.

um function calls are pure overhead actually, you're just wasting cycles

Jump in the discussion.

No email address required.

true in the context in python, but in any other decent, less dynamic language function calls are inlined aggressively.

Jump in the discussion.

No email address required.

The actual program being run is the interpreter kernel so there's not even a real processor-level function call, in python it's pretty much just as slow as anything else (ignoring the instruction cache)

Jump in the discussion.

No email address required.

That's orthogonal to what I was talking about; my point is that cpython can't even inline function calls* in its byte code due to the dynamic nature of functions as objects; you can get/set arbitrary attributes on them.

* not necessarily actual call instructions

Jump in the discussion.

No email address required.

Wait until you see how many function pointers my C++ codebase has :soyjakanimeglasses:

kill me please dear god

Jump in the discussion.

No email address required.

he doesn't use lambdas [auto& = std::move(x)] (int y) -> std::string mutable { x += y; return "hello world"; }

he hasn't descended into template heck where the are no classes/functions only nested templates with 5+ parameters each

he hasn't read category theory for programmers and reimplemented the haskell prelude in c++

he uses function pointers, std::function, virtual functions (non zero cost abstractions anime puke)

he uses c++, not rust

its unironically over keep yourself safe

Jump in the discussion.

No email address required.

he hasn't descended into template heck where the are no classes/functions only nested templates with 5+ parameters each

I have templates that are templated on other templates recursively, please end me I'm not even entirely sure how they get resolved

Jump in the discussion.

No email address required.

More comments

the rdrama :marseyoverseether: dot net code :marseycodecellove: philosophy :marseynietzsche:

Jump in the discussion.

No email address required.

My favorite part is it's not even true in native Python, it's just as slow as anything else but people still try their hardest

Jump in the discussion.

No email address required.

it used to be a pretty :marseyglam: bad pain :marseydomesticabuse: point :marseyhesklennyyouknow: of python :marseycodecel: but they've actually :marseyakshually: apparently done :marseyclapping: some stuff to make :marseyyarn: function calls not terribly onerous in pyland

Jump in the discussion.

No email address required.

umm actually they should be optimized out at compile time, have you heard of rust? :marseyfemboy:

Jump in the discussion.

No email address required.

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