catchup
This commit is contained in:
parent
1313b8cb7e
commit
a56eb52f08
54
2021/10/10.py
Normal file
54
2021/10/10.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
with open("input.txt") as f:
|
||||
lines = f.read().split('\n')
|
||||
|
||||
score = 0
|
||||
incomplete = []
|
||||
for line in lines:
|
||||
corrupt = False
|
||||
stack = []
|
||||
for char in line:
|
||||
if char in ['(','[','{','<']:
|
||||
stack.append(char)
|
||||
elif char == ')':
|
||||
prev = stack.pop()
|
||||
if prev != '(':
|
||||
score += 3
|
||||
corrupt = True
|
||||
break
|
||||
elif char == ']':
|
||||
prev = stack.pop()
|
||||
if prev != '[':
|
||||
score += 57
|
||||
corrupt = True
|
||||
break
|
||||
elif char == '}':
|
||||
prev = stack.pop()
|
||||
if prev != '{':
|
||||
score += 1197
|
||||
corrupt = True
|
||||
break
|
||||
elif char == '>':
|
||||
prev = stack.pop()
|
||||
if prev != '<':
|
||||
score += 25137
|
||||
corrupt = True
|
||||
break
|
||||
if not corrupt:
|
||||
incomplete.append(stack)
|
||||
print(score)
|
||||
scores = []
|
||||
for stack in incomplete:
|
||||
score = 0
|
||||
while stack:
|
||||
prev = stack.pop()
|
||||
if prev == '(':
|
||||
score = 5*score + 1
|
||||
elif prev == '[':
|
||||
score = 5*score + 2
|
||||
elif prev == '{':
|
||||
score = 5*score + 3
|
||||
elif prev == '<':
|
||||
score = 5*score + 4
|
||||
scores.append(score)
|
||||
from statistics import median
|
||||
print(median(scores))
|
94
2021/10/input.txt
Normal file
94
2021/10/input.txt
Normal file
|
@ -0,0 +1,94 @@
|
|||
(<{([{([[[<{(<(){}>({}()))}[(<[]<>>{<>})[{{}()}<{}[]>]]><[[<{}{}>([]{})]<<<>{}>[<>{}]}]{{([]{})({}())}<
|
||||
<[<<{<[(([[[[<[][]>][(<>()){<>()}]]][<{<<>{}>(()<>)}><([<>{}]{()()})([()()]([]{}))>]]){{<{[[<><>]({}{})]
|
||||
<(({(<{<({[<[[[]()][<>[]]]{{{}()}({}[])}>]{[{<<>[]>[()[]]}][[[{}[]](<>{})]{[()()]((){})}]}}(
|
||||
{{[{[[[{{{{{[([]())(<>())](<[]<>>{[]{}})}([[(){}]<<>()>]<<()()><{}{}>>)}}((<[{{}()}<()>]<({}{
|
||||
({([({([[({([{[]<>}]<<<>{}>[<>[]]>)})({{{[()[]]<()>}(([]<>)<[]()>>}({[<>{}]}([{}<>]([]())))})]][{<
|
||||
<(((<<<{([[[<{()<>}{{}()}>[({}())([][])]]{{(()())(<>{})}(<{}><[]{}>)}]<[{([]())<{}>}<[[][]](<>)>]<{
|
||||
(([{[{([(<{{[<{}><<><>>]<<{}<>>>}(({<>{}}(<>[])){{<>[]}})}>)({(<<([]<>){[][]}>([{}<>]({}()))>)<(([<>{}]<{}[]>
|
||||
<((<[[<({[<({{<>[]}}{<<>{}><{}[]>}){([(){}])<(()[])<<>[]>>}>(<((<>[])[<>[]]){{<>{}}{{}()}}>)]}[<
|
||||
<[{<[<(<[[[{[<[][]>][[<>()}({}())]}]<<((<>[]))[((){}){[]()}]><((()<>){{}{}})[(()[])<<><>>]>>]]{<[[<<<
|
||||
[{(<(({{{(<(({{}{}}))>{<[(<>[])<(){}>]<<()<>>{[][]}>>((([]()){[]})({<>[]}<()[]>))})({<<({}())>[(()())
|
||||
<{[<<[<[([{{<<(){}><()[]>>([{}<>][{}<>])}}[[[[(){}]({}())][(()[])<[]()>]]]]({([<<>>[<>()]]([[]<>]<{}{}>))((<{
|
||||
({{{<(([{(([{{()()}{<>{}}}[<<>>[()()]]]))<[<[[{}{}]{[]()}]<([]<>){[]()}>}{{{[]}[<>()]}}]>}]
|
||||
[(([({[<([{<<({}{})<{}[]>>><<{()()}([])>{{[]<>}[()()]}>}{{<([]()){{}{}}>[{<>()}{{}()}]}[({<>()}{(){}})<[{}(
|
||||
{([{({[<<<<<[{{}[]}](<{}<>>)>>({(<{}()>[[][]])})>><<[[({<><>}({}[]})]([{{}()}[[]()]][({}{})({}{})])
|
||||
<{([{[<{<<{<<{<>[]}<()<>>>[<<>()>(()[])]>{<({}())([]())>}}<({{{}{}}([]{})}{{{}<>}({}<>)})[{<{}()>(()[
|
||||
[<{{(<{<<{<(<{[]<>}{(){}}>{{()<>}[[][]]}){[{<>[]]<{}<>>]<({}{})>}>}{<(<{[]{}}(<>())>)[{{[][]}[()]}<<()><
|
||||
[[[<[[[[<{(((<[][]>(()<>)))<<<<>{}>{<><>}}{({}())(<>{})}>)[({[<><>][{}()]}[([]{})])({(<><>)[(){}]}(<{
|
||||
({[<[<<([<{<{(<>{}){[]>}<{{}[]}(<><>)>>}><[(<({}())><<<>()>[()[]]>)([{<>{}}({}{})])]({{{<>{}}{{}<>}}[[<
|
||||
[(<[{<[[(<[<{[()()][{}[]]}{[(){}][<><>]}>]{({<[][]>[()<>]}[([][])<{}>])}><[(<([][])<<>()>>[[<>()](<><>)]
|
||||
[({(<{[<{[(<<[()<>]([]{})>[<[][]>({}{})]>(<{[]<>}<<>[]>>((()[]){{}[]}))){[<({}{})<()<>>><<()[]>([])>]<<<
|
||||
<(<((({{<[{<[<()[]><()<>>]{<<>{}><()[]>]>{<{()[]}{<>()}>({()<>}[<>{}])}}<<[[()()]<[][]>]<<()<>><{}{}>>>
|
||||
<<{{[{([{(({[<()<>>[{}()]]{<()()><[][]>}}(((()<>]{{}{}})([<>[]]{<>[]})))[<[<{}()>({}())]<(()<>)[[]()]>
|
||||
<<[[(<[<[[<{(<<>{}>{[]<>})}<<[[]<>](<>())><{{}<>}[()()]>>>((({{}()}<()()>))[[(<>{})<<>{}>]{(()){{}<>}}]
|
||||
[{<{([<<{{[[[<[]{}>{(){}}][{{}()}{[][]}>][({{}()}[{}{}]){({}<>)}]]{{[({}())([])][<{}><{}()>
|
||||
[{((({([{((<<[[]()]<{}>><<[][]><[]{}>>>[[[<>()]{[]{}}][{()<>}[(){}]]]))<<{[<[]{}>({}{})]}>(<{[[]
|
||||
{[[[{[{{((<[(<<>[]><{}()>)[{<>()}[()<>]]]{({{}{}}({}{})){[()()](())>}>)({(<<[]()><[]>>)}{[[({}[]){()<
|
||||
<<<((<<({({<<[[]][[][])>>[({<><>}[<>()])<[{}{}]([][])>]})([{(<[][]>[{}<>])<[<><>]>}<<([]{})(()<>)>[[{
|
||||
[({[([{[{<(({([][])<{}[]>}({(){}}[()()]))<([{}[])(<>[]))[({}()){[]<>}]>){<{[()[]]<{}{}>}>{<[()()]<[]
|
||||
{<<[<<[((([{[{[]()}<<>>][<()[]>]}[[{{}()}{[][]}]<({}{})(<>{})>]]){({[{()<>}<{}<>}]{(()<>)}}<([[]][[](
|
||||
<<({{[{<<{<[{[()()][[][]]}]>(([{[]}(()())]<{()[]}<<>[]>>)(((()[])[()()])))}{((<{{}()}{{}{}}>{[[]
|
||||
<({[{[(([{([[<{}<>>]]<[[()<>](<>())]>){[[[(){}]]{<{}<>>{()<>}}]{<[[]{}]<{}{}>>{[()<>]}}>}]{(([{([]{}
|
||||
[<{{{<<[{(({[({}()){{}[]}](({}{}){[]{}})}(<{{}[]>>[<()<>>{{}<>}]))[[<<[][]>><(()())[<>()]>][({()[]}(()
|
||||
(<{{{{[({[{{<[{}<>]((){})><[[]{}](<><>)>}(({[][])<{}[]>)[<()()>[<>[]]])}]})]<{{[<{(([]{})<()()>){
|
||||
(<<<<[{((<{({({}[])[[]()]}[<{}{}>([]<>)])(({[]()})({(){}}(<><>)))}[[[<[]{}><()()>]<{{}()}{{}
|
||||
(([([[<{{{([<(<><>){(){}}>[[[]{}]]]({[[]{}]<()()>}<<<><>>>))}}}]<[[<({[{<><>}{<>()}]{([]<>){()<>}}
|
||||
(<(<{<(<(<(({{<>}<[]()>}([[]{}])){<(<>())[{}()]>([()[]](()))})[[[{()()}({}{})]<<{}>([]{})>]([<()[]><{}<>>
|
||||
[(<[[{{((<[<<<[]>[{}{}]}<[[]<>]<[]{}>>>{{[[]()][()<>]}}]>{({([{}()]<()()>)[<()[]><(){}>]}[
|
||||
{[<{<<<<<[{(<{[][]}({}())>(((){}){()()}))}]>>{(<({<(<>{}){[]{}}>{((){})<<>()>}}<<[<><>][<>()]>[<[][]><<><>>
|
||||
<{[({<[{{{<{<(()[])><{[]()}<(){}>>}{[([][])[()[]]]}>}({{({{}<>}<[]()>)}}<([{{}<>}]<({}<>)<()<>>>)<<([](
|
||||
<[<{(<({[<[({<()<>>{(){}}}<{{}[]}(()<>)>)]{<(<<><>>(<>))<{{}<>}({}<>)>>{{<()[]>{{}{}}}{<[]<>>}}}>]}{<[
|
||||
{{[<({{{{[{({[{}[]]{{}[]}})(({[][]}<<>()>)<[<>{}]{[]<>}>)}{{<[<>{}][()()]>([(){}](<>{}))}}]
|
||||
([{<[(((({[({<()<>><<>{}>})]{<[([])[<>()]]{(<>)[[]{}]}>[<(<>)({}[])>[({}){<><>}]]}}{[[<(<>)(()[])>
|
||||
{(<{<{<({[({{[{}[]]}})[({(<><>){[][]}})<([()[]](<>{}))<[{}()]((){})>]]]{{{<(()<>){()<>}>}[(({}())[
|
||||
<(<[<{{{[[<{({()()}([]{})){[<>]<(){}>}}>[[[{[]<>}{<>[]}][(<>{}){<><>}]](({(){}}<[]<>>))]][<({{{}(
|
||||
<{((([<{<[[(({()<>}(()<>)))[<{[][]}[<><>]>]]]><<(({([][])[{}{}]}[<[]{}>[<><>]])([{(){}}[()()]][[[]{}]({}[])])
|
||||
{{<[[{<<[<<{{<<>{}>([]())}[[<>[]](()[])]}[<({}{})[<><>]>[[{}]{[]}>]>>[<([([])(()[])])><{(<(){}>{()[]})
|
||||
[<[[<<(<{<({{<<><>>([]<>)}}{<[{}{}]{[][]}>[{[]{}}<<><>>]})({{<[]<>>(()[])}(({}[])[()<>])})
|
||||
(([<{[{[[<{(({<>[]}{()[]}))[{[()[]]{<><>}}(({}{})[{}[]])]}[{(<{}<>>(<><>))<(()<>)([][])>}{<{[]()}<(){}>
|
||||
(<<{{<{(<{<<[{{}{}}{{}()>]{{<>[]}<<>[]>}>>}>)<[({[{([]<>)(())}[({}{}){(){}}]]{[[(){}]]<([])([][
|
||||
({{(<[<[<<<{[{<>[]}]<{{}<>}>}>{<<[[]()]((){})><<[]()>[(){}]>>{{([]()){[]()}}(<()()>)}}>({[([<><>][<>()]){(()(
|
||||
<[{(<(<{([[[<([]{})<<><>>>[[()]([]<>)]]]{[<[<>{}]([]())}([<>()][{}<>])]<<([]{}){{}<>}><{{}()}{<>()}>>}])<
|
||||
(({{({[[[<({[({})<<>[]>]{<{}()>{()[]}}}){[[(<>{})([]{})]{[{}()](()[])}]([<()()>]{((){})<{}()>})}><[[
|
||||
[[[(({<{[([(({()<>}<[][]>))][[(([]())[[][]]){<[]>}]([[[][]]<<>[]>]([{}]{()()}))])]{{[({[<>[]][[]]}{(<
|
||||
([([{<[(({[{{<(){}>([]<>)}(<[]<>><(){}>)}]({([{}{}][()<>])}{(([]<>){[]<>}){[<>[]]<{}<>>}})}))<((<
|
||||
{<(<[{<<<(({<{()<>}<[]<>>>}{<[<>{}>{{}<>}>({(){}}{()})}){[{(())(()())}({()[]}(()<>))]<[{(){}}({}<>)]<[<>()]>
|
||||
{{[<[{([<(<<<<[]{}>[[][]]>{[()[]]<<>[]>}>{(({}())([][]))({[]()}<()<>>)}>[(({[]<>}(()[]))[<{}<>>{()[]}])
|
||||
<{<{{{{<[{{(<{{}{}}{()()}>[([]{}){[]()}])<{[[]<>]<<>()>}[<{}[]>{[]()}]>}<((<()()>[()[]]))>}][{[{<<{}
|
||||
({(([({[[(<<([<>()]{{}<>})(<()()>[[]()])>(<[()[]]{[]()}>{[()()]{()()}})>){[{[[()<>](()<>>]}([[
|
||||
({{<{[[{{<{<{([]){[]<>}}{{[]{}}<[]<>>}>}(<{{{}[]}<{}>>>)>}(([[[([]())(<>)]{[[]{}][[]]}]<<({}()
|
||||
[{([({([<<<<{{()[]}<{}{}>}[[[]()]{<>{}}]>[([<>{}]<[][]>){{[]<>}({}{})}]>{[[<[][]>([]())]{[<><>](<>[
|
||||
([({{<<({(([[<<>()><{}<>>]<(()[])({}())>]){((<[]()>[(){}])(<<><>>{{}[]})){[[[]()][[]<>]]<<[]()>[{
|
||||
[{(<<({[<[<<{[{}<>}[{}()]}{[[]<>]{()()}}>(<[()[]][()()]>)>(<{[()<>](()<>)}<<<>>({}{})>>([{
|
||||
(<[[{(({[{{({[()<>]}(({}[])[<><>]))([[[]<>](<>[])]{<(){}>[()<>]})}(((<<><>>({}()))({<>}((){}))
|
||||
(<{{<<<[({({[{{}<>}[<><>]][(()<>)<<>()>]}>})(<(<((()[])({}))<<<>>([][])>>)(({<()()>[()<>]})(({{}{}}({}()))(
|
||||
({<{{[<<{([((((){})<[]()>)(<<>[]>({}())))<{({}[])([])}{<{}[]>{()()}}>]<<{[()[]]{[]<>}}>>]}{<{([<[]<>><(){}>
|
||||
<[<[<<({{<[[{{<><>}<{}<>>}<[[][]>({}<>)>]]>(<<{(<>{}){()[]}}[<()<>>[[]]]>>[{<<<><>>((){})>[<()()>[{}<>]]}<([
|
||||
{<<(<[([<[[{<{[]<>}{{}[]}>{[<>()]{{}{}}}}<{<()<>>[()[]]}{(<>{})({}())}>]]><[([<<()<>>{()[]}>(<<>>
|
||||
{<({<[{{({(((({}[])(()<>)){(()[])})<[<(){}>(()<>)][<{}<>><[][]>]>){<{([]()][()<>]}<(<>())<()
|
||||
{{(([<[({<[(((<>()){()[]}){(<>{}){[]<>}}]<{{{}()}<{}{}>}[[{}()]{{}<>}]>][<{[[][]]{[]<>}}<<(){}>
|
||||
{[{(<{{([[{<{{<>}[<>{}]}{<[][]>{[]()}}>(((()[])({}()))<<()()>{[][]}>)}[<(((){}){{}[]})[({}[])]
|
||||
{[<([[({{({{[<{}[]>{[]()}])}<[[[[]{}]][<()[]>({}[])]]<<[[]<>]<<>{}>><{<>{}}(<>{})>>>)}}){{<([
|
||||
[([<(([[[{{<{<[]{}>{[][]}}[<[]()>(()[])]>{[{<>{}><()[]>][[[]]]}}<[[((){})[[]<>]][(()[])([][])]]>}(<{[[[]()
|
||||
[([{[({[{{{{<{()[]}>{<<><>>}}[[(<><>)([]())][([][]){<>[]}]]}}}]})]{{{[<{{{<{{}()}(<>{})>}{<({
|
||||
[<[{<[[[[<<<[<()[]>[()()]]<[{}()](<>)>><([<>{}]({}[]))>><<[<{}{}>[<>()]]((<>{}){[]()})>[([
|
||||
[<{{[{([[<<[<(<>{})({}<>)>]{<[[]{}]>{(()<>)[<>[]]}}>{[((()[])(()[])){({}{}}[[]{}]}]{([{}[]]
|
||||
{<<(<{<<([(<{<{}>[()()]}{{{}{}}[{}()]}>[{{[]()}{{}<>}>{{()()}([]{})}]){(<[()<>]<[]{}>>[{(){}}([]<>)])}]
|
||||
[<[<([<[{{{(<({}<>)[{}()]>[[<>()]])(({()[]}){({}){[][]}})}}}(<([<{{}[]}[{}{}]>{(()())}])[((({}[])[
|
||||
{(<(([(([[{<{<()[]>(()[])}(<<><>>(<><>))><[[[]{}][()()]]{([]{})({})}>}]({<[{()()}(<>()))<[[]](()())>>}<({{{}{
|
||||
{(({{<{([<{<<<(){}><{}()>>{[<><>]<{}()>}><[<{}{}>]<{<>[]}<(){}>>>}{{({[]<>}{()[]})}}><{(<{{}()}{{}()}>({(){
|
||||
{<<{(([<(({{{((){})<{}()>}}<(<()<>>([]<>)){{<><>}{()()}}>}<{{(())[<>[]]}}<<<(){}}<[][]>>[<{}()>[<><>]]>>))>
|
||||
<[{{{<{{[[[[({()<>}([]()))<(())([]())>]{{{<>()}{(){}}}{{{}[]}}}]][({{<<>()>}[([][])<()>)}((([
|
||||
[(({[<<[[({({{(){}}<[]()>})})][<[[([{}{}][[]()])]([(<>{})<{}()>][{{}{}}([]())])][[{[()()]<<>{}>}<<<>
|
||||
([(<<[(<[<<{[((){})(<>[]}]({<>}[<>[]])}(({{}[]}((){})))>{({{[]<>}[[]<>]}<{{}}<{}>>){(<[]()
|
||||
[[{{<[<{<{{{{([])[[]()]}((<>)(()[]))}(<<{}{}>([]{})>)}[{(<()<>>([]{}))<(<>())[()()]>}{<{[]()}[
|
||||
<<(<<[(<(([{({()<>}({}<>))[{()[]}[[][]]]}]((<{<>{}}[<>[]]><(()<>){()<>}>)[(<<>>{()<>})<([]()}[[]()]
|
||||
[<<<{[<<<[(<<{{}()>{[][]}>[[(){}](<>[])]>{[[[]<>]]<({}){()()}>})][{({<{}[]>}){[{[]()}[{}[]]]{[[][
|
||||
[{([<<[{((<<((<>[])<()<>>)>>)(<<(<()()>)(({})<<>>)>>))}]>(<{{{[((<()()>[{}{}])[<[][]>([]()}])[<[{}[]]{{}{}}>
|
||||
[{{{[(<([([{{{<>{}}}{[{}[]]<[]()>}}{{[(){}][[]{}]}((()<>)(()()))}]<<{{()<>}<()()>}>>){[[{(<>
|
||||
<<[[(<([({([{<{}[]>(<><>)}])}{(({[{}]({}{})}[[<>{}]])[{[()[]]{{}()]}{[{}[]][[]<>]}])<<(<[]()><(
|
||||
(({[<[[[[{<{<<()()>[<>[]]>{(()<>){()}}}{<(()())([]<>)>{[()<>]<<>{}>}}>}](({([[{}<>]<(){}>])<([[
|
||||
(<<[{[[<{<[(({<><>}[<><>])({(){}}(<>[])))<[([]<>){{}{}}]>>>}[<{({[<>()](()())}{([]())[<>[]]})[[(<>[])
|
||||
<[<(({([(((<[<{}[]>{<>{}}]{{<><>}<[]<>>}><([[]<>]{[]{}})[<{}>{()}]>)){[[{([]<>)}{<{}()>{<>{}}}]{[{[]<
|
||||
{{((<<{((([(<[()[]]([][])>)[{{(){}}[{}()]}<([]{}){[]{}}>]]{[[({}())[()]]][{[[]{}]<[][]>}{<{}[]>[<
|
||||
([(([{{[({<[[<(){}>[[]<>]]]<[{()<>}<(){}>]>>[{((<>[]))}]}(<[[{()()}{{}<>}][(()[]>{{}{}}]][<({}[])>[
|
10
2021/10/sample.txt
Normal file
10
2021/10/sample.txt
Normal file
|
@ -0,0 +1,10 @@
|
|||
[({(<(())[]>[[{[]{<()<>>
|
||||
[(()[<>])]({[<{<<[]>>(
|
||||
{([(<{}[<>[]}>{[]{[(<()>
|
||||
(((({<>}<{<{<>}{[]{[]{}
|
||||
[[<[([]))<([[{}[[()]]]
|
||||
[{[{({}]{}}([{[{{{}}([]
|
||||
{<[[]]>}<{[{[{[]{()[[[]
|
||||
[<(<(<(<{}))><([]([]()
|
||||
<{([([[(<>()){}]>(<<{{
|
||||
<{([{{}}[<[[[<>{}]]]>[]]
|
111
2021/11/11.py
Normal file
111
2021/11/11.py
Normal file
|
@ -0,0 +1,111 @@
|
|||
width = 10
|
||||
height = 10
|
||||
grid = {}
|
||||
with open("input.txt") as f:
|
||||
lines = f.read().split('\n')
|
||||
for i in range(height):
|
||||
for j in range(width):
|
||||
grid[(i,j)] = int(lines[i][j])
|
||||
|
||||
def steps(grid, steps):
|
||||
"""
|
||||
Step through grid multiple times
|
||||
and count the flashes.
|
||||
"""
|
||||
flashes = 0
|
||||
for i in range(steps):
|
||||
flashes += step(grid)
|
||||
return flashes
|
||||
|
||||
def step(grid):
|
||||
"""
|
||||
Step through grid once
|
||||
and count the flashes.
|
||||
"""
|
||||
increment(grid)
|
||||
flashes = flash(grid)
|
||||
return flashes
|
||||
|
||||
def increment(grid):
|
||||
"""
|
||||
Increment the entire grid.
|
||||
"""
|
||||
for i in range(height):
|
||||
for j in range(width):
|
||||
grid[(i,j)] += 1
|
||||
|
||||
def flash(grid):
|
||||
"""
|
||||
Calculate which octopi should flash.
|
||||
Also count how many flashes.
|
||||
"""
|
||||
flashes = 0
|
||||
flashed = set()
|
||||
cascade = []
|
||||
# find which octopi should flash and add them to queue
|
||||
for i in range(height):
|
||||
for j in range(width):
|
||||
point = (i,j)
|
||||
if grid[point] > 9 and point not in flashed:
|
||||
flashed.add(point)
|
||||
cascade += neighbors(point)
|
||||
grid[point] = 0 # reset
|
||||
flashes += 1
|
||||
# process the queue of flashing octopi
|
||||
while cascade:
|
||||
point = cascade.pop()
|
||||
# if it hasn't already flashed, increment its energy
|
||||
if point not in flashed:
|
||||
grid[point] = grid[point] + 1
|
||||
# then figure out if it should flash
|
||||
if grid[point] > 9 and point not in flashed:
|
||||
flashed.add(point)
|
||||
cascade += neighbors(point)
|
||||
grid[point] = 0 # reset
|
||||
flashes += 1
|
||||
return flashes
|
||||
|
||||
def neighbors(point):
|
||||
"""
|
||||
Get a list of neighboring points,
|
||||
diagonals included.
|
||||
"""
|
||||
i,j = point
|
||||
n = [
|
||||
(i-1,j-1),
|
||||
(i-1,j),
|
||||
(i-1,j+1),
|
||||
(i,j-1),
|
||||
#(i,j) is excluded
|
||||
(i,j+1),
|
||||
(i+1,j-1),
|
||||
(i+1,j),
|
||||
(i+1,j+1)
|
||||
]
|
||||
return [ (i,j) for (i,j) in n if (-1 < i < height) and (-1 < j < width) ]
|
||||
|
||||
def display(grid):
|
||||
"""
|
||||
Pretty print the grid.
|
||||
"""
|
||||
for i in range(height):
|
||||
row = []
|
||||
for j in range(width):
|
||||
row.append(grid[(i,j)])
|
||||
print(row)
|
||||
|
||||
grid1 = grid.copy()
|
||||
part1 = steps(grid1,100)
|
||||
#display(grid1)
|
||||
print(part1)
|
||||
|
||||
grid2 = grid.copy()
|
||||
from collections import Counter
|
||||
i = 0
|
||||
while True:
|
||||
step(grid2)
|
||||
i += 1
|
||||
c = Counter(grid2.values())
|
||||
if c[0] == width * height: # all are flashing at same time
|
||||
break
|
||||
print(i)
|
10
2021/11/input.txt
Normal file
10
2021/11/input.txt
Normal file
|
@ -0,0 +1,10 @@
|
|||
8261344656
|
||||
7773351175
|
||||
7527856852
|
||||
1763614673
|
||||
8674556743
|
||||
6853382153
|
||||
4135852388
|
||||
2846715522
|
||||
7477425863
|
||||
4723888888
|
10
2021/11/sample.txt
Normal file
10
2021/11/sample.txt
Normal file
|
@ -0,0 +1,10 @@
|
|||
5483143223
|
||||
2745854711
|
||||
5264556173
|
||||
6141336146
|
||||
6357385478
|
||||
4167524645
|
||||
2176841721
|
||||
6882881134
|
||||
4846848554
|
||||
5283751526
|
56
2021/13/13.py
Normal file
56
2021/13/13.py
Normal file
|
@ -0,0 +1,56 @@
|
|||
paper = {}
|
||||
instructions = []
|
||||
with open("input.txt") as f:
|
||||
points, folds = f.read().split('\n\n')
|
||||
for point in points.split('\n'):
|
||||
x, y = point.split(',')
|
||||
paper[(int(x),int(y))] = True
|
||||
for fold in folds.split('\n'):
|
||||
axis = fold[11:]
|
||||
axis, value = axis.split('=')
|
||||
instructions.append((axis, int(value)))
|
||||
|
||||
def fold(paper, instruction):
|
||||
axis, value = instruction
|
||||
if axis == 'x':
|
||||
return fold_x(paper, value)
|
||||
elif axis == 'y':
|
||||
return fold_y(paper, value)
|
||||
|
||||
def fold_x(paper, value):
|
||||
folded_paper = {}
|
||||
for point in paper:
|
||||
x, y = point
|
||||
if x > value:
|
||||
x = value - (x - value)
|
||||
folded_paper[(x,y)] = True
|
||||
return folded_paper
|
||||
|
||||
def fold_y(paper, value):
|
||||
folded_paper = {}
|
||||
for point in paper:
|
||||
x, y = point
|
||||
if y > value:
|
||||
y = value - (y - value)
|
||||
folded_paper[(x,y)] = True
|
||||
return folded_paper
|
||||
|
||||
part1 = fold(paper, instructions[0])
|
||||
print(len(part1))
|
||||
|
||||
for instruction in instructions:
|
||||
paper = fold(paper, instruction)
|
||||
|
||||
xs = []
|
||||
ys = []
|
||||
for point in paper:
|
||||
x, y = point
|
||||
xs.append(x)
|
||||
ys.append(y)
|
||||
width = max(xs)
|
||||
height = max(ys)
|
||||
|
||||
from matplotlib import pyplot as plot
|
||||
plot.scatter(xs,ys)
|
||||
plot.axis('equal')
|
||||
plot.show()
|
859
2021/13/input.txt
Normal file
859
2021/13/input.txt
Normal file
|
@ -0,0 +1,859 @@
|
|||
176,226
|
||||
749,474
|
||||
1218,784
|
||||
1193,451
|
||||
478,224
|
||||
1019,620
|
||||
865,667
|
||||
884,178
|
||||
622,505
|
||||
43,856
|
||||
475,619
|
||||
1295,96
|
||||
497,499
|
||||
589,326
|
||||
117,267
|
||||
559,361
|
||||
402,863
|
||||
1220,277
|
||||
167,617
|
||||
955,560
|
||||
1011,135
|
||||
311,618
|
||||
865,198
|
||||
855,761
|
||||
698,241
|
||||
180,786
|
||||
728,523
|
||||
832,222
|
||||
865,67
|
||||
1280,649
|
||||
155,844
|
||||
990,406
|
||||
688,505
|
||||
990,488
|
||||
1116,288
|
||||
1047,266
|
||||
181,408
|
||||
27,401
|
||||
883,257
|
||||
253,610
|
||||
1213,131
|
||||
967,395
|
||||
581,605
|
||||
316,302
|
||||
1260,425
|
||||
967,171
|
||||
721,774
|
||||
618,668
|
||||
604,51
|
||||
858,323
|
||||
520,553
|
||||
271,730
|
||||
749,567
|
||||
1274,892
|
||||
1054,102
|
||||
117,301
|
||||
201,598
|
||||
227,257
|
||||
1193,267
|
||||
146,658
|
||||
87,498
|
||||
1017,313
|
||||
1211,628
|
||||
118,668
|
||||
1059,259
|
||||
48,169
|
||||
698,292
|
||||
405,516
|
||||
676,110
|
||||
1101,667
|
||||
298,453
|
||||
251,119
|
||||
1046,529
|
||||
1245,774
|
||||
1019,722
|
||||
1009,365
|
||||
117,593
|
||||
27,469
|
||||
599,164
|
||||
915,775
|
||||
1088,91
|
||||
530,407
|
||||
124,571
|
||||
494,378
|
||||
97,59
|
||||
995,443
|
||||
311,49
|
||||
345,822
|
||||
175,469
|
||||
174,560
|
||||
427,66
|
||||
261,120
|
||||
371,38
|
||||
73,844
|
||||
145,637
|
||||
674,675
|
||||
1275,646
|
||||
787,44
|
||||
1074,57
|
||||
478,229
|
||||
1140,630
|
||||
999,51
|
||||
559,252
|
||||
30,649
|
||||
1256,182
|
||||
836,882
|
||||
801,679
|
||||
271,394
|
||||
176,665
|
||||
989,211
|
||||
887,533
|
||||
431,147
|
||||
760,539
|
||||
1207,523
|
||||
73,172
|
||||
786,690
|
||||
504,674
|
||||
1039,858
|
||||
1034,702
|
||||
540,277
|
||||
457,89
|
||||
393,436
|
||||
995,397
|
||||
728,371
|
||||
556,850
|
||||
180,747
|
||||
345,72
|
||||
887,361
|
||||
484,742
|
||||
692,598
|
||||
835,619
|
||||
1057,158
|
||||
1159,891
|
||||
412,201
|
||||
801,33
|
||||
391,628
|
||||
1116,736
|
||||
1240,344
|
||||
530,487
|
||||
667,397
|
||||
639,171
|
||||
880,168
|
||||
390,813
|
||||
788,176
|
||||
1250,793
|
||||
780,487
|
||||
445,667
|
||||
1217,857
|
||||
832,705
|
||||
42,763
|
||||
813,499
|
||||
700,444
|
||||
114,619
|
||||
1191,884
|
||||
119,10
|
||||
791,313
|
||||
917,436
|
||||
1243,343
|
||||
1290,513
|
||||
15,798
|
||||
728,75
|
||||
179,833
|
||||
683,873
|
||||
773,260
|
||||
830,271
|
||||
567,620
|
||||
610,450
|
||||
395,567
|
||||
231,361
|
||||
1135,469
|
||||
1278,46
|
||||
1136,399
|
||||
780,357
|
||||
403,567
|
||||
671,778
|
||||
343,51
|
||||
902,78
|
||||
676,241
|
||||
345,486
|
||||
504,224
|
||||
136,593
|
||||
458,400
|
||||
806,224
|
||||
274,873
|
||||
753,347
|
||||
591,714
|
||||
699,856
|
||||
966,427
|
||||
991,773
|
||||
169,859
|
||||
85,339
|
||||
817,175
|
||||
470,733
|
||||
932,52
|
||||
378,310
|
||||
164,694
|
||||
999,619
|
||||
552,168
|
||||
523,44
|
||||
807,303
|
||||
843,623
|
||||
999,299
|
||||
160,826
|
||||
97,723
|
||||
557,347
|
||||
790,553
|
||||
1143,617
|
||||
701,637
|
||||
1109,598
|
||||
1193,515
|
||||
418,505
|
||||
522,649
|
||||
184,733
|
||||
801,705
|
||||
227,42
|
||||
1255,235
|
||||
222,159
|
||||
92,784
|
||||
95,364
|
||||
912,344
|
||||
85,787
|
||||
1170,511
|
||||
756,809
|
||||
473,184
|
||||
226,731
|
||||
676,513
|
||||
1130,108
|
||||
251,775
|
||||
219,484
|
||||
219,708
|
||||
151,357
|
||||
1192,674
|
||||
360,505
|
||||
1267,486
|
||||
972,592
|
||||
626,221
|
||||
1268,292
|
||||
445,248
|
||||
830,495
|
||||
69,143
|
||||
401,485
|
||||
1213,884
|
||||
1175,313
|
||||
1241,303
|
||||
612,627
|
||||
634,676
|
||||
947,620
|
||||
729,498
|
||||
721,326
|
||||
1299,451
|
||||
544,537
|
||||
1079,306
|
||||
373,172
|
||||
1036,873
|
||||
519,357
|
||||
1163,203
|
||||
1197,268
|
||||
802,147
|
||||
95,585
|
||||
181,413
|
||||
858,213
|
||||
604,537
|
||||
452,323
|
||||
662,488
|
||||
276,401
|
||||
1178,357
|
||||
743,128
|
||||
236,57
|
||||
1210,309
|
||||
905,516
|
||||
99,492
|
||||
430,168
|
||||
1083,637
|
||||
706,625
|
||||
345,408
|
||||
569,555
|
||||
831,61
|
||||
32,400
|
||||
1208,352
|
||||
892,505
|
||||
97,884
|
||||
58,7
|
||||
227,852
|
||||
1143,572
|
||||
643,751
|
||||
311,548
|
||||
12,450
|
||||
276,394
|
||||
505,707
|
||||
370,355
|
||||
599,659
|
||||
445,67
|
||||
557,884
|
||||
1211,154
|
||||
1084,137
|
||||
1238,523
|
||||
551,747
|
||||
264,589
|
||||
755,850
|
||||
887,431
|
||||
311,845
|
||||
27,465
|
||||
254,814
|
||||
102,726
|
||||
10,750
|
||||
184,676
|
||||
676,784
|
||||
831,541
|
||||
1039,500
|
||||
937,172
|
||||
140,607
|
||||
831,353
|
||||
479,61
|
||||
264,813
|
||||
259,119
|
||||
840,733
|
||||
239,262
|
||||
1136,750
|
||||
68,341
|
||||
1275,248
|
||||
445,26
|
||||
766,537
|
||||
1265,396
|
||||
1098,53
|
||||
774,668
|
||||
50,537
|
||||
152,175
|
||||
118,674
|
||||
472,331
|
||||
1283,401
|
||||
1299,443
|
||||
1163,691
|
||||
1012,493
|
||||
909,50
|
||||
639,723
|
||||
870,393
|
||||
975,185
|
||||
1212,492
|
||||
229,777
|
||||
1310,172
|
||||
627,149
|
||||
42,292
|
||||
1206,287
|
||||
136,145
|
||||
97,131
|
||||
11,443
|
||||
671,171
|
||||
335,325
|
||||
686,549
|
||||
44,444
|
||||
1059,119
|
||||
1208,726
|
||||
197,420
|
||||
1120,301
|
||||
753,803
|
||||
1215,642
|
||||
975,709
|
||||
564,281
|
||||
197,474
|
||||
207,150
|
||||
805,707
|
||||
552,253
|
||||
144,563
|
||||
556,716
|
||||
1083,497
|
||||
503,357
|
||||
271,500
|
||||
1268,602
|
||||
145,215
|
||||
756,163
|
||||
290,296
|
||||
589,120
|
||||
1176,341
|
||||
691,198
|
||||
535,296
|
||||
962,625
|
||||
296,574
|
||||
555,873
|
||||
445,446
|
||||
171,722
|
||||
932,584
|
||||
944,401
|
||||
955,807
|
||||
580,479
|
||||
723,103
|
||||
355,334
|
||||
910,814
|
||||
73,624
|
||||
971,3
|
||||
698,653
|
||||
202,547
|
||||
1295,723
|
||||
1083,66
|
||||
283,436
|
||||
65,777
|
||||
295,738
|
||||
751,361
|
||||
1190,750
|
||||
519,177
|
||||
991,26
|
||||
494,826
|
||||
95,82
|
||||
1084,731
|
||||
1083,257
|
||||
1027,436
|
||||
1213,579
|
||||
75,150
|
||||
792,77
|
||||
518,77
|
||||
704,171
|
||||
72,523
|
||||
1116,158
|
||||
306,513
|
||||
388,65
|
||||
315,443
|
||||
97,619
|
||||
93,663
|
||||
282,892
|
||||
112,254
|
||||
134,647
|
||||
373,274
|
||||
113,268
|
||||
69,420
|
||||
151,751
|
||||
426,626
|
||||
1178,494
|
||||
473,486
|
||||
1211,413
|
||||
1119,465
|
||||
519,313
|
||||
691,795
|
||||
535,598
|
||||
170,712
|
||||
831,93
|
||||
1299,133
|
||||
1266,444
|
||||
937,786
|
||||
125,184
|
||||
718,813
|
||||
423,431
|
||||
229,290
|
||||
440,393
|
||||
1074,281
|
||||
1197,178
|
||||
965,72
|
||||
214,501
|
||||
263,621
|
||||
922,9
|
||||
788,649
|
||||
306,558
|
||||
909,284
|
||||
55,730
|
||||
1207,371
|
||||
1198,469
|
||||
994,302
|
||||
263,721
|
||||
311,299
|
||||
104,287
|
||||
550,355
|
||||
391,714
|
||||
216,331
|
||||
808,175
|
||||
1265,498
|
||||
694,855
|
||||
749,409
|
||||
746,613
|
||||
212,53
|
||||
378,584
|
||||
1026,355
|
||||
145,355
|
||||
425,604
|
||||
132,357
|
||||
557,507
|
||||
1047,621
|
||||
1159,3
|
||||
284,66
|
||||
1026,592
|
||||
28,553
|
||||
257,143
|
||||
470,228
|
||||
309,408
|
||||
315,497
|
||||
530,805
|
||||
882,611
|
||||
1113,420
|
||||
372,892
|
||||
1165,705
|
||||
892,11
|
||||
1236,479
|
||||
683,745
|
||||
20,513
|
||||
447,150
|
||||
753,507
|
||||
1290,558
|
||||
251,635
|
||||
721,120
|
||||
1193,739
|
||||
60,218
|
||||
309,486
|
||||
927,858
|
||||
509,705
|
||||
1178,537
|
||||
320,488
|
||||
1032,501
|
||||
618,598
|
||||
1034,500
|
||||
344,19
|
||||
763,858
|
||||
892,883
|
||||
1292,667
|
||||
561,679
|
||||
99,266
|
||||
774,226
|
||||
113,324
|
||||
627,877
|
||||
671,723
|
||||
907,567
|
||||
1056,841
|
||||
100,507
|
||||
253,64
|
||||
1237,605
|
||||
38,407
|
||||
1034,425
|
||||
654,172
|
||||
445,491
|
||||
348,625
|
||||
1233,128
|
||||
832,224
|
||||
445,448
|
||||
401,284
|
||||
140,511
|
||||
554,809
|
||||
1088,735
|
||||
567,498
|
||||
1116,437
|
||||
1135,751
|
||||
730,479
|
||||
581,50
|
||||
967,51
|
||||
709,745
|
||||
370,715
|
||||
32,46
|
||||
231,306
|
||||
209,739
|
||||
67,343
|
||||
837,315
|
||||
1196,723
|
||||
98,402
|
||||
1036,425
|
||||
98,850
|
||||
1233,340
|
||||
946,2
|
||||
1292,227
|
||||
1225,339
|
||||
920,529
|
||||
1283,465
|
||||
284,539
|
||||
1079,809
|
||||
868,53
|
||||
1059,567
|
||||
780,537
|
||||
171,172
|
||||
1215,812
|
||||
468,287
|
||||
1059,635
|
||||
401,50
|
||||
479,833
|
||||
999,49
|
||||
587,791
|
||||
611,486
|
||||
977,824
|
||||
343,52
|
||||
1303,284
|
||||
1017,784
|
||||
574,526
|
||||
1165,215
|
||||
127,722
|
||||
253,158
|
||||
512,228
|
||||
612,205
|
||||
643,397
|
||||
927,659
|
||||
1300,144
|
||||
1126,676
|
||||
1299,751
|
||||
80,424
|
||||
1200,537
|
||||
692,296
|
||||
535,436
|
||||
641,855
|
||||
919,714
|
||||
223,299
|
||||
725,591
|
||||
1213,723
|
||||
658,516
|
||||
838,331
|
||||
826,294
|
||||
1267,856
|
||||
470,653
|
||||
97,212
|
||||
837,710
|
||||
756,835
|
||||
831,425
|
||||
502,175
|
||||
290,2
|
||||
319,773
|
||||
1049,355
|
||||
919,833
|
||||
591,845
|
||||
373,786
|
||||
636,219
|
||||
540,802
|
||||
1257,597
|
||||
478,222
|
||||
298,1
|
||||
28,341
|
||||
850,9
|
||||
184,666
|
||||
251,327
|
||||
554,835
|
||||
995,891
|
||||
848,190
|
||||
1087,147
|
||||
1282,553
|
||||
152,560
|
||||
1236,415
|
||||
879,147
|
||||
674,219
|
||||
591,180
|
||||
370,144
|
||||
293,110
|
||||
1159,751
|
||||
940,592
|
||||
1073,830
|
||||
97,275
|
||||
503,21
|
||||
321,742
|
||||
1176,647
|
||||
1243,418
|
||||
107,858
|
||||
840,666
|
||||
281,219
|
||||
1265,189
|
||||
97,579
|
||||
291,620
|
||||
67,418
|
||||
749,679
|
||||
278,501
|
||||
272,182
|
||||
319,26
|
||||
1193,634
|
||||
6,488
|
||||
1001,408
|
||||
1131,61
|
||||
20,336
|
||||
666,750
|
||||
223,147
|
||||
1255,164
|
||||
1197,324
|
||||
20,241
|
||||
1139,722
|
||||
940,403
|
||||
627,45
|
||||
366,407
|
||||
363,172
|
||||
126,668
|
||||
1004,336
|
||||
711,164
|
||||
534,144
|
||||
773,525
|
||||
610,444
|
||||
520,647
|
||||
6,600
|
||||
455,133
|
||||
759,747
|
||||
766,357
|
||||
509,355
|
||||
759,147
|
||||
692,2
|
||||
1268,355
|
||||
530,313
|
||||
325,119
|
||||
117,379
|
||||
711,730
|
||||
753,91
|
||||
760,735
|
||||
1211,292
|
||||
775,296
|
||||
420,782
|
||||
338,592
|
||||
706,537
|
||||
465,697
|
||||
460,9
|
||||
503,143
|
||||
428,78
|
||||
1193,155
|
||||
1220,617
|
||||
1217,791
|
||||
528,226
|
||||
355,87
|
||||
1044,885
|
||||
801,579
|
||||
536,226
|
||||
145,539
|
||||
388,9
|
||||
551,147
|
||||
1155,844
|
||||
1012,45
|
||||
1260,619
|
||||
431,523
|
||||
1034,873
|
||||
84,873
|
||||
74,479
|
||||
691,696
|
||||
202,213
|
||||
229,499
|
||||
353,529
|
||||
643,451
|
||||
194,288
|
||||
169,35
|
||||
38,39
|
||||
93,499
|
||||
54,182
|
||||
966,19
|
||||
790,229
|
||||
170,630
|
||||
132,89
|
||||
1215,687
|
||||
1260,537
|
||||
1038,40
|
||||
175,21
|
||||
55,164
|
||||
425,731
|
||||
1101,739
|
||||
753,884
|
||||
793,107
|
||||
321,11
|
||||
922,829
|
||||
1081,499
|
||||
1079,85
|
||||
883,66
|
||||
194,736
|
||||
547,858
|
||||
356,893
|
||||
465,197
|
||||
540,275
|
||||
791,357
|
||||
522,89
|
||||
865,827
|
||||
38,597
|
||||
59,75
|
||||
135,313
|
||||
50,843
|
||||
749,215
|
||||
311,346
|
||||
378,52
|
||||
325,775
|
||||
775,744
|
||||
669,855
|
||||
683,149
|
||||
487,870
|
||||
1245,120
|
||||
1081,395
|
||||
769,122
|
||||
1282,445
|
||||
190,301
|
||||
1178,649
|
||||
946,892
|
||||
42,539
|
||||
1283,493
|
||||
209,369
|
||||
1196,171
|
||||
753,547
|
||||
529,184
|
||||
1146,200
|
||||
823,277
|
||||
647,284
|
||||
567,128
|
||||
107,36
|
||||
517,787
|
||||
423,361
|
||||
644,750
|
||||
1252,7
|
||||
333,294
|
||||
1217,26
|
||||
1303,610
|
||||
45,396
|
||||
175,61
|
||||
920,813
|
||||
952,544
|
||||
522,718
|
||||
848,359
|
||||
348,241
|
||||
1046,589
|
||||
263,861
|
||||
791,537
|
||||
356,1
|
||||
868,617
|
||||
1049,774
|
||||
823,572
|
||||
301,365
|
||||
634,381
|
||||
442,841
|
||||
1198,254
|
||||
27,493
|
||||
1283,849
|
||||
432,735
|
||||
33,558
|
||||
875,708
|
||||
38,855
|
||||
770,725
|
||||
263,609
|
||||
361,110
|
||||
1272,39
|
||||
729,172
|
||||
363,620
|
||||
1083,738
|
||||
908,31
|
||||
910,53
|
||||
848,862
|
||||
1178,89
|
||||
1010,369
|
||||
509,691
|
||||
72,11
|
||||
69,474
|
||||
676,676
|
||||
281,257
|
||||
1113,474
|
||||
544,357
|
||||
1174,593
|
||||
348,65
|
||||
38,465
|
||||
1260,394
|
||||
760,159
|
||||
145,189
|
||||
731,744
|
||||
266,885
|
||||
971,397
|
||||
181,481
|
||||
634,218
|
||||
103,523
|
||||
816,378
|
||||
1242,789
|
||||
145,257
|
||||
999,276
|
||||
1186,123
|
||||
|
||||
fold along x=655
|
||||
fold along y=447
|
||||
fold along x=327
|
||||
fold along y=223
|
||||
fold along x=163
|
||||
fold along y=111
|
||||
fold along x=81
|
||||
fold along y=55
|
||||
fold along x=40
|
||||
fold along y=27
|
||||
fold along y=13
|
||||
fold along y=6
|
21
2021/13/sample.txt
Normal file
21
2021/13/sample.txt
Normal file
|
@ -0,0 +1,21 @@
|
|||
6,10
|
||||
0,14
|
||||
9,10
|
||||
0,3
|
||||
10,4
|
||||
4,11
|
||||
6,0
|
||||
6,12
|
||||
4,1
|
||||
0,13
|
||||
10,12
|
||||
3,4
|
||||
3,0
|
||||
8,4
|
||||
1,10
|
||||
2,14
|
||||
8,10
|
||||
9,0
|
||||
|
||||
fold along y=7
|
||||
fold along x=5
|
37
2021/14/14.py
Normal file
37
2021/14/14.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
rules = {}
|
||||
polymer = []
|
||||
with open("sample.txt") as f:
|
||||
template, instructions = f.read().split('\n\n')
|
||||
for element in template:
|
||||
polymer.append(element)
|
||||
for rule in instructions.split('\n'):
|
||||
a, b = rule.split(' -> ')
|
||||
rules[a] = b
|
||||
|
||||
def step(polymer):
|
||||
polymer2 = []
|
||||
for i, element in enumerate(polymer):
|
||||
polymer2.append(element)
|
||||
if i < len(polymer) - 1:
|
||||
|
||||
pair = element + polymer[i+1]
|
||||
if pair in rules:
|
||||
insertion = rules[pair]
|
||||
polymer2.append(insertion)
|
||||
return polymer2
|
||||
|
||||
|
||||
part1 = polymer.copy()
|
||||
for i in range(10):
|
||||
part1 = step(part1)
|
||||
|
||||
from collections import Counter
|
||||
c = Counter(part1)
|
||||
f = c.most_common()
|
||||
most = f[0][1]
|
||||
least = f[-1][1]
|
||||
print(most - least)
|
||||
|
||||
c2 = Counter()
|
||||
def step2(polymer):
|
||||
|
102
2021/14/input.txt
Normal file
102
2021/14/input.txt
Normal file
|
@ -0,0 +1,102 @@
|
|||
VHCKBFOVCHHKOHBPNCKO
|
||||
|
||||
SO -> F
|
||||
OP -> V
|
||||
NF -> F
|
||||
BO -> V
|
||||
BH -> S
|
||||
VB -> B
|
||||
SV -> B
|
||||
BK -> S
|
||||
KC -> N
|
||||
SP -> O
|
||||
CP -> O
|
||||
VN -> O
|
||||
HO -> S
|
||||
PC -> B
|
||||
CS -> O
|
||||
PO -> K
|
||||
KF -> B
|
||||
BP -> K
|
||||
VO -> O
|
||||
HB -> N
|
||||
PH -> O
|
||||
FF -> O
|
||||
FB -> K
|
||||
CC -> H
|
||||
FK -> F
|
||||
HV -> P
|
||||
CO -> S
|
||||
OC -> N
|
||||
KV -> V
|
||||
SS -> O
|
||||
FC -> O
|
||||
NP -> B
|
||||
OH -> B
|
||||
OF -> K
|
||||
KB -> K
|
||||
BN -> C
|
||||
OK -> C
|
||||
NC -> O
|
||||
NO -> O
|
||||
FS -> C
|
||||
VP -> K
|
||||
KP -> S
|
||||
VS -> B
|
||||
VV -> N
|
||||
NN -> P
|
||||
KH -> P
|
||||
OB -> H
|
||||
HP -> H
|
||||
KK -> H
|
||||
FH -> F
|
||||
KS -> V
|
||||
BS -> V
|
||||
SN -> H
|
||||
CB -> B
|
||||
HN -> K
|
||||
SB -> O
|
||||
OS -> K
|
||||
BC -> H
|
||||
OV -> N
|
||||
PN -> B
|
||||
VH -> N
|
||||
SK -> C
|
||||
PV -> K
|
||||
VC -> N
|
||||
PF -> S
|
||||
NB -> B
|
||||
PP -> S
|
||||
NS -> F
|
||||
PB -> B
|
||||
CV -> C
|
||||
HK -> P
|
||||
PK -> S
|
||||
NH -> B
|
||||
SH -> V
|
||||
KO -> H
|
||||
NV -> B
|
||||
HH -> V
|
||||
FO -> O
|
||||
CK -> O
|
||||
VK -> F
|
||||
HF -> O
|
||||
BF -> C
|
||||
BV -> P
|
||||
KN -> K
|
||||
VF -> C
|
||||
FN -> V
|
||||
ON -> C
|
||||
SF -> F
|
||||
SC -> C
|
||||
OO -> S
|
||||
FP -> K
|
||||
PS -> C
|
||||
NK -> O
|
||||
BB -> V
|
||||
HC -> H
|
||||
FV -> V
|
||||
CH -> N
|
||||
HS -> V
|
||||
CF -> F
|
||||
CN -> S
|
18
2021/14/sample.txt
Normal file
18
2021/14/sample.txt
Normal file
|
@ -0,0 +1,18 @@
|
|||
NNCB
|
||||
|
||||
CH -> B
|
||||
HH -> N
|
||||
CB -> H
|
||||
NH -> C
|
||||
HB -> C
|
||||
HC -> B
|
||||
HN -> C
|
||||
NN -> C
|
||||
BH -> H
|
||||
NC -> B
|
||||
NB -> B
|
||||
BN -> B
|
||||
BB -> N
|
||||
BC -> B
|
||||
CC -> N
|
||||
CN -> C
|
Loading…
Reference in a new issue