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

help-circle



  • Python

    It is a binary 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))