diff options
-rw-r--r-- | Program.fs | 2 | ||||
-rw-r--r-- | solutions/day10.fs | 34 |
2 files changed, 36 insertions, 0 deletions
diff --git a/Program.fs b/Program.fs index 4d78505..fb79423 100644 --- a/Program.fs +++ b/Program.fs @@ -26,5 +26,7 @@ match (day, part) with | (8, 2) -> printf $"{Day8.part2 ()}\n" | (9, 1) -> printf $"{Day9.part1 ()}\n" | (9, 2) -> printf $"{Day9.part2 ()}\n" +| (10, 1) -> printf $"{Day10.part1()}\n" +| (10, 2) -> printf $"{Day10.part2()}\n" | (x, y) when (1 <= x && x <= 25) && (y = 1 || y = 2) -> raise (NotImplemented("not implemented yet")) | _ -> raise (NotImplemented("invalid values")) \ No newline at end of file diff --git a/solutions/day10.fs b/solutions/day10.fs new file mode 100644 index 0000000..799850d --- /dev/null +++ b/solutions/day10.fs @@ -0,0 +1,34 @@ +namespace Solutions + +module Day10 = + open System.IO + open System.Text.RegularExpressions + + let (|InstRegex|_|) pattern line = + let matched = Regex.Match(line, pattern) + if matched.Success then + matched.Groups |> Seq.tail |> Seq.map (fun x -> x.Value) |> List.ofSeq |> Some + else None + + let parseInst line = + match line with + | InstRegex "noop" [] -> [0] + | InstRegex "addx (-?\d+)" x -> [0; (int x[0])] + | _ -> failwith "invalid input" + + let changes = File.ReadLines("inputs/day10.txt") |> Seq.map parseInst |> List.concat + + let executeInsts changes = + List.scan (+) 1 changes + + let getStateSum cycles (states: int list) = + cycles |> List.map (fun x -> x * states[x-1]) |> List.sum + + let part1 () = executeInsts changes |> getStateSum [20; 60; 100; 140; 180; 220] + + let part2 () = executeInsts changes + |> List.mapi (fun pixel signal -> abs (signal - (pixel % 40)) <= 1) + |> List.map (fun x -> if x then "#" else " ") + |> List.chunkBySize 40 + |> List.map (String.concat "") + |> List.iter (printf "%s\n") \ No newline at end of file |