Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ tap_history

# AOC specific
input*
test_input*
43 changes: 0 additions & 43 deletions IMPLEMENTATION.md

This file was deleted.

114 changes: 0 additions & 114 deletions TESTS.md

This file was deleted.

123 changes: 123 additions & 0 deletions aoc/day10_1.tap
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
get_file_content(): string = {
file = open(args.get(0), "r");
content: string = file.read();
file.close();
content
}

pow2(n): int = {
mut res = 1;
for i in 0..<n {
res = res * 2;
}
res
}

solve() = {
content = get_file_content();
lines: [string] = content.split("\n").filter((l) => l.length() > 0);

mut total_presses = 0;

for line in lines {
parts = line.split(" ").filter((s) => s.length() > 0);

// Parse target machine state
// Format: [.##.]
diag_part = parts[0];
diag_len = diag_part.length();

mut target_mask = 0;
for i in 1..<(diag_len - 1) {
char = diag_part[i];
if char == "#" {
light_idx = i - 1;
target_mask = target_mask + pow2(light_idx);
}
}

// Parse buttons
// Format: (0,2) (1,3) ... until we hit {
mut buttons: [int] = [];
mut p_idx = 1;
mut parsing_buttons = true;

while parsing_buttons {
if p_idx >= parts.length() {
parsing_buttons = false;
} else {
part = parts[p_idx];
if part[0] == "{" {
parsing_buttons = false;
} else { // It's a button: (1,2,3)
// Strip parens at 0 and length-1
// built-in substring takes (string, len) params
inner = part.substring(1, part.length() - 2);
nums = inner.split(",");
mut btn_mask = 0;
for num_s in nums {
if num_s.length() > 0 {
idx = num_s.parse_int();
btn_mask = btn_mask + pow2(idx);
}
}
buttons.push(btn_mask);
p_idx = p_idx + 1;
}
}
}

// --- BFS to find minimum presses ---
// State: current bitmask of lights
// Start: 0 (all lights off initially)
// Target: target_mask

mut q: [int] = [];
q.push(0);

mut dists = Map();
dists.insert(0, 0);

mut min_steps = -1;
mut head = 0;

// Optimization: check if we are already there
if target_mask == 0 {
min_steps = 0;
}

while min_steps == -1 {
// Safety break for empty queue
if head >= q.length() {
print("Error: Could not solve machine: " + line);
break;
}

curr = q[head];
head = head + 1;

d = dists.get(curr);

if curr == target_mask {
min_steps = d;
} else {
// Try pressing each button
for btn in buttons {
// Apply button using XOR
next_state = curr ^ btn;

if !dists.has(next_state) {
dists.insert(next_state, d + 1);
q.push(next_state);
}
}
}
}

total_presses = total_presses + min_steps;
}

print("Total minimum presses: " + total_presses.to_string());
}

solve();
Loading