Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4990c25

Browse files
committed
Day 17
1 parentc42376a commit4990c25

File tree

2 files changed

+217
-0
lines changed

2 files changed

+217
-0
lines changed

‎Data/2024/Day17_sample.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Register A: 729
2+
Register B: 0
3+
Register C: 0
4+
5+
Program: 0,1,5,4,3,0

‎csharp/2024/Day17.cs‎

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
usingSystem;
2+
usingSystem.Collections.Generic;
3+
usingSystem.IO;
4+
usingSystem.Linq;
5+
usingSystem.Text;
6+
usingSystem.Text.RegularExpressions;
7+
usingAdventOfCode.Util;
8+
9+
namespaceAdventOfCode2024
10+
{
11+
internalclassDay17
12+
{
13+
privatestatic(long[]Registers,List<int>Program)Load()
14+
{
15+
RegexregA=newRegex("^Register A: (-?\\d+)$");
16+
RegexregB=newRegex("^Register B: (-?\\d+)$");
17+
RegexregC=newRegex("^Register C: (-?\\d+)$");
18+
19+
longa=0;
20+
longb=0;
21+
longc=0;
22+
23+
foreach(stringdatainData.Enumerate())
24+
{
25+
if(string.IsNullOrEmpty(data))
26+
{
27+
continue;
28+
}
29+
30+
Matchm=regA.Match(data);
31+
32+
if(m.Success)
33+
{
34+
a=int.Parse(m.Groups[1].ValueSpan);
35+
continue;
36+
}
37+
38+
m=regB.Match(data);
39+
40+
if(m.Success)
41+
{
42+
b=int.Parse(m.Groups[1].ValueSpan);
43+
continue;
44+
}
45+
46+
m=regC.Match(data);
47+
48+
if(m.Success)
49+
{
50+
c=int.Parse(m.Groups[1].ValueSpan);
51+
continue;
52+
}
53+
54+
return([a,b,c],data.Substring("Program: ".Length).Split(',').Select(int.Parse).ToList());
55+
}
56+
57+
thrownewInvalidDataException();
58+
}
59+
60+
internalstaticvoidProblem1()
61+
{
62+
(long[]registers,List<int>program)=Load();
63+
64+
intip=0;
65+
66+
while(true)
67+
{
68+
intinc=Compute(ip,registers,program);
69+
70+
if(inc==int.MinValue)
71+
{
72+
break;
73+
}
74+
75+
ip+=inc;
76+
}
77+
78+
Console.WriteLine();
79+
}
80+
81+
internalstaticvoidProblem2()
82+
{
83+
(long[]registers,List<int>program)=Load();
84+
long[]originalRegisters=registers.AsSpan().ToArray();
85+
stringprogramStr=string.Join(",",program.Select(i=>i.ToString()))+",";
86+
87+
StringBuilderbuilder=new();
88+
Queue<long>aValues=new();
89+
aValues.Enqueue(0);
90+
91+
while(aValues.TryDequeue(outlonga))
92+
{
93+
a<<=3;
94+
95+
for(inti=0;i<=7;i++)
96+
{
97+
a=(a>>3<<3)|i;
98+
builder.Clear();
99+
longsaveA=a;
100+
101+
originalRegisters.AsSpan().CopyTo(registers);
102+
registers[0]=a;
103+
104+
intip=0;
105+
106+
while(true)
107+
{
108+
intinc=Compute(ip,registers,program,builder);
109+
110+
if(inc==int.MinValue)
111+
{
112+
break;
113+
}
114+
115+
ip+=inc;
116+
}
117+
118+
stringoutput=builder.ToString();
119+
if(programStr.EndsWith(output))
120+
{
121+
Console.WriteLine(saveA);
122+
123+
Console.WriteLine(output);
124+
125+
if(programStr.Equals(output))
126+
{
127+
Console.WriteLine($"Submit{saveA}");
128+
return;
129+
}
130+
131+
aValues.Enqueue(a);
132+
}
133+
}
134+
}
135+
}
136+
137+
privatestaticintCompute(intip,long[]registers,List<int>program,StringBuilderbuilder=default)
138+
{
139+
if(ip>program.Count-1)
140+
{
141+
returnint.MinValue;
142+
}
143+
144+
intop=program[ip];
145+
intarg=program[ip+1];
146+
intinc=2;
147+
148+
switch(op)
149+
{
150+
case0:
151+
// adv
152+
registers[0]/=(int)double.Pow(2,ComboReg(arg,registers));
153+
break;
154+
case1:
155+
// bxl
156+
registers[1]^=arg;
157+
break;
158+
case2:
159+
// bst
160+
registers[1]=ComboReg(arg,registers)&7;
161+
break;
162+
case3:
163+
// jnz
164+
if(registers[0]!=0)
165+
{
166+
intdelta=arg-ip;
167+
inc=delta;
168+
}
169+
170+
break;
171+
case4:
172+
// bxc
173+
registers[1]^=registers[2];
174+
break;
175+
case5:
176+
// out
177+
stringoutput=$"{ComboReg(arg,registers)&7},";
178+
179+
if(builderis notnull)
180+
{
181+
builder.Append(output);
182+
}
183+
else
184+
{
185+
Console.Write(output);
186+
}
187+
188+
break;
189+
case6:
190+
// bdv
191+
registers[1]=registers[0]/(int)double.Pow(2,ComboReg(arg,registers));
192+
break;
193+
case7:
194+
registers[2]=registers[0]/(int)double.Pow(2,ComboReg(arg,registers));
195+
break;
196+
default:
197+
thrownewInvalidDataException();
198+
}
199+
200+
returninc;
201+
202+
staticlongComboReg(intarg,long[]registers)
203+
{
204+
returnargswitch
205+
{
206+
<4=>arg,
207+
<6=>registers[arg-4],
208+
};
209+
}
210+
}
211+
}
212+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp