Coder Social home page Coder Social logo

adventofcode-2023-python's Introduction

adventofcode-2021-python

Useful functions

  • read file into list
with open('data/my_input/1.in') as f:
    lines = [  line.strip() for line in f]
  • read multi-line with emptyline-separator into list
with open('data/my_input/6.in') as f:
    lines = f.read().split('\n\n')  
  • usual regex functions
num=lambda s : re.findall('(\d+)-(\d+) (.): (.*)',s) 
l=num(line)


a= re.compile(r'.*ecl:([a-zA-Z0-9]+)', re.IGNORECASE)
b=a.match(s) 
return b and b.groups()[0] in fs
  • Counter from collections
from collections import Counter

l=Counter(s)
[ (k,v) for k,v in l.items()]
[ v for v in l.values()]
[ k for k in l.keys()]
  • "advanced" search
lis=sorted(lis,key=lambda p:p[1],reverse=True)
  • Networkx
import networkx as nx
d=nx.DiGraph()
for li in l:
    x,y,z=re.findall('[A-Z]',li)
    d.add_edge(y,z)
print(''.join(nx.lexicographical_topological_sort(d)))

another example :
    G = nx.DiGraph()
    for n in open("data/my_input/day6-input.file").readlines():
        G.add_edge(*[x.strip() for x in n.split(")")])
    print(nx.transitive_closure(G).size())
    print(nx.shortest_path_length(G.to_undirected(), "YOU", "SAN") - 2)
  • geographic move
dx=dict(zip('LRUD',[-1,1,0,0]))
dy=dict(zip('LRUD',[0,0,1,-1]))

e.g.:
direction='L'
x+=dx[direction]
y+=dy[direction]

or

LRUD = [(0, 1), (0, -1), (1, 0), (-1, 0)]
LRUD = {(0, 1), (0, -1), (1, 0), (-1, 0)}
for lrud in LRUD:
    dx, dy = lrud

or (less efficient)

for dj in [-1, 0, 1]:
     for di in [-1, 0, 1]:
          if (dj,di) == (0,0):
              continue
  • alphabetic search
string.ascii_lowercase
  • visualization
def printmap(d2):
    xmin = min([x for x, y in d2])
    ymin = min([y for x, y in d2])
    xmax = max([x for x, y in d2])
    ymax = max([y for x, y in d2])

    # print(xmin, ymin, xmax, ymax)
    for i in range(xmin - 1, xmax + 2):
        sprint = ""
        for j in range(ymin - 1, ymax + 2):
            sprint += str(d2[i, j]) if (i, j) in d2 else "."
        print(sprint.replace("1", "O").replace("0", " "))
        

Usual algorithms

  • DFS (stack)
queue=[]
visited=[]
queue.append(a)
while queue:
    node=queue.pop()
    if node in visited:
        #donothing
    else:
        # do
        visited.append(node)
        queue.append(resultFromDo(node))
  • BFS (queue) - shortest path
queue=[]
visited=[]
queue.append(a)
while queue:
    node=queue.pop(0)
    if node in visited:
        #donothing
    else:
        # do
        visited.append(node)
        queue.append(resultFromDo(node))

adventofcode-2023-python's People

Contributors

cthiounn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.