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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Regler
Advent of Code 2024
Commits
69435179
Commit
69435179
authored
5 months ago
by
Felix Agner
Browse files
Options
Downloads
Patches
Plain Diff
day 17
parent
da2659dc
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
day 17/day_17_felix.cpp
+98
-0
98 additions, 0 deletions
day 17/day_17_felix.cpp
day 17/day_17_felix_part2.cpp
+37
-0
37 additions, 0 deletions
day 17/day_17_felix_part2.cpp
with
135 additions
and
0 deletions
day 17/day_17_felix.cpp
0 → 100644
+
98
−
0
View file @
69435179
#include
<bits/stdc++.h>
using
namespace
std
;
const
bool
DEBUG
=
true
;
#define ll long long
array
<
ll
,
3
>
REGISTERS
=
{
0
,
0
,
0
};
vector
<
int
>
OPTCODES
;
vector
<
int
>
OPERANDS
;
void
read_input
()
{
string
line
;
int
reg
=
0
;
while
(
getline
(
cin
,
line
)
&&
line
.
size
()
>
0
)
{
REGISTERS
[
reg
]
=
stoi
(
line
.
substr
(
12
));
if
(
DEBUG
)
cout
<<
"Register "
<<
reg
<<
" = "
<<
REGISTERS
[
reg
]
<<
endl
;
reg
++
;
}
getline
(
cin
,
line
);
if
(
DEBUG
)
cout
<<
"Start reading optcodes from "
<<
line
<<
endl
;
stringstream
ss
(
line
);
char
comma
;
int
optcode
,
operand
;
for
(
int
i
=
0
;
i
<
8
;
i
++
)
ss
>>
comma
;
//clear start
while
(
ss
>>
operand
>>
comma
>>
optcode
)
{
OPTCODES
.
push_back
(
optcode
);
OPERANDS
.
push_back
(
operand
);
if
(
DEBUG
)
cout
<<
" Operand "
<<
operand
<<
" Optcode "
<<
optcode
<<
endl
;
ss
>>
comma
;
}
}
ll
part1
()
{
int
i_point
=
0
;
string
result
=
""
;
string
nocommas
=
""
;
while
(
i_point
<
OPTCODES
.
size
())
{
if
(
DEBUG
)
cout
<<
"Point "
<<
i_point
<<
" REGISTERS: "
<<
REGISTERS
[
0
]
<<
" "
<<
REGISTERS
[
1
]
<<
" "
<<
REGISTERS
[
2
]
<<
endl
;
int
instruction
=
OPERANDS
[
i_point
];
int
optcode
=
OPTCODES
[
i_point
];
ll
combo
=
optcode
<
4
?
optcode
:
REGISTERS
[
optcode
-
4
];
double
a
;
if
(
DEBUG
)
cout
<<
" Instruction "
<<
instruction
<<
" Optcode "
<<
optcode
<<
" Combo "
<<
combo
<<
endl
;
switch
(
instruction
)
{
case
0
:
// adv
a
=
REGISTERS
[
0
]
/
pow
(
2
,
combo
);
REGISTERS
[
0
]
=
trunc
(
a
);
if
(
DEBUG
)
cout
<<
" ADV "
<<
REGISTERS
[
0
]
<<
endl
;
break
;
case
1
:
// bxl
REGISTERS
[
1
]
=
REGISTERS
[
1
]
^
optcode
;
if
(
DEBUG
)
cout
<<
" BXL "
<<
REGISTERS
[
1
]
<<
endl
;
break
;
case
2
:
// bst
REGISTERS
[
1
]
=
combo
%
8
;
break
;
case
3
:
// jnz
if
(
DEBUG
)
cout
<<
" JNZ "
<<
REGISTERS
[
0
]
<<
endl
;
if
(
REGISTERS
[
0
]
!=
0
){
i_point
=
optcode
/
2
;
// divide by two as we have two things...
i_point
--
;
// to compensate the increment
}
break
;
case
4
:
// bxc
REGISTERS
[
1
]
=
REGISTERS
[
1
]
^
REGISTERS
[
2
];
if
(
DEBUG
)
cout
<<
" BXC "
<<
REGISTERS
[
1
]
<<
endl
;
break
;
case
5
:
// out
result
+=
to_string
(
combo
%
8
);
result
+=
","
;
nocommas
+=
to_string
(
combo
%
8
);
if
(
DEBUG
)
cout
<<
" OUT "
<<
combo
%
8
<<
endl
;
break
;
case
6
:
// bdv
a
=
REGISTERS
[
0
]
/
pow
(
2
,
combo
);
REGISTERS
[
1
]
=
trunc
(
a
);
if
(
DEBUG
)
cout
<<
" BDV "
<<
REGISTERS
[
1
]
<<
endl
;
break
;
case
7
:
// cdv
a
=
REGISTERS
[
0
]
/
pow
(
2
,
combo
);
REGISTERS
[
2
]
=
trunc
(
a
);
if
(
DEBUG
)
cout
<<
" CDV "
<<
REGISTERS
[
2
]
<<
endl
;
break
;
}
i_point
++
;
}
cout
<<
result
<<
endl
;
cout
<<
nocommas
<<
endl
;
return
stoll
(
nocommas
);
}
int
main
()
{
read_input
();
part1
();
return
0
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
day 17/day_17_felix_part2.cpp
0 → 100644
+
37
−
0
View file @
69435179
#include
<bits/stdc++.h>
using
namespace
std
;
const
bool
DEBUG
=
true
;
#define ll long long
vector
<
int
>
PROGRAM
=
{
2
,
4
,
1
,
1
,
7
,
5
,
4
,
7
,
1
,
4
,
0
,
3
,
5
,
5
,
3
,
0
};
ll
find_previous_A
(
int
point
,
ll
A
)
{
ll
Aprev
;
ll
B
,
C
;
if
(
point
<
0
)
return
A
;
string
debugspacs
(
16
-
point
,
' '
);
for
(
int
a
=
0
;
a
<
pow
(
2
,
3
);
a
++
)
{
Aprev
=
pow
(
2
,
3
)
*
A
+
a
;
//cout << " Testing Aprev " << Aprev << endl;
B
=
Aprev
%
8
;
B
=
B
^
1
;
C
=
trunc
(
Aprev
/
pow
(
2
,
B
));
B
=
B
^
C
;
B
=
B
^
4
;
B
=
B
%
8
;
// mod 8 before printing in the out-command
//cout << " Resulted in B " << B << endl;
if
(
B
==
PROGRAM
[
point
])
{
cout
<<
debugspacs
<<
"Point "
<<
point
<<
" Aprev "
<<
Aprev
<<
" B "
<<
B
<<
" C "
<<
C
<<
endl
;
ll
result
=
find_previous_A
(
point
-
1
,
Aprev
);
if
(
result
!=
-
1
)
return
result
;
}
}
return
-
1
;
}
int
main
()
{
cout
<<
find_previous_A
(
PROGRAM
.
size
()
-
1
,
0
)
<<
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