Skip to content

Commit 48f8237

Browse files
committed
Add 2023 day 12, first part, sloooooow version
1 parent 4b17731 commit 48f8237

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

2023/day12/day12a

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env -S ruby -W0
2+
3+
def to_list(record)
4+
record.scan(/#+/).map(&:length).join(",")
5+
end
6+
7+
def generate(record)
8+
locs = record.chars.each_with_object([]).with_index do |(c, locs), idx|
9+
locs << idx if c == "?"
10+
end
11+
12+
warn "Question mark locations: #{locs}"
13+
14+
len = locs.length
15+
warn "Generating #{2**len} patterns"
16+
(0...2**len).to_a.map do |n|
17+
arrangement = n.to_s(2).rjust(len, "0").tr("01", ".#")
18+
new_record = record.clone
19+
arrangement.chars.zip(locs).each { |spring, idx| new_record[idx] = spring }
20+
21+
new_record
22+
end
23+
end
24+
25+
file = File.open(ARGV[0])
26+
27+
entries = file.readlines.map do |line|
28+
(record, list) = line.split
29+
{record:, list:}
30+
end
31+
32+
sum_counts = entries.sum do |entry|
33+
warn "Original: #{entry[:record]}"
34+
generate(entry[:record]).count do |record|
35+
warn "#{record} vs. #{entry[:list]}"
36+
to_list(record) == entry[:list]
37+
end
38+
end
39+
40+
puts sum_counts

0 commit comments

Comments
 (0)