Mercurial > repos > other > adventofcode2023
annotate day6b.rb @ 39:0e17e4bd97a9 default tip
Rewrite as four-dimensional route finding
The grid isn't just a 2D grid. The constraints make it 4D:
* X
* Y
* Last direction
* Number of steps in that direction
By tracking all four dimensions, we can find the shortest route
for _all_ combinations of the constraint. Previously, we were dropping
routes that were currently longer but ended up shorter because
they could take subsequent steps that other routes couldn't.
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 22 Sep 2024 11:30:53 +0100 |
parents | 6e0615e54e71 |
children |
rev | line source |
---|---|
7
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 #! /usr/bin/env ruby |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 if ARGV.length != 1 |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 abort("Incorrect arguments - needs input file") |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 elsif not File.exist? (ARGV[0]) |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 abort("File #{ARGV[0]} did not exist") |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 end |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 file = ARGV[0] |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 time, distance = File.open(file, "r").each_line(chomp: true).map {|v| v.split(":")[1].split(" ").join("").to_i} |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 events = [[time, distance]] |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 product = events.map do |event| |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 mid = (event[0].to_f / 2).floor |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 first_success = (1..mid).bsearch {|i| (event[0] - i) * i > event[1]} |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 num_success = (mid - first_success + 1) * 2 |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 event[0].odd? ? num_success : num_success - 1 |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 end.reduce(:*) |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 |
6e0615e54e71
Restructure input parsing for day6b
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 puts product |