aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--day24/input57
-rwxr-xr-xday24/part154
2 files changed, 111 insertions, 0 deletions
diff --git a/day24/input b/day24/input
new file mode 100644
index 0000000..1fbfe25
--- /dev/null
+++ b/day24/input
@@ -0,0 +1,57 @@
+42/37
+28/28
+29/25
+45/8
+35/23
+49/20
+44/4
+15/33
+14/19
+31/44
+39/14
+25/17
+34/34
+38/42
+8/42
+15/28
+0/7
+49/12
+18/36
+45/45
+28/7
+30/43
+23/41
+0/35
+18/9
+3/31
+20/31
+10/40
+0/22
+1/23
+20/47
+38/36
+15/8
+34/32
+30/30
+30/44
+19/28
+46/15
+34/50
+40/20
+27/39
+3/14
+43/45
+50/42
+1/33
+6/39
+46/44
+22/35
+15/20
+43/31
+23/23
+19/27
+47/15
+43/43
+25/36
+26/38
+1/10
diff --git a/day24/part1 b/day24/part1
new file mode 100755
index 0000000..a1dcabd
--- /dev/null
+++ b/day24/part1
@@ -0,0 +1,54 @@
+#!/usr/bin/env ruby
+
+input = $stdin.readlines.map(&:chomp).map{|c| c.split('/').map(&:to_i)}
+
+class BridgeFactory
+ def initialize(pieces, bridge = [], be = 0)
+ @pieces = pieces.dup
+ @bridge = bridge.dup
+ @be = be
+ @complete = false
+ end
+
+ def complete?
+ return @complete
+ end
+
+ def build_iter
+ @complete = true
+ nexts = get_nexts
+ return [self] + nexts.map do |nxt|
+ nbe = nxt[0] == nxt[1] ? nxt[0] : (nxt - [@be])[0]
+ BridgeFactory.new(@pieces - [nxt], @bridge + [nxt], nbe)
+ end
+ end
+
+ def strength
+ return @bridge.flatten.inject(:+) || 0
+ end
+
+ private
+
+ def get_nexts
+ return @pieces.select do |piece|
+ piece[0] == @be || piece[1] == @be
+ end
+ end
+end
+
+bfs = [BridgeFactory.new(input)]
+
+loop do
+ new_bfs = []
+ bfs.each do |bf|
+ if bf.complete? then
+ new_bfs << bf
+ else
+ new_bfs += bf.build_iter
+ end
+ end
+ break if new_bfs.map(&:complete?).all?
+ bfs = new_bfs
+end
+
+puts bfs.map(&:strength).max