From: Ummon Date: Fri, 15 Dec 2017 14:45:36 +0000 (+0100) Subject: Rename DayX to Day0X X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=c0943d392ab178bc68585fa40dabe6a26957593f;p=advent_of_code_2017.git Rename DayX to Day0X --- diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 6571eca..a63b01a 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -37,7 +37,8 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 15 + + 11 @@ -57,15 +58,15 @@ - - - - - - - - - + + + + + + + + + @@ -75,28 +76,34 @@ - + + PreserveNewest + + + PreserveNewest + + PreserveNewest - + PreserveNewest - + PreserveNewest - + PreserveNewest - + PreserveNewest - + PreserveNewest - + PreserveNewest - + PreserveNewest diff --git a/AdventOfCode2017/Day01.fs b/AdventOfCode2017/Day01.fs new file mode 100644 index 0000000..2c7cc52 --- /dev/null +++ b/AdventOfCode2017/Day01.fs @@ -0,0 +1,12 @@ +module AdventOfCode2017.Day01 + +let readDigit d = int d - int '0' + +let parseInput (str : string) : int[] = str.ToCharArray () |> Array.map readDigit + +let solveCaptcha (shift : int) (captcha : int[]) = + let l = captcha.Length + [ for i in 0 .. l - 1 -> if captcha.[i] = captcha.[(i + shift) % l] then captcha.[i] else 0 ] |> List.sum + +let solveCaptcha1 = solveCaptcha 1 +let solveCaptcha2 (captcha : int[]) = solveCaptcha (captcha.Length / 2) captcha diff --git a/AdventOfCode2017/Day02.fs b/AdventOfCode2017/Day02.fs new file mode 100644 index 0000000..0b4ba73 --- /dev/null +++ b/AdventOfCode2017/Day02.fs @@ -0,0 +1,21 @@ +module AdventOfCode2017.Day02 + +open System + +let parseInput (str : string) : int[][] = + str.Split ([| '\n' |], StringSplitOptions.RemoveEmptyEntries) + |> Array.map (fun line -> line.Split ([| ' '; '\t' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int) + +let checksum1 (a : int[][]) = + a |> Array.sumBy (fun ns -> Array.max ns - Array.min ns) + +let checksum2 (a : int[][]) = + a + |> Array.sumBy ( + fun ns -> + seq { + for a in ns do + for b in ns do + if a <> b && a % b = 0 then yield a / b + } |> Seq.head + ) diff --git a/AdventOfCode2017/Day03.fs b/AdventOfCode2017/Day03.fs new file mode 100644 index 0000000..a9f6094 --- /dev/null +++ b/AdventOfCode2017/Day03.fs @@ -0,0 +1,33 @@ +module AdventOfCode2017.Day03 + +let directions = [| 1, 0; 0, 1; -1, 0; 0, -1 |] + +let spiral = + Seq.unfold ( + fun (pos, dir, n, i) -> + let x, y = directions.[dir] + let pos' = fst pos + x, snd pos + y + let nMax = (i + 1) * 2 - 1 + let i', n' = if n = nMax then i + 1, 0 else i, n + 1 + let dir' = if i <> i' || n' = nMax / 2 + 1 then (dir + 1) % 4 else dir + Some (pos, (pos', dir', n', i')) + ) ((0, 0), 0, 0, 0) + +let spiralManhattanDistanceSum (n : int) = + let x, y = spiral |> Seq.item (n - 1) + abs x + abs y + +let spiralAdjacentSumBiggerThan (n : int) = + let neighborsSum (dic : Map) (pos : int * int) = + let x, y = pos + [ for dx in -1 .. 1 do for dy in -1 .. 1 -> x + dx, y + dy ] + |> List.sumBy (fun (x, y) -> match dic |> Map.tryFind (x, y) with Some v -> v | None -> 0) + + spiral + |> Seq.skip 1 + |> Seq.scan ( + fun (_sum, dic) pos -> + let sum = neighborsSum dic pos + sum, dic |> Map.add pos sum + ) (1, Map.empty |> Map.add (0, 0) 1) + |> Seq.pick (fun (sum, _) -> if sum > n then Some sum else None) \ No newline at end of file diff --git a/AdventOfCode2017/Day04.fs b/AdventOfCode2017/Day04.fs new file mode 100644 index 0000000..627b8aa --- /dev/null +++ b/AdventOfCode2017/Day04.fs @@ -0,0 +1,15 @@ +module AdventOfCode2017.Day04 + +let forallDistinctPairs (f : string -> string -> bool) (pp : string) = + let words = pp.Split ' ' + [ + for a = 0 to words.Length - 1 do + for b in a + 1 .. words.Length - 1 -> f words.[a] words.[b] + ] |> List.forall not + +let isAnagram w1 w2 = Seq.compareWith compare (Seq.sort w1) (Seq.sort w2) = 0 + +let passphraseValid = forallDistinctPairs (=) +let passphraseValidAnagram = forallDistinctPairs isAnagram + +let nbPassphrasesValid (f : string -> bool) = Seq.map f >> Seq.sumBy (fun v -> if v then 1 else 0) \ No newline at end of file diff --git a/AdventOfCode2017/Day05.fs b/AdventOfCode2017/Day05.fs new file mode 100644 index 0000000..bc07fbc --- /dev/null +++ b/AdventOfCode2017/Day05.fs @@ -0,0 +1,19 @@ +module AdventOfCode2017.Day05 + +open System + +let parseInput (str : string) : int[] = + str.Split ([| '\r'; '\t'; ' ' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int + +let nbSteps (next : int -> int) (instructions : int[]) = + let is = instructions.[*] + let mutable cursor, steps = 0, 0 + while cursor >= 0 && cursor < instructions.Length do + let i = is.[cursor] + is.[cursor] <- next is.[cursor] + cursor <- cursor + i + steps <- steps + 1 + steps + +let nbSteps1 = nbSteps ((+) 1) +let nbSteps2 = nbSteps (fun i -> if i >= 3 then i - 1 else i + 1) \ No newline at end of file diff --git a/AdventOfCode2017/Day06.fs b/AdventOfCode2017/Day06.fs new file mode 100644 index 0000000..43cf2bc --- /dev/null +++ b/AdventOfCode2017/Day06.fs @@ -0,0 +1,37 @@ +module AdventOfCode2017.Day06 + +open System + +let parseInput (str : string) : int[] = + str.Split ([| '\r'; '\t'; ' ' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int + +// Custom equality: more efficient than '='. +let inline (|=|) (a1 : 'a[]) (a2 : 'a[]) : bool = + if a1.Length <> a2.Length then + false + else + let rec result i = + if i = a1.Length then + true + elif a1.[i] <> a2.[i] then + false + else + result (i + 1) + result 0 + +let nbRedistribution (blocks : int[]) = + let rec next (previous : int[] list) = + let blocks = List.head previous |> Array.copy + let i, max = blocks |> Array.indexed |> Array.maxBy snd + blocks.[i] <- 0 + for offset = i + 1 to i + max do + let i' = offset % blocks.Length + blocks.[i'] <- blocks.[i'] + 1 + + match previous |> List.tryFindIndex ((|=|) blocks) with + | Some i -> previous, i + 1 + | None -> next (blocks :: previous) + + let blockList, cycleLength = next [ blocks ] + + List.length blockList, cycleLength diff --git a/AdventOfCode2017/Day07.fs b/AdventOfCode2017/Day07.fs new file mode 100644 index 0000000..4321b87 --- /dev/null +++ b/AdventOfCode2017/Day07.fs @@ -0,0 +1,46 @@ +module AdventOfCode2017.Day07 + +open System +open System.Linq +open System.Collections.Generic + +type Tower = + { + Name : string + Weight : int + Above : List + } + +type Input = (Tower * string list) list + +let parseInput (lines : string list) : Input = + lines + |> List.map ( + fun line -> + let items = line.Split ([| '\r'; '\t'; ' '; ','; ')'; '(' |], StringSplitOptions.RemoveEmptyEntries) + { Name = items.[0]; Weight = int items.[1]; Above = List () }, [ for i in 3 .. items.Length - 1 -> items.[i] ] + ) + +let buildTower (input : Input) : Tower = + let towers = Dictionary () + + for tower, _ in input do + towers.Add (tower.Name, tower) + + for tower, towersAbove in input do + for towerAbove in towersAbove do + tower.Above.Add towers.[towerAbove] + towers.Remove towerAbove |> ignore + + towers.First().Value + +// Returns the tower and its corrected weight. +let rec findUnbalanced (tower : Tower) : (Tower * int) option = + let rec weight tower = + tower.Weight + (tower.Above |> Seq.sumBy weight) + + match tower.Above |> List.ofSeq |> List.groupBy weight |> List.sortBy (snd >> List.length) with + | [ w1, [ unbalanced ]; w2, _ ] -> + findUnbalanced unbalanced |> Option.orElse (Some (unbalanced, unbalanced.Weight + w2 - w1)) + | _ -> + None \ No newline at end of file diff --git a/AdventOfCode2017/Day08.fs b/AdventOfCode2017/Day08.fs new file mode 100644 index 0000000..24dfbbd --- /dev/null +++ b/AdventOfCode2017/Day08.fs @@ -0,0 +1,27 @@ +module AdventOfCode2017.Day08 + +open System + +type Instruction = string * string * int * string * (int -> int -> bool) * int + +let parseInput (lines : string[]) : Instruction list = + lines + |> List.ofArray + |> List.map ( + fun line -> + let line = line.Split ' ' + line.[0], line.[1], int line.[2], line.[4], (match line.[5] with ">" -> (>) | "<" -> (<) | ">=" -> (>=) | "<=" -> (<=) | "!=" -> (<>) | "==" | _ -> (=)), int line.[6] + ) + +let execute (input : Instruction list) : int * int = + let highest, register = + input + |> List.fold ( + fun (highest, register) (reg, ins, value, regCond, op, valueCond) -> + if op (register |> Map.tryFind regCond |> Option.defaultValue 0) valueCond then + let regValue' = (register |> Map.tryFind reg |> Option.defaultValue 0) + match ins with "inc" -> value | "dec" -> -value | _ -> 0 + max highest regValue', register |> Map.add reg regValue' + else + highest, register + ) (Int32.MinValue, Map.empty) + highest, (register |> Map.toList |> List.map snd |> List.max) diff --git a/AdventOfCode2017/Day09.fs b/AdventOfCode2017/Day09.fs new file mode 100644 index 0000000..53d6672 --- /dev/null +++ b/AdventOfCode2017/Day09.fs @@ -0,0 +1,13 @@ +module AdventOfCode2017.Day09 + +let score (input : string) = + let rec next (l : int) (lSum : int) (gb : bool) (gbSize : int) = + function + | '{' :: tail when not gb -> next (l + 1) (lSum + l) gb gbSize tail + | '}' :: tail when not gb -> next (l - 1) lSum gb gbSize tail + | '<' :: tail when not gb -> next l lSum true gbSize tail + | '!' :: _ :: tail when gb -> next l lSum gb gbSize tail + | '>' :: tail when gb -> next l lSum false gbSize tail + | _ :: tail -> next l lSum gb (gbSize + if gb then 1 else 0) tail + | [] -> lSum, gbSize + List.ofSeq input |> next 1 0 false 0 \ No newline at end of file diff --git a/AdventOfCode2017/Day1.fs b/AdventOfCode2017/Day1.fs deleted file mode 100644 index 79e523b..0000000 --- a/AdventOfCode2017/Day1.fs +++ /dev/null @@ -1,12 +0,0 @@ -module AdventOfCode2017.Day1 - -let readDigit d = int d - int '0' - -let parseInput (str : string) : int[] = str.ToCharArray () |> Array.map readDigit - -let solveCaptcha (shift : int) (captcha : int[]) = - let l = captcha.Length - [ for i in 0 .. l - 1 -> if captcha.[i] = captcha.[(i + shift) % l] then captcha.[i] else 0 ] |> List.sum - -let solveCaptcha1 = solveCaptcha 1 -let solveCaptcha2 (captcha : int[]) = solveCaptcha (captcha.Length / 2) captcha diff --git a/AdventOfCode2017/Day2.fs b/AdventOfCode2017/Day2.fs deleted file mode 100644 index bd91ef4..0000000 --- a/AdventOfCode2017/Day2.fs +++ /dev/null @@ -1,21 +0,0 @@ -module AdventOfCode2017.Day2 - -open System - -let parseInput (str : string) : int[][] = - str.Split ([| '\n' |], StringSplitOptions.RemoveEmptyEntries) - |> Array.map (fun line -> line.Split ([| ' '; '\t' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int) - -let checksum1 (a : int[][]) = - a |> Array.sumBy (fun ns -> Array.max ns - Array.min ns) - -let checksum2 (a : int[][]) = - a - |> Array.sumBy ( - fun ns -> - seq { - for a in ns do - for b in ns do - if a <> b && a % b = 0 then yield a / b - } |> Seq.head - ) diff --git a/AdventOfCode2017/Day3.fs b/AdventOfCode2017/Day3.fs deleted file mode 100644 index 71e57b6..0000000 --- a/AdventOfCode2017/Day3.fs +++ /dev/null @@ -1,33 +0,0 @@ -module AdventOfCode2017.Day3 - -let directions = [| 1, 0; 0, 1; -1, 0; 0, -1 |] - -let spiral = - Seq.unfold ( - fun (pos, dir, n, i) -> - let x, y = directions.[dir] - let pos' = fst pos + x, snd pos + y - let nMax = (i + 1) * 2 - 1 - let i', n' = if n = nMax then i + 1, 0 else i, n + 1 - let dir' = if i <> i' || n' = nMax / 2 + 1 then (dir + 1) % 4 else dir - Some (pos, (pos', dir', n', i')) - ) ((0, 0), 0, 0, 0) - -let spiralManhattanDistanceSum (n : int) = - let x, y = spiral |> Seq.item (n - 1) - abs x + abs y - -let spiralAdjacentSumBiggerThan (n : int) = - let neighborsSum (dic : Map) (pos : int * int) = - let x, y = pos - [ for dx in -1 .. 1 do for dy in -1 .. 1 -> x + dx, y + dy ] - |> List.sumBy (fun (x, y) -> match dic |> Map.tryFind (x, y) with Some v -> v | None -> 0) - - spiral - |> Seq.skip 1 - |> Seq.scan ( - fun (_sum, dic) pos -> - let sum = neighborsSum dic pos - sum, dic |> Map.add pos sum - ) (1, Map.empty |> Map.add (0, 0) 1) - |> Seq.pick (fun (sum, _) -> if sum > n then Some sum else None) \ No newline at end of file diff --git a/AdventOfCode2017/Day4.fs b/AdventOfCode2017/Day4.fs deleted file mode 100644 index fdab92f..0000000 --- a/AdventOfCode2017/Day4.fs +++ /dev/null @@ -1,15 +0,0 @@ -module AdventOfCode2017.Day4 - -let forallDistinctPairs (f : string -> string -> bool) (pp : string) = - let words = pp.Split ' ' - [ - for a = 0 to words.Length - 1 do - for b in a + 1 .. words.Length - 1 -> f words.[a] words.[b] - ] |> List.forall not - -let isAnagram w1 w2 = Seq.compareWith compare (Seq.sort w1) (Seq.sort w2) = 0 - -let passphraseValid = forallDistinctPairs (=) -let passphraseValidAnagram = forallDistinctPairs isAnagram - -let nbPassphrasesValid (f : string -> bool) = Seq.map f >> Seq.sumBy (fun v -> if v then 1 else 0) \ No newline at end of file diff --git a/AdventOfCode2017/Day5.fs b/AdventOfCode2017/Day5.fs deleted file mode 100644 index dea50e3..0000000 --- a/AdventOfCode2017/Day5.fs +++ /dev/null @@ -1,19 +0,0 @@ -module AdventOfCode2017.Day5 - -open System - -let parseInput (str : string) : int[] = - str.Split ([| '\r'; '\t'; ' ' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int - -let nbSteps (next : int -> int) (instructions : int[]) = - let is = instructions.[*] - let mutable cursor, steps = 0, 0 - while cursor >= 0 && cursor < instructions.Length do - let i = is.[cursor] - is.[cursor] <- next is.[cursor] - cursor <- cursor + i - steps <- steps + 1 - steps - -let nbSteps1 = nbSteps ((+) 1) -let nbSteps2 = nbSteps (fun i -> if i >= 3 then i - 1 else i + 1) \ No newline at end of file diff --git a/AdventOfCode2017/Day6.fs b/AdventOfCode2017/Day6.fs deleted file mode 100644 index 113c47d..0000000 --- a/AdventOfCode2017/Day6.fs +++ /dev/null @@ -1,37 +0,0 @@ -module AdventOfCode2017.Day6 - -open System - -let parseInput (str : string) : int[] = - str.Split ([| '\r'; '\t'; ' ' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int - -// Custom equality: more efficient than '='. -let inline (|=|) (a1 : 'a[]) (a2 : 'a[]) : bool = - if a1.Length <> a2.Length then - false - else - let rec result i = - if i = a1.Length then - true - elif a1.[i] <> a2.[i] then - false - else - result (i + 1) - result 0 - -let nbRedistribution (blocks : int[]) = - let rec next (previous : int[] list) = - let blocks = List.head previous |> Array.copy - let i, max = blocks |> Array.indexed |> Array.maxBy snd - blocks.[i] <- 0 - for offset = i + 1 to i + max do - let i' = offset % blocks.Length - blocks.[i'] <- blocks.[i'] + 1 - - match previous |> List.tryFindIndex ((|=|) blocks) with - | Some i -> previous, i + 1 - | None -> next (blocks :: previous) - - let blockList, cycleLength = next [ blocks ] - - List.length blockList, cycleLength diff --git a/AdventOfCode2017/Day7.fs b/AdventOfCode2017/Day7.fs deleted file mode 100644 index 6f312f7..0000000 --- a/AdventOfCode2017/Day7.fs +++ /dev/null @@ -1,46 +0,0 @@ -module AdventOfCode2017.Day7 - -open System -open System.Linq -open System.Collections.Generic - -type Tower = - { - Name : string - Weight : int - Above : List - } - -type Input = (Tower * string list) list - -let parseInput (lines : string list) : Input = - lines - |> List.map ( - fun line -> - let items = line.Split ([| '\r'; '\t'; ' '; ','; ')'; '(' |], StringSplitOptions.RemoveEmptyEntries) - { Name = items.[0]; Weight = int items.[1]; Above = List () }, [ for i in 3 .. items.Length - 1 -> items.[i] ] - ) - -let buildTower (input : Input) : Tower = - let towers = Dictionary () - - for tower, _ in input do - towers.Add (tower.Name, tower) - - for tower, towersAbove in input do - for towerAbove in towersAbove do - tower.Above.Add towers.[towerAbove] - towers.Remove towerAbove |> ignore - - towers.First().Value - -// Returns the tower and its corrected weight. -let rec findUnbalanced (tower : Tower) : (Tower * int) option = - let rec weight tower = - tower.Weight + (tower.Above |> Seq.sumBy weight) - - match tower.Above |> List.ofSeq |> List.groupBy weight |> List.sortBy (snd >> List.length) with - | [ w1, [ unbalanced ]; w2, _ ] -> - findUnbalanced unbalanced |> Option.orElse (Some (unbalanced, unbalanced.Weight + w2 - w1)) - | _ -> - None \ No newline at end of file diff --git a/AdventOfCode2017/Day8.fs b/AdventOfCode2017/Day8.fs deleted file mode 100644 index b9f6789..0000000 --- a/AdventOfCode2017/Day8.fs +++ /dev/null @@ -1,27 +0,0 @@ -module AdventOfCode2017.Day8 - -open System - -type Instruction = string * string * int * string * (int -> int -> bool) * int - -let parseInput (lines : string[]) : Instruction list = - lines - |> List.ofArray - |> List.map ( - fun line -> - let line = line.Split ' ' - line.[0], line.[1], int line.[2], line.[4], (match line.[5] with ">" -> (>) | "<" -> (<) | ">=" -> (>=) | "<=" -> (<=) | "!=" -> (<>) | "==" | _ -> (=)), int line.[6] - ) - -let execute (input : Instruction list) : int * int = - let highest, register = - input - |> List.fold ( - fun (highest, register) (reg, ins, value, regCond, op, valueCond) -> - if op (register |> Map.tryFind regCond |> Option.defaultValue 0) valueCond then - let regValue' = (register |> Map.tryFind reg |> Option.defaultValue 0) + match ins with "inc" -> value | "dec" -> -value | _ -> 0 - max highest regValue', register |> Map.add reg regValue' - else - highest, register - ) (Int32.MinValue, Map.empty) - highest, (register |> Map.toList |> List.map snd |> List.max) diff --git a/AdventOfCode2017/Day9.fs b/AdventOfCode2017/Day9.fs deleted file mode 100644 index 0931607..0000000 --- a/AdventOfCode2017/Day9.fs +++ /dev/null @@ -1,13 +0,0 @@ -module AdventOfCode2017.Day9 - -let score (input : string) = - let rec next (l : int) (lSum : int) (gb : bool) (gbSize : int) = - function - | '{' :: tail when not gb -> next (l + 1) (lSum + l) gb gbSize tail - | '}' :: tail when not gb -> next (l - 1) lSum gb gbSize tail - | '<' :: tail when not gb -> next l lSum true gbSize tail - | '!' :: _ :: tail when gb -> next l lSum gb gbSize tail - | '>' :: tail when gb -> next l lSum false gbSize tail - | _ :: tail -> next l lSum gb (gbSize + if gb then 1 else 0) tail - | [] -> lSum, gbSize - List.ofSeq input |> next 1 0 false 0 \ No newline at end of file diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index d69d9bb..fb0db9c 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -3,48 +3,48 @@ open System.IO open System -let day1 () = - let captcha = File.ReadAllText "Data/day1.input" |> Day1.parseInput - sprintf "part1 = %A, part2 = %A" (Day1.solveCaptcha1 captcha) (Day1.solveCaptcha2 captcha) +let day01 () = + let captcha = File.ReadAllText "Data/day01.input" |> Day01.parseInput + sprintf "part1 = %A, part2 = %A" (Day01.solveCaptcha1 captcha) (Day01.solveCaptcha2 captcha) -let day2 () = - let array = File.ReadAllText "Data/day2.input" |> Day2.parseInput - sprintf "part1 = %A, part2 = %A" (Day2.checksum1 array) (Day2.checksum2 array) +let day02 () = + let array = File.ReadAllText "Data/day02.input" |> Day02.parseInput + sprintf "part1 = %A, part2 = %A" (Day02.checksum1 array) (Day02.checksum2 array) -let day3 () = - let input = 325489 - sprintf "part1 = %A, part2 = %A" (Day3.spiralManhattanDistanceSum input) (Day3.spiralAdjacentSumBiggerThan input) +let day03 () = + let input = File.ReadAllText "Data/day03.input" |> int + sprintf "part1 = %A, part2 = %A" (Day03.spiralManhattanDistanceSum input) (Day03.spiralAdjacentSumBiggerThan input) -let day4 () = - let input = File.ReadAllLines "Data/day4.input" - sprintf "part1 = %A, part2 = %A" (Day4.nbPassphrasesValid Day4.passphraseValid input) (Day4.nbPassphrasesValid Day4.passphraseValidAnagram input) +let day04 () = + let input = File.ReadAllLines "Data/day04.input" + sprintf "part1 = %A, part2 = %A" (Day04.nbPassphrasesValid Day04.passphraseValid input) (Day04.nbPassphrasesValid Day04.passphraseValidAnagram input) -let day5 () = - let input = File.ReadAllText "Data/day5.input" |> Day5.parseInput - sprintf "part1 = %A, part2 = %A" (Day5.nbSteps1 input) (Day5.nbSteps2 input) +let day05 () = + let input = File.ReadAllText "Data/day05.input" |> Day05.parseInput + sprintf "part1 = %A, part2 = %A" (Day05.nbSteps1 input) (Day05.nbSteps2 input) -let day6 () = - let input = File.ReadAllText "Data/day6.input" |> Day6.parseInput - let part1, part2 = Day6.nbRedistribution input +let day06 () = + let input = File.ReadAllText "Data/day06.input" |> Day06.parseInput + let part1, part2 = Day06.nbRedistribution input sprintf "part1 = %A, part2 = %A" part1 part2 -let day7 () = - let input = File.ReadAllLines "Data/day7.input" |> List.ofArray |> Day7.parseInput - let tower = Day7.buildTower input - sprintf "part1 = %A, part2 = %A" tower.Name (Day7.findUnbalanced tower |> Option.map (fun (t, w) -> t.Name, w)) +let day07 () = + let input = File.ReadAllLines "Data/day07.input" |> List.ofArray |> Day07.parseInput + let tower = Day07.buildTower input + sprintf "part1 = %A, part2 = %A" tower.Name (Day07.findUnbalanced tower |> Option.map (fun (t, w) -> t.Name, w)) -let day8 () = - let input = File.ReadAllLines "Data/day8.input" |> Day8.parseInput - let part1, part2 = Day8.execute input +let day08 () = + let input = File.ReadAllLines "Data/day08.input" |> Day08.parseInput + let part1, part2 = Day08.execute input sprintf "part1 = %A, part2 = %A" part1 part2 -let day9 () = - let input = File.ReadAllText "Data/day9.input" - let part1, part2 = Day9.score input +let day09 () = + let input = File.ReadAllText "Data/day09.input" + let part1, part2 = Day09.score input sprintf "part1 = %A, part2 = %A" part1 part2 let day10 () = - let input = "83,0,193,1,254,237,187,40,88,27,2,255,149,29,42,100" + let input = File.ReadAllText "Data/day10.input" sprintf "part1 = %A, part2 = %A" (Day10.knotHash1 input 256) (Day10.knotHash2 input) let day11 () = @@ -76,15 +76,15 @@ let doDay (n : int) = sw.Start () let result = match n with - | 1 -> day1 () - | 2 -> day2 () - | 3 -> day3 () - | 4 -> day4 () - | 5 -> day5 () - | 6 -> day6 () - | 7 -> day7 () - | 8 -> day8 () - | 9 -> day9 () + | 1 -> day01 () + | 2 -> day02 () + | 3 -> day03 () + | 4 -> day04 () + | 5 -> day05 () + | 6 -> day06 () + | 7 -> day07 () + | 8 -> day08 () + | 9 -> day09 () | 10 -> day10 () | 11 -> day11 () | 12 -> day12 () diff --git a/Tests/Day01 tests.fs b/Tests/Day01 tests.fs new file mode 100644 index 0000000..543c902 --- /dev/null +++ b/Tests/Day01 tests.fs @@ -0,0 +1,24 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day01 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + Day01.solveCaptcha1 (Day01.parseInput "1122") =! 3 + Day01.solveCaptcha1 (Day01.parseInput "1111") =! 4 + Day01.solveCaptcha1 (Day01.parseInput "1234") =! 0 + Day01.solveCaptcha1 (Day01.parseInput "91212129") =! 9 + + [] + let ``(Part2) From web page`` () = + Day01.solveCaptcha2 (Day01.parseInput "1212") =! 6 + Day01.solveCaptcha2 (Day01.parseInput "1221") =! 0 + Day01.solveCaptcha2 (Day01.parseInput "123425") =! 4 + Day01.solveCaptcha2 (Day01.parseInput "123123") =! 12 + Day01.solveCaptcha2 (Day01.parseInput "12131415") =! 4 diff --git a/Tests/Day02 tests.fs b/Tests/Day02 tests.fs new file mode 100644 index 0000000..df49709 --- /dev/null +++ b/Tests/Day02 tests.fs @@ -0,0 +1,25 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day02 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + let input = + "5 1 9 5 + 7 5 3 + 2 4 6 8" + Day02.checksum1 (Day02.parseInput input) =! 18 + + [] + let ``(Part2) From web page`` () = + let input = + "5 9 2 8 + 9 4 7 3 + 3 8 6 5" + Day02.checksum2 (Day02.parseInput input) =! 9 \ No newline at end of file diff --git a/Tests/Day03 tests.fs b/Tests/Day03 tests.fs new file mode 100644 index 0000000..bd057e7 --- /dev/null +++ b/Tests/Day03 tests.fs @@ -0,0 +1,26 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day03 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + Day03.spiralManhattanDistanceSum 12 =! 3 + Day03.spiralManhattanDistanceSum 23 =! 2 + Day03.spiralManhattanDistanceSum 1024 =! 31 + + [] + let ``(Part2) From web page`` () = + Day03.spiralAdjacentSumBiggerThan 1 =! 2 + Day03.spiralAdjacentSumBiggerThan 2 =! 4 + Day03.spiralAdjacentSumBiggerThan 3 =! 4 + Day03.spiralAdjacentSumBiggerThan 4 =! 5 + Day03.spiralAdjacentSumBiggerThan 5 =! 10 + Day03.spiralAdjacentSumBiggerThan 20 =! 23 + Day03.spiralAdjacentSumBiggerThan 100 =! 122 + Day03.spiralAdjacentSumBiggerThan 500 =! 747 \ No newline at end of file diff --git a/Tests/Day04 tests.fs b/Tests/Day04 tests.fs new file mode 100644 index 0000000..4278661 --- /dev/null +++ b/Tests/Day04 tests.fs @@ -0,0 +1,23 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day04 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + Assert.True (Day04.passphraseValid "aa bb cc dd ee") + Assert.False (Day04.passphraseValid "aa bb cc dd aa") + Assert.True (Day04.passphraseValid "aa bb cc dd aaa") + + [] + let ``(Part2) From web page`` () = + Assert.True (Day04.passphraseValidAnagram "abcde fghij") + Assert.False (Day04.passphraseValidAnagram "abcde xyz ecdab") + Assert.True (Day04.passphraseValidAnagram "a ab abc abd abf abj") + Assert.True (Day04.passphraseValidAnagram "iiii oiii ooii oooi oooo") + Assert.False (Day04.passphraseValidAnagram "oiii ioii iioi iiio") \ No newline at end of file diff --git a/Tests/Day05 tests.fs b/Tests/Day05 tests.fs new file mode 100644 index 0000000..3c3dd9d --- /dev/null +++ b/Tests/Day05 tests.fs @@ -0,0 +1,17 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day05 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + Day05.nbSteps1 [| 0; 3; 0; 1; -3 |] =! 5 + + [] + let ``(Part2) From web page`` () = + Day05.nbSteps2 [| 0; 3; 0; 1; -3 |] =! 10 \ No newline at end of file diff --git a/Tests/Day06 tests.fs b/Tests/Day06 tests.fs new file mode 100644 index 0000000..b362298 --- /dev/null +++ b/Tests/Day06 tests.fs @@ -0,0 +1,19 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day06 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + let l, _ = Day06.nbRedistribution [| 0; 2; 7; 0 |] + l =! 5 + + [] + let ``(Part2) From web page`` () = + let _, cycleLength = Day06.nbRedistribution [| 0; 2; 7; 0 |] + cycleLength =! 4 \ No newline at end of file diff --git a/Tests/Day07 tests.fs b/Tests/Day07 tests.fs new file mode 100644 index 0000000..eed8195 --- /dev/null +++ b/Tests/Day07 tests.fs @@ -0,0 +1,78 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day07 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + let input = + [ + "pbga (66)" + "xhth (57)" + "ebii (61)" + "havc (66)" + "ktlj (57)" + "fwft (72) -> ktlj, cntj, xhth" + "qoyq (66)" + "padx (45) -> pbga, havc, qoyq" + "tknk (41) -> ugml, padx, fwft" + "jptl (61)" + "ugml (68) -> gyxo, ebii, jptl" + "gyxo (61)" + "cntj (57)" + ] + let tower = Day07.buildTower (Day07.parseInput input) + tower.Name =! "tknk" + + [] + let ``(Part2) From web page`` () = + let input = + [ + "pbga (66)" + "xhth (57)" + "ebii (61)" + "havc (66)" + "ktlj (57)" + "fwft (72) -> ktlj, cntj, xhth" + "qoyq (66)" + "padx (45) -> pbga, havc, qoyq" + "tknk (41) -> ugml, padx, fwft" + "jptl (61)" + "ugml (68) -> gyxo, ebii, jptl" + "gyxo (61)" + "cntj (57)" + ] + let tower = Day07.buildTower (Day07.parseInput input) + + match Day07.findUnbalanced tower with + | Some (tower, weight) -> + tower.Name =! "ugml" + weight =! 60 + | None -> failwith "no tower found" + + [] + let ``(Part2) A balanced tree`` () = + let input = + [ + "pbga (66)" + "xhth (57)" + "ebii (61)" + "havc (66)" + "ktlj (57)" + "fwft (72) -> ktlj, cntj, xhth" + "qoyq (66)" + "padx (45) -> pbga, havc, qoyq" + "tknk (41) -> ugml, padx, fwft" + "jptl (61)" + "ugml (60) -> gyxo, ebii, jptl" + "gyxo (61)" + "cntj (57)" + ] + let tower = Day07.buildTower (Day07.parseInput input) + + Day07.findUnbalanced tower =! None \ No newline at end of file diff --git a/Tests/Day08 tests.fs b/Tests/Day08 tests.fs new file mode 100644 index 0000000..9892fbf --- /dev/null +++ b/Tests/Day08 tests.fs @@ -0,0 +1,33 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day08 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + let input = + [| + "b inc 5 if a > 1" + "a inc 1 if b < 5" + "c dec -10 if a >= 1" + "c inc -20 if c == 10" + |] + let p1, _ = Day08.execute (Day08.parseInput input) + p1 = 1 + + [] + let ``(Part2) From web page`` () = + let input = + [| + "b inc 5 if a > 1" + "a inc 1 if b < 5" + "c dec -10 if a >= 1" + "c inc -20 if c == 10" + |] + let _, p2 = Day08.execute (Day08.parseInput input) + p2 = 10 \ No newline at end of file diff --git a/Tests/Day09 tests.fs b/Tests/Day09 tests.fs new file mode 100644 index 0000000..798cf99 --- /dev/null +++ b/Tests/Day09 tests.fs @@ -0,0 +1,30 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day09 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + Day09.score "{}" |> fst =! 1 + Day09.score "{{{}}}" |> fst =! 6 + Day09.score "{{},{}}" |> fst =! 5 + Day09.score "{{{},{},{{}}}}" |> fst =! 16 + Day09.score "{,,,}" |> fst =! 1 + Day09.score "{{},{},{},{}}" |> fst =! 9 + Day09.score "{{},{},{},{}}" |> fst =! 9 + Day09.score "{{},{},{},{}}" |> fst =! 3 + + [] + let ``(Part2) From web page`` () = + Day09.score "<>" |> snd =! 0 + Day09.score "" |> snd =! 17 + Day09.score "<<<<>" |> snd =! 3 + Day09.score "<{!>}>" |> snd =! 2 + Day09.score "" |> snd =! 0 + Day09.score ">" |> snd =! 0 + Day09.score """<{o"i!a,<{i""" |> snd =! 10 \ No newline at end of file diff --git a/Tests/Day1 tests.fs b/Tests/Day1 tests.fs deleted file mode 100644 index f367c21..0000000 --- a/Tests/Day1 tests.fs +++ /dev/null @@ -1,24 +0,0 @@ -namespace AdventOfCode2017.Tests - -open Xunit -open Xunit.Abstractions -open Swensen.Unquote - -open AdventOfCode2017 - -type ``Day1 tests`` (output : ITestOutputHelper) = - - [] - let ``(Part1) From web page`` () = - Day1.solveCaptcha1 (Day1.parseInput "1122") =! 3 - Day1.solveCaptcha1 (Day1.parseInput "1111") =! 4 - Day1.solveCaptcha1 (Day1.parseInput "1234") =! 0 - Day1.solveCaptcha1 (Day1.parseInput "91212129") =! 9 - - [] - let ``(Part2) From web page`` () = - Day1.solveCaptcha2 (Day1.parseInput "1212") =! 6 - Day1.solveCaptcha2 (Day1.parseInput "1221") =! 0 - Day1.solveCaptcha2 (Day1.parseInput "123425") =! 4 - Day1.solveCaptcha2 (Day1.parseInput "123123") =! 12 - Day1.solveCaptcha2 (Day1.parseInput "12131415") =! 4 diff --git a/Tests/Day2 tests.fs b/Tests/Day2 tests.fs deleted file mode 100644 index 18bedfc..0000000 --- a/Tests/Day2 tests.fs +++ /dev/null @@ -1,25 +0,0 @@ -namespace AdventOfCode2017.Tests - -open Xunit -open Xunit.Abstractions -open Swensen.Unquote - -open AdventOfCode2017 - -type ``Day2 tests`` (output : ITestOutputHelper) = - - [] - let ``(Part1) From web page`` () = - let input = - "5 1 9 5 - 7 5 3 - 2 4 6 8" - Day2.checksum1 (Day2.parseInput input) =! 18 - - [] - let ``(Part2) From web page`` () = - let input = - "5 9 2 8 - 9 4 7 3 - 3 8 6 5" - Day2.checksum2 (Day2.parseInput input) =! 9 \ No newline at end of file diff --git a/Tests/Day3 tests.fs b/Tests/Day3 tests.fs deleted file mode 100644 index c0a5d46..0000000 --- a/Tests/Day3 tests.fs +++ /dev/null @@ -1,26 +0,0 @@ -namespace AdventOfCode2017.Tests - -open Xunit -open Xunit.Abstractions -open Swensen.Unquote - -open AdventOfCode2017 - -type ``Day3 tests`` (output : ITestOutputHelper) = - - [] - let ``(Part1) From web page`` () = - Day3.spiralManhattanDistanceSum 12 =! 3 - Day3.spiralManhattanDistanceSum 23 =! 2 - Day3.spiralManhattanDistanceSum 1024 =! 31 - - [] - let ``(Part2) From web page`` () = - Day3.spiralAdjacentSumBiggerThan 1 =! 2 - Day3.spiralAdjacentSumBiggerThan 2 =! 4 - Day3.spiralAdjacentSumBiggerThan 3 =! 4 - Day3.spiralAdjacentSumBiggerThan 4 =! 5 - Day3.spiralAdjacentSumBiggerThan 5 =! 10 - Day3.spiralAdjacentSumBiggerThan 20 =! 23 - Day3.spiralAdjacentSumBiggerThan 100 =! 122 - Day3.spiralAdjacentSumBiggerThan 500 =! 747 \ No newline at end of file diff --git a/Tests/Day4 tests.fs b/Tests/Day4 tests.fs deleted file mode 100644 index 3057786..0000000 --- a/Tests/Day4 tests.fs +++ /dev/null @@ -1,23 +0,0 @@ -namespace AdventOfCode2017.Tests - -open Xunit -open Xunit.Abstractions -open Swensen.Unquote - -open AdventOfCode2017 - -type ``Day4 tests`` (output : ITestOutputHelper) = - - [] - let ``(Part1) From web page`` () = - Assert.True (Day4.passphraseValid "aa bb cc dd ee") - Assert.False (Day4.passphraseValid "aa bb cc dd aa") - Assert.True (Day4.passphraseValid "aa bb cc dd aaa") - - [] - let ``(Part2) From web page`` () = - Assert.True (Day4.passphraseValidAnagram "abcde fghij") - Assert.False (Day4.passphraseValidAnagram "abcde xyz ecdab") - Assert.True (Day4.passphraseValidAnagram "a ab abc abd abf abj") - Assert.True (Day4.passphraseValidAnagram "iiii oiii ooii oooi oooo") - Assert.False (Day4.passphraseValidAnagram "oiii ioii iioi iiio") \ No newline at end of file diff --git a/Tests/Day5 tests.fs b/Tests/Day5 tests.fs deleted file mode 100644 index 3ddf954..0000000 --- a/Tests/Day5 tests.fs +++ /dev/null @@ -1,17 +0,0 @@ -namespace AdventOfCode2017.Tests - -open Xunit -open Xunit.Abstractions -open Swensen.Unquote - -open AdventOfCode2017 - -type ``Day5 tests`` (output : ITestOutputHelper) = - - [] - let ``(Part1) From web page`` () = - Day5.nbSteps1 [| 0; 3; 0; 1; -3 |] =! 5 - - [] - let ``(Part2) From web page`` () = - Day5.nbSteps2 [| 0; 3; 0; 1; -3 |] =! 10 \ No newline at end of file diff --git a/Tests/Day6 tests.fs b/Tests/Day6 tests.fs deleted file mode 100644 index 9d2d0ad..0000000 --- a/Tests/Day6 tests.fs +++ /dev/null @@ -1,19 +0,0 @@ -namespace AdventOfCode2017.Tests - -open Xunit -open Xunit.Abstractions -open Swensen.Unquote - -open AdventOfCode2017 - -type ``Day6 tests`` (output : ITestOutputHelper) = - - [] - let ``(Part1) From web page`` () = - let l, _ = Day6.nbRedistribution [| 0; 2; 7; 0 |] - l =! 5 - - [] - let ``(Part2) From web page`` () = - let _, cycleLength = Day6.nbRedistribution [| 0; 2; 7; 0 |] - cycleLength =! 4 \ No newline at end of file diff --git a/Tests/Day7 tests.fs b/Tests/Day7 tests.fs deleted file mode 100644 index 0565ab1..0000000 --- a/Tests/Day7 tests.fs +++ /dev/null @@ -1,78 +0,0 @@ -namespace AdventOfCode2017.Tests - -open Xunit -open Xunit.Abstractions -open Swensen.Unquote - -open AdventOfCode2017 - -type ``Day7 tests`` (output : ITestOutputHelper) = - - [] - let ``(Part1) From web page`` () = - let input = - [ - "pbga (66)" - "xhth (57)" - "ebii (61)" - "havc (66)" - "ktlj (57)" - "fwft (72) -> ktlj, cntj, xhth" - "qoyq (66)" - "padx (45) -> pbga, havc, qoyq" - "tknk (41) -> ugml, padx, fwft" - "jptl (61)" - "ugml (68) -> gyxo, ebii, jptl" - "gyxo (61)" - "cntj (57)" - ] - let tower = Day7.buildTower (Day7.parseInput input) - tower.Name =! "tknk" - - [] - let ``(Part2) From web page`` () = - let input = - [ - "pbga (66)" - "xhth (57)" - "ebii (61)" - "havc (66)" - "ktlj (57)" - "fwft (72) -> ktlj, cntj, xhth" - "qoyq (66)" - "padx (45) -> pbga, havc, qoyq" - "tknk (41) -> ugml, padx, fwft" - "jptl (61)" - "ugml (68) -> gyxo, ebii, jptl" - "gyxo (61)" - "cntj (57)" - ] - let tower = Day7.buildTower (Day7.parseInput input) - - match Day7.findUnbalanced tower with - | Some (tower, weight) -> - tower.Name =! "ugml" - weight =! 60 - | None -> failwith "no tower found" - - [] - let ``(Part2) A balanced tree`` () = - let input = - [ - "pbga (66)" - "xhth (57)" - "ebii (61)" - "havc (66)" - "ktlj (57)" - "fwft (72) -> ktlj, cntj, xhth" - "qoyq (66)" - "padx (45) -> pbga, havc, qoyq" - "tknk (41) -> ugml, padx, fwft" - "jptl (61)" - "ugml (60) -> gyxo, ebii, jptl" - "gyxo (61)" - "cntj (57)" - ] - let tower = Day7.buildTower (Day7.parseInput input) - - Day7.findUnbalanced tower =! None \ No newline at end of file diff --git a/Tests/Day8 tests.fs b/Tests/Day8 tests.fs deleted file mode 100644 index 823ca8d..0000000 --- a/Tests/Day8 tests.fs +++ /dev/null @@ -1,33 +0,0 @@ -namespace AdventOfCode2017.Tests - -open Xunit -open Xunit.Abstractions -open Swensen.Unquote - -open AdventOfCode2017 - -type ``Day8 tests`` (output : ITestOutputHelper) = - - [] - let ``(Part1) From web page`` () = - let input = - [| - "b inc 5 if a > 1" - "a inc 1 if b < 5" - "c dec -10 if a >= 1" - "c inc -20 if c == 10" - |] - let p1, _ = Day8.execute (Day8.parseInput input) - p1 = 1 - - [] - let ``(Part2) From web page`` () = - let input = - [| - "b inc 5 if a > 1" - "a inc 1 if b < 5" - "c dec -10 if a >= 1" - "c inc -20 if c == 10" - |] - let _, p2 = Day8.execute (Day8.parseInput input) - p2 = 10 \ No newline at end of file diff --git a/Tests/Day9 tests.fs b/Tests/Day9 tests.fs deleted file mode 100644 index 8f60226..0000000 --- a/Tests/Day9 tests.fs +++ /dev/null @@ -1,30 +0,0 @@ -namespace AdventOfCode2017.Tests - -open Xunit -open Xunit.Abstractions -open Swensen.Unquote - -open AdventOfCode2017 - -type ``Day9 tests`` (output : ITestOutputHelper) = - - [] - let ``(Part1) From web page`` () = - Day9.score "{}" |> fst =! 1 - Day9.score "{{{}}}" |> fst =! 6 - Day9.score "{{},{}}" |> fst =! 5 - Day9.score "{{{},{},{{}}}}" |> fst =! 16 - Day9.score "{,,,}" |> fst =! 1 - Day9.score "{{},{},{},{}}" |> fst =! 9 - Day9.score "{{},{},{},{}}" |> fst =! 9 - Day9.score "{{},{},{},{}}" |> fst =! 3 - - [] - let ``(Part2) From web page`` () = - Day9.score "<>" |> snd =! 0 - Day9.score "" |> snd =! 17 - Day9.score "<<<<>" |> snd =! 3 - Day9.score "<{!>}>" |> snd =! 2 - Day9.score "" |> snd =! 0 - Day9.score ">" |> snd =! 0 - Day9.score """<{o"i!a,<{i""" |> snd =! 10 \ No newline at end of file diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index 96bd24e..55387ec 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -55,15 +55,15 @@ - - - - - - - - - + + + + + + + + +