Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Advent of Code 2024
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Regler
Advent of Code 2024
Commits
64b0b794
Commit
64b0b794
authored
7 months ago
by
Felix Agner
Browse files
Options
Downloads
Patches
Plain Diff
so many connections man
parent
e782c502
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
day 023/day_23_felix.cpp
+134
-0
134 additions, 0 deletions
day 023/day_23_felix.cpp
with
134 additions
and
0 deletions
day 023/day_23_felix.cpp
0 → 100644
+
134
−
0
View file @
64b0b794
/*
Usage:
g++ -o main day_X_felix.cpp (or compile however you like)
./main < input.txt (pipe the input)
Options:
-d : debug mode
*/
#include
<bits/stdc++.h>
using
namespace
std
;
bool
DEBUG
=
false
;
// Standard definitions for many problems
#define coord pair <int, int>
#define ll long long
array
<
set
<
int
>
,
676
>
LINKS
;
set
<
int
>
t_starters
;
set
<
int
>
all_ids
;
int
int_id
(
char
c1
,
char
c2
)
{
int
i1
=
(
int
)(
c1
)
-
97
;
int
i2
=
(
int
)(
c2
)
-
97
;
return
i1
*
26
+
i2
;
}
string
id_to_str
(
int
id
)
{
string
s
=
""
;
s
+=
(
char
)(
id
/
26
+
97
);
s
+=
(
char
)(
id
%
26
+
97
);
return
s
;
}
void
read_input
()
{
string
line
;
while
(
getline
(
cin
,
line
))
{
stringstream
ss
(
line
);
char
c1
,
c2
,
dash
;
ss
>>
c1
>>
c2
>>
dash
;
int
id1
=
int_id
(
c1
,
c2
);
all_ids
.
insert
(
id1
);
if
(
c1
==
't'
)
t_starters
.
insert
(
id1
);
ss
>>
c1
>>
c2
;
int
id2
=
int_id
(
c1
,
c2
);
all_ids
.
insert
(
id2
);
if
(
c1
==
't'
)
t_starters
.
insert
(
id2
);
LINKS
[
id1
].
insert
(
id2
);
LINKS
[
id2
].
insert
(
id1
);
}
cout
<<
"Total number of nodes: "
<<
all_ids
.
size
()
<<
endl
;
}
ll
part1
()
{
ll
total
=
0
;
set
<
tuple
<
int
,
int
,
int
>>
counted
;
for
(
int
n1
:
t_starters
)
for
(
int
n2
:
LINKS
[
n1
])
for
(
int
n3
:
LINKS
[
n2
])
{
if
(
counted
.
find
({
n1
,
n2
,
n3
})
!=
counted
.
end
())
continue
;
if
(
!
count
(
LINKS
[
n3
].
begin
(),
LINKS
[
n3
].
end
(),
n1
))
continue
;
counted
.
insert
({
n1
,
n2
,
n3
});
counted
.
insert
({
n1
,
n3
,
n2
});
counted
.
insert
({
n2
,
n1
,
n3
});
counted
.
insert
({
n2
,
n3
,
n1
});
counted
.
insert
({
n3
,
n1
,
n2
});
counted
.
insert
({
n3
,
n2
,
n1
});
total
++
;
}
return
total
;
}
bool
all_linked
(
int
n
,
set
<
int
>
s
)
{
for
(
int
n2
:
s
)
if
(
LINKS
[
n
].
find
(
n2
)
==
LINKS
[
n
].
end
())
return
false
;
return
true
;
}
// check if set s contains all elements of set t
bool
contains_all
(
set
<
int
>
s
,
set
<
int
>
t
)
{
for
(
int
n
:
t
)
if
(
s
.
find
(
n
)
==
s
.
end
())
return
false
;
return
true
;
}
string
set_to_str
(
set
<
int
>
s
)
{
string
str
=
""
;
set
<
string
>
str_set
;
for
(
int
n
:
s
)
str_set
.
insert
(
id_to_str
(
n
));
// automatically sorted
for
(
string
s
:
str_set
)
str
+=
s
+
","
;
// remove last ,
str
.
pop_back
();
return
str
;
}
set
<
int
>
part2
()
{
set
<
set
<
int
>>
sets
;
for
(
int
n
:
all_ids
)
sets
.
insert
({
n
});
int
set_size
=
1
;
while
(
sets
.
size
()
>
1
)
{
if
(
DEBUG
)
cout
<<
"Set size: "
<<
set_size
<<
" | Number of sets: "
<<
sets
.
size
()
<<
endl
;
set
<
set
<
int
>>
new_sets
;
for
(
set
<
int
>
s
:
sets
)
for
(
int
n
:
all_ids
)
{
if
(
s
.
find
(
n
)
!=
s
.
end
())
continue
;
if
(
all_linked
(
n
,
s
))
{
set
<
int
>
new_s
=
s
;
new_s
.
insert
(
n
);
new_sets
.
insert
(
new_s
);
}
}
sets
=
new_sets
;
set_size
++
;
}
return
*
sets
.
begin
();
}
int
main
(
int
argc
,
char
*
argv
[])
{
for
(
int
i
=
0
;
i
<
argc
;
i
++
)
if
(
string
(
argv
[
i
])
==
"-d"
)
DEBUG
=
true
;
read_input
();
if
(
DEBUG
)
{
int
largest
=
0
;
for
(
int
id
:
all_ids
)
if
(
LINKS
[
id
].
size
()
>
largest
)
largest
=
LINKS
[
id
].
size
();
cout
<<
"Most links: "
<<
largest
<<
endl
;
}
ll
p1
=
part1
();
cout
<<
"Part 1: "
<<
p1
<<
endl
;
set
<
int
>
p2
=
part2
();
cout
<<
"Part 2: "
<<
set_to_str
(
p2
)
<<
endl
;
return
0
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment