Posts

Advent of Code: Day 5

Link to problem:  https://adventofcode.com/2024/day/5 When I first looked at this, my first instinct was to treat it like a partial order, and make the DAG to represent it. But then I thought on it a bit more-- The way the question is stated doesn't actually make the input a partial ordering, it doesn't have to be transitive-- so I wasn't sure whether it'd be possible to properly represent this as a DAG. I decided not to think too hard, and just use a simple approach that I knew would work. I'll probably implement it as a DAG later on though, when I'm doing this in another language. I decided to read the "rules" into a dict -- for each number on the left, I'd keep a set of numbers that appear in its right in the rules -- i.e. a set of numbers that should never precede it. Let's call N the number of rules, M the number of updates, and L the length of the longest update list. Then the maximum number of keys and values in the rules dict would be N...

Advent of Code: Day 4

This is earlier than I expected but we're already at the point where I no longer know if I'm making a great solution... but it works, and in less than a second, and I'll take that as a win for now. Link to problem:  https://adventofcode.com/2024/day/4 import numpy as np grid = [] with open ( "input.txt" ) as file : for line in file : grid.append ( line.strip ()) grid = np.array ( grid ) rows = len ( grid ) cols = len ( grid [ 0 ]) I just wanted to make myself a getLetter function, mostly just to take away the code checking for out of bounds errors from my actual solution code... no real reason to do this though. def getLetter ( i , j ) -> str : if i < 0 or i >= rows or j < 0 or j >= cols : return '-' return grid [ i ][ j ] Then... I initially started of by writing code to like-- have a conditional for each direction. Stopped within 10 seconds, I hate writing out a lot of repetitive code, and like to try and gene...

Advent of Code: Day 3

 Link to problem:  https://adventofcode.com/2024/day/3 Oh no... regex. I hate regex. Perplexity is great at regex. import re pattern = r "mul\((\d{1,3}),\s*(\d{1,3})\)" matches = re.findall ( pattern , input ) print ( matches ) Thanks Perplexity! sum = 0 for a , b in matches : product = int ( a ) * int ( b ) sum += product print ( sum ) That's problem 1 done. Now... for problem 2: pattern = r "mul\(\d{1,3},\s?\d{1,3}\)|do\(\)|don't\(\)" matches = re.findall ( pattern , input ) print ( matches ) Thanks again Perplexity! do = True sum = 0 for i in matches : if i == 'do()' : do = True continue if i == "don't()" : do = False continue if do : a , b = i.lstrip ( 'mul(' ) .rstrip ( ')' ) .split ( ',' ) product = int ( a ) * int ( b ) sum += product print ( sum ) This one felt like cheating... I'll come back to do this in some terrible language or just do the string...

Advent of Code: Day 2

 Link to problem:  https://adventofcode.com/2024/day/2 Day 2 seems pretty straightforward too, at least the first problem: Each line is processed independently so I figured I'd define a function rather than having a really messy for loop. And... I was struggling to get my limited attention spam to parse 5 conditional statements.... so I brought it down to 4 by using that set safe safe = { 1 , 2 , 3 } def isSafe ( levels : tuple [ int ]) -> bool : increasing = True decreasing = True for a , b in zip ( levels , levels [ 1 :]): if abs ( a - b ) not in safe : return False if a > b : increasing = False if b > a : decreasing = False if not ( increasing or decreasing ): return False return True Yes, I know, this is not a very good use of zip... I wouldn't ever use it in that situation if it was possible that the input was very large, it's very inefficient both in its use of memory and time. But I just disc...

Advent of Code: Day 1

 I'm going to start off being boring in Python, but it's something! And hopefully I don't make any embarrassingly inefficient choices along the way. 2024 Day 1 Problem:  https://adventofcode.com/2024/day/1#part1 Splitting the list into 2: list_1 = [] list_2 = [] first = True for i in number_list : if first : list_1.append ( i ) first = False else : list_2.append ( i ) first = True print ( list_1 ) print ( list_2 ) Sorting the list:  list_1.sort () list_2.sort () And finally, adding up the distances, using the zip function which I recently discovered: sum = 0 for i , j in zip ( list_1 , list_2 ): sum += abs ( int ( j ) - int ( i )) print ( sum ) I might come back and do this in Haskell or some other impractical language... we'll see... but for right now, the ease of Python might just give me a few extra % on my final projects and exams, so I'll have to stick to that. And that brings me to part 2 :)  Here, it feels like casting the second ...