Skip to content

Commit baab58e

Browse files
committed
Solutions for Day 2
* Part 1 refactored * Part 2 solved
1 parent 5dcaafd commit baab58e

File tree

2 files changed

+74
-29
lines changed

2 files changed

+74
-29
lines changed

bin/day2bin.ml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
let option_list =
2-
List.map Day2.is_valid_game (Day2.input_line_list "day2/input.txt")
3-
4-
let unpack a b = match b with Some x -> a + x | None -> a
5-
let sum = List.fold_left unpack 0 option_list
61
let () = Printf.printf "\nSolving Day 2!\n"
7-
let () = Printf.printf "The sum of the Game IDs is: %d\n\n" sum
2+
let sum = Day2.sum_valid_games (Day2.input_line_list "day2/input.txt")
3+
let () = Printf.printf "The sum of the Game IDs is: %d\n" sum
4+
let sum_prod = Day2.sum_prod_max (Day2.input_line_list "day2/input.txt")
5+
let () = Printf.printf "The sum of products of max balls is: %d\n\n" sum_prod

day2/day2.ml

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,76 @@
11
let input_line_list file = In_channel.input_lines (In_channel.open_text file)
22

3-
let validate_trials sets =
4-
let validate_set set =
5-
let validate_color s =
6-
let balls_and_color = String.split_on_char ' ' s in
7-
let balls, colour =
8-
(int_of_string (List.nth balls_and_color 0), List.nth balls_and_color 1)
9-
in
10-
match colour with
11-
| "red" -> balls <= 12
12-
| "green" -> balls <= 13
13-
| "blue" -> balls <= 14
14-
| _ -> false
15-
in
16-
let ball_list_in_set =
17-
List.map String.trim (String.split_on_char ',' set)
3+
type ball = Red of int | Green of int | Blue of int | None
4+
5+
let game_int_and_ball_strings s =
6+
let game_and_sets = String.split_on_char ':' s in
7+
let game_int = int_of_string (Str.string_after (List.hd game_and_sets) 5) in
8+
let ball_strings =
9+
List.map String.trim
10+
(Str.split (Str.regexp "[;,]") (List.nth game_and_sets 1))
11+
in
12+
(game_int, ball_strings)
13+
14+
let string_to_ball s =
15+
let color_count_split = String.split_on_char ' ' s in
16+
let count, color =
17+
(List.nth color_count_split 0, List.nth color_count_split 1)
18+
in
19+
match color with
20+
| "red" -> Red (int_of_string count)
21+
| "green" -> Green (int_of_string count)
22+
| "blue" -> Blue (int_of_string count)
23+
| _ -> None
24+
25+
let validate_ball ball =
26+
match ball with
27+
| Red r -> if r <= 12 then Red r else None
28+
| Green g -> if g <= 13 then Green g else None
29+
| Blue b -> if b <= 14 then Blue b else None
30+
| None -> None
31+
32+
let sum_valid_games game_list =
33+
let validate_game s =
34+
let game_int, balls_string = game_int_and_ball_strings s in
35+
let ball_list = List.map string_to_ball balls_string in
36+
let valid_balls = List.map validate_ball ball_list in
37+
let valid_balls_bool =
38+
List.map (fun x -> match x with None -> false | _ -> true) valid_balls
1839
in
19-
List.fold_left ( && ) true (List.map validate_color ball_list_in_set)
40+
if List.fold_left ( && ) true valid_balls_bool then Some game_int else None
41+
in
42+
let rec sum_valid_games_helper acc game_list_int_option =
43+
match game_list_int_option with
44+
| [] -> acc
45+
| h :: t -> (
46+
match h with
47+
| Some a -> sum_valid_games_helper (acc + a) t
48+
| None -> sum_valid_games_helper acc t)
2049
in
21-
List.fold_left ( && ) true
22-
(List.map validate_set (String.split_on_char ';' sets))
50+
sum_valid_games_helper 0 (List.map validate_game game_list)
2351

24-
let is_valid_game s =
25-
let sets = String.split_on_char ':' s in
26-
let day, trials =
27-
(int_of_string (Str.string_after (List.nth sets 0) 5), List.nth sets 1)
52+
let sum_prod_max list =
53+
let max_each_color s =
54+
let _, balls_string = game_int_and_ball_strings s in
55+
let ball_list = List.map string_to_ball balls_string in
56+
let rec max_balls red green blue valid_balls =
57+
match valid_balls with
58+
| [] -> (red, green, blue)
59+
| Red r :: t ->
60+
if red < r then max_balls r green blue t
61+
else max_balls red green blue t
62+
| Blue b :: t ->
63+
if blue < b then max_balls red green b t
64+
else max_balls red green blue t
65+
| Green g :: t ->
66+
if green < g then max_balls red g blue t
67+
else max_balls red green blue t
68+
| None :: t -> max_balls red green blue t
69+
in
70+
max_balls 0 0 0 ball_list
71+
in
72+
let prod_max s =
73+
let r, g, b = max_each_color s in
74+
r * g * b
2875
in
29-
match validate_trials trials with true -> Some day | false -> None
76+
List.fold_left ( + ) 0 (List.map prod_max list)

0 commit comments

Comments
 (0)