diff --git a/aoc2020.ml b/aoc2020.ml index 8c6e290..a6f9ee7 100644 --- a/aoc2020.ml +++ b/aoc2020.ml @@ -3,4 +3,4 @@ let days = [|Day1.main|] let _ = match Array.to_list Sys.argv with | _exename :: day :: args -> days.(int_of_string day - 1) args - | _ -> Usage.exit "[day_num] args..." + | _ -> Usage.exit " args..." diff --git a/bracket.ml b/bracket.ml new file mode 100644 index 0000000..f47e41d --- /dev/null +++ b/bracket.ml @@ -0,0 +1,17 @@ +let bracket ~make ~act ~free = + let x = make () in + match act x with + | result -> free x; result + | exception e -> free x; raise e + +let infile ~act name = + bracket ~make:(fun () -> open_in name) ~act ~free:close_in_noerr + +let infile_lines ~line:f ~of_seq name = + let get_line file = + match input_line file with + | exception End_of_file -> None + | line -> Some (f line, file) + in + let act file = of_seq (Seq.unfold get_line file) in + infile name ~act diff --git a/bracket.mli b/bracket.mli new file mode 100644 index 0000000..ffa4bbb --- /dev/null +++ b/bracket.mli @@ -0,0 +1,5 @@ +val bracket: make:(unit -> 'a) -> act:('a -> 'b) -> free:('a -> unit) -> 'b + +val infile: act:(in_channel -> 'b) -> string -> 'b + +val infile_lines: line:(string -> 'a) -> of_seq:('a Seq.t -> 'b) -> string -> 'b diff --git a/data/day1 b/data/day1 new file mode 100644 index 0000000..127b7a7 --- /dev/null +++ b/data/day1 @@ -0,0 +1,200 @@ +1293 +1207 +1623 +1675 +1842 +1410 +85 +1108 +557 +1217 +1506 +1956 +1579 +1614 +1360 +1544 +1946 +1666 +1972 +1814 +1699 +1778 +1529 +2002 +1768 +1173 +1407 +1201 +1264 +1739 +1774 +1951 +1980 +1428 +1381 +1714 +884 +1939 +1295 +1694 +1168 +1971 +1352 +1462 +1828 +1402 +1433 +1542 +1144 +1331 +1427 +1261 +1663 +1820 +1570 +1874 +1486 +1613 +1769 +1721 +1753 +1142 +1677 +2010 +1640 +1465 +1171 +534 +1790 +2005 +1604 +1891 +1247 +1281 +1867 +1403 +2004 +1668 +1416 +2001 +1359 +686 +1965 +1728 +1551 +1565 +1128 +1832 +1757 +1350 +1808 +1711 +1799 +1590 +1989 +1547 +1140 +1905 +1368 +1179 +1902 +1473 +1908 +1859 +1257 +1394 +1244 +1800 +1695 +1731 +1474 +1781 +1885 +1154 +1990 +1929 +1193 +1302 +1831 +1226 +1418 +1400 +1435 +1645 +1655 +1843 +1227 +1481 +1754 +1290 +1685 +1498 +71 +1286 +1137 +1288 +1758 +1987 +1471 +1839 +1545 +1682 +1615 +1475 +1849 +1985 +1568 +1795 +1184 +1863 +1362 +1271 +1802 +1944 +1821 +1880 +1788 +1733 +1150 +1314 +1727 +1434 +1833 +1312 +1457 +160 +1629 +1967 +1505 +1239 +1266 +1838 +1687 +1630 +1591 +1893 +1450 +1234 +1755 +1523 +1533 +1499 +1865 +1725 +1444 +1517 +1167 +1738 +1519 +1263 +1901 +1627 +1644 +1771 +1812 +1270 +1497 +1707 +1708 +1396 diff --git a/day1.ml b/day1.ml index 92b3c3c..7a41bd2 100644 --- a/day1.ml +++ b/day1.ml @@ -1,6 +1,36 @@ -let main2 _input = - print_endline "boop" +module IntSet = Set.Make(Int) + +let read_ints = + Bracket.infile_lines ~line:int_of_string ~of_seq:IntSet.of_seq + +let find_with target ints = + let open IntSet in + let p x = mem (target - x) ints in + let res = min_elt (filter p ints) in + res, target - res + +let find = find_with 2020 + +let main_part_1 input = + let ints = read_ints input in + let x, y = find ints in + Printf.printf "%d * %d = %d\n" x y (x * y) + +let main_part_2 input = + let ints = read_ints input in + let exception Found of int * int * int in + let go x = + match find_with (2020 - x) ints with + | exception Not_found -> () + | y, z -> raise (Found (x, y, z)) + in + match IntSet.iter go ints with + | exception Found (x, y, z) -> + Printf.printf "%d * %d * %d = %d\n" x y z (x * y * z) + | _ -> raise Not_found + +let mains = [|main_part_1; main_part_2|] let main = function - | [input] -> main2 input - | _ -> failwith "usage: $0 1 infile" + | [part; input] -> mains.(int_of_string part - 1) input + | _ -> Usage.exit "1 "