Added 2017/25 Part 1
This commit is contained in:
parent
fcd169e923
commit
4cb6a22981
@ -1,5 +1,6 @@
|
|||||||
#!/bin/python3
|
#!/bin/python3
|
||||||
import sys,re
|
import sys,re
|
||||||
|
from copy import deepcopy
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
sys.path.insert(0, '../../')
|
sys.path.insert(0, '../../')
|
||||||
from fred import list2int, lprint,get_re
|
from fred import list2int, lprint,get_re
|
||||||
@ -14,6 +15,25 @@ log = True
|
|||||||
# #
|
# #
|
||||||
#########################################
|
#########################################
|
||||||
|
|
||||||
|
def find_match(comp:list,match:str,result:list):
|
||||||
|
i_match = get_re(r"^(\d+)\/(\d+)$",match)
|
||||||
|
#print(comp,match)
|
||||||
|
comp = [i for i in comp if i != match]
|
||||||
|
#print(comp,result)
|
||||||
|
for i in result:
|
||||||
|
try:
|
||||||
|
comp = [j for j in comp if j != i]
|
||||||
|
except:
|
||||||
|
print(i,'cant be removed from',j)
|
||||||
|
for j in comp:
|
||||||
|
j_match = get_re(r"^(\d+)\/(\d+)$",j)
|
||||||
|
#print(match,j)
|
||||||
|
#input()
|
||||||
|
if i_match.group(2) == j_match.group(1) or i_match.group(2) == j_match.group(2):
|
||||||
|
result.append(j)
|
||||||
|
find_match(comp[:],match,result[:])
|
||||||
|
return result
|
||||||
|
|
||||||
if part == 1:
|
if part == 1:
|
||||||
components = []
|
components = []
|
||||||
with open(input_f) as file:
|
with open(input_f) as file:
|
||||||
@ -21,25 +41,19 @@ if part == 1:
|
|||||||
components.append(line.rstrip())
|
components.append(line.rstrip())
|
||||||
|
|
||||||
lprint(components,log)
|
lprint(components,log)
|
||||||
|
original_comp = deepcopy(components)
|
||||||
pairs = []
|
pairs = []
|
||||||
|
result = []
|
||||||
|
|
||||||
for i in components:
|
for i in components[:]:
|
||||||
t = []
|
print(i)
|
||||||
i_match = get_re(r"^(\d+)\/(\d+)$",i)
|
if i[0] == '0':
|
||||||
|
result.append(find_match(original_comp,i,[i]))
|
||||||
|
|
||||||
if i_match.group(1) == '0': #can start
|
|
||||||
pairs.append([i])
|
|
||||||
|
|
||||||
for j in (components):
|
pprint(result)
|
||||||
j_match = get_re(r"^(\d+)\/(\d+)$",j)
|
#nput()
|
||||||
if i_match.group(2) == j_match.group(1) or i_match.group(2) == j_match.group(2):
|
#lprint(pairs,log)
|
||||||
#if i[2] == j[0] or i[2] == j[2]:
|
|
||||||
t.append(j)
|
|
||||||
pairs.append(t)
|
|
||||||
lprint(t,log)
|
|
||||||
input()
|
|
||||||
lprint(pairs,log)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
58
2017/24/test.py
Normal file
58
2017/24/test.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
from collections import defaultdict
|
||||||
|
import pprint
|
||||||
|
def build_graph(pairs):
|
||||||
|
graph = defaultdict(list)
|
||||||
|
for pair in pairs:
|
||||||
|
a, b = map(int, pair.split('/'))
|
||||||
|
graph[a].append(b)
|
||||||
|
graph[b].append(a)
|
||||||
|
return graph
|
||||||
|
|
||||||
|
def find_all_paths(graph, start, visited=None, path=None):
|
||||||
|
if visited is None:
|
||||||
|
visited = set()
|
||||||
|
if path is None:
|
||||||
|
path = []
|
||||||
|
|
||||||
|
visited.add(start)
|
||||||
|
path.append(start)
|
||||||
|
|
||||||
|
paths = [list(path)] # Store the current path as a possible connection
|
||||||
|
|
||||||
|
for neighbor in graph[start]:
|
||||||
|
if neighbor not in visited:
|
||||||
|
paths.extend(find_all_paths(graph, neighbor, visited.copy(), path.copy()))
|
||||||
|
|
||||||
|
return paths
|
||||||
|
|
||||||
|
def count_connections(pairs):
|
||||||
|
graph = build_graph(pairs)
|
||||||
|
all_paths = []
|
||||||
|
|
||||||
|
for pair in pairs:
|
||||||
|
a, b = map(int, pair.split('/'))
|
||||||
|
paths_a = find_all_paths(graph, a)
|
||||||
|
paths_b = find_all_paths(graph, b)
|
||||||
|
|
||||||
|
# Combine paths starting from both nodes in the pair
|
||||||
|
for path in paths_a:
|
||||||
|
if b in path:
|
||||||
|
all_paths.append(path)
|
||||||
|
|
||||||
|
for path in paths_b:
|
||||||
|
if a in path:
|
||||||
|
all_paths.append(path)
|
||||||
|
|
||||||
|
# Remove duplicate paths
|
||||||
|
unique_paths = {tuple(path) for path in all_paths}
|
||||||
|
return len(unique_paths), unique_paths
|
||||||
|
|
||||||
|
# Example usage
|
||||||
|
pairs = ['0/2','2/2','2/3','3/4','3/5','0/1','10/1','9/10']
|
||||||
|
connection_count, connections = count_connections(pairs)
|
||||||
|
|
||||||
|
print(f"Number of connections: {connection_count}")
|
||||||
|
print("Connections:")
|
||||||
|
for connection in connections:
|
||||||
|
print(connection)
|
||||||
|
pprint(connections)
|
@ -93,7 +93,29 @@ appears on the tape. In the above example, the *diagnostic checksum* is
|
|||||||
Recreate the Turing machine and save the computer! *What is the
|
Recreate the Turing machine and save the computer! *What is the
|
||||||
diagnostic checksum* it produces once it\'s working again?
|
diagnostic checksum* it produces once it\'s working again?
|
||||||
|
|
||||||
To begin, [get your puzzle input](25/input).
|
Your puzzle answer was `633`.
|
||||||
|
|
||||||
Answer:
|
The first half of this puzzle is complete! It provides one gold star: \*
|
||||||
|
|
||||||
|
## \-\-- Part Two \-\-- {#part2}
|
||||||
|
|
||||||
|
The Turing machine, and soon the entire computer, springs back to life.
|
||||||
|
A console glows dimly nearby, awaiting your command.
|
||||||
|
|
||||||
|
> reboot printer
|
||||||
|
Error: That command requires priority 50. You currently have priority 0.
|
||||||
|
You must deposit 50 stars to increase your priority to the required level.
|
||||||
|
|
||||||
|
The console flickers for a moment, and then prints another message:
|
||||||
|
|
||||||
|
Star accepted.
|
||||||
|
You must deposit 49 stars to increase your priority to the required level.
|
||||||
|
|
||||||
|
The *garbage collector* winks at you, then continues sweeping.
|
||||||
|
|
||||||
|
You don\'t have enough stars to reboot the printer, though. You need 3
|
||||||
|
more.
|
||||||
|
|
||||||
|
Although it hasn\'t changed, you can still [get your puzzle
|
||||||
|
input](25/input).
|
||||||
|
|
||||||
|
129
2017/25/solution.py
Normal file
129
2017/25/solution.py
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
#!/bin/python3
|
||||||
|
import sys,re
|
||||||
|
from pprint import pprint
|
||||||
|
sys.path.insert(0, '../../')
|
||||||
|
from fred import list2int,get_re,nprint,lprint
|
||||||
|
|
||||||
|
input_f = 'test'
|
||||||
|
|
||||||
|
part = 1
|
||||||
|
#########################################
|
||||||
|
# #
|
||||||
|
# Part 1 #
|
||||||
|
# #
|
||||||
|
#########################################
|
||||||
|
|
||||||
|
max_steps = 12302209
|
||||||
|
begin_state = 'A'
|
||||||
|
|
||||||
|
states = {
|
||||||
|
'A': {
|
||||||
|
0: {
|
||||||
|
'W': 1,
|
||||||
|
'M': 'R',
|
||||||
|
'C': 'B'
|
||||||
|
},
|
||||||
|
1: {
|
||||||
|
'W': 0,
|
||||||
|
'M': 'L',
|
||||||
|
'C': 'D'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'B': {
|
||||||
|
0: {
|
||||||
|
'W': 1,
|
||||||
|
'M': 'R',
|
||||||
|
'C': 'C'
|
||||||
|
},
|
||||||
|
1: {
|
||||||
|
'W': 0,
|
||||||
|
'M': 'R',
|
||||||
|
'C': 'F'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'C': {
|
||||||
|
0: {
|
||||||
|
'W': 1,
|
||||||
|
'M': 'L',
|
||||||
|
'C': 'C'
|
||||||
|
},
|
||||||
|
1: {
|
||||||
|
'W': 1,
|
||||||
|
'M': 'L',
|
||||||
|
'C': 'A'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'D': {
|
||||||
|
0: {
|
||||||
|
'W': 0,
|
||||||
|
'M': 'L',
|
||||||
|
'C': 'E'
|
||||||
|
},
|
||||||
|
1: {
|
||||||
|
'W': 1,
|
||||||
|
'M': 'R',
|
||||||
|
'C': 'A'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'E': {
|
||||||
|
0: {
|
||||||
|
'W': 1,
|
||||||
|
'M': 'L',
|
||||||
|
'C': 'A'
|
||||||
|
},
|
||||||
|
1: {
|
||||||
|
'W': 0,
|
||||||
|
'M': 'R',
|
||||||
|
'C': 'B'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'F': {
|
||||||
|
0: {
|
||||||
|
'W': 0,
|
||||||
|
'M': 'R',
|
||||||
|
'C': 'C'
|
||||||
|
},
|
||||||
|
1: {
|
||||||
|
'W': 0,
|
||||||
|
'M': 'R',
|
||||||
|
'C': 'E'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if part == 1:
|
||||||
|
end = False
|
||||||
|
steps = 0
|
||||||
|
|
||||||
|
line = [0,0,0,0,0,0,0,0,0]
|
||||||
|
idx = 4
|
||||||
|
state = begin_state
|
||||||
|
while not end:
|
||||||
|
|
||||||
|
if idx-1 <= 0:
|
||||||
|
line.insert(0, [0])
|
||||||
|
idx += 1
|
||||||
|
if idx+2 > len(line):
|
||||||
|
line.append(0)
|
||||||
|
line.append(0)
|
||||||
|
cur = line[idx]
|
||||||
|
line[idx] = states[state][cur]['W']
|
||||||
|
if states[state][cur]['M'] == 'L':
|
||||||
|
idx -= 1
|
||||||
|
else:
|
||||||
|
idx += 1
|
||||||
|
state = states[state][cur]['C']
|
||||||
|
steps += 1
|
||||||
|
if steps == max_steps:
|
||||||
|
end = True
|
||||||
|
|
||||||
|
print(line.count(1))
|
||||||
|
|
||||||
|
#########################################
|
||||||
|
# #
|
||||||
|
# Part 2 #
|
||||||
|
# #
|
||||||
|
#########################################
|
||||||
|
if part == 2:
|
||||||
|
exit()
|
@ -2,7 +2,7 @@
|
|||||||
import sys,re
|
import sys,re
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
sys.path.insert(0, '../../')
|
sys.path.insert(0, '../../')
|
||||||
from fred import list2int
|
from fred import list2int,get_re,nprint,lprint
|
||||||
|
|
||||||
input_f = 'test'
|
input_f = 'test'
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user