comparison day12-failed.rb @ 23:f34254b67082

Add failed and almost complete Day 12 part 1 Read some discussion around solutions in other languages, realised that recursion may work. And we can do caching. Also includes failed map/reduce attempts.
author IBBoard <dev@ibboard.co.uk>
date Sat, 16 Dec 2023 20:04:27 +0000
parents
children
comparison
equal deleted inserted replaced
22:ad73a2ff3d06 23:f34254b67082
1 #! /usr/bin/env ruby
2
3 if ARGV.length != 1
4 abort("Incorrect arguments - needs input file")
5 elsif not File.exist? (ARGV[0])
6 abort("File #{ARGV[0]} did not exist")
7 end
8
9 file = ARGV[0]
10
11 RowData = Struct.new(:symbols, :counts)
12 CandidateCollector = Struct.new(:completed, :pending)
13
14 def find_earliest_positions(symbols, counts)
15 # We need to skip items (to separate each object) but I don't know enough Ruby to
16 # do that in a map/reduce/etc (if it's even possible), so just use classic iteration
17 char_pos = 0
18 count_pos = 0
19 match_start = 0
20 earliest_matches = []
21 # puts "Counts: #{counts}"
22 while char_pos < symbols.length
23 # FIXME: Doesn't take account of pre-existing fixed blocks if there are enough question marks
24 if symbols[char_pos] != "."
25 if (char_pos - match_start + 1) == counts[count_pos]
26 if symbols[char_pos+1] != "#"
27 earliest_matches << match_start
28 # puts "At #{char_pos} found #{counts[count_pos]} chars from #{match_start} -- #{earliest_matches}"
29 # We have to allow one space as well as moving on
30 match_start = char_pos + 2
31 count_pos += 1
32 else
33 # We can't have found it because there's not a space! Shuffle along one.
34 match_start += 1
35 end
36 # Else we're still in the middle of it
37 end
38 elsif match_start < char_pos
39 match_start = char_pos + 1
40 end
41 char_pos += 1
42 end
43 earliest_matches
44 end
45
46 data = File.open(file, "r").each_line(chomp: true).map do |line|
47 symbols, counts_str = line.split(" ")
48 counts = counts_str.split(",").map(&:to_i)
49
50 # There's nothing like Python's strip to strip all matching characters
51 # So use a regex to just get the characters that could be items
52 candidate_symbols = /\.*?([^.].+[^.]).*?/.match(symbols)[1]
53 puts candidate_symbols
54 earliest_matches = find_earliest_positions(candidate_symbols, counts)
55 latest_matches = find_earliest_positions(candidate_symbols.reverse, counts.reverse).reverse.zip(counts).map {|pos, count| candidate_symbols.length - pos - count}
56 puts "#{earliest_matches} to #{latest_matches}"
57 # Assume lines are less than 100 chars and put a fake end in so we reduce to the end
58 earliest_matches << 100
59 latest_matches << 100
60 ranges = earliest_matches.zip(latest_matches).map {|vals| Range.new(*vals)}
61 sum = ranges.zip(counts).reduce([1, [(-1..-1), 0]]) do |accum, next_range_parts|
62 size, cur_range_parts = accum
63 cur_range, cur_count = cur_range_parts
64 next_range, next_count = next_range_parts
65
66 if cur_range.max >= next_range.min
67 # Overlap - do combinations
68 remaining_next_range = next_range.max - cur_range.max - cur_count
69 triangular = ((next_range.size * (next_range.size + 1)) / 2)
70 puts "#{remaining_next_range} => #{triangular}"
71 new_range = (cur_range.max + cur_count + 1)..next_range.end
72 [size * triangular, [new_range, next_count]]
73 else
74 puts "#{cur_range} => #{cur_range.size}"
75 [size * cur_range.size, next_range_parts]
76 end
77 end
78 puts sum[0]
79 puts
80
81 =begin
82 # Aborted attempt trying to use regex to start to find positions for us
83 count_parts = counts.split(",").map {|count| "([#\?]{#{count.to_i}})"}
84 counts_re = /[.?]*#{count_parts.join("[.?]+")}[.?]*/
85 puts symbols
86 match = symbols.match(counts_re)
87 captures = match.captures
88 puts "#{captures}"
89 found = captures.map {|v| v.each_char.all?{|c| c == "#"}}
90 puts "#{found}"
91 puts captures.each.with_index(1).map {|v, i| "#{v} at #{match.offset(i)}"}
92 =end
93 RowData.new(symbols, counts)
94 end
95 puts data
96 # Rest of algorithm here