Added 2017/12 Part 1 and 2

This commit is contained in:
FrederikBaerentsen 2024-11-24 19:14:52 +01:00
parent ac2a79d085
commit b07b0b3e57
5 changed files with 186 additions and 3 deletions

80
2017/12/12.md Normal file
View File

@ -0,0 +1,80 @@
## \-\-- Day 12: Digital Plumber \-\--
Walking along the memory banks of the stream, you find a small village
that is experiencing a little confusion: some programs can\'t
communicate with each other.
Programs in this village communicate using a fixed system of *pipes*.
Messages are passed between programs using these pipes, but most
programs aren\'t connected to each other directly. Instead, programs
pass messages between each other until the message reaches the intended
recipient.
For some reason, though, some of these messages aren\'t ever reaching
their intended recipient, and the programs suspect that some
pipes
are missing. They would like you to investigate.
You walk through the village and record the ID of each program and the
IDs with which it can communicate directly (your puzzle input). Each
program has one or more programs with which it can communicate, and
these pipes are bidirectional; if `8` says it can communicate with `11`,
then `11` will say it can communicate with `8`.
You need to figure out how many programs are in the group that contains
program ID `0`.
For example, suppose you go door-to-door like a travelling salesman and
record the following list:
0 <-> 2
1 <-> 1
2 <-> 0, 3, 4
3 <-> 2, 4
4 <-> 2, 3, 6
5 <-> 6
6 <-> 4, 5
In this example, the following programs are in the group that contains
program ID `0`:
- Program `0` by definition.
- Program `2`, directly connected to program `0`.
- Program `3` via program `2`.
- Program `4` via program `2`.
- Program `5` via programs `6`, then `4`, then `2`.
- Program `6` via programs `4`, then `2`.
Therefore, a total of `6` programs are in this group; all but program
`1`, which has a pipe that connects it to itself.
*How many programs* are in the group that contains program ID `0`?
Your puzzle answer was `113`.
## \-\-- Part Two \-\-- {#part2}
There are more programs than just the ones in the group containing
program ID `0`. The rest of them have no way of reaching that group, and
still might have no way of reaching each other.
A *group* is a collection of programs that can all communicate via pipes
either directly or indirectly. The programs you identified just a moment
ago are all part of the same group. Now, they would like you to
determine the total number of groups.
In the example above, there were `2` groups: one consisting of programs
`0,2,3,4,5,6`, and the other consisting solely of program `1`.
*How many groups are there* in total?
Your puzzle answer was `202`.
Both parts of this puzzle are complete! They provide two gold stars:
\*\*
At this point, you should [return to your Advent calendar](/2017) and
try another puzzle.
If you still want to see it, you can [get your puzzle
input](12/input).

101
2017/12/solution.py Normal file
View File

@ -0,0 +1,101 @@
#!/bin/python3
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int
input_f = 'input'
part = 2
#########################################
# #
# Part 1 #
# #
#########################################
def visit(next,visited,records):
#print('Starting at',next, ' having visited',visited)
#print(records[next])
if len(records[next]) == 1:
if records[next][0] not in visited:
visited.append(records[next][0])
next = records[next][0]
visit(next,visited,records)
else:
for i in records[next]:
if i not in visited:
visited.append(i)
visit(i,visited,records)
return visited
if part == 1:
records = {}
visited = []
with open(input_f) as file:
for line in file:
l = line.rstrip().replace(' ','')
if '<->' in l:
x = l.split('<->')
records[int(x[0])] = list2int(x[1].split(','))
#pprint(records)
start_node = 0
visited = visit(start_node,visited,records)
print(visited)
print(len(visited))
#########################################
# #
# Part 2 #
# #
#########################################
def visit(next,visited,records):
#print('Starting at',next, ' having visited',visited)
#print(records[next])
if len(records[next]) == 1:
if records[next][0] not in visited:
visited.append(records[next][0])
next = records[next][0]
visit(next,visited,records)
else:
for i in records[next]:
if i not in visited:
visited.append(i)
visit(i,visited,records)
return visited
if part == 2:
records = {}
visited = []
with open(input_f) as file:
for line in file:
l = line.rstrip().replace(' ','')
if '<->' in l:
x = l.split('<->')
records[int(x[0])] = list2int(x[1].split(','))
#pprint(records)
start_node = 0
groups = []
for i in range(0,len(records)):
if not any(i in sublist for sublist in groups):
#print(i, ' not in ', groups)
visited = visit(i,visited,records)
groups.append(visited)
#print('Visited',visited)
visited = []
#print(len(visited))
pprint(groups)
print(len(groups))

Binary file not shown.

2
fred.py Normal file
View File

@ -0,0 +1,2 @@
def list2int(x):
return list(map(int, x))

View File

@ -11,7 +11,7 @@ part = 1
# # # #
######################################### #########################################
if part == 1 if part == 1:
with open(input_f) as file: with open(input_f) as file:
for line in file: for line in file: