annotate day9b.rb @ 14:dcc060e59c47

Replace "each… each…" with a reduce and a flat map
author IBBoard <dev@ibboard.co.uk>
date Sat, 09 Dec 2023 20:07:16 +0000
parents 891878e52a31
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 #! /usr/bin/env ruby
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 if ARGV.length != 1
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 abort("Incorrect arguments - needs input file")
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 elsif not File.exist? (ARGV[0])
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 abort("File #{ARGV[0]} did not exist")
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 end
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 file = ARGV[0]
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 histories = File.open(file, "r").each_line(chomp: true).map {|line| line.split(" ").map(&:to_i)}
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 def diff_array(arr)
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 arr.each_cons(2)
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 end
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 predictions = histories.map do |history|
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 vals = history
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 layers = [history]
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 while not vals.all? {|v| v == 0}
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 vals = vals.each_cons(2).map {|a,b| b - a}
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 layers << vals
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 end
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 layers.reverse!.each_cons(2).map {|diffs, vals| vals.insert(0, vals[0] - diffs[0])}
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 layers[-1][0]
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 end
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 puts predictions.sum
891878e52a31 Implement Day 9 - diffing and predicting
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 # Rest of algorithm here