Skip to content

Commit 0dded0a

Browse files
committed
day10 in python???? wtf
1 parent e79845d commit 0dded0a

7 files changed

Lines changed: 474 additions & 39 deletions

File tree

2025/Day10/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## --- Day 10: Factory ---
2+
Just across the hall, you find a large factory. Fortunately, the Elves here have plenty of time to decorate. Unfortunately, it's because the factory machines are all offline, and none of the Elves can figure out the initialization procedure.
3+
4+
The Elves do have the manual for the machines, but the section detailing the initialization procedure was eaten by a [Shiba Inu](https://en.wikipedia.org/wiki/Shiba_Inu). All that remains of the manual are some indicator light diagrams, button wiring schematics, and [joltage](3) requirements for each machine.
5+
6+
_Visit the website for the full story and [full puzzle](https://adventofcode.com/2025/day/10) description._

2025/Day10/Solution.cs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
namespace AdventOfCode.Y2025.Day10;
2+
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.Collections.Immutable;
7+
using System.Linq;
8+
using System.Text.RegularExpressions;
9+
using System.Text;
10+
using System.Numerics;
11+
using AdventOfCode.Model;
12+
using System.Security.Cryptography;
13+
using AngleSharp.Html.Dom.Events;
14+
15+
record Problem(int target, int[] buttons, int[] jolts);
16+
17+
[ProblemName("Factory")]
18+
class Solution : Solver {
19+
20+
public object PartOne(string input) {
21+
var res = 0;
22+
foreach (var p in Parse(input)) {
23+
var limit = 1 << p.buttons.Length;
24+
var tries = Enumerable.Range(0, limit).OrderBy(BitCount).ToArray();
25+
26+
var q = -1;
27+
foreach (var n in tries) {
28+
if ((Xor(p.buttons, n) ^ p.target) == 0) {
29+
q = n;
30+
break;
31+
}
32+
}
33+
if (q == -1) {
34+
throw new Exception();
35+
}
36+
res += BitCount(q);
37+
}
38+
return res;
39+
}
40+
41+
42+
public object PartTwo(string input) {
43+
var res = 0L;
44+
var i = 1;
45+
foreach (var p in Parse(input)) {
46+
Console.WriteLine(i + " " + p.buttons.Length + " " + p.jolts.Length);
47+
i++;
48+
}
49+
return res;
50+
}
51+
52+
bool TooMuch(Problem p, int[] state) {
53+
return Enumerable.Range(0, state.Length).Any(i => state[i] > p.jolts[i]);
54+
}
55+
56+
int Xor(int[] buttons, int mask) {
57+
var res = 0;
58+
var i = 0;
59+
while (mask != 0) {
60+
if ((mask & 1) != 0) {
61+
res ^= buttons[i];
62+
}
63+
mask >>= 1;
64+
i++;
65+
}
66+
return res;
67+
}
68+
69+
int Solve(Problem p, int pushes, int[] state) {
70+
if (pushes > 11) {
71+
return int.MaxValue;
72+
}
73+
74+
if (Array.Equals(state, p.jolts)) {
75+
return pushes;
76+
}
77+
78+
if (Enumerable.Range(0, state.Length).Any(i => state[i] > p.jolts[i])) {
79+
return int.MaxValue;
80+
}
81+
82+
var res = int.MaxValue;
83+
for (int i = 0; i < p.buttons.Length; i++) {
84+
var stateT = Push(state, p.buttons[i]);
85+
var m = Solve(p, pushes + 1, stateT);
86+
if (m < res) {
87+
res = m;
88+
}
89+
}
90+
return res;
91+
}
92+
93+
int[] Push(int[] state, int button) {
94+
var res = state.ToArray();
95+
for (int i = 0; i < state.Length; i++) {
96+
if ((button & (1 << i)) != 0) {
97+
res[i]++;
98+
}
99+
}
100+
return res;
101+
}
102+
103+
int BitCount(int n) {
104+
int res = 0;
105+
while (n != 0) {
106+
n &= n - 1;
107+
res++;
108+
}
109+
return res;
110+
}
111+
112+
// [.##.] (3) (1,3) (2) (2,3) (0,2) (0,1) {3,5,4,7}
113+
IEnumerable<Problem> Parse(string input) {
114+
var lines = input.Split("\n");
115+
foreach (var line in lines) {
116+
var parts = line.Split(" ").ToArray();
117+
var num = Convert.ToInt32(
118+
string.Join("",
119+
parts.First()
120+
.Replace("[", "")
121+
.Replace("]", "")
122+
.Replace('.', '0')
123+
.Replace('#', '1')
124+
.Reverse()
125+
),
126+
2);
127+
128+
var buttons =
129+
from part in parts[1..^1]
130+
let digits = Regex.Matches(part, @"\d").Select(m => int.Parse(m.Value))
131+
let mask = (from d in digits select 1 << d).Sum()
132+
select mask;
133+
134+
var jolts =
135+
parts.Last()
136+
.Replace("{", "")
137+
.Replace("}", "")
138+
.Split(",")
139+
.Select(int.Parse);
140+
;
141+
yield return new Problem(num, buttons.ToArray(), jolts.ToArray());
142+
}
143+
}
144+
145+
}

0 commit comments

Comments
 (0)