about summary refs log tree commit diff stats
path: root/.gitignore
blob: bef84b47f237138b34e4fd1da8e1101cbd91d4c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
*.o
*.o.depend
*.orig
*.rej
*.rej.orig
*~
javascript.h
xombrero
xombrero.cat1
xombrero.1.manlint
tags
tooltip.h
osx/tooltip.h
linux/tooltip.h
ariable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
module Solutions.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")