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:
parent
8270ee7aa8
commit
b9a36a476a
60
inputs/day16.tt
Normal file
60
inputs/day16.tt
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
Valve XB has flow rate=0; tunnels lead to valves WZ, LE
|
||||
Valve BM has flow rate=0; tunnels lead to valves PL, RI
|
||||
Valve GC has flow rate=0; tunnels lead to valves HN, IT
|
||||
Valve RM has flow rate=0; tunnels lead to valves ZQ, YL
|
||||
Valve ZM has flow rate=5; tunnels lead to valves SN, KE, UW, MY, GW
|
||||
Valve UH has flow rate=0; tunnels lead to valves HM, HN
|
||||
Valve GW has flow rate=0; tunnels lead to valves LE, ZM
|
||||
Valve HN has flow rate=19; tunnels lead to valves UW, UH, GL, WZ, GC
|
||||
Valve VT has flow rate=0; tunnels lead to valves ZD, PE
|
||||
Valve VI has flow rate=0; tunnels lead to valves JS, AA
|
||||
Valve YL has flow rate=12; tunnels lead to valves PM, MH, RM, CM
|
||||
Valve LA has flow rate=0; tunnels lead to valves SN, IY
|
||||
Valve CM has flow rate=0; tunnels lead to valves YL, UL
|
||||
Valve JI has flow rate=24; tunnels lead to valves UV, WH, XW, OJ
|
||||
Valve ZD has flow rate=25; tunnels lead to valves VB, XW, VT
|
||||
Valve VB has flow rate=0; tunnels lead to valves QX, ZD
|
||||
Valve FO has flow rate=0; tunnels lead to valves WH, PE
|
||||
Valve JS has flow rate=0; tunnels lead to valves VI, IY
|
||||
Valve RI has flow rate=0; tunnels lead to valves PE, BM
|
||||
Valve XD has flow rate=14; tunnel leads to valve CP
|
||||
Valve ES has flow rate=11; tunnels lead to valves GU, WU
|
||||
Valve WZ has flow rate=0; tunnels lead to valves XB, HN
|
||||
Valve HW has flow rate=0; tunnels lead to valves QY, LE
|
||||
Valve KE has flow rate=0; tunnels lead to valves AA, ZM
|
||||
Valve IY has flow rate=22; tunnels lead to valves JS, AB, LA, IT
|
||||
Valve XW has flow rate=0; tunnels lead to valves ZD, JI
|
||||
Valve BE has flow rate=0; tunnels lead to valves AB, LE
|
||||
Valve QY has flow rate=23; tunnels lead to valves AM, HW, MY
|
||||
Valve MH has flow rate=0; tunnels lead to valves YL, AM
|
||||
Valve IT has flow rate=0; tunnels lead to valves GC, IY
|
||||
Valve DA has flow rate=0; tunnels lead to valves QR, CP
|
||||
Valve PM has flow rate=0; tunnels lead to valves LE, YL
|
||||
Valve WU has flow rate=0; tunnels lead to valves SM, ES
|
||||
Valve UL has flow rate=0; tunnels lead to valves CM, QR
|
||||
Valve SM has flow rate=13; tunnel leads to valve WU
|
||||
Valve XC has flow rate=0; tunnels lead to valves ZJ, AA
|
||||
Valve OJ has flow rate=0; tunnels lead to valves GI, JI
|
||||
Valve SN has flow rate=0; tunnels lead to valves LA, ZM
|
||||
Valve WH has flow rate=0; tunnels lead to valves JI, FO
|
||||
Valve UW has flow rate=0; tunnels lead to valves HN, ZM
|
||||
Valve HM has flow rate=0; tunnels lead to valves UH, PE
|
||||
Valve AB has flow rate=0; tunnels lead to valves BE, IY
|
||||
Valve QR has flow rate=8; tunnels lead to valves WW, UL, DA
|
||||
Valve UV has flow rate=0; tunnels lead to valves JI, FF
|
||||
Valve ZQ has flow rate=20; tunnel leads to valve RM
|
||||
Valve ZJ has flow rate=0; tunnels lead to valves PE, XC
|
||||
Valve GL has flow rate=0; tunnels lead to valves HN, PL
|
||||
Valve CP has flow rate=0; tunnels lead to valves DA, XD
|
||||
Valve AM has flow rate=0; tunnels lead to valves QY, MH
|
||||
Valve PL has flow rate=17; tunnels lead to valves GL, YE, BM, FF, QX
|
||||
Valve YE has flow rate=0; tunnels lead to valves PL, AA
|
||||
Valve PE has flow rate=4; tunnels lead to valves FO, RI, ZJ, VT, HM
|
||||
Valve MY has flow rate=0; tunnels lead to valves QY, ZM
|
||||
Valve QX has flow rate=0; tunnels lead to valves VB, PL
|
||||
Valve GI has flow rate=0; tunnels lead to valves OJ, AA
|
||||
Valve WW has flow rate=0; tunnels lead to valves GU, QR
|
||||
Valve FF has flow rate=0; tunnels lead to valves UV, PL
|
||||
Valve LE has flow rate=6; tunnels lead to valves HW, GW, XB, BE, PM
|
||||
Valve GU has flow rate=0; tunnels lead to valves ES, WW
|
||||
Valve AA has flow rate=0; tunnels lead to valves KE, GI, VI, YE, XC
|
||||
76
src/day16.ml
Normal file
76
src/day16.ml
Normal file
|
|
@ -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 |}]
|
||||
;;
|
||||
Loading…
Reference in New Issue
Block a user