view 2023/day15.ml @ 71:936b17a8e4ff

Day 15 Part 1
author Lewin Bormann <lbo@spheniscida.de>
date Thu, 28 Dec 2023 19:10:41 +0100
parents
children 039e082065a4
line wrap: on
line source

open Angstrom
open Base
open Core

type initseq = string list

module Parse = struct
  let parse_initseq s : initseq = String.split s ~on:','
end

module Part1 = struct
  let hash s =
    let l = String.to_list s in
    let step st c = Int.(17 * (st + Char.to_int c) % 256) in
    List.fold l ~init:0 ~f:step

  let hash_all steps = List.map steps ~f:hash

  let hash_initseq s =
    String.chop_suffix_if_exists ~suffix:"\n" s
    |> Parse.parse_initseq |> hash_all

  let hash_sum s = hash_initseq s |> List.fold ~init:0 ~f:( + )
end

let () =
  let inp = In_channel.(input_all stdin) in
  let s = Part1.hash_sum inp in
  printf "Part 1: %d\n" s