brute force day 16.1 with incorrect algorithm lol
it gets the right answer for the input but not for the example input
This commit is contained in:
1 parent
8270ee7aa8
commit
b9a36a476a
2 files changed
+136
No files matched your search
@@ -0,0 +1,76 @@
|
||||
open Containers
|
||||
|
||||
let example_lines =
|
||||
String.lines
|
||||
{|Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
|
||||
Valve BB has flow rate=13; tunnels lead to valves CC, AA
|
||||
Valve CC has flow rate=2; tunnels lead to valves DD, BB
|
||||
Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE
|
||||
Valve EE has flow rate=3; tunnels lead to valves FF, DD
|
||||
Valve FF has flow rate=0; tunnels lead to valves EE, GG
|
||||
Valve GG has flow rate=0; tunnels lead to valves FF, HH
|
||||
Valve HH has flow rate=22; tunnel leads to valve GG
|
||||
Valve II has flow rate=0; tunnels lead to valves AA, JJ
|
||||
Valve JJ has flow rate=21; tunnel leads to valve II
|
||||
|}
|
||||
;;
|
||||
|
||||
type valve =
|
||||
{ name : string
|
||||
; flow : int
|
||||
; tunnels : string list
|
||||
}
|
||||
[@@deriving show { with_path = false }]
|
||||
|
||||
let parse_valve line =
|
||||
let int_matches = List.map int_of_string Re.(matches (Pcre.regexp {|\d+|}) line) in
|
||||
let valve_matches = Re.(matches (Pcre.regexp {|[A-Z]{2}|}) line) in
|
||||
match int_matches, valve_matches with
|
||||
| [ flow ], name :: tunnels -> name, { name; flow; tunnels }
|
||||
| _ -> failwith "parse error"
|
||||
;;
|
||||
|
||||
let solve_part_1 lines =
|
||||
let valves = Hashtbl.create 100 in
|
||||
lines
|
||||
|> List.iter (fun line ->
|
||||
let name, valve = parse_valve line in
|
||||
Hashtbl.add valves name valve);
|
||||
let get_valve name = Option.get_exn_or "poo" @@ Hashtbl.get valves name in
|
||||
let traverse =
|
||||
Utils.memo (fun self released current time opened visited ->
|
||||
if time = 0
|
||||
then released
|
||||
else (
|
||||
let open_current =
|
||||
if current.flow > 0 && (not @@ List.mem current.name opened)
|
||||
then (
|
||||
let time' = time - 1 in
|
||||
let released' = released + (current.flow * time') in
|
||||
[ self released' current time' (current.name :: opened) visited ])
|
||||
else []
|
||||
in
|
||||
let follow_a_tunnel =
|
||||
current.tunnels
|
||||
|> List.filter (fun tunnel -> not @@ List.mem tunnel visited)
|
||||
|> List.map (fun name ->
|
||||
let current' = get_valve name in
|
||||
let time' = time - 1 in
|
||||
self released current' time' opened (current.name :: visited))
|
||||
in
|
||||
Option.get_or ~default:released
|
||||
@@ List.reduce Int.max (open_current @ follow_a_tunnel)))
|
||||
in
|
||||
let start = get_valve "AA" in
|
||||
traverse 0 start 30 [] []
|
||||
;;
|
||||
|
||||
let%expect_test "Day 16.1 example" =
|
||||
Printf.printf "%i\n" @@ solve_part_1 @@ example_lines;
|
||||
[%expect {| 1651 |}]
|
||||
;;
|
||||
|
||||
let%expect_test "Day 16.1" =
|
||||
Printf.printf "%i" @@ solve_part_1 @@ Utils.lines_of_input 16;
|
||||
[%expect {| 2265 |}]
|
||||
;;
|
||||
Reference in new issue
Block a user