annotate day2.rb @ 1:49dd1ae93696

Implement day 2 with map and reduce
author IBBoard <dev@ibboard.co.uk>
date Sat, 02 Dec 2023 16:16:58 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 #! /usr/bin/env ruby
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 if ARGV.length != 4
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 abort("Incorrect arguments - needs numbers for Red, Green and Blue then input file")
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 elsif not File.exist? (ARGV[3])
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 abort("File #{ARGV[3]} did not exist")
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 end
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 max_colours = {
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 "red" => ARGV[0].to_i,
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 "green" => ARGV[1].to_i,
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 "blue" => ARGV[2].to_i
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 }
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 file = ARGV[3]
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 sum = 0
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 File.open(file, "r").each_line do |line|
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 game, all_picks = line.split(":")
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 all_valid = all_picks.split(";").all? do |pick|
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 pick.split(",").all? do |colour_pick|
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 num_picked, colour_picked = colour_pick.strip.split(" ")
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 # The last line of a Ruby block is the return value
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 num_picked.to_i <= max_colours[colour_picked]
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 end
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 end
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 if all_valid
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 puts game
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 sum += game.split(" ")[1].to_i
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 end
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 end
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 puts sum