• 2 Posts
  • 1.95K Comments
Joined 1 year ago
cake
Cake day: August 22nd, 2023

help-circle

  • Python

    Had to rely on an external polygon library for this one. Part 1 could have been easily done without it but part 2 would be diffucult (you can even use the simplify function to count the number of straight edges in internal and external boundaries modulo checking the collinearity of the start and end of the boundary)

    
    import numpy as np
    from pathlib import Path
    from shapely import box, union, MultiPolygon, Polygon, MultiLineString
    cwd = Path(__file__).parent
    
    def parse_input(file_path):
      with file_path.open("r") as fp:
        garden = list(map(list, fp.read().splitlines()))
    
      return np.array(garden)
    
    def get_polygon(plant, garden):
      coords = list(map(tuple, list(np.argwhere(garden==plant))))
      for indc,coord in enumerate(coords):
    
        box_next = box(xmin=coord[0], ymin=coord[1], xmax=coord[0]+1,
                       ymax=coord[1]+1)
    
        if indc==0:
          poly = box_next
        else:
          poly = union(poly, box_next)
    
      if isinstance(poly, Polygon):
        poly = MultiPolygon([poly])
    
      return poly
    
    def are_collinear(coords, tol=None):
        coords = np.array(coords, dtype=float)
        coords -= coords[0]
        return np.linalg.matrix_rank(coords, tol=tol)==1
    
    def simplify_boundary(boundary):
    
      # if the object has internal and external boundaries then split them
      # and recurse
      if isinstance(boundary, MultiLineString):
        coordinates = []
        for b in boundary.geoms:
          coordinates.append(simplify_boundary(b))
        return list(np.concat(coordinates, axis=0))
    
      simple_boundary = boundary.simplify(0)
      coords = [np.array(x) for x in list(simple_boundary.coords)[:-1]]
      resolved = False
    
      while not resolved:
    
        end_side=\
        np.concat([x[:,None] for x in [coords[-1], coords[0], coords[1]]], axis=1)
    
        if  are_collinear(end_side.T):
          coords = coords[1:]
        else:
          resolved = True
    
      return coords
    
    def solve_problem(file_name):
    
      garden = parse_input(Path(cwd, file_name))
      unique_plants = set(garden.flatten())
      total_price = 0
      discounted_total_price = 0
    
      for plant in unique_plants:
    
        polygon = get_polygon(plant, garden)
    
        for geom in polygon.geoms:
          coordinates = simplify_boundary(geom.boundary)
          total_price += geom.area*geom.length
          discounted_total_price += geom.area*len(coordinates)
    
      return int(total_price), int(discounted_total_price)
    
    

  • Python

    I initially cached the calculate_next function but honestly number of unique numbers don’t grow that much (couple thousands) so I did not feel a difference when I removed the cache. Using a dict just blazes through the problem.

    from pathlib import Path
    from collections import defaultdict
    cwd = Path(__file__).parent
    
    def parse_input(file_path):
      with file_path.open("r") as fp:
        numbers = list(map(int, fp.read().splitlines()[0].split(' ')))
    
      return numbers
    
    def calculate_next(val):
    
      if val == 0:
        return [1]
      if (l:=len(str(val)))%2==0:
        return [int(str(val)[:int(l/2)]), int(str(val)[int(l/2):])]
      else:
        return [2024*val]
    
    def solve_problem(file_name, nblinks):
    
      numbers = parse_input(Path(cwd, file_name))
      nvals = 0
    
      for indt, node in enumerate(numbers):
    
        last_nodes = {node:1}
        counter = 0
    
        while counter<nblinks:
          new_nodes = defaultdict(int)
    
          for val,count in last_nodes.items():
            val_next_nodes = calculate_next(val)
    
            for node in val_next_nodes:
              new_nodes[node] += count
    
          last_nodes = new_nodes
          counter += 1
        nvals += sum(last_nodes.values())
    
      return nvals
    
    




  • Python

    Not surprisingly, trees

    import numpy as np
    from pathlib import Path
    
    cwd = Path(__file__).parent
    
    cross = np.array([[-1,0],[1,0],[0,-1],[0,1]])
    
    class Node():
      def __init__(self, coord, parent):
        self.coord = coord
        self.parent = parent
    
      def __repr__(self):
        return f"{self.coord}"
    
    def parse_input(file_path):
    
      with file_path.open("r") as fp:
        data = list(map(list, fp.read().splitlines()))
    
      return np.array(data, dtype=int)
    
    def find_neighbours(node_pos, grid):
    
      I = list(filter(lambda x: all([c>=0 and o-c>0 for c,o in zip(x,grid.shape)]),
                      list(cross + node_pos)))
    
      candidates = grid[tuple(np.array(I).T)]
      J = np.argwhere(candidates-grid[tuple(node_pos)]==1).flatten()
    
      return list(np.array(I).T[:, J].T)
    
    def construct_tree_paths(grid):
    
      roots = list(np.argwhere(grid==0))
      trees = []
    
      for root in roots:
    
        levels = [[Node(root, None)]]
        while len(levels[-1])>0 or len(levels)==1:
          levels.append([Node(node, root) for root in levels[-1] for node in
                         find_neighbours(root.coord, grid)])
        trees.append(levels)
    
      return trees
    
    def trace_back(trees, grid):
    
      paths = []
    
      for levels in trees:
        for node in levels[-2]:
    
          path = ""
          while node is not None:
            coord = ",".join(node.coord.astype(str))
            path += f"{coord} "
            node = node.parent
          paths.append(path)
    
      return paths
    
    def solve_problem(file_name):
    
      grid = parse_input(Path(cwd, file_name))
      trees = construct_tree_paths(grid)
      trails = trace_back(trees, grid)
      ntrails = len(set(trails))
      nreached = sum([len(set([tuple(x.coord) for x in levels[-2]])) for levels in trees])
    
      return nreached, ntrails
    


  • Wow I got thrashed by chatgpt. Strictly speaking that is correct, it is more akin to Tree Search. But even then not strictly because in tree search you are searching through a list of objects that is known, you build a tree out of it and based on some conditions eliminate half of the remaining tree each time. Here I have some state space (as chatgpt claims!) and again based on applying certain conditions, I eliminate some portion of the search space successively (so I dont have to evaluate that part of the tree anymore). To me both are very similar in spirit as both methods avoid evaluating some function on all the possible inputs and successively chops off a fraction of the search space. To be more correct I will atleast replace it with tree search though, thanks. And thanks for taking a close look at my solution and improving it.


  • there is no monthly interest in regular accounts here unless you put it in a savers account. but yes I do and I know that my pension usually invests the money too without much flexibility on where to invest it in. so unless you are Ron Swanson there is no complete disconnection from this web. but the fix is easy: all I have to say is “individually major share holder” since those will be the ones deciding about company policies not me.

    I think any company whose only shareholders are made up of people holding 0.000001% in shares wont suffer from the same consequences a company does when there are shareholders like %10, 20, 30 etc. Same difference between having billions or hundred thousands.







  • Python

    It is a tree search

    def parse_input(path):
    
      with path.open("r") as fp:
        lines = fp.read().splitlines()
    
      roots = [int(line.split(':')[0]) for line in lines]
      node_lists = [[int(x)  for x in line.split(':')[1][1:].split(' ')] for line in lines]
    
      return roots, node_lists
    
    def construct_tree(root, nodes, include_concat):
    
      levels = [[] for _ in range(len(nodes)+1)]
      levels[0] = [(str(root), "")]
      # level nodes are tuples of the form (val, operation) where both are str
      # val can be numerical or empty string
      # operation can be *, +, || or empty string
    
      for indl, level in enumerate(levels[1:], start=1):
    
        node = nodes[indl-1]
    
        for elem in levels[indl-1]:
    
          if elem[0]=='':
            continue
    
          if elem[0][-len(str(node)):] == str(node) and include_concat:
            levels[indl].append((elem[0][:-len(str(node))], "||"))
          if (a:=int(elem[0]))%(b:=int(node))==0:
            levels[indl].append((str(int(a/b)), '*'))
          if (a:=int(elem[0])) - (b:=int(node))>0:
            levels[indl].append((str(a - b), "+"))
    
      return levels[-1]
    
    def solve_problem(file_name, include_concat):
    
      roots, node_lists = parse_input(Path(cwd, file_name))
      valid_roots = []
    
      for root, nodes in zip(roots, node_lists):
    
        top = construct_tree(root, nodes[::-1], include_concat)
    
        if any((x[0]=='1' and x[1]=='*') or (x[0]=='0' and x[1]=='+') or
               (x[0]=='' and x[1]=='||') for x in top):
    
          valid_roots.append(root)
    
      return sum(valid_roots)
    


  • Well also because he was likely just a pawn. at the root of all this are the shareholders who are billionaires and who likely make the calls regarding company policies. this guy was likely just their lapdog. so even though a rule of no more than 500mil would not deal with this guy, it would definitely have prevented the existence of a parasite company of this scale. I would still say though 50 mil should be sufficient. It will allow you almost all the reasonable luxuries you can imagine if that is your thing.



  • Python

    sort using a compare function

    from math import floor
    from pathlib import Path
    from functools import cmp_to_key
    cwd = Path(__file__).parent
    
    def parse_protocol(path):
    
      with path.open("r") as fp:
        data = fp.read().splitlines()
    
      rules = data[:data.index('')]
      page_to_rule = {r.split('|')[0]:[] for r in rules}
      [page_to_rule[r.split('|')[0]].append(r.split('|')[1]) for r in rules]
    
      updates = list(map(lambda x: x.split(','), data[data.index('')+1:]))
    
      return page_to_rule, updates
    
    def sort_pages(pages, page_to_rule):
    
      compare_pages = lambda page1, page2:\
        0 if page1 not in page_to_rule or page2 not in page_to_rule[page1] else -1
    
      return sorted(pages, key = cmp_to_key(compare_pages))
    
    def solve_problem(file_name, fix):
    
      page_to_rule, updates = parse_protocol(Path(cwd, file_name))
    
      to_print = [temp_p[int(floor(len(pages)/2))] for pages in updates
                  if (not fix and (temp_p:=pages) == sort_pages(pages, page_to_rule))
                  or (fix and (temp_p:=sort_pages(pages, page_to_rule)) != pages)]
    
      return sum(map(int,to_print))