For you, I'm glad you're getting paid by a sponsor. You do great work and you should be paid for it. I'll gladly take the slight annoyance if it means you're benefiting from your own hard work
There is no real advantage to going backwards. You can implement the exact same algorithm _she_ implemented in the video by going from the start of the list. Think about it, if you have two lists A and B that are really the exact same list but reversed relative to each other, what the backwards algorithm does to solve list A would be the same thing the forwards algorithm does to solve list B and vice versa.
this is untrue because the operators are left-to-right, also *she. i may be misunderstanding your approach here though so if you implemented it this way i would like to see how you solved it
@@hyper-neutrino Oh, right, computer science degree, I should have known, my bad. Anyway yeah I just forgot to consider that because there's no order of operations it matters whether you go forwards or backwards. If it was like a real mathematical equation, if you flip the whole thing around you still get the same result. 2 + 3 * 4 + 5 and 5 + 4 * 3 + 2 are the same thing in math so my brain just assumed it to be true for the puzzle. Of course it's not, I was just being dumb.
@@QuotePilgrim Also, even if the list were analysed by operation precedence, this would be wrong once you consider non-commutative operations (such as concatenation). The only reason why handling the input in reverse is more useful than forward is because two of the operations (multiplication and concatenation, and technically addition also) don't have a well-defined inverse operation, which allows us to reduce the amount of nodes to search in the ternary tree.
yeah, i realize i've been forgetting to put those in. i remembered because i had a sponsor segment and so i wanted to let people skip it more easily :)
whats wrong with my code from collections import deque def read_input(file_path): result = {} with open(file_path, 'r') as file: for line in file: key, value = line.split(':') key = int(key.strip()) value = deque(map(int, value.strip().split())) result[key] = value return result def re(a, b): if len(b) == 1: return b[0] f = b.popleft() s = b.popleft() # Addition sum_val = f + s b.appendleft(sum_val) add = re(a, b) if add == a: return a b.popleft() # Subtraction diff_val = f - s b.appendleft(diff_val) subtract = re(a, b) if subtract == a: return a b.popleft() # Multiplication prod = f * s b.appendleft(prod) product = re(a, b) if product == a: return a b.popleft() # Division (ensure no division by zero) if s != 0: div_val = f // s b.appendleft(div_val) divide = re(a, b) if divide == a: return a b.popleft() # Restore the deque b.appendleft(s) b.appendleft(f) return prod def ist(p): return re(p[0], p[1]) == p[0] def main(): file_path = 's.txt' data = read_input(file_path) # Print the map to verify the result ss = 0 for key, value in data.items(): if ist((key, value)): ss += key print(f'this makes a valid pair {key}') print(f'Sum of all the keys is: {ss}') if __name__ == '__main__': main() can someone help
For you, I'm glad you're getting paid by a sponsor. You do great work and you should be paid for it. I'll gladly take the slight annoyance if it means you're benefiting from your own hard work
So elegant, I didn't think of looking at the input from end to start... Once you know it, it seems so natural :D
There is no real advantage to going backwards. You can implement the exact same algorithm _she_ implemented in the video by going from the start of the list.
Think about it, if you have two lists A and B that are really the exact same list but reversed relative to each other, what the backwards algorithm does to solve list A would be the same thing the forwards algorithm does to solve list B and vice versa.
this is untrue because the operators are left-to-right, also *she. i may be misunderstanding your approach here though so if you implemented it this way i would like to see how you solved it
@@hyper-neutrino Oh, right, computer science degree, I should have known, my bad.
Anyway yeah I just forgot to consider that because there's no order of operations it matters whether you go forwards or backwards. If it was like a real mathematical equation, if you flip the whole thing around you still get the same result. 2 + 3 * 4 + 5 and 5 + 4 * 3 + 2 are the same thing in math so my brain just assumed it to be true for the puzzle. Of course it's not, I was just being dumb.
@@QuotePilgrim Also, even if the list were analysed by operation precedence, this would be wrong once you consider non-commutative operations (such as concatenation). The only reason why handling the input in reverse is more useful than forward is because two of the operations (multiplication and concatenation, and technically addition also) don't have a well-defined inverse operation, which allows us to reduce the amount of nodes to search in the ternary tree.
But how did elephant 🐘 run away with operator?
It's a good idea not to try to overanalyse the stories :P
Really cool to see that you got a sponsor for the video. WEll deserved payment for the consistently great work.
thanks! also that's monaspace krypton IIRC (monaspace.githubnext.com/)
Thank you for another great explanation of how to solve the puzzle!
I did it the exact way you showed first, but in Rust, so it's not nearly as compact. I like how concise Python is
The length of my Object Pascal solution would make you cry :P
@ Oh. My second programming language was Pascal. The memories…
@@shrugalic The day job is React/JS so AOC is about the only time I use Pascal these days.
chapters!
yeah, i realize i've been forgetting to put those in. i remembered because i had a sponsor segment and so i wanted to let people skip it more easily :)
@@hyper-neutrinoappreciated, ty
clever, going backwards
I'm just going "straightforward" ;-)
def matches1(target, array):
if len(array) == 1:
return target == array[0]
a = array[0]
b = array[1]
tail = array[2:]
return (matches1(target, [a + b] + tail)
or matches1(target, [a * b] + tail))
def matches2(target, array):
if len(array) == 1:
return target == array[0]
a = array[0]
b = array[1]
tail = array[2:]
return (matches2(target, [a + b] + tail)
or matches2(target, [a * b] + tail)
or matches2(target, [int(str(a) + str(b))] + tail))
so a DFS
yep
whats wrong with my code
from collections import deque
def read_input(file_path):
result = {}
with open(file_path, 'r') as file:
for line in file:
key, value = line.split(':')
key = int(key.strip())
value = deque(map(int, value.strip().split()))
result[key] = value
return result
def re(a, b):
if len(b) == 1:
return b[0]
f = b.popleft()
s = b.popleft()
# Addition
sum_val = f + s
b.appendleft(sum_val)
add = re(a, b)
if add == a:
return a
b.popleft()
# Subtraction
diff_val = f - s
b.appendleft(diff_val)
subtract = re(a, b)
if subtract == a:
return a
b.popleft()
# Multiplication
prod = f * s
b.appendleft(prod)
product = re(a, b)
if product == a:
return a
b.popleft()
# Division (ensure no division by zero)
if s != 0:
div_val = f // s
b.appendleft(div_val)
divide = re(a, b)
if divide == a:
return a
b.popleft()
# Restore the deque
b.appendleft(s)
b.appendleft(f)
return prod
def ist(p):
return re(p[0], p[1]) == p[0]
def main():
file_path = 's.txt'
data = read_input(file_path)
# Print the map to verify the result
ss = 0
for key, value in data.items():
if ist((key, value)):
ss += key
print(f'this makes a valid pair {key}')
print(f'Sum of all the keys is: {ss}')
if __name__ == '__main__':
main()
can someone help