1
0
Fork 0

Compare commits

..

No commits in common. "c14c6f5ea7be3b173674f0a86f308124b1c36bb8" and "18fdc0cb34e7762eb9947759e347c49469a8331b" have entirely different histories.

3 changed files with 4 additions and 1044 deletions

File diff suppressed because it is too large Load Diff

View File

@ -4,9 +4,9 @@ let priority_of_item c =
(* ASCII lower case is 97-122, upper case is 65-90 *)
(* Lower case start at 1, upper case at 27 *)
let code = Char.code c in
if code > Char.code 'Z'
then code - (Char.code 'a') + 1
else code - (Char.code 'A') + 27
if code > 90
then code - 96
else code - 65 + 27
in
let sum_of_priorities fn str =
@ -48,9 +48,7 @@ let rec sum_badge_priorities sum lines =
in
let contents = read_file "inputs/3.txt" in
let lines = String.split_on_char '\n' contents
|> List.filter (fun l -> String.trim l <> "")
in
let lines = List.filter (fun l -> String.trim l <> "") (String.split_on_char '\n' contents) in
let sum1 = sum_rucksack_priorities lines in
let sum2 = sum_badge_priorities 0 lines in
Printf.printf "Part 1: %d\n" sum1;

View File

@ -1,38 +0,0 @@
#use "lib/utils.ml" ;;
let parse_range str =
let nums = String.split_on_char '-' str in
let start = int_of_string (List.nth nums 0) in
let rend = int_of_string (List.nth nums 1) in
(start, rend)
in
let is_full_overlap x y =
let ((x1, x2), (y1, y2)) = (x, y) in
(x1 >= y1 && x2 <= y2) || (y1 >= x1 && y2 <= x2)
in
let is_partial_overlap x y =
let ((x1, x2), (y1, y2)) = (x, y) in
not (x1 > y2 || x2 < y1)
in
let count_overlaps lines fn =
let check_overlap line =
let ranges = String.split_on_char ',' line in
let left = parse_range (List.nth ranges 0) in
let right = parse_range (List.nth ranges 1) in
if fn left right then 1 else 0
in
List.map check_overlap lines
|> List.fold_left (+) 0
in
let contents = read_file "inputs/4.txt" in
let lines = String.split_on_char '\n' contents
|> List.filter (fun l -> String.trim l <> "")
in
let nfull = count_overlaps lines is_full_overlap in
let npartial = count_overlaps lines is_partial_overlap in
Printf.printf "%d full overlaps\n" nfull;
Printf.printf "%d partial overlaps\n" npartial