├── .gitignore ├── AoC ├── Basic.lean ├── AoC_2017 │ ├── day5.lean │ ├── day1.lean │ ├── day2.lean │ └── day4.lean ├── AoC_2015 │ ├── day1.lean │ ├── day2.lean │ ├── day5.lean │ ├── day3.lean │ └── day6.lean ├── AoC_2018 │ ├── day1.lean │ ├── day5.lean │ ├── day2.lean │ ├── day6.lean │ └── day3.lean ├── AoC_2019 │ └── day1.lean ├── AoC_2020 │ ├── day3.lean │ ├── day1.lean │ └── day2.lean └── AoC_2021 │ ├── day1.lean │ └── day2.lean ├── lean-toolchain ├── Fad ├── Basic.lean ├── Chapter8-Ex.lean ├── Chapter9.lean ├── Chapter9-Ex.lean ├── Chapter2-Ex.lean ├── Chapter12.lean ├── Chapter2.lean ├── Chapter12-Ex.lean ├── Chapter7-Ex.lean ├── Chapter6.lean ├── Chapter7.lean ├── Chapter4-Ex.lean ├── Chapter3-Ex.lean ├── Chapter5.lean ├── Chapter1-Ex.lean ├── Chapter8.lean ├── Chapter4.lean ├── Chapter5-Ex.lean └── Chapter1.lean ├── README.md ├── Main.lean ├── lakefile.lean ├── data ├── AoC2018_day6.txt ├── AoC2017_day2.txt ├── AoC2019_day1.txt ├── AoC2017_day1.txt ├── AoC2020_day1.txt ├── AoC2015_day1.txt ├── AoC2015_day3.txt ├── AoC2018_day2.txt ├── AoC2018_day1.txt ├── AoC2015_day6.txt ├── AoC2017_day5.txt ├── AoC2020_day3.txt └── AoC2021_day2.txt ├── Fad.lean ├── AoC.lean └── lake-manifest.json /.gitignore: -------------------------------------------------------------------------------- 1 | /.lake 2 | **/.DS_Store -------------------------------------------------------------------------------- /AoC/Basic.lean: -------------------------------------------------------------------------------- 1 | 2 | /- to utitilites -/ 3 | -------------------------------------------------------------------------------- /lean-toolchain: -------------------------------------------------------------------------------- 1 | leanprover/lean4:v4.15.0-rc1 2 | -------------------------------------------------------------------------------- /Fad/Basic.lean: -------------------------------------------------------------------------------- 1 | 2 | /- basic definitions can be copied here -/ 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Functional Algorithms Design 2 | 3 | This repo will contain the implementation in Lean of the Haskell codes from [Algorithm Design with Haskell](https://www.amazon.com/Algorithm-Design-Haskell-Richard-Bird/dp/1108491618) 4 | 5 | It may also contains more ideas! 6 | 7 | -------------------------------------------------------------------------------- /Main.lean: -------------------------------------------------------------------------------- 1 | import Fad 2 | import AoC 3 | 4 | /- 5 | def main : IO Unit := 6 | IO.println s!"Hello, Alexandre!" 7 | -/ 8 | 9 | open AoC2018D6 in 10 | 11 | def main : IO Unit := do 12 | IO.println s!"Part 1: {largest input}" 13 | IO.println s!"Part 2: {safe_size input 10000}" 14 | -------------------------------------------------------------------------------- /lakefile.lean: -------------------------------------------------------------------------------- 1 | import Lake 2 | open Lake DSL 3 | 4 | package "fad" where 5 | -- add package configuration options here 6 | 7 | lean_lib «Fad» where 8 | -- add library configuration options here 9 | 10 | lean_lib «AoC» where 11 | -- add library configuration options here 12 | 13 | require "leanprover-community" / "mathlib" 14 | 15 | @[default_target] 16 | lean_exe "fad" where 17 | root := `Main 18 | -------------------------------------------------------------------------------- /Fad/Chapter8-Ex.lean: -------------------------------------------------------------------------------- 1 | import Fad.Chapter8 2 | 3 | namespace Chapter8 4 | 5 | def splits : List α → List (List α × List α) 6 | | [] => [] 7 | | x :: xs => 8 | ([], x :: xs) :: (splits xs).map (fun (ys, zs) => (x :: ys, zs)) 9 | 10 | def splitsn : List α → List (List α × List α) 11 | | [] => [] 12 | | [_] => [] 13 | | x :: xs => 14 | ([x], xs) :: (splitsn xs).map (fun (ys, zs) => (x :: ys, zs)) 15 | 16 | 17 | end Chapter8 18 | -------------------------------------------------------------------------------- /Fad/Chapter9.lean: -------------------------------------------------------------------------------- 1 | 2 | namespace Chapter9 3 | 4 | /- 9.1 Graphs and spanning trees -/ 5 | 6 | abbrev Vertex := Nat 7 | abbrev Weight := Int 8 | abbrev Edge := (Vertex × Vertex × Weight) 9 | abbrev Graph := (List Vertex × List Edge) 10 | 11 | def nodes (g : Graph) : List Vertex := g.1 12 | def edges (g : Graph) : List Edge := g.2 13 | def souce (e : Edge) : Vertex := e.1 14 | def target (e : Edge) : Vertex := e.2.1 15 | def weight (e : Edge) : Weight := e.2.2 16 | 17 | abbrev AdjArray := Array (List (Vertex × Weight)) 18 | 19 | 20 | 21 | 22 | end Chapter9 23 | -------------------------------------------------------------------------------- /Fad/Chapter9-Ex.lean: -------------------------------------------------------------------------------- 1 | import Fad.Chapter9 2 | import Fad.Chapter3 3 | 4 | namespace Chapter9 5 | open Chapter3 (accumArray) 6 | 7 | def toAdj (g : Graph) : AdjArray := 8 | accumArray (flip List.cons) [] g.1.length (edges g) 9 | 10 | def toGraph (adj : AdjArray) : Graph := 11 | let vs := List.range adj.size 12 | let es : List Edge := adj.toList.enum |>.flatMap 13 | (λ (v, ps) => ps.map (λ p => (v, p.1, p.2))) 14 | (vs, es) 15 | 16 | def g : Graph := 17 | ([0, 1, 2, 3], 18 | [(0, 1, 1), (1, 2, 5), (1, 3, 10), (2, 3, 2), (3, 0, 4)]) 19 | 20 | 21 | 22 | end Chapter9 23 | -------------------------------------------------------------------------------- /data/AoC2018_day6.txt: -------------------------------------------------------------------------------- 1 | 45, 315 2 | 258, 261 3 | 336, 208 4 | 160, 322 5 | 347, 151 6 | 321, 243 7 | 232, 148 8 | 48, 202 9 | 78, 161 10 | 307, 230 11 | 170, 73 12 | 43, 73 13 | 74, 248 14 | 177, 296 15 | 330, 266 16 | 314, 272 17 | 175, 291 18 | 75, 142 19 | 278, 193 20 | 279, 337 21 | 228, 46 22 | 211, 164 23 | 131, 100 24 | 110, 338 25 | 336, 338 26 | 231, 353 27 | 184, 213 28 | 300, 56 29 | 99, 231 30 | 119, 159 31 | 180, 349 32 | 130, 193 33 | 308, 107 34 | 140, 40 35 | 222, 188 36 | 356, 44 37 | 73, 107 38 | 304, 313 39 | 199, 238 40 | 344, 158 41 | 49, 225 42 | 64, 117 43 | 145, 178 44 | 188, 265 45 | 270, 215 46 | 48, 181 47 | 213, 159 48 | 174, 311 49 | 114, 231 50 | 325, 162 51 | -------------------------------------------------------------------------------- /Fad.lean: -------------------------------------------------------------------------------- 1 | -- This module serves as the root of the `Fad` library. 2 | -- Import modules here that should be built as part of the library. 3 | import Fad.Basic 4 | 5 | import Fad.Chapter1 6 | import Fad.«Chapter1-Ex» 7 | 8 | import Fad.Chapter2 9 | import Fad.«Chapter2-Ex» 10 | 11 | import Fad.Chapter3 12 | import Fad.«Chapter3-Ex» 13 | 14 | import Fad.Chapter4 15 | import Fad.«Chapter4-Ex» 16 | 17 | import Fad.Chapter5 18 | import Fad.«Chapter5-Ex» 19 | 20 | import Fad.Chapter6 21 | 22 | import Fad.Chapter7 23 | import Fad.«Chapter7-Ex» 24 | 25 | import Fad.Chapter8 26 | import Fad.«Chapter8-Ex» 27 | 28 | import Fad.Chapter9 29 | import Fad.«Chapter9-Ex» 30 | 31 | import Fad.Chapter12 32 | import Fad.«Chapter12-Ex» 33 | -------------------------------------------------------------------------------- /AoC.lean: -------------------------------------------------------------------------------- 1 | -- This module serves as the root of the `AoC` library. 2 | -- Import modules here that should be built as part of the library. 3 | import AoC.Basic 4 | import AoC.AoC_2015.day1 5 | import AoC.AoC_2015.day2 6 | import AoC.AoC_2015.day3 7 | import AoC.AoC_2015.day5 8 | import AoC.AoC_2015.day6 9 | 10 | import AoC.AoC_2017.day1 11 | import AoC.AoC_2017.day2 12 | import AoC.AoC_2017.day4 13 | import AoC.AoC_2017.day5 14 | 15 | import AoC.AoC_2018.day1 16 | import AoC.AoC_2018.day2 17 | import AoC.AoC_2018.day3 18 | import AoC.AoC_2018.day5 19 | import AoC.AoC_2018.day6 20 | 21 | import AoC.AoC_2019.day1 22 | 23 | import AoC.AoC_2020.day1 24 | import AoC.AoC_2020.day2 25 | import AoC.AoC_2020.day3 26 | 27 | import AoC.AoC_2021.day1 28 | import AoC.AoC_2021.day2 29 | -------------------------------------------------------------------------------- /AoC/AoC_2017/day5.lean: -------------------------------------------------------------------------------- 1 | -- Problem: https://adventofcode.com/2017/day/5 2 | 3 | namespace AoC2017D5 4 | 5 | def content : String := 6 | include_str "../../data/AoC2017_day5.txt" 7 | 8 | def input : Array Int := 9 | content.splitOn "\n" |>.filter (· ≠ "") 10 | |>.filterMap String.toInt? |>.toArray 11 | 12 | 13 | -- Parte 1 14 | 15 | partial def proc₁ (arr : Array Int) (i : Int) 16 | (steps : Nat) (f : Int → Int) : Nat := 17 | if i < 0 ∨ i >= arr.size then 18 | steps 19 | else 20 | let off := arr[i.toNat]! 21 | let new := arr.set! i.toNat <| f off 22 | proc₁ new (i + off) (steps + 1) f 23 | 24 | -- #eval proc₁ input 0 0 (λ x => x + 1) 25 | 26 | 27 | -- Parte 2 28 | 29 | -- #eval proc₁ input 0 0 (λ x => if x ≥ 3 then x - 1 else x + 1) 30 | 31 | end AoC2017D5 32 | -------------------------------------------------------------------------------- /AoC/AoC_2015/day1.lean: -------------------------------------------------------------------------------- 1 | -- Problem: https://adventofcode.com/2015/day/1 2 | 3 | 4 | namespace AoC2015D1 5 | 6 | def content : String := include_str "../../data/AoC2015_day1.txt" 7 | 8 | def input : List Char := content.toList 9 | 10 | -- PART 1 11 | 12 | def part1 (input : String) : Int := 13 | input.foldl (λ acc ch => if ch = '(' then acc + 1 else acc - 1) 0 14 | 15 | -- #eval part1 content 16 | 17 | 18 | -- PART 2 19 | 20 | def find_basement : List Char → Int → Int → Option Int 21 | | [] , _ , _ => none 22 | | _ , -1 , pos => some pos 23 | | '(' :: xs, floor, pos => find_basement xs (floor + 1) (pos + 1) 24 | | ')' :: xs, floor, pos => find_basement xs (floor - 1) (pos + 1) 25 | | _ :: _, _, _ => none 26 | 27 | -- #eval find_basement input 0 0 28 | 29 | 30 | end AoC2015D1 31 | -------------------------------------------------------------------------------- /AoC/AoC_2018/day1.lean: -------------------------------------------------------------------------------- 1 | -- Problem: https://adventofcode.com/2018/day/1 2 | 3 | import Std.Data.HashSet 4 | 5 | namespace AoC2018D1 6 | 7 | -- PART 1 8 | 9 | def content : String := include_str "../../data/AoC2018_day1.txt" 10 | 11 | def input : List Int := content.splitOn "\n" |>.filterMap 12 | (λ s => (if s.startsWith "+" then s.drop 1 else s).toInt?) 13 | 14 | -- #eval input.foldl (fun acc x => acc + x) 0 15 | 16 | -- PART 2 17 | 18 | partial def first_duplicate_freq (lst : List Int) : Int := 19 | let rec aux (seen : Std.HashSet Int) (acc : Int) (rem : List Int) : Int := 20 | match rem with 21 | | [] => aux seen acc lst 22 | | h :: ts => 23 | let new := acc + h 24 | if seen.contains new then 25 | new 26 | else 27 | aux (seen.insert new) new ts 28 | aux (Std.HashSet.empty.insert 0) 0 lst 29 | 30 | -- #eval first_duplicate_freq input 31 | 32 | end AoC2018D1 33 | -------------------------------------------------------------------------------- /AoC/AoC_2019/day1.lean: -------------------------------------------------------------------------------- 1 | 2 | /- # Problem https://adventofcode.com/2019/day/1 -/ 3 | 4 | namespace AoC2019D1 5 | 6 | def content : String := include_str "../../data/AoC2019_day1.txt" 7 | 8 | def input : List Nat := 9 | content.splitOn "\n" |>.filter (· ≠ "") |>.map String.toNat! 10 | 11 | -- Part 1 12 | 13 | def solution₁ : Nat := 14 | input.foldr (λ n acc => ((n / 3) - 2) + acc) 0 15 | 16 | -- #eval input.map (λ n => n / 3 - 2) |>.sum 17 | 18 | 19 | -- Part 2 20 | 21 | def fuel₁ (n : Nat) : Nat := 22 | let mass := n / 3 - 2 23 | if mass = 0 then 24 | 0 25 | else 26 | mass + fuel₁ mass 27 | 28 | def fuel₂ : Nat → Nat → Nat 29 | | 0 , total => total 30 | | n + 1, total => 31 | let mass := (n + 1) / 3 - 2 32 | fuel₂ mass (total + mass) 33 | 34 | /- 35 | #eval input.foldr (λ n acc => fuel₁ n + acc) 0 36 | #eval input.foldr (λ n acc => fuel₂ n acc) 0 37 | #eval input.foldr (λ n acc => fuel₂ n 0 + acc) 0 38 | -/ 39 | 40 | end AoC2019D1 41 | -------------------------------------------------------------------------------- /AoC/AoC_2017/day1.lean: -------------------------------------------------------------------------------- 1 | --Problem: https://adventofcode.com/2017/day/1 2 | 3 | import Fad.Chapter3 4 | 5 | namespace AoC2017D1 6 | 7 | def input : String := include_str "../../data/AoC2017_day1.txt" 8 | 9 | -- Parte 1 10 | 11 | def chToDig (c : Char) : Nat := 12 | if c.val > 57 ∨ c.val < 48 then 0 else c.val.toNat - 48 13 | 14 | def aux : Nat → List Nat → Nat → Nat 15 | | _, [], acc => acc 16 | | l, (x::xs), acc => 17 | if x == l then aux x xs (acc + x) else aux x xs acc 18 | 19 | def solve1 (str : String) : Nat := 20 | let xs := str.toList.map chToDig 21 | aux (xs.getLast!) xs 0 22 | 23 | -- #eval solve1 $ input.replace "\n" "" 24 | 25 | -- Parte 2 26 | 27 | def solve2 (str : String) : Nat := 28 | let as := Subtype.mk (str.toList.map chToDig) (by rfl) 29 | let p := List.splitInTwo as 30 | List.zip p.1.val p.2.val 31 | |>.foldl (fun acc (x, y) => if x == y then acc + x else acc) 0 32 | |> (. * 2) 33 | 34 | -- #eval solve2 $ input.replace "\n" "" 35 | 36 | 37 | end AoC2017D1 38 | -------------------------------------------------------------------------------- /AoC/AoC_2020/day3.lean: -------------------------------------------------------------------------------- 1 | 2 | -- Problem: https://adventofcode.com/2020/day/3 3 | 4 | namespace AoC2020D3 5 | 6 | def content : String := include_str "../../data/AoC2020_day3.txt" 7 | 8 | def input : List (String) := 9 | content.splitOn "\n" |>.map String.trim 10 | 11 | -- Part 1 12 | 13 | def dfs (ls : List String) (dx dy : Nat) (x : Nat) : Nat := 14 | match ls with 15 | | [] => 0 16 | | l::ls' => 17 | let x' := (x + dx) % l.length 18 | let rs := ls'.drop (dy - 1) 19 | cond (l.get ⟨x⟩ = '#') 1 0 + dfs rs dx dy x' 20 | termination_by (ls.length, x) 21 | 22 | def solve₁ (ls : List String) : Nat := 23 | dfs ls 3 1 0 24 | 25 | --#eval solve₁ input 26 | 27 | -- Part 2 28 | 29 | def solve₂ (ls : List String) (dx dy : Nat) : Nat := 30 | dfs ls dx dy 0 31 | 32 | /- 33 | #eval [(1,1),(3,1),(5,1),(7,1),(1,2)].map 34 | (λ ⟨dx,dy⟩ => solve₂ input dx dy) 35 | 36 | #eval [(1,1),(3,1),(5,1),(7,1),(1,2)].foldl 37 | (λ acc ⟨dx,dy⟩ => acc * solve₂ input dx dy) 1 38 | -/ 39 | 40 | end AoC2020D3 41 | -------------------------------------------------------------------------------- /Fad/Chapter2-Ex.lean: -------------------------------------------------------------------------------- 1 | import Fad.Chapter2 2 | import Fad.Chapter1 3 | 4 | namespace Chapter2 5 | open Chapter1 6 | 7 | 8 | -- 2.9 9 | 10 | def op1 {a : Type} : (List a) → a → a 11 | | [], y => y 12 | | (x::_), _ => x 13 | 14 | example {xss : List (List α)} {e : α} (h : (concat1 xss) ≠ []) : 15 | List.head (concat1 xss) h = foldr op1 e xss:= by 16 | induction xss with 17 | | nil => contradiction 18 | | cons xs xss ih => 19 | cases xs with 20 | | nil => simp [concat1, List.head, foldr, op1]; exact ih h 21 | | cons x xs => simp [concat1, List.head, foldr, op1] 22 | 23 | 24 | -- 2.12 25 | 26 | def inits (as : List a) : List (List a) := 27 | let rec help (f : List a → List a) : List a → List (List a) 28 | | [] => (f []) :: [] 29 | | x :: xs => 30 | (f []) :: help (f ∘ List.cons x) xs 31 | help id as 32 | 33 | 34 | -- 2.14 35 | 36 | def tails1 (xs : List α) : List (List α) := 37 | List.takeWhile (¬ ·.isEmpty) (iterate xs.length List.tail xs) 38 | 39 | 40 | end Chapter2 41 | -------------------------------------------------------------------------------- /AoC/AoC_2020/day1.lean: -------------------------------------------------------------------------------- 1 | /- 2 | # Problem: https://adventofcode.com/2020/day/1 3 | -/ 4 | 5 | namespace AoC2020D1 6 | 7 | def content : String := 8 | include_str "../../data/AoC2020_day1.txt" 9 | 10 | def process_data (s : String) : List Nat := 11 | s.splitOn " " |>.map String.toNat! 12 | 13 | def input : List Nat := 14 | content.splitOn "\n" |>.filter (· ≠ "") 15 | |>.map (String.toNat! ∘ String.trim) 16 | 17 | 18 | -- Part 1 19 | 20 | def solve₁ (xs : List Nat) (val: Nat): Option (Nat × Nat) := 21 | match xs with 22 | | [] => none 23 | | x::xs' => 24 | match xs'.find? (λ y => x+y=val) with 25 | | none => solve₁ xs' val 26 | | some y => some (x, y) 27 | 28 | -- #eval solve₁ input 2020 29 | -- #eval 1917 * 103 30 | 31 | 32 | -- Part 2 33 | 34 | def solve₂ (xs : List Nat) (val: Nat) 35 | : Option (Nat × Nat × Nat) := 36 | match xs with 37 | | [] => none 38 | | x::xs' => 39 | match solve₁ xs' (2020-x) with 40 | | none => solve₂ xs' val 41 | | some (y, z) => some (x, y, z) 42 | 43 | --#eval solve₂ input 2020 44 | -- #eval 443 * 232 * 1345 45 | 46 | 47 | end AoC2020D1 48 | -------------------------------------------------------------------------------- /AoC/AoC_2018/day5.lean: -------------------------------------------------------------------------------- 1 | -- Problem: https://adventofcode.com/2018/day/5 2 | 3 | namespace AoC2018D5 4 | 5 | def content : String := include_str "../../data/AoC2018_day5.txt" 6 | def input : String := content.trim 7 | 8 | -- Part 1 9 | 10 | def reacts (c₁ c₂ : Char) : Bool := 11 | c₁.toLower = c₂.toLower ∧ c₁.isLower ≠ c₂.isLower 12 | 13 | def fullyReact (p : String) : String := 14 | let rec loop (ts : List Char) (rem : List Char) : List Char := 15 | match rem with 16 | | [] => ts.reverse 17 | | c :: cs => 18 | match ts with 19 | | [] => loop [c] cs 20 | | a :: as => 21 | if reacts a c then loop as cs else loop (c :: ts) cs 22 | (loop [] p.toList).asString 23 | 24 | -- #eval fullyReact input |>.length 25 | 26 | -- Part 2 27 | 28 | def removeUnit (p : String) (unit : Char) : String := 29 | p.toList.filter (·.toLower ≠ unit.toLower) |>.asString 30 | 31 | def shortestP (p : String) : Nat := 32 | let units := "abcdefghijklmnopqrstuvwxyz".toList 33 | let lengths := units.map (λ u => fullyReact (removeUnit p u) |>.length) 34 | lengths.foldl Nat.min p.length 35 | 36 | -- #eval shortestP input 37 | 38 | end AoC2018D5 39 | -------------------------------------------------------------------------------- /data/AoC2017_day2.txt: -------------------------------------------------------------------------------- 1 | 626 2424 2593 139 2136 163 1689 367 2235 125 2365 924 135 2583 1425 2502 2 | 183 149 3794 5221 5520 162 5430 4395 2466 1888 3999 3595 195 181 6188 4863 3 | 163 195 512 309 102 175 343 134 401 372 368 321 350 354 183 490 4 | 2441 228 250 2710 200 1166 231 2772 1473 2898 2528 2719 1736 249 1796 903 5 | 3999 820 3277 3322 2997 1219 1014 170 179 2413 183 3759 3585 2136 3700 188 6 | 132 108 262 203 228 104 205 126 69 208 235 311 313 258 110 117 7 | 963 1112 1106 50 186 45 154 60 1288 1150 986 232 872 433 48 319 8 | 111 1459 98 1624 2234 2528 93 1182 97 583 2813 3139 1792 1512 1326 3227 9 | 371 374 459 83 407 460 59 40 42 90 74 163 494 250 488 444 10 | 1405 2497 2079 2350 747 1792 2412 2615 89 2332 1363 102 81 2346 122 1356 11 | 1496 2782 2257 2258 961 214 219 2998 400 230 2676 3003 2955 254 2250 2707 12 | 694 669 951 455 2752 216 1576 3336 251 236 222 2967 3131 3456 1586 1509 13 | 170 2453 1707 2017 2230 157 2798 225 1891 945 943 2746 186 206 2678 2156 14 | 3632 3786 125 2650 1765 1129 3675 3445 1812 3206 99 105 1922 112 1136 3242 15 | 6070 6670 1885 1994 178 230 5857 241 253 5972 7219 252 806 6116 4425 3944 16 | 2257 155 734 228 204 2180 175 2277 180 2275 2239 2331 2278 1763 112 2054 -------------------------------------------------------------------------------- /data/AoC2019_day1.txt: -------------------------------------------------------------------------------- 1 | 106947 2 | 129138 3 | 56893 4 | 75116 5 | 96763 6 | 108475 7 | 62574 8 | 137915 9 | 73556 10 | 69652 11 | 74098 12 | 131265 13 | 77267 14 | 72940 15 | 74859 16 | 128578 17 | 128024 18 | 125887 19 | 140457 20 | 97314 21 | 126150 22 | 148019 23 | 116715 24 | 54231 25 | 98892 26 | 73242 27 | 131621 28 | 122572 29 | 107437 30 | 75142 31 | 103755 32 | 141267 33 | 141024 34 | 80452 35 | 60619 36 | 104099 37 | 51541 38 | 63863 39 | 106932 40 | 75346 41 | 77073 42 | 57263 43 | 128967 44 | 124504 45 | 79388 46 | 124167 47 | 100073 48 | 97108 49 | 74180 50 | 137778 51 | 73793 52 | 131458 53 | 67579 54 | 102695 55 | 143794 56 | 96093 57 | 64490 58 | 96122 59 | 88901 60 | 53381 61 | 77850 62 | 96527 63 | 51943 64 | 107511 65 | 120126 66 | 64622 67 | 63053 68 | 116916 69 | 83990 70 | 143278 71 | 72390 72 | 101767 73 | 135915 74 | 126354 75 | 109714 76 | 56139 77 | 129849 78 | 89505 79 | 115213 80 | 145001 81 | 56506 82 | 83700 83 | 59071 84 | 80895 85 | 143177 86 | 120738 87 | 78270 88 | 100436 89 | 108389 90 | 62456 91 | 145335 92 | 102210 93 | 111779 94 | 95937 95 | 52626 96 | 134932 97 | 61925 98 | 97086 99 | 50211 100 | 96413 101 | -------------------------------------------------------------------------------- /AoC/AoC_2017/day2.lean: -------------------------------------------------------------------------------- 1 | -- Problem: https://adventofcode.com/2017/day/2 2 | 3 | namespace AoC2017D2 4 | 5 | def inp : String := include_str "../../data/AoC2017_day2.txt" 6 | 7 | def parseSheet (txt : String) : List (List Nat) := 8 | txt.replace "\r" "" 9 | |>.split (· == '\n') 10 | |>.map (fun line => 11 | line.split (fun c => c == ' ' || c == '\t') 12 | |>.filterMap String.toNat? 13 | ) 14 | 15 | def sheet : List (List Nat) := parseSheet inp 16 | 17 | -- Parte 1 18 | def rowDiff (rw : List Nat) : Nat := 19 | match rw with 20 | | [] => 0 21 | | x :: xs => 22 | let mx := List.foldl max x xs 23 | let mn := List.foldl min x xs 24 | mx - mn 25 | 26 | def checksum (sheet : List (List Nat)) : Nat := 27 | sheet.foldl (fun acc rw => acc + rowDiff rw) 0 28 | 29 | -- #eval checksum sheet 30 | 31 | -- Parte 2 32 | def genPairs (rw : List Nat) : List (Nat × Nat) := 33 | rw.foldl (fun acc x => 34 | acc ++ (rw.filter (fun y => y ≠ x)).map (fun y => (x, y)) 35 | ) [] 36 | 37 | def divPair (rw : List Nat) : Nat := 38 | let pairs := genPairs rw 39 | let valid := pairs.filter (fun (a, b) => a % b == 0) 40 | match valid.head? with 41 | | some (a, b) => a / b 42 | | none => 0 43 | 44 | def checksum2 (sheet : List (List Nat)) : Nat := 45 | sheet.foldl (fun acc rw => acc + divPair rw) 0 46 | 47 | -- #eval checksum2 sheet 48 | 49 | end AoC2017D2 50 | -------------------------------------------------------------------------------- /AoC/AoC_2015/day2.lean: -------------------------------------------------------------------------------- 1 | -- Problem: https://adventofcode.com/2015/day/2 2 | 3 | namespace AoC2015D2 4 | 5 | def content : String := include_str "../../data/AoC2015_day2.txt" 6 | 7 | def input := content.split (· == '\n') 8 | 9 | -- Part 1 10 | 11 | def parse_dimensions (s : String) : Option (Int × Int × Int) := 12 | match s.splitOn "x" with 13 | | [l, w, h] => some (l.toInt!, w.toInt!, h.toInt!) 14 | | _ => none 15 | 16 | def paper_for_box (l w h : Int) : Int := 17 | let sides := [l*w, w*h, h*l] 18 | let surface_area := 2 * l * w + 2 * w * h + 2 * h * l 19 | surface_area + sides.foldl min (l * w) -- Add the smallest side area 20 | 21 | def total_paper (input : List String) : Int := 22 | input.foldl (fun acc s => 23 | match parse_dimensions s with 24 | | some (l, w, h) => acc + paper_for_box l w h 25 | | none => acc) 0 26 | 27 | 28 | -- #eval total_paper input 29 | 30 | 31 | -- Part 2: 32 | -- Calculate the ribbon required for a single box 33 | 34 | def ribbon_for_box (l w h : Int) : Int := 35 | let perimeters := [2 * (l + w), 2 * (w + h), 2 * (h + l)] 36 | let volume := l * w * h 37 | perimeters.foldl min (2 * (l + w)) + volume 38 | 39 | -- Process a list of dimension strings to compute total ribbon 40 | def total_ribbon (input : List String) : Int := 41 | input.foldl (fun acc s => 42 | match parse_dimensions s with 43 | | some (l, w, h) => acc + ribbon_for_box l w h 44 | | none => acc) 0 45 | 46 | -- #eval total_ribbon input 47 | 48 | end AoC2015D2 49 | -------------------------------------------------------------------------------- /AoC/AoC_2020/day2.lean: -------------------------------------------------------------------------------- 1 | /-! 2 | # Problem: https://adventofcode.com/2020/day/2 3 | -/ 4 | 5 | namespace AoC2020D2 6 | 7 | def content : String := include_str "../../data/AoC2020_day2.txt" 8 | 9 | def input : List (String) := 10 | content.splitOn "\n" 11 | 12 | structure Password where 13 | low : Nat 14 | high : Nat 15 | letter : Char 16 | pwd : String 17 | deriving Repr 18 | 19 | def parse (s : String) : Password := 20 | let ss := s.splitOn " " 21 | let lowhigh := ss.get! 0 |>.splitOn "-" 22 | let low := lowhigh.get! 0 |>.toNat! 23 | let high := lowhigh.get! 1 |>.toNat! 24 | let letter := ss.get! 1 |>.get! 0 25 | let password := ss.get! 2 26 | Password.mk low high letter password 27 | 28 | def count_letters (s : String) (c : Char) : Nat := 29 | s.foldl (λ n x => if x = c then n + 1 else n) 0 30 | 31 | -- Part 1 32 | 33 | def solve₁ (ps: List Password) : Nat := 34 | match ps with 35 | | [] => 0 36 | | p::ps' => 37 | let n := count_letters p.pwd p.letter 38 | if p.low ≤ n ∧ n ≤ p.high then 1 + solve₁ ps' 39 | else solve₁ ps' 40 | 41 | -- #eval solve₁ $ input.map parse 42 | 43 | -- Part 2 44 | 45 | def solve₂ (ps: List Password) : Nat := 46 | match ps with 47 | | [] => 0 48 | | p::ps' => 49 | let a := p.pwd.get! ⟨p.low-1⟩ 50 | let b := p.pwd.get! ⟨p.high-1⟩ 51 | let flag₁ := (a = p.letter) 52 | let flag₂ := (b = p.letter) 53 | if (flag₁ ∧ ¬flag₂) ∨ (¬flag₁ ∧ flag₂) then 54 | 1 + solve₂ ps' 55 | else 56 | solve₂ ps' 57 | 58 | -- #eval solve₂ $ input.map parse 59 | 60 | end AoC2020D2 61 | -------------------------------------------------------------------------------- /Fad/Chapter12.lean: -------------------------------------------------------------------------------- 1 | import Fad.Chapter1 2 | 3 | namespace Chapter12 4 | open Chapter1 (concatMap) 5 | 6 | abbrev Segment (a : Type) := List a 7 | abbrev Partition (a : Type) := List (Segment a) 8 | 9 | def splits {a : Type} : List a → List (List a × List a) 10 | | [] => [] 11 | | x :: xs => ([x], xs) :: (splits xs).map fun ⟨ys, zs⟩ => (x :: ys, zs) 12 | 13 | partial def parts: List a → List (Partition a) 14 | | [] => [[]] 15 | | xs => 16 | (splits xs).flatMap fun ⟨ys, zs⟩ => 17 | (parts zs).map fun yss => ys :: yss 18 | 19 | 20 | def cons (x : a) (p : Partition a) : Partition a := 21 | [x] :: p 22 | 23 | def glue (x : a) : Partition a → Partition a 24 | | s :: p => (x :: s) :: p 25 | | [] => panic! "glue: empty partition" 26 | 27 | def extendl (x : a) : Partition a → List (Partition a) 28 | | [] => [cons x []] 29 | | p => [cons x p, glue x p] 30 | 31 | def parts₁ : List Nat → List (Partition Nat) := 32 | List.foldr (concatMap ∘ extendl) [[]] 33 | 34 | 35 | def last [Inhabited a] : List a → a 36 | | [x] => x 37 | | [] => panic! "last: empty list" 38 | | _::xs => last xs 39 | 40 | def init [Inhabited a] : List a → List a 41 | | [_] => [] 42 | | [] => panic! "init: empty list" 43 | | x :: xs => x :: init xs 44 | 45 | def snoc (x : a) (p : Partition a) : Partition a := 46 | p ++ [[x]] 47 | 48 | def bind (x : a) (p : Partition a) : Partition a := 49 | init p ++ [last p ++ [x]] 50 | 51 | def extendr (x : a) : Partition a → List (Partition a) 52 | | [] => [snoc x []] 53 | | p => [snoc x p, bind x p] 54 | 55 | def parts₂ : List Nat → List (Partition Nat) := 56 | List.foldl (flip (concatMap ∘ extendr)) [[]] 57 | 58 | 59 | 60 | end Chapter12 61 | -------------------------------------------------------------------------------- /AoC/AoC_2018/day2.lean: -------------------------------------------------------------------------------- 1 | -- Problem: https://adventofcode.com/2018/day/2 2 | 3 | namespace AoC2018D2 4 | 5 | def content : String := include_str "../../data/AoC2018_day2.txt" 6 | 7 | def input := content.splitOn "\n" 8 | 9 | -- PART 1 10 | 11 | def count_letters (s : String) : List (Char × Nat) := 12 | s.toList.foldl (fun acc c => 13 | match acc.find? (·.1 == c) with 14 | | some (_, n) => acc.map (fun p => if p.1 == c then (c, n + 1) else p) 15 | | none => (c, 1) :: acc 16 | ) [] 17 | 18 | def has_exactly (n : Nat) (s : String) : Bool := 19 | count_letters s |>.any (·.2 == n) 20 | 21 | def count_exactly (n : Nat) (ss : List String) : Nat := 22 | ss.foldl (fun acc s => if has_exactly n s then acc + 1 else acc) 0 23 | 24 | def checksum (ss : List String) : Nat := 25 | let twos := count_exactly 2 ss 26 | let threes := count_exactly 3 ss 27 | twos * threes 28 | 29 | -- #eval checksum input 30 | 31 | 32 | -- PART 2 33 | 34 | def differ_by_one (s1 s2 : String) : Bool := 35 | let diffs := s1.toList.zip s2.toList |>.filter (fun (c1, c2) => c1 != c2) 36 | diffs.length == 1 37 | 38 | def find_differ_by_one (ss: List String) : Option (String × String) := 39 | let rec aux : List String → Option (String × String) 40 | | [] => default 41 | | h :: t => 42 | match t.find? (differ_by_one h) with 43 | | some s => some (h, s) 44 | | none => aux t 45 | aux ss 46 | 47 | def boxes := (find_differ_by_one input).getD default 48 | 49 | def common_letters (s1 s2 : String) : String := 50 | s1.toList.zip s2.toList |>.filter (fun (c₁, c₂) => c₁ = c₂) |>.map (·.1) |>.asString 51 | 52 | -- #eval common_letters boxes.1 boxes.2 53 | 54 | end AoC2018D2 55 | -------------------------------------------------------------------------------- /AoC/AoC_2021/day1.lean: -------------------------------------------------------------------------------- 1 | 2 | /- # Problema https://adventofcode.com/2021/day/1 -/ 3 | 4 | namespace AoC2021D1 5 | 6 | def content : String := include_str "../../data/AoC2021_day1.txt" 7 | 8 | def input : List Nat := 9 | content.splitOn "\n" |>.filter (· ≠ "") |>.map (·.trim.toNat!) 10 | 11 | 12 | /- # Part 1 13 | 14 | Analisar uma sequência de medições de profundidade do mar. 15 | 16 | - O input é uma lista de números inteiros, representando as 17 | profundidades do mar, coletados em diferentes momentos. 18 | 19 | Objetivo: Contar o número de vezes que uma medição de profundidade é 20 | maior que a medição anterior. 21 | 22 | -/ 23 | 24 | def countIncreases : List Nat → Nat 25 | | [] => 0 26 | | [_] => 0 27 | | (x :: y :: xs) => (if y > x then 1 else 0) + countIncreases (y :: xs) 28 | 29 | -- #eval countIncreases input 30 | 31 | 32 | /- # Part 2 33 | 34 | Análise de somas de janelas deslizantes de três medições consecutivas 35 | de profundidade do mar. 36 | 37 | - Em vez de comparar as medições individuais, agora deve-se considerar 38 | janelas de três medições consecutivas. 39 | 40 | - Para cada janela, a soma das três medições é calculada. 41 | 42 | Objetivo: Contar o número de vezes que a soma de uma janela de três 43 | medições consecutivas é maior que a soma da janela anterior. 44 | 45 | -/ 46 | 47 | def sublists (n: Nat) : List α → List (List α) → List (List α) 48 | | [] , acc => acc.reverse 49 | | x :: xs, acc => 50 | let slice := List.take n (x :: xs) 51 | if slice.length < 3 then 52 | acc.reverse 53 | else 54 | sublists n xs (slice :: acc) 55 | 56 | -- #eval sublists 3 input [] 57 | -- #eval countIncreases $ sublists 3 input [] |>.map List.sum 58 | 59 | end AoC2021D1 60 | -------------------------------------------------------------------------------- /AoC/AoC_2017/day4.lean: -------------------------------------------------------------------------------- 1 | -- Problem: https://adventofcode.com/2017/day/4 2 | 3 | import Std 4 | 5 | namespace AoC2017D4 6 | 7 | -- Entrada e processamento 8 | def content : String := include_str "../../data/AoC2017_day4.txt" 9 | 10 | def input : List String := 11 | content.splitOn "\n" |>.filter (· ≠ "") 12 | 13 | 14 | -- Parte 1 15 | 16 | def isValid1 (ph : String) : Bool := 17 | let wrds := ph.splitOn " " 18 | let rec hasDup (rem : List String) (seen : Std.HashSet String) : Bool := 19 | match rem with 20 | | [] => true 21 | | w :: rest => 22 | if seen.contains w then false 23 | else hasDup rest (seen.insert w) 24 | hasDup wrds Std.HashSet.empty 25 | 26 | def countValid1 (phrs : List String) : Nat := 27 | phrs.foldl (fun acc ph => if isValid1 ph then acc + 1 else acc) 0 28 | 29 | -- #eval countValid1 input 30 | 31 | -- Parte 2 32 | def sortChars (lst : List Char) : List Char := 33 | let rec ins (x : Char) (sorted : List Char) : List Char := 34 | match sorted with 35 | | [] => [x] 36 | | y :: ys => if x < y then x :: y :: ys else y :: ins x ys 37 | lst.foldr ins [] 38 | 39 | def sortStr (s : String) : String := 40 | (sortChars s.toList).asString 41 | 42 | def isValid2 (ph : String) : Bool := 43 | let wrds := ph.splitOn " " |>.map sortStr 44 | let rec hasDup (rem : List String) (seen : Std.HashSet String) : Bool := 45 | match rem with 46 | | [] => true 47 | | w :: rest => 48 | if seen.contains w then false 49 | else hasDup rest (seen.insert w) 50 | hasDup wrds Std.HashSet.empty 51 | 52 | def countValid2 (phrs : List String) : Nat := 53 | phrs.foldl (fun acc ph => if isValid2 ph then acc + 1 else acc) 0 54 | 55 | -- #eval countValid2 input 56 | 57 | end AoC2017D4 58 | -------------------------------------------------------------------------------- /AoC/AoC_2015/day5.lean: -------------------------------------------------------------------------------- 1 | -- Problem: https://adventofcode.com/2015/day/5 2 | 3 | namespace AoC2015D5 4 | 5 | def content : String := include_str "../../data/AoC2015_day5.txt" 6 | 7 | def input := content.split (· == '\n') 8 | 9 | -- Part 1 10 | 11 | def is_vowel (c : Char) : Bool := 12 | c ∈ "aeoiu".toList 13 | 14 | def rule₁ (s : List Char) : Bool := 15 | (s.foldl (λ acc c => if is_vowel c then acc + 1 else acc) 0) ≥ 3 16 | 17 | def rule₂ : List Char -> Bool 18 | | [] => false 19 | | [_] => false 20 | | c1 :: c2 :: cs => if c1 = c2 then true else rule₂ (c2 :: cs) 21 | 22 | def rule₃ (s : List Char) : Bool := 23 | let f := ["ab", "cd", "pq", "xy"] 24 | ¬(s.zip s.tail |>.any (λ p => f.contains s!"{p.1}{p.2}")) 25 | 26 | def is_nice (s : String) : Bool := 27 | let cs := s.toList 28 | rule₁ cs ∧ rule₂ cs ∧ rule₃ cs 29 | 30 | -- #eval input.foldl (λ acc s => if is_nice s then acc + 1 else acc) 0 31 | 32 | -- Part 2 33 | 34 | def pairs_of_chairs : List Char -> List (List Char) 35 | | [] => [] 36 | | [_] => [] 37 | | [c1, c2] => [[c1, c2]] 38 | | c1 :: c2 :: c3 :: cs => if c1 ≠ c2 ∨ c2 ≠ c3 39 | then [c1, c2] :: pairs_of_chairs (c2 :: c3 :: cs) 40 | else pairs_of_chairs (c2 :: c3 :: cs) 41 | 42 | def check_replication : List (List Char) -> Bool 43 | | [] => false 44 | | x :: xs => if x ∈ xs then true 45 | else check_replication xs 46 | 47 | def rule₄ : List Char -> Bool := 48 | λ x => (check_replication (pairs_of_chairs x)) 49 | 50 | def rule₅ : List Char -> Bool 51 | | [] => false 52 | | [_] => false 53 | | [_, _] => false 54 | | c1 :: c2 :: c3 :: rest => if c1 = c3 then true else rule₅ (c2 :: c3 :: rest) 55 | 56 | 57 | def is_nice₂ (s : String) : Bool := 58 | let chars := s.toList 59 | rule₄ chars ∧ rule₅ chars 60 | 61 | def count_nice_strings₂ (ss : List String) : Nat := 62 | ss.foldl (λ acc s => if is_nice₂ s then acc + 1 else acc) 0 63 | 64 | -- #eval count_nice_strings₂ input 65 | 66 | end AoC2015D5 67 | -------------------------------------------------------------------------------- /data/AoC2017_day1.txt: -------------------------------------------------------------------------------- 1 | 494751136895345894732582362629576539599184296195318162664695189393364372585778868512194863927652788149779748657989318645936221887731542718562643272683862627537378624843614831337441659741281289638765171452576466381314558821636595394981788588673443769343597851883955668818165723174939893841654914556681324133667446412138511724424292394454166623639872425168644336248217213826339741267546823779383343362789527461579565822966859349777937921933694912369552152772735167832762563719664315456987186713541153781499646178238762644186484381142249926194743713139262596264878458636595896487362658672224346241358667234115974528626523648311919886566497837217169673923935143386823757293148719377821517314629812886912412829924484513493885672343964151252433622341141661523814465991516961684511941471572895453711624986269342398786175846925783918686856442684489873327497698963658862856336682422797551251489126661954848572297228765445646745256499679451426358865477844467458533962981852292513358871483321161973583245698763531598395467675529181496911117769834127516441369261275244225978893617456524385518493112272169767775861256649728253754964675812534546226295535939697352141217337346738553495616832783757866928174519145357234834584788253893618549484385733283627199445369658339175644484859385884574943219267922729967571943843794565736975716174727852348441254492886794362934343868643337828637454277582276962353246357835493338372219824371517526474283541714897994127864461433627894831268659336264234436872715374727211764167739169341999573855627775114848275268739159272518673316753672995297888734844388928439859359992475637439771269232916542385876779616695129412366735112593669719335783511355773814685491876721452994714318863716542473187246351548626157775143333161422867924437526253865859969947366972895674966845993244925218766937543487875485647329995285821739359369998935331986126873726737672159265827566443794515755939813676194755474477224152139987944419463371386499841415227734673733555261543871359797796529847861748979527579985757964742667473767269248335229836818297477665453189662485548925521497365877771665365728224394427883312135322325169141784 2 | -------------------------------------------------------------------------------- /data/AoC2020_day1.txt: -------------------------------------------------------------------------------- 1 | 2004 2 | 1671 3 | 1678 4 | 1304 5 | 1242 6 | 1882 7 | 1605 8 | 1034 9 | 1883 10 | 1589 11 | 1881 12 | 1546 13 | 1713 14 | 1218 15 | 1982 16 | 1395 17 | 1277 18 | 1417 19 | 1497 20 | 1499 21 | 1847 22 | 1989 23 | 1172 24 | 1684 25 | 1243 26 | 1843 27 | 1661 28 | 1662 29 | 1421 30 | 1790 31 | 1344 32 | 1458 33 | 1074 34 | 1809 35 | 1990 36 | 1369 37 | 1386 38 | 1736 39 | 1972 40 | 1634 41 | 1229 42 | 1123 43 | 1870 44 | 1595 45 | 1934 46 | 1399 47 | 1732 48 | 1545 49 | 1208 50 | 368 51 | 1907 52 | 1143 53 | 443 54 | 1929 55 | 1965 56 | 1872 57 | 1738 58 | 1967 59 | 997 60 | 1473 61 | 1041 62 | 1991 63 | 1868 64 | 1180 65 | 1409 66 | 1379 67 | 1568 68 | 1163 69 | 1869 70 | 1391 71 | 1956 72 | 1249 73 | 1505 74 | 351 75 | 2001 76 | 462 77 | 1219 78 | 1731 79 | 1802 80 | 1798 81 | 1626 82 | 1438 83 | 1099 84 | 1477 85 | 1980 86 | 1708 87 | 1666 88 | 1066 89 | 1121 90 | 1359 91 | 1426 92 | 1734 93 | 1768 94 | 1836 95 | 1453 96 | 923 97 | 1660 98 | 1878 99 | 1522 100 | 1024 101 | 1429 102 | 232 103 | 1952 104 | 1730 105 | 1763 106 | 1981 107 | 1388 108 | 1337 109 | 1317 110 | 1922 111 | 1044 112 | 1999 113 | 1341 114 | 1178 115 | 1524 116 | 1185 117 | 1257 118 | 1256 119 | 1061 120 | 1262 121 | 1022 122 | 1778 123 | 1917 124 | 1205 125 | 1272 126 | 1842 127 | 1533 128 | 1194 129 | 1746 130 | 1691 131 | 1617 132 | 1667 133 | 1940 134 | 1171 135 | 1792 136 | 1773 137 | 1411 138 | 1902 139 | 1859 140 | 1978 141 | 1764 142 | 1482 143 | 1276 144 | 735 145 | 1716 146 | 1915 147 | 1675 148 | 1126 149 | 1830 150 | 1227 151 | 1299 152 | 1535 153 | 1700 154 | 1658 155 | 1771 156 | 1823 157 | 1055 158 | 1602 159 | 1590 160 | 1983 161 | 1885 162 | 1735 163 | 103 164 | 1766 165 | 14 166 | 1486 167 | 1939 168 | 1525 169 | 1916 170 | 1279 171 | 544 172 | 1406 173 | 1674 174 | 1948 175 | 1971 176 | 1651 177 | 1715 178 | 1943 179 | 1784 180 | 2008 181 | 1800 182 | 1720 183 | 1557 184 | 1467 185 | 1371 186 | 1637 187 | 1345 188 | 1924 189 | 1565 190 | 1976 191 | 1827 192 | 1890 193 | 1848 194 | 1465 195 | 1573 196 | 1231 197 | 1310 198 | 1754 199 | 1569 200 | 1532 -------------------------------------------------------------------------------- /AoC/AoC_2021/day2.lean: -------------------------------------------------------------------------------- 1 | import Std.Internal.Parsec 2 | 3 | /- # Problema https://adventofcode.com/2021/day/2 -/ 4 | 5 | namespace AoC2021D2 6 | 7 | def content : String := include_str "../../data/AoC2021_day2.txt" 8 | 9 | structure Command where 10 | direction : String 11 | unit : Nat 12 | deriving Repr, Inhabited 13 | 14 | def parseCommand (s : String) : Command := 15 | match s.trim.splitOn " " with 16 | | [d, u] => 17 | { direction := d, unit := u.toNat! } 18 | | _ => 19 | { direction := "", unit := 0 } 20 | 21 | def input : List Command := 22 | content.splitOn "\n" |>.filter (· ≠ "") |>.map parseCommand 23 | 24 | -- Part 1 25 | 26 | def execute₁ (pos : Nat × Nat) : List Command → Nat × Nat 27 | | [] => pos 28 | | c :: xs => 29 | match c with 30 | | ⟨"forward", n⟩ => execute₁ (pos.1 + n, pos.2) xs 31 | | ⟨"down", n⟩ => execute₁ (pos.1, pos.2 + n) xs 32 | | ⟨"up", n⟩ => execute₁ (pos.1, pos.2 - n) xs 33 | | ⟨_,_⟩ => execute₁ pos xs 34 | 35 | 36 | def exec₁ (pos : Nat × Nat) (c : Command) : Nat × Nat := 37 | match c with 38 | | ⟨"forward", n⟩ => (pos.1 + n, pos.2) 39 | | ⟨"down", n⟩ => (pos.1, pos.2 + n) 40 | | ⟨"up", n⟩ => (pos.1, pos.2 - n) 41 | | ⟨_,_⟩ => pos 42 | 43 | 44 | -- #eval (λ a => a.1 * a.2) $ execute₁ (0,0) input 45 | -- #eval (λ a => a.1 * a.2) $ input.foldl exec₁ (0,0) 46 | 47 | -- Part 2 48 | 49 | def execute₂ (pos : Nat × Nat) (aim : Nat) : List Command → Nat × Nat 50 | | [] => pos 51 | | c :: xs => 52 | match c with 53 | | ⟨"forward", n⟩ => execute₂ (pos.1 + n, pos.2 + aim * n) aim xs 54 | | ⟨"down", n⟩ => execute₂ pos (aim + n) xs 55 | | ⟨"up", n⟩ => execute₂ pos (aim - n) xs 56 | | ⟨_,_⟩ => execute₂ pos aim xs 57 | 58 | 59 | def exec₂ (pos : Nat × Nat × Nat) (c : Command) : Nat × Nat × Nat := 60 | match c with 61 | | ⟨"forward", n⟩ => (pos.1 + n, pos.2.1 + pos.2.2 * n, pos.2.2) 62 | | ⟨"down", n⟩ => (pos.1, pos.2.1, pos.2.2 + n) 63 | | ⟨"up", n⟩ => (pos.1, pos.2.1, pos.2.2 - n) 64 | | ⟨_,_⟩ => pos 65 | 66 | 67 | -- #eval (λ a => a.1 * a.2) $ execute₂ (0,0) 0 input 68 | -- #eval (λ a => a.1 * a.2.1) $ input.foldl exec₂ (0, 0,0) 69 | -------------------------------------------------------------------------------- /Fad/Chapter2.lean: -------------------------------------------------------------------------------- 1 | import Fad.Chapter1 2 | import Fad.«Chapter1-Ex» 3 | 4 | namespace Chapter2 5 | open Chapter1 (dropWhile) 6 | 7 | /- 8 | #eval List.append [1,2,3] [4,5,6] 9 | #eval List.head! (List.iota 1000) 10 | -/ 11 | 12 | def concat₀ : List (List a) → List a 13 | | [] => [] 14 | | (xs :: xss) => xs ++ concat₀ xss 15 | 16 | def concat₁ (xss : List (List a)) : List a := 17 | dbg_trace "concat₁ {xss.length}" 18 | match xss with 19 | | [] => [] 20 | | (xs :: xss) => xs ++ concat₁ xss 21 | 22 | -- #eval concat₁ [[1, 2], [3, 4], [5, 6]] 23 | 24 | 25 | -- 2.4 Amortised running times 26 | 27 | def build (p : a → a → Bool) : List a → List a := 28 | List.foldr insert [] 29 | where 30 | insert x xs := x :: dropWhile (p x) xs 31 | 32 | -- #eval build (· = ·) [4,4,2,1,1,1,2,5] 33 | 34 | example : 35 | build (fun x y => x = y) [4,4,2,1] = [4, 2, 1] := by 36 | unfold build 37 | unfold List.foldr 38 | unfold build.insert 39 | unfold List.foldr 40 | unfold dropWhile; simp 41 | rfl 42 | 43 | 44 | /- primeiro argumento evita lista infinita -/ 45 | def iterate : Nat → (a → a) → a → List a 46 | | 0, _, x => [x] 47 | | Nat.succ n, f, x => x :: iterate n f (f x) 48 | 49 | def bits (n : Nat) : List (List Bool) := 50 | iterate n inc [] 51 | where 52 | inc : List Bool → List Bool 53 | | [] => [true] 54 | | (false :: bs) => true :: bs 55 | | (true :: bs) => false :: inc bs 56 | 57 | 58 | def init₀ : List α → List α 59 | | [] => panic! "no elements" 60 | | [_] => [] 61 | | x :: xs => x :: init₀ xs 62 | 63 | def init₁ : List α → Option (List α) 64 | | [] => none 65 | | [_] => some [] 66 | | x :: xs => 67 | match init₁ xs with 68 | | none => none 69 | | some ys => some (x :: ys) 70 | 71 | def init₂ : List α → Option (List α) 72 | | [] => none 73 | | [_] => some [] 74 | | x :: xs => init₂ xs >>= (fun ys => pure (x :: ys)) 75 | 76 | def prune (p : List a → Bool) (xs : List a) : List a := 77 | List.foldr cut [] xs 78 | where 79 | cut x xs := Chapter1.until' done init₀ (x :: xs) 80 | done (xs : List a) := xs.isEmpty ∨ p xs 81 | 82 | def ordered : List Nat → Bool 83 | | [] => true 84 | | [_] => true 85 | | x :: y :: xs => x ≤ y ∧ ordered (y :: xs) 86 | 87 | -- #eval prune ordered [3,7,8,2,3] 88 | 89 | end Chapter2 90 | -------------------------------------------------------------------------------- /AoC/AoC_2018/day6.lean: -------------------------------------------------------------------------------- 1 | -- Problem: https://adventofcode.com/2018/day/6 2 | 3 | namespace AoC2018D6 4 | 5 | def content : String := (include_str "../../data/AoC2018_day6.txt") 6 | 7 | def to_coord (s : String) : (Nat × Nat) := 8 | let parts := s.split (· == ',') |>.map (·.trim.toNat!) 9 | (parts[0]!, parts[1]!) 10 | 11 | def input := content.split (· == '\n') |>.map to_coord 12 | 13 | 14 | -- Part 1 15 | 16 | def manhattan_distance (p1 p2 : Nat × Nat) : Nat := 17 | Int.natAbs (p1.1 - p2.1) + Int.natAbs (p1.2 - p2.2) 18 | 19 | def bound_box (cs : List (Nat × Nat)) : (Nat × Nat) × (Nat × Nat) := 20 | let xs := cs.map (·.1) 21 | let ys := cs.map (·.2) 22 | ((xs.min?.getD 0, ys.min?.getD 0), (xs.max?.getD 0, ys.max?.getD 0)) 23 | 24 | def closest (cs : List (Nat × Nat)) (p : Nat × Nat) : Option (Nat × Nat) := 25 | let dists := cs.map (fun c => (c, manhattan_distance c p)) 26 | let min_dist := dists.map (·.2) |>.min?.getD 0 27 | let closest := dists.filter (·.2 == min_dist) 28 | if closest.length == 1 then some closest.head!.1 else none 29 | 30 | def fin_areas (cs : List (Nat × Nat)) : List (Nat × Nat) := 31 | let ((min_x, min_y), (max_x, max_y)) := bound_box cs 32 | let border := (List.range (max_x - min_x + 1)).flatMap (fun x => 33 | [(min_x + x, min_y), (min_x + x, max_y)]) ++ 34 | (List.range (max_y - min_y + 1)).flatMap (fun y => 35 | [(min_x, min_y + y), (max_x, min_y + y)]) 36 | let inf_coords := border.map (closest cs) |>.filterMap id |>.eraseDups 37 | cs.filter (· ∉ inf_coords) 38 | 39 | def area (cs : List (Nat × Nat)) (target : Nat × Nat) : Nat := 40 | let ((min_x, min_y), (max_x, max_y)) := bound_box cs 41 | let pts := (List.range (max_x - min_x + 1)).flatMap (fun x => 42 | (List.range (max_y - min_y + 1)).map (fun y => (min_x + x, min_y + y))) 43 | pts.filter (closest cs · == some target) |>.length 44 | 45 | def largest (cs : List (Nat × Nat)) : Nat := 46 | fin_areas cs |>.map (area cs) |>.max?.getD 0 47 | 48 | -- #eval largest input 49 | 50 | -- Par 2 51 | 52 | def total (cs : List (Nat × Nat)) (p : Nat × Nat) : Nat := 53 | cs.foldl (fun acc c => acc + manhattan_distance c p) 0 54 | 55 | def safe_size (cs : List (Nat × Nat)) (dist : Nat) : Nat := 56 | let ((min_x, min_y), (max_x, max_y)) := bound_box cs 57 | let pts := (List.range (max_x - min_x + 1)).flatMap (fun x => 58 | (List.range (max_y - min_y + 1)).map (fun y => (min_x + x, min_y + y))) 59 | pts.filter (fun p => total cs p < dist) |>.length 60 | 61 | -- #eval safe_size input 10000 62 | 63 | end AoC2018D6 64 | -------------------------------------------------------------------------------- /AoC/AoC_2018/day3.lean: -------------------------------------------------------------------------------- 1 | import Std.Internal.Parsec 2 | import Std.Data.HashMap 3 | 4 | -- Problem: https://adventofcode.com/2018/day/3 5 | 6 | namespace AoC2018D3 7 | 8 | open Std.Internal.Parsec 9 | open Std.Internal.Parsec.String 10 | 11 | def content : String := include_str "../../data/AoC2018_day3.txt" 12 | 13 | def input := content.split (· == '\n') |>.filter (· ≠ "") 14 | 15 | structure Claim where 16 | id : Nat 17 | left : Nat 18 | top : Nat 19 | width : Nat 20 | height : Nat 21 | deriving Repr, Inhabited 22 | 23 | def parseClaim : Parser Claim := do 24 | let id ← pstring "#" *> digits 25 | let _ ← pstring " @ " 26 | let left ← digits <* pstring "," 27 | let top ← digits <* pstring ": " 28 | let width ← digits <* pstring "x" 29 | let height ← digits 30 | return { id := id, left := left, top := top, width := width, height := height } 31 | 32 | -- #eval parseClaim "#6 @ 703,27: 3x9".mkIterator 33 | 34 | def parseClaims (is : List String) : List Claim := 35 | let p := Std.Internal.Parsec.String.Parser.run parseClaim 36 | let help (i : String) : Claim := 37 | match p i with 38 | | Except.ok c => c 39 | | Except.error _ => default 40 | is.map help 41 | 42 | abbrev Map := Std.HashMap (Nat × Nat) Nat 43 | 44 | 45 | -- Part 1 46 | 47 | def fabric : Map := 48 | (List.range 1000).foldl (fun m i => 49 | (List.range 1000).foldl (fun m j => 50 | m.insert (i, j) 0) m) Std.HashMap.empty 51 | 52 | def claims := parseClaims input 53 | 54 | def mark_claim (c : Claim) (m : Map) : Map := 55 | let idx := 56 | List.range c.width |>.flatMap 57 | (fun w => List.range c.height |>.map 58 | (fun h => (c.left + w, c.top + h))) 59 | idx.foldl (fun m p => m.modify p Nat.succ) m 60 | 61 | def mark_claims : List Claim → Map → Map 62 | | [] , m => m 63 | | c :: cs, m => mark_claims cs (mark_claim c m) 64 | 65 | def count_overlaps (m : Map) : Nat := 66 | m.fold (fun acc _ v => if v > 1 then acc + 1 else acc) 0 67 | 68 | -- #eval count_overlaps $ mark_claims claims fabric 69 | 70 | 71 | -- Part 2 72 | 73 | def is_intact (m : Map) (c : Claim) : Bool := 74 | let indices := 75 | List.range c.width |>.flatMap 76 | (fun w => List.range c.height |>.map 77 | (fun h => (c.left + w, c.top + h))) 78 | indices.all (fun p => m[p]! == 1) 79 | 80 | def find_intact_claim (cs : List Claim) (m : Map) : Option Nat := 81 | match cs.find? (is_intact m ·) with 82 | | some claim => some claim.id 83 | | none => none 84 | 85 | -- #eval (find_intact_claim claims <| mark_claims claims fabric).getD 0 86 | 87 | end AoC2018D3 88 | -------------------------------------------------------------------------------- /Fad/Chapter12-Ex.lean: -------------------------------------------------------------------------------- 1 | import Fad.Chapter1 2 | import Fad.Chapter12 3 | 4 | namespace Chapter12 5 | open Chapter1 (concatMap) 6 | 7 | /- # Exercicio 12.1 8 | a definição recursiva de `parts` provavelmente teria uma prova mais simples 9 | mas não consigo usar na prova por ser `partial`. 10 | -/ 11 | 12 | example : ∀ xs : List Nat, (parts₁ xs).length = 2^(xs.length - 1) := by 13 | intro xs 14 | induction xs with 15 | | nil => simp [parts₁] 16 | | cons x xs ih => 17 | sorry 18 | 19 | /- # Exercício 12.2 20 | como parts é partial, não consigo expandir em uma prova. E `parts₁` 21 | obviamente não é a primeira definição de `parts` tratada no exercício. 22 | -/ 23 | 24 | example : parts₁ [1,2] = [[[1], [2]], [[1, 2]]] := by 25 | unfold parts₁ 26 | rw [List.foldr]; rw [Function.comp] 27 | rw [List.foldr]; rw [Function.comp] 28 | simp [concatMap, Chapter1.concat1] 29 | simp [List.foldr] 30 | simp [cons] 31 | simp [extendl] 32 | simp [cons] 33 | simp [glue] 34 | 35 | 36 | /- # Exercicio 12.3 -/ 37 | 38 | def parts₃ {α : Type} : List α → List (Partition α) := 39 | let step (x : α) : List (Partition α) → List (Partition α) 40 | | [[]] => [[[x]]] 41 | | ps => ps.map (cons x) ++ ps.map (glue x) 42 | List.foldr step [[]] 43 | 44 | -- #eval parts₃ [1,2,3] 45 | 46 | 47 | /- # Exercicio 12.4 -/ 48 | 49 | section 50 | open List (filter all head!) 51 | 52 | def ok (xs : List Nat) : Bool := xs.all (· > 0) 53 | 54 | def okextendl (x : Nat) : Partition Nat → List (Partition Nat) := 55 | filter (ok ∘ head!) ∘ extendl x 56 | 57 | variable (h : ∀ x ps, filter (flip all ok) (concatMap (extendl x) ps) = 58 | concatMap (okextendl x) (filter (flip all ok) ps)) 59 | 60 | example 61 | : List.filter (flip List.all ok) ∘ parts₁ = List.foldr (concatMap ∘ okextendl) [[]] := by 62 | funext xs 63 | unfold parts₁ 64 | simp [Function.comp] 65 | induction xs with 66 | | nil => 67 | simp [List.foldr, concatMap, flip] 68 | | cons a as ih => 69 | have h1 := h a 70 | sorry 71 | 72 | end 73 | 74 | /- # Exercicio 12.5 -/ 75 | 76 | section 77 | open List (range head! tail zipWith and) 78 | 79 | def leftmin (xs : List Nat) := xs.all (head! xs ≤ ·) 80 | def rightmax (xs : List Nat) := xs.all (· ≤ last xs) 81 | def ordered (xs : List Nat) := and $ zipWith (· ≤ ·) xs (tail xs) 82 | def nmatch (xs : List Nat) := and $ zipWith (· ≠ ·) xs (range xs.length) 83 | 84 | def suffixClosed (p : List Nat → Bool) : Prop := 85 | ∀ xs ys, p (xs ++ ys) → p ys 86 | 87 | def prefixClosed (p : List Nat → Bool) : Prop := 88 | ∀ xs ys, p (xs ++ ys) → p xs 89 | 90 | example : suffixClosed leftmin := by 91 | unfold suffixClosed 92 | intro xs ys 93 | unfold leftmin; simp 94 | intro h 95 | sorry 96 | 97 | 98 | end 99 | 100 | 101 | end Chapter12 102 | -------------------------------------------------------------------------------- /AoC/AoC_2015/day3.lean: -------------------------------------------------------------------------------- 1 | -- Problem: https://adventofcode.com/2015/day/3 2 | 3 | namespace AoC2015D3 4 | 5 | -- Part 1: 6 | 7 | def input : String := include_str "../../data/AoC2015_day3.txt" 8 | 9 | -- Define the type of coordinates (a pair of integers) 10 | def coord := (Int × Int) 11 | 12 | -- Provide an instance of BEq for coord to allow comparison 13 | instance : BEq coord := 14 | ⟨fun (a b : coord) => a.1 == b.1 && a.2 == b.2⟩ 15 | 16 | def move (pos : coord) (dir : Char) : coord := 17 | match dir with 18 | | '>' => (pos.1 + 1, pos.2) -- move east 19 | | '<' => (pos.1 - 1, pos.2) -- move west 20 | | '^' => (pos.1, pos.2 + 1) -- move north 21 | | 'v' => (pos.1, pos.2 - 1) -- move south 22 | | _ => pos -- invalid direction, stay in place 23 | 24 | def is_visited (pos : coord) (visited : List coord) : Bool := 25 | visited.contains pos 26 | 27 | -- Add a coordinate to the list if it has not been visited 28 | def visit (pos : coord) (visited : List coord) : List coord := 29 | if is_visited pos visited then visited else pos :: visited 30 | 31 | -- Compute the number of unique houses visited 32 | def count_unique_houses (directions : String) : Nat := 33 | let initialPos : coord := (0, 0) -- Santa starts at (0, 0) 34 | let moves := directions.toList.foldl 35 | (fun (state : coord × List coord) (dir : Char) => 36 | let newPos := move state.1 dir -- move Santa and update position 37 | (newPos, visit newPos state.2)) 38 | (initialPos, [initialPos]) 39 | moves.2.length -- return the length of the list of visited houses 40 | 41 | 42 | -- #eval count_unique_houses input 43 | 44 | -- Part2 45 | -- Compute the number of unique houses visited by Santa and Robo-Santa 46 | def count_unique_houses_with_robot (directions : String) : Nat := 47 | let initialPos : coord := (0, 0) -- both Santa and Robo-Santa start at (0, 0) 48 | let moves := directions.toList.foldl 49 | (fun (state : (coord × coord) × List coord × Nat) (dir : Char) => 50 | let (santaPos, roboPos) := state.1 -- current positions of Santa and Robo-Santa 51 | let visited := state.2.1 -- list of visited positions 52 | let turn := state.2.2 -- turn number (even for Santa, odd for Robo-Santa) 53 | let (newPos, newVisited) := 54 | if turn % 2 == 0 then -- Santa's turn 55 | let newSantaPos := move santaPos dir 56 | (newSantaPos, visit newSantaPos visited) 57 | else -- Robo-Santa's turn 58 | let newRoboPos := move roboPos dir 59 | (newRoboPos, visit newRoboPos visited) 60 | ((if turn % 2 == 0 then newPos else santaPos, if turn % 2 == 1 then newPos else roboPos), newVisited, turn + 1)) 61 | ((initialPos, initialPos), [initialPos], 0) 62 | moves.2.1.length -- return the length of the list of visited houses 63 | 64 | 65 | -- #eval count_unique_houses_with_robot input 66 | 67 | end AoC2015D3 68 | -------------------------------------------------------------------------------- /lake-manifest.json: -------------------------------------------------------------------------------- 1 | {"version": "1.1.0", 2 | "packagesDir": ".lake/packages", 3 | "packages": 4 | [{"url": "https://github.com/leanprover-community/mathlib4", 5 | "type": "git", 6 | "subDir": null, 7 | "scope": "leanprover-community", 8 | "rev": "b202b867947571f7d316d1f591be7e759dc0165c", 9 | "name": "mathlib", 10 | "manifestFile": "lake-manifest.json", 11 | "inputRev": "master", 12 | "inherited": false, 13 | "configFile": "lakefile.lean"}, 14 | {"url": "https://github.com/leanprover-community/plausible", 15 | "type": "git", 16 | "subDir": null, 17 | "scope": "leanprover-community", 18 | "rev": "8e5cb8d424df462f84997dd68af6f40e347c3e35", 19 | "name": "plausible", 20 | "manifestFile": "lake-manifest.json", 21 | "inputRev": "v4.15.0-rc1", 22 | "inherited": true, 23 | "configFile": "lakefile.toml"}, 24 | {"url": "https://github.com/leanprover-community/LeanSearchClient", 25 | "type": "git", 26 | "subDir": null, 27 | "scope": "leanprover-community", 28 | "rev": "d7caecce0d0f003fd5e9cce9a61f1dd6ba83142b", 29 | "name": "LeanSearchClient", 30 | "manifestFile": "lake-manifest.json", 31 | "inputRev": "main", 32 | "inherited": true, 33 | "configFile": "lakefile.toml"}, 34 | {"url": "https://github.com/leanprover-community/import-graph", 35 | "type": "git", 36 | "subDir": null, 37 | "scope": "leanprover-community", 38 | "rev": "ed3b856bd8893ade75cafe13e8544d4c2660f377", 39 | "name": "importGraph", 40 | "manifestFile": "lake-manifest.json", 41 | "inputRev": "v4.15.0-rc1", 42 | "inherited": true, 43 | "configFile": "lakefile.toml"}, 44 | {"url": "https://github.com/leanprover-community/ProofWidgets4", 45 | "type": "git", 46 | "subDir": null, 47 | "scope": "leanprover-community", 48 | "rev": "2b000e02d50394af68cfb4770a291113d94801b5", 49 | "name": "proofwidgets", 50 | "manifestFile": "lake-manifest.json", 51 | "inputRev": "v0.0.48", 52 | "inherited": true, 53 | "configFile": "lakefile.lean"}, 54 | {"url": "https://github.com/leanprover-community/aesop", 55 | "type": "git", 56 | "subDir": null, 57 | "scope": "leanprover-community", 58 | "rev": "43bcb1964528411e47bfa4edd0c87d1face1fce4", 59 | "name": "aesop", 60 | "manifestFile": "lake-manifest.json", 61 | "inputRev": "master", 62 | "inherited": true, 63 | "configFile": "lakefile.toml"}, 64 | {"url": "https://github.com/leanprover-community/quote4", 65 | "type": "git", 66 | "subDir": null, 67 | "scope": "leanprover-community", 68 | "rev": "ad942fdf0b15c38bface6acbb01d63855a2519ac", 69 | "name": "Qq", 70 | "manifestFile": "lake-manifest.json", 71 | "inputRev": "v4.14.0", 72 | "inherited": true, 73 | "configFile": "lakefile.lean"}, 74 | {"url": "https://github.com/leanprover-community/batteries", 75 | "type": "git", 76 | "subDir": null, 77 | "scope": "leanprover-community", 78 | "rev": "74dffd1a83cdd2969a31c9892b0517e7c6f50668", 79 | "name": "batteries", 80 | "manifestFile": "lake-manifest.json", 81 | "inputRev": "main", 82 | "inherited": true, 83 | "configFile": "lakefile.toml"}, 84 | {"url": "https://github.com/leanprover/lean4-cli", 85 | "type": "git", 86 | "subDir": null, 87 | "scope": "leanprover", 88 | "rev": "0c8ea32a15a4f74143e4e1e107ba2c412adb90fd", 89 | "name": "Cli", 90 | "manifestFile": "lake-manifest.json", 91 | "inputRev": "main", 92 | "inherited": true, 93 | "configFile": "lakefile.toml"}], 94 | "name": "fad", 95 | "lakeDir": ".lake"} 96 | -------------------------------------------------------------------------------- /AoC/AoC_2015/day6.lean: -------------------------------------------------------------------------------- 1 | import Std.Internal.Parsec 2 | 3 | -- Problem: https://adventofcode.com/2015/day/6 4 | 5 | namespace AoC2015D6 6 | 7 | open Std.Internal.Parsec 8 | open Std.Internal.Parsec.String 9 | 10 | def content : String := include_str "../../data/AoC2015_day6.txt" 11 | 12 | def input := content.split (· == '\n') |>.filter (· ≠ "") 13 | 14 | -- utils 15 | 16 | inductive Action where 17 | | TurnOn 18 | | TurnOff 19 | | Toggle 20 | | Invalid 21 | deriving Repr 22 | 23 | structure Command where 24 | action : Action 25 | x : Nat × Nat 26 | y : Nat × Nat 27 | deriving Repr 28 | 29 | 30 | def Grid (α : Type) : Type := List (List α) 31 | 32 | def parseCommand : Parser Command := do 33 | let a ← pstring "turn on" <|> pstring "turn off" <|> pstring "toggle" 34 | let _ ← pstring " " 35 | let x1 ← digits <* pstring "," 36 | let x2 ← digits <* pstring " through " 37 | let y1 ← digits <* pstring "," 38 | let y2 ← digits 39 | return { action := match a with 40 | | "turn on" => Action.TurnOn 41 | | "turn off" => Action.TurnOff 42 | | "toggle" => Action.Toggle 43 | | _ => Action.Invalid, 44 | x := (x1, x2), 45 | y := (y1, y2) } 46 | 47 | -- #eval parseCommand "toggle 223,39 through 454,511".mkIterator 48 | 49 | def parseInstructions (is : List String) : List Command := 50 | let p := Std.Internal.Parsec.String.Parser.run parseCommand 51 | let d := { action := Action.Invalid, x := (0, 0), y := (0, 0) } 52 | is.map 53 | (λ i => match p i with 54 | | Except.ok c => c 55 | | Except.error _ => d) 56 | 57 | 58 | def applyCommand (grid : Grid α) (cmd : Command) (f : Action → α → α) : Grid α := 59 | grid.enum.map (λ ⟨i, row⟩ => 60 | if cmd.x.1 ≤ i ∧ i ≤ cmd.y.1 then 61 | row.enum.map (λ ⟨j, light⟩ => 62 | if cmd.x.2 ≤ j ∧ j ≤ cmd.y.2 then 63 | f cmd.action light 64 | else light) 65 | else row) 66 | 67 | 68 | def count (f : α → Nat) (grid : Grid α) : Nat := 69 | grid.foldl (λ acc row => 70 | acc + row.foldl (λ acc2 light => acc2 + f light) 0) 0 71 | 72 | 73 | def grid2pgm (grid : Grid α) (f : α → Nat) : String := 74 | let ms := grid.map (λ r => r.map f |>.max? |>.getD 0) 75 | let mt := ms.max? |>.getD 0 76 | let header := ["P2", "# test ", "1000 1000", s!"{mt}"] 77 | let fmt (line : List α) := String.intercalate " " (line.map (toString ∘ (mt - ·) ∘ f)) 78 | let lines := grid.map fmt 79 | String.intercalate "\n" (header ++ lines) ++ "\n" 80 | 81 | 82 | def solve (f : Action → α → α) : Grid α → List Command → Grid α 83 | | g, [] => g 84 | | g, (c::cs) => solve f (applyCommand g c f) cs 85 | 86 | 87 | -- Part 1 88 | 89 | def initGrid₁ : Grid Bool := 90 | List.replicate 1000 (List.replicate 1000 false) 91 | 92 | def applyAction₁ : Action → Bool → Bool 93 | | Action.TurnOn , _ => true 94 | | Action.TurnOff, _ => false 95 | | Action.Toggle , l => ¬ l 96 | | _ , l => l 97 | 98 | -- #eval count (λ c => cond c 1 0) $ solve applyAction₁ initGrid₁ $ parseInstructions input 99 | 100 | -- Part 2 101 | 102 | def initGrid₂ : Grid Nat := 103 | List.replicate 1000 (List.replicate 1000 0) 104 | 105 | def applyAction₂ : Action → Nat → Nat 106 | | Action.TurnOn , n => n + 1 107 | | Action.TurnOff, n => n - 1 108 | | Action.Toggle , n => n + 2 109 | | _ , n => n 110 | 111 | -- #eval count id $ solve applyAction₂ initGrid₂ $ parseInstructions input 112 | -- #eval solve applyAction₂ initGrid₂ [{action := Action.TurnOn, x := (0, 0), y := (0, 0)}] 113 | 114 | end AoC2015D6 115 | -------------------------------------------------------------------------------- /Fad/Chapter7-Ex.lean: -------------------------------------------------------------------------------- 1 | import Fad.Chapter6 2 | import Fad.Chapter7 3 | 4 | namespace Chapter7 5 | 6 | /- # Exercicio 7.1 -/ 7 | 8 | def minimumBy {α : Type} (cmp : α → α → Ordering) : List α → Option α 9 | | [] => none 10 | | x :: xs => 11 | some (xs.foldr (λ y r => cond (cmp y r = Ordering.lt) y r) x) 12 | 13 | def minWith₁ {α β : Type} [Ord β] (f : α → β) (xs : List α) : Option α := 14 | let helper (x y : α) : Ordering := compare (f x) (f y) 15 | minimumBy helper xs 16 | 17 | def minWith₂ {α β : Type} [Ord β] (f : α → β) (xs : List α) : Option α := 18 | let tuple (x : α) : β × α := (f x, x) 19 | let cmp (x y : β × α) := compare x.1 y.1 20 | minimumBy cmp (xs.map tuple) >>= (λ pair => some pair.2) 21 | 22 | instance : Ord Float where 23 | compare x y := 24 | if x < y then Ordering.lt 25 | else if x == y then Ordering.eq 26 | else Ordering.gt 27 | 28 | 29 | /- # Exercicio 7.2 -/ 30 | 31 | def minsWith {α β : Type} [Ord β] (f : α → β) (xs : List α) : List α := 32 | let step (x : α) (ys : List α) : List α := 33 | match ys with 34 | | [] => [x] 35 | | y :: ys => 36 | match compare (f x) (f y) with 37 | | Ordering.lt => [x] 38 | | Ordering.eq => x :: y :: ys 39 | | Ordering.gt => y :: ys 40 | xs.foldr step [] 41 | 42 | def minsWith' {α β : Type} [Ord β] (f : α → β) (xs : List α) : List α := 43 | let step (x : β × α) (ys : List (β × α)) := 44 | match ys with 45 | | [] => [x] 46 | | y :: ys => 47 | match compare (x.fst) (y.fst) with 48 | | Ordering.lt => [x] 49 | | Ordering.eq => x :: y :: ys 50 | | Ordering.gt => y :: ys 51 | xs.map tuple |>.foldr step [] |>.map (·.snd) 52 | where tuple x := (f x, x) 53 | 54 | 55 | /- # Exercicio 7.4 -/ 56 | 57 | def interleave {α : Type} : List α → List α → List (List α) 58 | | xs, [] => [xs] 59 | | [], ys => [ys] 60 | | x :: xs, y :: ys => 61 | let l := interleave xs (y :: ys) |>.map (x :: ·) 62 | let r := interleave (x :: xs) ys |>.map (y :: ·) 63 | l ++ r 64 | 65 | def cp {α β : Type} (xs : List α) (ys : List β) : List (α × β) := 66 | Chapter1.concatMap (λ x ↦ ys.map (λ y ↦ (x, y))) xs 67 | 68 | def perms₁ {α : Type} : List α → List (List α) 69 | | [] => [[]] 70 | | [x] => [[x]] 71 | | xs => 72 | let p := List.splitInTwo (Subtype.mk xs rfl) 73 | let yss := perms p.1 74 | let zss := perms p.2 75 | Chapter1.concatMap (Function.uncurry interleave) (cp yss zss) 76 | 77 | 78 | /- # Exercicio 7.5 -/ 79 | 80 | open Chapter6 (minimum) 81 | 82 | example (x : Nat) 83 | : minimum ([].map (x :: ·)) ≠ x :: (minimum []) := by 84 | rw [List.map] 85 | rw [minimum] 86 | rw [Chapter6.foldr1] 87 | exact List.ne_cons_self 88 | 89 | 90 | /- # Exercicio 7.10 -/ 91 | 92 | def pick₁ : List Nat → Option (Nat × List Nat) 93 | | [] => none 94 | | [x] => some (x, []) 95 | | x :: xs => 96 | match pick₁ xs with 97 | | none => none 98 | | some (y, ys) => 99 | if x ≤ y then some (x, xs) else some (y, x :: ys) 100 | 101 | 102 | -- # Exercicio 7.12 103 | 104 | namespace S73 105 | 106 | /- response: both have the same 'cost' (here 'sum') and 107 | 'minWith' takes the first smallest from the list. 108 | If `mktuples` produces decreasing order, it would 109 | return the other `minimum`. -/ 110 | 111 | def mktuples' : List Denom → Nat → List Tuple 112 | | [1] , n => [[n]] 113 | | [] , _ => panic! "mktuples: invalid empty list" 114 | | d :: ds, n => 115 | let rs := List.iota (n / d + 1) |>.map (· - 1) 116 | rs.flatMap (λ c => mktuples₀ ds (n - c * d) |>.map (λ cs => c :: cs)) 117 | 118 | def mkchange' (ds : List Denom) : Nat → Tuple := 119 | minWith List.sum ∘ mktuples' ds 120 | 121 | -- #eval mkchange₀ [7,3,1] 54 122 | -- #eval mkchange' [7,3,1] 54 123 | 124 | end S73 125 | 126 | /- # Exercicio 7.16 127 | not clear how to prove formally 128 | -/ 129 | 130 | namespace S73 131 | 132 | def urds := [100, 50, 20, 15, 5, 2, 1] 133 | 134 | /- 135 | #eval mkchange₀ urds 30 136 | #eval mkchange₁ urds 30 137 | #eval mkchange urds 30 138 | #eval mkchange₀ [4,3,1] 6 139 | #eval mkchange₁ [4,3,1] 6 140 | -/ 141 | 142 | end S73 143 | 144 | end Chapter7 145 | -------------------------------------------------------------------------------- /Fad/Chapter6.lean: -------------------------------------------------------------------------------- 1 | 2 | import Fad.Chapter1 3 | import Fad.Chapter5 4 | import Fad.«Chapter1-Ex» 5 | import Fad.«Chapter4-Ex» 6 | 7 | namespace Chapter6 8 | open Chapter1 (unwrap until' single) 9 | open Chapter5.S52 (halve length_halve_fst length_halve_snd) 10 | open Chapter5.Quicksort (qsort₁) 11 | open Chapter4.BST2 (partition3) 12 | 13 | -- # Section 6.1: minimum and maximum 14 | 15 | def foldr1 [Inhabited a] (f : a → a → a) : List a → a 16 | | [] => default 17 | | [x] => x 18 | | x::xs => f x (foldr1 f xs) 19 | 20 | def foldl1 [Inhabited a] (f : a → a → a) : List a → a 21 | | [] => default 22 | | x::xs => xs.foldl f x 23 | 24 | def minimum [Inhabited a] [LE a] [DecidableRel (α := a) (· ≤ ·)] 25 | : List a → a := 26 | foldr1 (fun x y => if x ≤ y then x else y) 27 | 28 | def maximum [Inhabited a] [Max a] : List a → a := 29 | foldr1 max 30 | 31 | 32 | def minmax₀ [Inhabited a] [Min a] [Max a] 33 | : List a → (a × a) 34 | | [] => (default, default) 35 | | x::xs => 36 | let op x q := (min x q.1, max x q.2) 37 | xs.foldr op (x,x) 38 | 39 | 40 | def minmax₁ [Inhabited a] [LT a] [DecidableRel (α := a) (· < ·)] 41 | : List a → (a × a) 42 | | [] => (default, default) 43 | | x::xs => 44 | let op x q := 45 | if x < q.1 then (x, q.2) 46 | else if q.2 < x then (q.1, x) 47 | else (q.1, q.2) 48 | xs.foldr op (x,x) 49 | 50 | 51 | def minmax₂ [Inhabited a] [Min a] [Max a] [LE a] [DecidableRel (α := a) (· ≤ ·)] 52 | : List a → (a × a) 53 | | [] => (default, default) 54 | | x::xs => 55 | if h₁ : xs.length = 0 then (x, x) 56 | else if h₂ : xs.length = 1 then 57 | if x ≤ xs.head! then (x, xs.head!) else (xs.head!, x) 58 | else 59 | let p := halve xs 60 | have : (halve xs).fst.length < xs.length := by 61 | simp [length_halve_fst]; omega 62 | have : (halve xs).snd.length < xs.length := by 63 | simp [length_halve_snd]; omega 64 | let q := minmax₂ p.1 65 | let r := minmax₂ p.2 66 | (min q.1 r.1, max q.2 r.2) 67 | termination_by xs => xs.length 68 | 69 | 70 | def pairWith (f : a → a → a) : List a → List a 71 | | [] => [] 72 | | [x] => [x] 73 | | x::y::xs => (f x y) :: pairWith f xs 74 | 75 | def mkPairs [LE a] [DecidableRel (α := a) (· ≤ ·)] 76 | : List a → List (a × a) 77 | | [] => [] 78 | | [x] => [(x, x)] 79 | | x::y::xs => 80 | if x ≤ y then 81 | (x, y) :: mkPairs xs 82 | else 83 | (y, x) :: mkPairs xs 84 | 85 | def minmax [Inhabited a] [LE a] [DecidableRel (α := a) (· ≤ ·)] 86 | [Min a] [Max a] (xs : List a) 87 | : (a × a) := 88 | let op p q := (min p.1 q.1, max p.2 q.2) 89 | (unwrap ∘ until' single (pairWith op) ∘ mkPairs) xs 90 | |>.getD (default, default) 91 | 92 | 93 | -- # Section 6.2: selection from one set 94 | 95 | /- 96 | #check let xs := [1,2,3]; 97 | xs.get (Fin.mk 2 (by simp [List.length]) : Fin xs.length) 98 | -/ 99 | 100 | def select₀ [Inhabited a] [LT a] [DecidableRel (α := a) (· < ·)] 101 | (k : Nat) (xs : List a) : a := 102 | (qsort₁ xs).get! (k - 1) 103 | 104 | 105 | def median [Inhabited a] [LT a] [DecidableRel (α := a) (· < ·)] 106 | (xs : List a) : a := 107 | let k := (xs.length + 1) / 2 108 | select₀ k xs 109 | 110 | 111 | partial def group [Inhabited a] (n : Nat) (xs : List a) : List (List a) := 112 | match xs with 113 | | [] => [] 114 | | xs => 115 | let p := xs.splitAt n 116 | p.1 :: (group n p.2) 117 | 118 | 119 | /-- `qsort₁` or `qsort` ? -/ 120 | def medians [Inhabited a] [LT a] 121 | [DecidableRel (α := a) (· < ·)] [DecidableRel (α := a) (· = ·)] 122 | : List a → List a := 123 | let middle (xs : List a) := xs.get! (((xs.length + 1) / 2) - 1) 124 | List.map (middle ∘ qsort₁) ∘ group 5 125 | 126 | 127 | /- `select₀` or `select` ? -/ 128 | def pivot [Inhabited a] [LT a] 129 | [DecidableRel (α := a) (· < ·)] [DecidableRel (α := a) (· = ·)] 130 | : List a → a 131 | | [x] => x 132 | | xs => 133 | let median xs := select₀ ((xs.length + 1) / 2) xs 134 | median (medians xs) 135 | 136 | 137 | partial def qsort [Inhabited a] [LT a] 138 | [DecidableRel (α := a) (· < ·)] [DecidableRel (α := a) (· = ·)] 139 | : List a → List a 140 | | [] => [] 141 | | xs => 142 | let p := partition3 (pivot xs) xs 143 | qsort p.1 ++ p.2.1 ++ qsort p.2.2 144 | 145 | 146 | /- this function breaks with k > xs.length -/ 147 | partial def select [Inhabited a] [LT a] 148 | [DecidableRel (α := a) (· < ·)] [DecidableRel (α := a) (· = ·)] 149 | (k : Nat) (xs : List a) : a := 150 | match partition3 (pivot xs) xs with 151 | | (us, vs, ws) => 152 | let m := us.length 153 | let n := vs.length 154 | if k ≤ m then select k us 155 | else if k ≤ m + n then vs.get! (k - m - 1) 156 | else if k > m + n then select (k - m - n) ws 157 | else panic! "unreachable code" 158 | 159 | 160 | end Chapter6 161 | -------------------------------------------------------------------------------- /Fad/Chapter7.lean: -------------------------------------------------------------------------------- 1 | import Fad.Chapter1 2 | import Fad.Chapter6 3 | 4 | namespace Chapter7 5 | 6 | -- 7.1 A generic greedy algorithm 7 | 8 | def NonEmptyList (α : Type) : Type := 9 | {l : List α // l.length > 0} 10 | 11 | def foldr1₀ (f : a → a → a) (as : NonEmptyList a) : a := 12 | let x := as.val.head (List.ne_nil_of_length_pos as.property) 13 | if h₂ : as.val.length = 1 then 14 | x 15 | else 16 | let as' := as.val.tail 17 | have : as.val.length - 1 < as.val.length := by 18 | have h₁ := as.property; omega 19 | f x (foldr1₀ f (Subtype.mk as' (by 20 | -- change as.val.tail.length > 0 21 | have h₁ := as.property 22 | rw [List.length_tail] 23 | omega))) 24 | termination_by as.val.length 25 | 26 | -- #eval foldr1₀ (fun a b => a + b ) (Subtype.mk [1,2,3,4,5,6] (by simp)) 27 | 28 | def foldr1₁ (f : a → a → a) (as : List a) (h : as.length > 0 := by decide) : a := 29 | let x := as.head (List.ne_nil_of_length_pos h) 30 | if h₂ : as.length = 1 then 31 | x 32 | else 33 | f x (foldr1₁ f as.tail (by rw [List.length_tail]; omega)) 34 | 35 | -- #eval foldr1₁ (fun a b => a + b ) [1,2,3] 36 | 37 | def foldr1 [Inhabited a] (f : a → a → a) : List a → a 38 | | [] => default 39 | | x::xs => xs.foldr f x 40 | 41 | example : foldr1 (fun a b => a + b ) [1,2,3] = 6 := by rfl 42 | 43 | def minWith {a b : Type} [LE b] [Inhabited a] [DecidableRel (α := b) (· ≤ ·)] 44 | (f : a → b) (as : List a) : a := 45 | let smaller f x y := cond (f x ≤ f y) x y 46 | foldr1 (smaller f) as 47 | 48 | 49 | -- 7.2 Greedy sorting algorithms 50 | 51 | open Chapter1 (tails) in 52 | 53 | def pairs (xs : List a) : List (Prod a a) := 54 | let step₁ : List a → List (Prod a a) → List (Prod a a) 55 | | [], acc => acc 56 | | x::ys, acc => 57 | let step₂ : List a → List (Prod a a) → List (Prod a a) 58 | | [], acc => acc 59 | | y::_, acc => (x, y) :: acc 60 | (tails ys).foldr step₂ acc 61 | (tails xs).foldr step₁ [] 62 | 63 | 64 | def ic [LT a] [DecidableRel (@LT.lt a _)] 65 | (xs : List a) : Nat := 66 | (pairs xs).filter (λ p => p.1 > p.2) |>.length 67 | 68 | 69 | def extend : a → List a → List (List a) 70 | | x, [] => [[x]] 71 | | x, (y::xs) => (x :: y :: xs) :: (extend x xs).map (y:: ·) 72 | 73 | 74 | open Chapter1 in 75 | 76 | def perms : List a → List (List a) := 77 | List.foldr (concatMap ∘ extend) [[]] 78 | 79 | def sort [LT a] [DecidableRel (@LT.lt a _)] 80 | : List a → List a := 81 | minWith ic ∘ perms 82 | 83 | 84 | def gstep [LT a] [DecidableRel (@LT.lt a _)] 85 | (x : a) : List a → List a := 86 | (minWith ic) ∘ extend x 87 | 88 | 89 | def picks (xs : List a) : List (a × List a) := 90 | let rec helper : a → List (a × List a) → List (a × List a) 91 | | _, [] => [] 92 | | x, ((y, ys) :: rest) => (y, x :: ys) :: (helper x rest) 93 | match xs with 94 | | [] => [] 95 | | x :: xs => (x, xs) :: helper x (picks xs) 96 | 97 | 98 | def pick [LE a] [h : DecidableRel (α := a) (· ≤ ·)] [Inhabited a] 99 | (xs : List a) : (a × List a) := 100 | match picks xs with 101 | | [] => (default, []) -- unreachable 102 | | [p] => p 103 | | p :: ps => 104 | let rec aux : (a × List a) → List (a × List a) → (a × List a) 105 | | (x, xs), [] => (x, xs) 106 | | (x, xs), (y, ys) :: rest => 107 | if x ≤ y then aux (x, xs) rest else aux (y, ys) rest 108 | aux p ps 109 | 110 | 111 | -- 7.3 Coin-changing 112 | 113 | namespace S73 114 | 115 | abbrev Denom := Nat 116 | abbrev Tuple := List Nat 117 | 118 | instance : Max Tuple where 119 | max x y := if x > y then x else y 120 | 121 | def usds : List Denom := [100,50,25,10,5,1] 122 | def ukds : List Denom := [200,100,50,20,10,5,2,1] 123 | 124 | def amount (ds : List Denom) (cs : Tuple) : Nat := 125 | List.sum (ds.zipWith (· * ·) cs) 126 | 127 | -- #eval amount usds [2,1,0,0,1,1] 128 | 129 | def mktuples₀ : List Denom → Nat → List Tuple 130 | | [1] , n => [[n]] 131 | | [] , _ => panic! "mktuples: invalid empty list" 132 | | d :: ds, n => 133 | (List.range (n / d + 1)).flatMap (λ c => 134 | mktuples₀ ds (n - c * d) |>.map (λ cs => c :: cs)) 135 | 136 | def mkchange₀ (ds : List Denom) : Nat → Tuple := 137 | minWith List.sum ∘ mktuples₀ ds 138 | 139 | def mkchange₁ (ds : List Denom) : Nat → Tuple := 140 | Chapter6.maximum ∘ mktuples₀ ds 141 | 142 | /- 143 | #eval mkchange₀ [7,3,1] 54 144 | #eval mkchange₁ [7,3,1] 54 145 | -/ 146 | 147 | partial def mktuples₁ : List Denom → Nat → List Tuple 148 | | [1] , n => [[n]] 149 | | [] , _ => panic! "mktuples: invalid empty list" 150 | | d :: ds, n => 151 | let extend ds c := mktuples₁ ds (n - c * d) |>.map (c :: ·) 152 | Chapter1.concatMap (extend ds) (List.range (n / d + 1)) 153 | 154 | /- 155 | #eval mktuples₀ [7,3,1] 23 156 | #eval mktuples₁ [7,3,1] 23 157 | -/ 158 | 159 | def mkchange : List Denom → Nat → Tuple 160 | | [1] , n => [n] 161 | | [] , _ => panic! "mkchange: invalid empty list" 162 | | d :: ds, n => 163 | let c := n / d 164 | c :: mkchange ds (n - c * d) 165 | 166 | /- 167 | #eval mkchange ukds 256 168 | #eval mkchange usds 256 169 | #eval mkchange [7,3,1] 54 170 | -/ 171 | 172 | end S73 173 | 174 | end Chapter7 175 | -------------------------------------------------------------------------------- /data/AoC2015_day1.txt: -------------------------------------------------------------------------------- 1 | ((((()(()(((((((()))(((()((((()())(())()(((()((((((()((()(()(((()(()((())))()((()()())))))))))()((((((())((()))(((((()(((((((((()()))((()(())()((())((()(()))((()))()))()(((((()(((()()))()())((()((((())()())()((((())()(()(()(((()(())(()(())(((((((())()()(((())(()(()(()(())))(()((((())((()))(((()(()()(((((()()(()(((()(((((())()))()((()(()))()((()((((())((((())(()(((())()()(()()()()()(())((((())((())(()()))()((((())))((((()())()((((())((()())((())(())(((((()((((()(((()((((())(()(((()()))()))((((((()((())()())))(((()(()))(()()(()(((()(()))((()()()())((()()()(((())())()())())())((()))(()(()))(((((()(()(())((()(())(())()((((()())()))((((())(())((())())((((()(((())(())((()()((((()((((((()(())()()(()(()()((((()))(())()())()))(())))(())))())()()(())(()))()((()(()(())()()))(()())))))(()))(()()))(())(((((()(()(()()((())()())))))((())())((())(()(())((()))(())(((()((((((((()()()(()))()()(((()))()((()()(())(())())()(()(())))(((((()(())(())(()))))())()))(()))()(()(((((((()((((())))())())())())()((((((((((((((()()((((((()()()())())()())())())(())(())))())((()())((()(()))))))()))))))))))))))))())((())((())()()))))))(((()((()(()()))((())(()()))()()())))(())))))))(()(((())))())()())))()()(())()))()(()))())((()()))))(()))))()))(()()(())))))))()(((()))))()(()))(())())))))()))((()))((()))())(())))))))))((((())()))()))()))())(())()()(())))())))(()())()))((()()(())))(())((((((()(())((()(((()(()()(())))()))))))()))()(()((()))()(()))(()(((())((((())())(())(()))))))))())))))))())())))))())))))()()(((())()(()))))))))())))))(())()()()))()))()))(()(())()()())())))))))())()(()(()))))()()()))))())(()))))()()))))()())))))(((())()()))(()))))))))))()()))))()()()))))(()())())()()())()(()))))()(()))(())))))))(((((())(())())()()))()()))(())))))()(()))))(())(()()))()())()))()))()))()))))())()()))())())))(()))(()))))))())()(((())()))))))))()))()())))())))())))()))))))))))()()))(()()))))))(())()(()))))())(()))))(()))))(()())))))())())()()))))())()))))))))(()))))()))))))()(()())))))))()))())))())))())))())))))))())(()()))))))(()())())))()())()))))))))))))))())))()(())))()))())()()(())(()()))(())))())()())(()(()(()))))())))))))))))())(()))()))()))))(())()())()())))))))))))()()))))))))))))())())))))(()())))))))))))())(())))()))))))))())())(()))()))(())))()))()()(())()))))))()((((())()))())())))))()))()))))((()())()))))())))(())))))))))))))))))()))))()()())()))()()))))())()))((()())))())))(()))(()())))))))()))()))))(())))))))(())))))())()()(()))())()))()()))))())()()))))())()))())))))))(()))))()())()))))))))(()))())))(()))()))))(())()))())())(())())())))))))((((())))))()))()))()())()(())))()))()))()())(()())()()(()())()))))())())))))(()))()))))())(()()(())))))(())()()((())())))))(())(())))))))())))))))))()(())))))))()())())())()(()))))))))(()))))))))())()()))()(()))))))()))))))())))))))(())))()()(())()())))))(((())))()((())()))())))(()()))())(())())))()(((()())))))()(()()())))()()(()()(()()))())()(()()()))())()()))()())(()))))())))))())))(())()()))))(()))))(())(()))(())))))()()))()))))())()))()()(())())))((()))())()))))))()()))))((()(()))))()()))))))())))))())(()((()())))))))))))()())())))()))(()))))))(()))(())()())))(()))))))))())()()()()))))(()())))))))((())))()))(()))(())(())()())()))))))))(())))())))(()))()()))(()()))(()))())))()(())))())((()((()(())))((())))()))))((((())())()())))(())))()))))))())(()()((())))())()(()())))))(()())()))())))))))((())())))))))(()(()))())()()(()()(((()(((()())))))()))))))()(())(()()((()()(())()()))())()())()))()())())())))))))(((())))))))()()))))))(((())()))(()()))(()()))))(()(()()((((())()())((()()))))(()(())))))()((()()()())()()((()((()()))(()))(((()()()))(((())))()(((())()))))))((()(())())))(()())(((((()(()))(()((()))(()())()))))(()(()))()(()))(())(((())(()()))))()()))(((()))))(()()()()))())))((()()()(())()))()))))()()))()))))))((((((()()()))))())((()()(((()))))(()(())(()()())())())))()(((()()))(())((())))(()))(()()()())((())())())(()))))()))()((()(())()(()()(())(()))(())()))(())(()))))(())(())())(()()(()((()()((())))((()))()((())))(((()()()()((((()))(()()))()()()(((())((())())(()()(()()()))()((())(())()))())(((()()(())))()((()()())()())(()(())())(((())(())())((())(())()(((()()))(())))((())(()())())(())((()()()((((((())))((()(((((())()))()))(())(()()))()))(())()()))(())((()()())()()(()))())()((())))()((()()())((((()())((())())())((()((()))()))((())((()()(()((()()(((())(()()))))((()((())()(((())(()((())())((())(()((((((())())()(()())()(())(((())((((((()(())(()((()()()((()()(()()()())))()()(((((()()))()((((((()))()(()(()(()(((()())((()))())()((()))(())))()))()()))())()()))())((((())(()(()))(((((((())(((()(((((()(((()()((((())(((())())))(()()()(()(()))()))((((((()))((()(((()(())((()((((()((((((())(((((())))(((()(()))))(((()(((())()((())(()((()))(((()()(((())((((()(()(((((()))(((()(((((((()(()()()(()(()(()()())(())(((((()(())())()())(()(()(()))()(()()()())(()()(()((()))()((())())()(()))((())(()))()(()))()(((()(()(()((((((()()()()())()(((((()()(((()()()((()(((((()))((((((((()()()(((((()))))))(()()()(())(()))(()()))))(())()))(((((()(((((()()(()(()())(((()))((((()((()(()(()((()(()((())))()(((()((()))((()))(((((((((()((()((()(())))()((((()((()()))((())(((()(((((()()(()(()()((()(()()()(((((((())())()())))))((((()()(()))()))(()((())()(()(((((((((()()(((()(()())(()((()())((())())((((()(((()(((()((((()((()((((()(()((((((())((((((((((((()()(()()((((((((((((((()((()()))()((((((((((((())((((()(()())((()(()(()))()(((((()()(((()()))()())(())((()(((((()((())(((((()((()(((((()))()()((((())()((((())(((((((((()(())(()(())))())(()((())(((())(())(())())(()(()(())()()((()((())()(((()(((((()(())))()(((()((())))((()()()(((()(((()((()(()(())(()((()())(()(()(((()(((((((((())(()((((()()))(()((((()()()()(((()((((((((()(()()((((((()(()()(()((()((((((((((()()(((((((()())(())))(((()()))(((((()((()()())(()()((((())((()((((()))))(())((()(()()(((()(()(((()((((()(((((()))())())(()((())()))(((()())((())((())((((()((()((((((())(()((((()()))((((((())()(()))((()(((())((((((((((()()(((((()(((((()((()()()((((())))(()))()((()(())()()((()((((((((((()((())(())(((((()(()(()()))((((()((((()()((()(((()(((((((((()(()((()((()))((((((()(((())()()((()(((((((()())))()()(()((()((()()(((()(()()()()((((()((())((((()(((((((((()(((()()(((()(()(((()(((()((())()(()((()(()(()(()))()(((()))(()((((()((())((((())((((((())(()))(()((((())((()(()((((((((()()((((((()(()(()()()(())((()((()()(((()(((((((()()((()(((((((()))(((((()(((()(()()()(()(((()((()()((())(()(((((((((()(()((()((((((()()((())()))(((((()((())()())()(((((((((((()))((((()()()()())(()()(()(()()))()))(()))(()(((()()))())(()(()))()()((())(()())()())()(()))()))(()()(()((((((())((()(((((((((((()(())()((()(()((()((()(()((()((((((((((()()())((())()(())))((())()())()(((((()(()())((((()((()(())(()))(((())()((()))(((((())(()))()()(()))(((())((((()((((()(())))(((((((()))))())()())(())((())()(()()((()(()))()(()()(()()((()())((())((()()))((((()))()()))(()()(())()()(((((()(())((()((((()))()))(()())())(((()()(()()))(())))))(()))((())(((((()((((()))()((((()))()((())(((())))(((()())))((()(()()(( -------------------------------------------------------------------------------- /Fad/Chapter4-Ex.lean: -------------------------------------------------------------------------------- 1 | import Fad.Chapter4 2 | import Fad.Chapter3 3 | 4 | namespace Chapter4 5 | 6 | /- 4.2 7 | We have smallest (a,b) = x such that f x < t ≤ f (x + 1) 8 | But for t = 1024 and f x = x^2 below f x = t and f (x + 1) > t 9 | 10 | #eval D1.smallest (fun x => dbg_trace "fun {x}"; x * x) 1024 (0, 1024) 11 | #eval (fun x => dbg_trace "fun {x}"; x * x) 32 12 | #eval (fun x => dbg_trace "fun {x}"; x * x) 33 13 | -/ 14 | 15 | /- 4.3 16 | 17 | Vamos provar por indução que 18 | 19 | T(x) = ⌈log(n-1)⌉. 20 | 21 | Para o caso base (n=2) 22 | 23 | T(2) = ⌈log(2-1)⌉ = ⌈log(1)⌉ = ⌈0⌉ = 0. 24 | 25 | Supondo valido para k < n, vamos provar para n. Como ⌈(n+1)/2⌉ < n, 26 | vale a hipotese de indução, então temos que provar que: 27 | 28 | T(n) = ⌈log(⌈(n+1)/2⌉ -1)⌉ + 1 = ⌈log(n-1)⌉. 29 | 30 | Podemos mostrar por desigualdade indireta, mostrando que o lado 31 | esquerdo é menor que k se e somente o lado direito é menor que k, para 32 | qualquer k natural. Pelo lado direito temos que ⌈log(n-1)⌉ <= k <=> 33 | n-1 <= 2^k. Pelo lado esquerdo: 34 | 35 | ⌈log(⌈(n+1)/2⌉ -1)⌉ + 1 <= k <=> ⌈log(⌈(n+1)/2⌉ -1)⌉ <= k-1, 36 | <=> log(⌈(n+1)/2⌉ -1) <= k-1, 37 | <=> ⌈(n+1)/2⌉ -1 <= 2^(k-1), 38 | <=> ⌈(n+1)/2⌉ <= 2^(k-1) + 1, 39 | <=> (n+1)/2 <= 2^(k-1) + 1, 40 | <=> n+1 <= 2^k + 2, 41 | <=> n-1 <= 2^k. 42 | 43 | O que completa a prova, uma vez que ambos os lados chegam na mesma 44 | proposição. 45 | 46 | -/ 47 | 48 | 49 | /- 4.4 : see the book -/ 50 | 51 | /-! 52 | # Exercicio 4.5 53 | 54 | Indexing the coordinates from zero, the positions are 55 | (0, 9), (5, 6), (7, 5), (9, 0) 56 | -/ 57 | 58 | -- # Exercicio 4.6 59 | 60 | -- #eval D2.search₁ (λ (x, y) => x ^ 3 + y ^ 3) 1729 61 | 62 | 63 | -- # Exercicio 4.7 64 | 65 | namespace BST1 66 | 67 | def flatcat : (t : Tree a) → (xs: List a) → List a 68 | | .null, xs => xs 69 | | .node l x r, xs => flatcat l (x :: flatcat r xs) 70 | 71 | def Tree.flatten₁ (t : Tree a) : List a := 72 | flatcat t [] 73 | 74 | 75 | example (t: Tree a) : 76 | t.flatten = t.flatten₁ := by 77 | induction t with 78 | | null => exact rfl 79 | | node l x r ihl ihr => 80 | simp [Tree.flatten₁] 81 | simp [Tree.flatten] 82 | simp [flatcat] 83 | simp [ihl, ihr] 84 | simp [Tree.flatten₁] 85 | sorry 86 | 87 | end BST1 88 | 89 | -- # Exercicio 4.8 90 | 91 | 92 | namespace BST1 93 | 94 | example {α : Type} (t : Tree α) : 95 | t.height ≤ t.size ∧ t.size < 2 ^ t.height := by 96 | apply And.intro 97 | { 98 | induction t with 99 | | null => simp [Tree.height, Tree.size] 100 | | node t₁ x t₂ ihl ihr => 101 | simp [Tree.height, Tree.size] 102 | sorry 103 | } 104 | { 105 | induction t with 106 | | null => simp [Tree.height, Tree.size] 107 | | node t₁ x t₂ ihl ihr => 108 | simp [Tree.height, Tree.size] 109 | sorry 110 | } 111 | 112 | end BST1 113 | 114 | /- 115 | namespace Chapter3 116 | 117 | example {α : Type} (t : Tree α) : 118 | t.height ≤ t.size ∧ t.size < 2 ^ t.height := by 119 | induction t with 120 | | leaf n => 121 | split 122 | case left => 123 | dsimp [Chapter3.Tree.height, Chapter3.Tree.size] 124 | exact nat.le_refl 1 125 | case right => 126 | dsimp [Tree.height, Tree.size] 127 | exact nat.lt_succ_self 1 128 | | node n t₁ t₂ => 129 | cases ih_t₁ with | intro ih_t₁_height ih_t₁_size 130 | cases ih_t₂ with | intro ih_t₂_height ih_t₂_size 131 | split 132 | case left => 133 | dsimp [Tree.height, Tree.size] 134 | exact nat.succ_le_of_lt (max_le ih_t₁_height ih_t₂_height) 135 | case right => 136 | dsimp [Tree.height, Tree.size] 137 | calc 138 | n < 2 ^ (1 + max t₁.height t₂.height) : by linarith [ih_t₁_size, ih_t₂_size] 139 | _ = 2 ^ t.height : by rw max_comm 140 | 141 | end Chapter3 142 | -/ 143 | 144 | -- # Exercise 4.9 145 | 146 | def partition {α : Type} (p : α → Bool) : List α → List α × List α := 147 | let op (x : α) (r : List α × List α) := 148 | if p x then (x :: r.1, r.2) else (r.1, x :: r.2) 149 | List.foldr op ([], []) 150 | 151 | 152 | -- # Exercicio 4.10 153 | 154 | namespace BST2 155 | 156 | def partition3 [LT a] 157 | [DecidableRel (α := a) (· < ·)] [DecidableRel (α := a) (· = ·)] 158 | (y : a) (xs : List a) : (List a × List a × List a) := 159 | let op x acc := 160 | let (us, vs, ws) := acc 161 | if x < y then (x :: us, vs, ws) 162 | else if x = y then (us, x :: vs, ws) 163 | else (us, vs, x :: ws) 164 | xs.foldr op ([], [], []) 165 | 166 | partial def mkTree₁ : (xs : List Nat) → Tree (List Nat) 167 | | [] => .null 168 | | (x :: xs) => 169 | match partition3 x (x :: xs) with 170 | | (us, vs, ws) => node (mkTree₁ us) vs (mkTree₁ ws) 171 | 172 | 173 | end BST2 174 | 175 | /- # Exercicio 4.13 -/ 176 | 177 | namespace DSet 178 | 179 | def merge [LT a] [DecidableEq a] [DecidableRel (α := a) (· < ·)] 180 | : List a → List a → List a 181 | | [], ys => ys 182 | | xs, [] => xs 183 | | (x :: xs), (y :: ys) => 184 | if x < y then x :: merge xs (y :: ys) 185 | else if x = y then x :: merge xs ys 186 | else y :: merge (x :: xs) ys 187 | 188 | end DSet 189 | 190 | 191 | /- # Exercicio 4.14 -/ 192 | 193 | namespace DSet 194 | open BST2 (Tree insert node) 195 | 196 | def union₁ [LT a] [DecidableRel (α := a) (· < ·)] 197 | (t₁ t₂ : Set a) : Set a := 198 | List.foldr insert t₁ (Tree.flatten t₂) 199 | 200 | partial def frm [Inhabited a] (l r : Nat) (xa : Array a) : Set a := 201 | if l = r then .null 202 | else 203 | let m := (l + r) / 2 204 | node (frm l m xa) xa[m]! (frm (m + 1) r xa) 205 | 206 | def build [Inhabited a] [LT a] [DecidableRel (α := a) (· < ·)] 207 | (xs : List a) : Set a := 208 | frm 0 xs.length xs.toArray 209 | 210 | def union₂ [Inhabited a] [LT a] [DecidableEq a] [DecidableRel (α := a) (· < ·)] 211 | (t₁ t₂ : Set a) : Set a := 212 | build $ merge (Tree.flatten t₁) (Tree.flatten t₂) 213 | 214 | -- #eval union₂ (BST2.mkTree [1,2,3]) (BST2.mkTree [4,2,6]) |>.flatten 215 | 216 | end DSet 217 | 218 | 219 | -- # Exercicio 4.16 220 | 221 | namespace BST2 222 | 223 | def balanceL (t₁ : Tree a) (x : a) (t₂ : Tree a) : Tree a := 224 | match t₂ with 225 | | Tree.null => Tree.null 226 | | Tree.node _ l y r => 227 | if l.height ≥ t₁.height + 2 228 | then balance (balanceL t₁ x l) y r 229 | else balance (node t₁ x l) y r 230 | 231 | end BST2 232 | 233 | -- # Exercicio 4.17 234 | 235 | namespace DynamicSet 236 | open BST2 237 | 238 | abbrev Set (α : Type) : Type := Tree α 239 | 240 | def pair (f : α -> β) (p : α × α) : (β × β) := (f p.1, f p.2) 241 | 242 | def split [LT α] [LE α] [DecidableRel (α := α) (· < ·)] [DecidableRel (α := α) (· ≤ ·)] 243 | (x : α) : Set α → Set α × Set α := 244 | pair mkTree ∘ List.partition (· ≤ x) ∘ Tree.flatten 245 | 246 | 247 | end DynamicSet 248 | 249 | end Chapter4 250 | -------------------------------------------------------------------------------- /Fad/Chapter3-Ex.lean: -------------------------------------------------------------------------------- 1 | import Fad.Chapter3 2 | import Fad.Chapter1 3 | import Fad.«Chapter1-Ex» 4 | import Lean.Data.AssocList 5 | 6 | namespace Chapter3 7 | 8 | /- # Exercicio 3.1 -/ 9 | 10 | section 11 | open SL1 12 | 13 | /- 14 | (['a', 'b', 'c'], ['d']) 15 | (['a'], ['d', 'c', 'b']) 16 | (['a', 'b'], ['d', 'c']) 17 | 18 | #eval toSL "abcd".toList 19 | #eval List.foldr consSL nilSL "abcd".toList 20 | #eval List.foldl (flip snocSL) nilSL "abcd".toList 21 | #eval consSL 'a' (snocSL 'd' (List.foldr consSL nilSL "bc".toList)) 22 | -/ 23 | 24 | end 25 | 26 | /- # Exercicio 3.2 -/ 27 | 28 | namespace SL1 29 | 30 | def nullSL {α : Type} : SymList α → Bool 31 | | (xs, ys) => xs.isEmpty ∧ ys.isEmpty 32 | 33 | def singleSL {α : Type} : SymList α → Prop 34 | | (xs, ys) => (xs.isEmpty ∧ ys.single) ∨ (ys.isEmpty ∧ xs.single) 35 | 36 | def lengthSL {α : Type} : SymList α → Nat 37 | | (xs, ys) => xs.length + ys.length 38 | 39 | end SL1 40 | 41 | /- # Exercicio 3.3 -/ 42 | 43 | namespace SL1 44 | 45 | def headSL? {α : Type} : SymList α → Option α 46 | | ([],[]) => none -- why not nilSL? 47 | | ([], ys) => List.head? ys 48 | | (xs, _) => List.head? xs 49 | 50 | end SL1 51 | 52 | /- # Exercicio 3.4 -/ 53 | 54 | namespace SL1 55 | 56 | def initSL {α : Type} : SymList α → Option (SymList α) 57 | | (xs, []) => if xs.isEmpty then none else some nilSL 58 | | (xs, [_]) => 59 | let (us, vs) := xs.splitAt (xs.length / 2) 60 | some (us, vs.reverse) 61 | | (xs, _ :: ys) => some (xs, ys) 62 | 63 | end SL1 64 | 65 | /- # Exercicio 3.5 -/ 66 | 67 | namespace SL1 68 | open Chapter1 (dropWhile) 69 | 70 | def dropWhileSL₁ (p : α → Bool) (sl : SymList α) : SymList α := 71 | let us := sl.1.dropWhile p 72 | if us.isEmpty then toSL (sl.2.reverse.dropWhile p) else (us, sl.2) 73 | 74 | partial def dropWhileSL (p : α → Bool) (sl : SymList α) : SymList α := 75 | if nullSL sl then 76 | nilSL 77 | else 78 | match headSL? sl with 79 | | none => nilSL 80 | | some x => 81 | if p x then 82 | match (tailSL sl) with 83 | | none => nilSL 84 | | some us => dropWhileSL p us 85 | else 86 | sl 87 | 88 | example (p : α → Bool) 89 | : dropWhile p ∘ fromSL = fromSL ∘ dropWhileSL₁ p := by 90 | funext xs 91 | simp [Function.comp] 92 | simp [fromSL] 93 | sorry -- it should not be proved 94 | 95 | 96 | end SL1 97 | 98 | /- # Exercicio 3.6 -/ 99 | 100 | namespace SL1 101 | 102 | partial def initsSL {a : Type} (xs : SymList a) : SymList (SymList a) := 103 | if nullSL xs then 104 | snocSL xs nilSL 105 | else 106 | match (initSL xs) with 107 | | none => nilSL 108 | | some i => snocSL xs (initsSL i) 109 | 110 | 111 | end SL1 112 | 113 | /- # Exercicio 3.7 -/ 114 | 115 | def inits {α : Type} (xs : List α) : List (List α) := 116 | ((List.map List.reverse) ∘ (Chapter1.scanl (flip List.cons) [])) xs 117 | 118 | 119 | /- # Exercicio 3.8 -/ 120 | 121 | def measure (ts : List (Tree a)) : Nat := 122 | ts.foldr (λ t acc => size t + acc) 0 123 | where 124 | size : Tree a → Nat 125 | | Tree.leaf _ => 1 126 | | Tree.node _ t1 t2 => 1 + size t1 + size t2 127 | 128 | def fromTs : List (Tree a) → List a 129 | | [] => [] 130 | | (Tree.leaf x) :: ts => 131 | have : measure ts < measure (Tree.leaf x :: ts) := by 132 | simp [measure,measure.size] 133 | x :: fromTs ts 134 | | (Tree.node n t1 t2) :: ts => 135 | have : measure (t1 :: t2 :: ts) < measure (Tree.node n t1 t2 :: ts) := by 136 | simp [measure, measure.size] 137 | rw [Nat.add_assoc]; simp 138 | fromTs (t1 :: t2 :: ts) 139 | termination_by x1 => measure x1 140 | 141 | 142 | -- 3.10 143 | 144 | def toRA {a : Type} : List a → RAList a := 145 | List.foldr consRA nilRA 146 | 147 | example : ∀ (xs : List a), xs = fromRA (toRA xs) := by 148 | intro xs 149 | induction xs with 150 | | nil => rfl 151 | | cons x xs ih => 152 | simp [toRA, fromRA, consRA] 153 | rw [ih] 154 | match toRA xs with 155 | | [] => rfl 156 | | (Digit.zero :: ds) => 157 | simp [fromRA, fromT, Tree.mk] 158 | rw [concatMap] 159 | sorry 160 | | (Digit.one t :: ds) => 161 | simp [fromRA, fromT, Tree.mk] 162 | rw [concatMap] 163 | sorry 164 | 165 | -- 3.11 166 | 167 | def updateT : Nat → α → Tree α → Tree α 168 | | 0, x, Tree.leaf _ => Tree.leaf x 169 | | _, _, Tree.leaf y => Tree.leaf y -- problem 170 | | k, x, Tree.node n t1 t2 => 171 | let m := n / 2 172 | if k < m then 173 | Tree.node n (updateT k x t1) t2 174 | else 175 | Tree.node n t1 (updateT (k - m) x t2) 176 | 177 | def updateRA : Nat → α → RAList α → RAList α 178 | | _, _, [] => [] 179 | | k, x, Digit.zero :: xs => Digit.zero :: (updateRA k x xs) 180 | | k, x, (Digit.one t) :: xs => 181 | if k < t.size then 182 | (Digit.one $ updateT k x t) :: xs 183 | else 184 | (Digit.one t) :: (updateRA (k- t.size) x xs) 185 | 186 | 187 | -- 3.12 188 | 189 | open Function (uncurry) in 190 | 191 | def updatesRA : RAList α → List (Nat × α) → RAList α 192 | | r, up => List.foldl (flip (uncurry updateRA)) r up 193 | 194 | -- infix: 60 " // " => updatesRA 195 | -- #eval fromRA <| (toRA ['a','b','c']) // [(2, 'x'), (0, 'y')] 196 | 197 | 198 | -- 3.13 199 | 200 | def unconsT : RAList a → Option (Tree a × RAList a) 201 | | [] => none 202 | | Digit.one t :: xs => 203 | if xs.isEmpty then 204 | some (t, []) 205 | else 206 | some (t, Digit.zero :: xs) 207 | | Digit.zero :: xs => 208 | match unconsT xs with 209 | | none => none 210 | | some (Tree.leaf _, _) => none 211 | | some (Tree.node _ t1 t2, ys) => some (t1, Digit.one t2 :: ys) 212 | 213 | def unconsRA (xs : RAList a) : Option (a × RAList a) := 214 | match unconsT xs with 215 | | some (Tree.leaf x, ys) => some (x, ys) 216 | | some (Tree.node _ _ _, _) => none 217 | | none => none 218 | 219 | /- 220 | #eval unconsT <| toRA ([] : List Nat) 221 | #eval do 222 | let a ← unconsRA <| toRA [1,2,3] 223 | pure (a.1, fromRA a.2) 224 | #eval (unconsRA <| toRA [1,2,3]) >>= (fun x => pure (x.1, fromRA x.2)) 225 | -/ 226 | 227 | def headRA (xs : RAList a) : Option a := 228 | Prod.fst <$> unconsRA xs 229 | 230 | def tailRA (xs : RAList a) : Option (RAList a) := 231 | Prod.snd <$> unconsRA xs 232 | 233 | 234 | -- 3.14 235 | 236 | def fa₀ (n : Nat) : Array Nat := 237 | Chapter1.scanl (· * ·) 1 (List.range' 1 n) |>.toArray 238 | 239 | 240 | 241 | -- # Exercicio 3.15 242 | 243 | /- 244 | ghci> import Data.Array 245 | ghci> listArray (0,5) [0..] 246 | array (0,5) [(0,0),(1,1),(2,2),(3,3),(4,4),(5,5)] 247 | ghci> accum (\ a b -> a + b) (listArray (0,5) [0..10]) [(1,10),(2,10)] 248 | array (0,5) [(0,0),(1,11),(2,12),(3,3),(4,4),(5,5)] 249 | ghci> accum (\ a b -> a + b) (listArray (0,5) [0..10]) [(1,10),(1,30)] 250 | array (0,5) [(0,0),(1,41),(2,2),(3,3),(4,4),(5,5)] 251 | -/ 252 | 253 | def accum : (e → v → e) → Array e → List (Nat × v) → Array e 254 | | _, a, [] => a 255 | | f, a, (p :: ps) => 256 | if h : p.1 < a.size then 257 | let i : Fin a.size := Fin.mk p.1 h 258 | accum f (a.set i (f a[i] p.2)) ps 259 | else 260 | accum f a ps 261 | 262 | -- #eval accum (λ a b => a + b) (List.range 5).toArray [(1,10), (1,10), (3,10)] 263 | 264 | def accumArray₁ (f : a → v → a) (e : a) (n : Nat) (is : List (Nat × v)) : Array a := 265 | accum f (Array.mkArray n e) is 266 | 267 | -- #eval accumArray₁ (λ a b => a + b) 0 5 [(1,10), (1,10), (3,10)] 268 | 269 | 270 | end Chapter3 271 | -------------------------------------------------------------------------------- /Fad/Chapter5.lean: -------------------------------------------------------------------------------- 1 | import Fad.Chapter1 2 | import Fad.«Chapter1-Ex» 3 | 4 | namespace Chapter5 5 | 6 | namespace Quicksort 7 | 8 | inductive Tree a where 9 | | null : Tree a 10 | | node : (Tree a) → a → (Tree a) → Tree a 11 | 12 | def mkTree [LT a] [DecidableRel (α := a) (· < ·)] : List a → Tree a 13 | | [] => Tree.null 14 | | x :: xs => 15 | let p := xs.partition (. < x) 16 | Tree.node (mkTree p.1) x (mkTree p.2) 17 | termination_by l => l.length 18 | decreasing_by 19 | all_goals simp 20 | [List.partition_eq_filter_filter, 21 | List.length_filter_le, 22 | Nat.lt_add_one_of_le] 23 | 24 | def Tree.flatten : Tree a → List a 25 | | null => [] 26 | | node l x r => l.flatten ++ [x] ++ r.flatten 27 | 28 | def qsort₀ [LT a] [DecidableRel (α := a) (· < ·)] : List a → List a := 29 | Tree.flatten ∘ mkTree 30 | 31 | def qsort₁ [h₁ : LT a] [h₂ : DecidableRel (α := a) (· < ·)] : List a → List a 32 | | [] => [] 33 | | (x :: xs) => 34 | let p := xs.partition (· < x) 35 | (qsort₁ p.1) ++ [x] ++ (qsort₁ p.2) 36 | termination_by xs => xs.length 37 | decreasing_by 38 | all_goals simp 39 | [List.partition_eq_filter_filter, 40 | List.length_filter_le, Nat.lt_add_one_of_le] 41 | 42 | def qsort₂ [Ord a] (f : a → a → Ordering) : List a → List a 43 | | [] => [] 44 | | (x :: xs) => 45 | let p := xs.partition (λ z => f z x = Ordering.lt) 46 | (qsort₂ f p.1) ++ [x] ++ (qsort₂ f p.2) 47 | termination_by xs => xs.length 48 | decreasing_by 49 | all_goals simp 50 | [List.partition_eq_filter_filter, 51 | List.length_filter_le, Nat.lt_add_one_of_le] 52 | 53 | /- 54 | #eval qsort₀ (List.iota 145) 55 | #eval qsort₁ (List.iota 145) 56 | #eval qsort₂ compare ['c','b','a'] 57 | -/ 58 | 59 | structure Person where 60 | name : String 61 | age : Nat 62 | deriving Repr 63 | 64 | instance : Ord Person where 65 | compare p q := compare p.age q.age 66 | 67 | /- alternative syntax 68 | instance : LT Person where 69 | lt a b := a.age < b.age 70 | -/ 71 | 72 | instance : LT Person := 73 | { lt := fun a b => a.age < b.age } 74 | 75 | instance (a b : Person) : Decidable (a < b) := 76 | inferInstanceAs (Decidable (a.age < b.age)) 77 | 78 | def people := [ 79 | Person.mk "Alice" 23, 80 | Person.mk "Bob" 25, 81 | Person.mk "Eve" 22] 82 | 83 | /- 84 | #eval qsort₂ compare people 85 | #eval qsort₁ people 86 | -/ 87 | 88 | end Quicksort 89 | 90 | 91 | namespace S52 -- Mergesort 92 | 93 | inductive Tree (α : Type) : Type where 94 | | null : Tree α 95 | | leaf : α → Tree α 96 | | node : Tree α → Tree α → Tree α 97 | deriving Repr, Inhabited 98 | 99 | def merge [LE a] [DecidableRel (α := a) (· ≤ ·)] : List a → List a → List a 100 | | [] , ys => ys 101 | | xs , [] => xs 102 | | (x :: xs), (y :: ys) => 103 | if x ≤ y then 104 | x :: merge xs (y :: ys) 105 | else 106 | y :: merge (x :: xs) ys 107 | 108 | 109 | def flatten [LE a] [DecidableRel (α := a) (· ≤ ·)] : Tree a → List a 110 | | Tree.null => [] 111 | | Tree.leaf x => [x] 112 | | Tree.node l r => merge (flatten l) (flatten r) 113 | 114 | 115 | def halve₀ (xs : List a) : (List a × List a) := 116 | let m := xs.length / 2 117 | (xs.take m,xs.drop m) 118 | 119 | def halve : (xs : List a) → (List a × List a) := 120 | let op x p := (p.2, x :: p.1) 121 | List.foldr op ([],[]) 122 | 123 | /- 124 | #eval halve₁ [1,2,3,4,5,6,7,8,9,10] 125 | #eval halve₁ ([] : List Nat) 126 | -/ 127 | 128 | -- https://leanprover-community.github.io/mathlib4_docs/Mathlib/Data/Nat/Defs.html#Nat.twoStepInduction 129 | 130 | def twoStepInduction {P : List a → Prop} 131 | (empty : P []) 132 | (single : ∀ as, as.length = 1 → P as) 133 | (more : ∀ a b as, P as → P (a :: b :: as)) : ∀ as, P as 134 | | [] => empty 135 | | [a] => single [a] (by simp) 136 | | a :: b :: cs => more _ _ _ (twoStepInduction empty single more _) 137 | 138 | 139 | theorem length_halve_fst : (halve xs).fst.length = xs.length / 2 := by 140 | induction xs using twoStepInduction with 141 | | empty => simp [halve] 142 | | single a h => 143 | have _ :: [] := a 144 | simp [halve] 145 | | more a b cs ih => 146 | rw [halve, List.foldr, List.foldr, ←halve] 147 | simp 148 | omega 149 | 150 | 151 | theorem length_halve_snd : (halve xs).snd.length = (xs.length + 1) / 2 := by 152 | induction xs using twoStepInduction with 153 | | empty => simp [halve] 154 | | single a h => 155 | have _ :: [] := a 156 | simp [halve] 157 | | more a b cs ih => 158 | rw [halve, List.foldr, List.foldr, ←halve] 159 | simp 160 | omega 161 | 162 | 163 | def mkTree : (as : List a) → Tree a 164 | | [] => Tree.null 165 | | x :: xs => 166 | if h : xs.length = 0 then Tree.leaf x 167 | else 168 | let p := halve (x :: xs) 169 | have : (halve (x :: xs)).fst.length < xs.length + 1 := 170 | by simp [length_halve_fst]; omega 171 | have : (halve (x :: xs)).snd.length < xs.length + 1 := 172 | by simp [length_halve_snd]; omega 173 | Tree.node (mkTree p.1) (mkTree p.2) 174 | termination_by xs => xs.length 175 | 176 | 177 | partial def mkPair [Inhabited a] : (n : Nat) → (xs : List a) → (Tree a × List a) 178 | | 0, xs => (Tree.null, xs) 179 | | 1, xs => (Tree.leaf xs.head!, xs.tail) 180 | | n, xs => 181 | let m := n / 2 182 | let (t₁, ys) := mkPair m xs 183 | let (t₂, zs) := mkPair (n - m) ys 184 | (Tree.node t₁ t₂, zs) 185 | 186 | def mkTree₀ [Inhabited a] (as : List a) : Tree a := 187 | mkPair as.length as |>.fst 188 | 189 | 190 | def msort₀ [LE a] [DecidableRel (α := a) (· ≤ ·)] (xs : List a) : List a := 191 | (flatten ∘ mkTree) xs 192 | 193 | 194 | def msort₁ [LE a] [DecidableRel (α := a) (· ≤ ·)] : List a → List a 195 | | [] => [] 196 | | x :: xs => 197 | if h: xs.length = 0 then [x] 198 | else 199 | let p := halve (x :: xs) 200 | have : (halve (x :: xs)).fst.length < xs.length + 1 := by 201 | simp [length_halve_fst]; omega 202 | have : (halve (x :: xs)).snd.length < xs.length + 1 := by 203 | simp [length_halve_snd]; omega 204 | merge (msort₁ p.1) (msort₁ p.2) 205 | termination_by xs => xs.length 206 | 207 | 208 | def pairWith (f : α → α → α) : (List α) → List α 209 | | [] => [] 210 | | [x] => [x] 211 | | (x :: y :: xs) => f x y :: pairWith f xs 212 | 213 | 214 | open Chapter1 (wrap unwrap single until') in 215 | 216 | def mkTree₁ : List a → Tree a 217 | | [] => .null 218 | | a :: as => 219 | unwrap (until' single (pairWith .node) (List.map .leaf (a::as))) |>.getD .null 220 | 221 | def msort₂ [LE a] [DecidableRel (α := a) (· ≤ ·)] (xs : List a) : List a := 222 | (flatten ∘ mkTree₁) xs 223 | 224 | open Chapter1 (wrap unwrap single until') in 225 | 226 | def msort₃ [LE a] [DecidableRel (α := a) (· ≤ ·)] : List a → List a 227 | | [] => [] 228 | | x::xs => 229 | unwrap (until' single (pairWith merge) (List.map wrap (x::xs))) |>.getD [] 230 | 231 | 232 | /- 233 | #eval msort₁ [5,3,4,2,1,1] 234 | #eval msort₁ [1,2,3,4,5] 235 | #eval msort₁ ['a','b','a'] 236 | -/ 237 | 238 | end S52 239 | 240 | namespace Heapsort 241 | 242 | inductive Tree (α : Type) : Type 243 | | null : Tree α 244 | | node : α → Tree α → Tree α → Tree α 245 | deriving Inhabited 246 | 247 | 248 | def flatten [LE a] [DecidableRel (α := a) (· ≤ ·)] : Tree a → List a 249 | | Tree.null => [] 250 | | Tree.node x u v => x :: S52.merge (flatten u) (flatten v) 251 | 252 | 253 | open Std.Format in 254 | 255 | def Tree.toFormat [ToString α] : (t: Tree α) → Std.Format 256 | | .null => Std.Format.text "." 257 | | .node x t₁ t₂ => 258 | bracket "(" (f!"{x}" ++ 259 | line ++ nest 2 t₁.toFormat ++ line ++ nest 2 t₂.toFormat) ")" 260 | 261 | instance [ToString a] : Repr (Tree a) where 262 | reprPrec e _ := Tree.toFormat e 263 | 264 | 265 | end Heapsort 266 | 267 | 268 | end Chapter5 269 | -------------------------------------------------------------------------------- /data/AoC2015_day3.txt: -------------------------------------------------------------------------------- 1 | ^^<<>^^>^^^><^>v^>v><><><^^v>v^v>>>^<>v<^<^>>>>><>^>>^>v^>><<^>v>v<>^v^v^vvv><>^^>v><>^><^^^v>>^v^>v><>v^^><vv^<<>v>>><<<>>^^^vv>>>^><<<>><><^>v<>^>v<^v^><<<<>^<>v>^v>vv<^<<>>>>^^v>vv^^<>^<>^^^^<^^^vv<^^v^^>v>^v^^^^>><v<>^v^><>^^><<^^<^^>vv<>v^<^v^>^^>^<>v^^vv<>>v><<<>vvv<>v<>><^<^v<>^vv>^^v<^<>>vv<^>>^>>vv^v>^v^<>^>>>>vv>^^>v>vv>v><^vv^<^<<^^vv^^v>^>>v><^<>v<><>^^<>v>><>^^>^^>>vvv^><<<<<^<^vv<^<>^^^<<<^>^^^vv<>^<>v<^v>^<<>^v<>>v<<^<^<<<><><>^>>>>^>v^v<>vv<^vvv^^^^vv>^v^^v^<^vv<^vv>v<^>vv<>>^>^><^>v>^v>vvv<>^>^v<><>vv>><^v^<><>>v^v^><^<^>vv>v<^>vvv>v<<<<<^>^vv>^><><>^<v^>^><><>>^>^>><^^^>^^>^^v^^<^v^^>v^^>>><<><><>^^<<^^v^>v>><>^^^><^vvv<^^^^^v><<><><>>^>vv>>^vv^^><v<^^>^<^^<^>>>^v<>v<^^^>vvv^v<<^><>>>>v>>>^^vvv^vvv<^^^^v^v^^<<^>v^v^<<><>><^v><<>><<<>^v>v<>^^vv>>^<>v^^<<^v>>v<>>^v^^>><^>v^<^v^^>><>v^>^v^v<<v<><>vv>>>>^>v<>v<<<>^^>vv^v<>^<<<<>>^^>^v<>^v<>>^v^<<^<^>>>^vv<>v^>>v<^^v>>^>><<><<<>>>^v>><^^vv>><>v^><>vv<^^v^^^v<>><^vvv<<^<>v>>>v>><>>><>>^v>v>^^<^>^>v><>vv>^v><<>>>>>>>^<<^vv^^vvvv<^^><<vvv<>^><v<>>^^<<^^vv>v>^vv>>^v^^vvvv>^^>>v^v^^><<^>v>>^^>^<^^<>vvv^vv>v>^v<><^vv^>^v>>>^^<^<^>^v^>^>>>^v>^>^^^>>^<>v^^<>^v<<^^>^^v<^v^>><^v^>^<>>^vv^vv^>v^><^^<^v<^><>v><^v^v^^^v>v^<>^<^^>^v^^<>v^<<>>vv<>>>>v>v<>^>>>v<>^^>^<^><>^><><>^<<>>><<^>^vv^v>>vv^<<^^<<><<^v^>>>v<<<v>^vv<^v>v<^>^^vv>v>><>><>^<>><><<^<<^v^v<v>vvv<^v^^^v^><^v>^<^>^<<>v^<><>>^v<>vvv<^>><^^>^>^v^vv<^><<^v>><^^v>^v<>^>vvvv><^>^<<>v>^>>^<^<<<^v^^^>^>>^>><><<^>v^^>v<<<^>vvv^^<<><^v^v^^^>^^>^vv<>v>>v^>vv^vv>v<^v^^>>^v^v<>>^^><><>>>^>^<>^^v^^><^<>><<^>vv^>>>v<<><<^>vv>vvv>^<><>>>>vv><<><<<<>><v>v^><>v^v^^><>v>v>^^v<^v<>>^^^^^>^^>v<^<^>>>^><^^>><<>>^><>^^^>v^^^>^^v^<>^^><^>>><><^>>vv<^>v<^v>v^<^vv^^><<<><><^v^v>v^>>^^vv^^v>^<^v<>^>^><^^v><^<^<>v^^>^><>>><<<><>v<<^v^^<^><>^<><>v<^^>^^<<>>^><^><^<^>^^v<>v>>><><<>^>v><><<<>^^^v>><<^v>^>>>>^vv<^<>>^<^^<^v>v^<<^<<<<<^<^>>^><<>><>v^v>^<^>v^<>^v^v^v><^vv<<^<>^^^<>^v>^v<>>^>v<<>v<>v^v>v<<<>>v>vv>>v<<>v<>v<^>^>^>v>^>^^^v<<>>>^vvv^^>^^<^vv^^^^>v>^v^>v^^v^>>^v>^vv>^^v^<<<<>^<><^<^<<^^>v^^^v<>>vvv>vv>^<^v>>^v<^^v^v>v<>^v<<<^^v^^^<^v>v^v^v>>v<>^v>vv^v>vv<<^v^v>v>><^vv>>>><<<><>^v^<^vvv>v<>><^v>^>>vv<><><>v><>>><^>vv>>^<>v^>>^><<<^><<>^v^>>><><>vv>^<>^>^v^^><^>>><<>v^<^vv>^<^vv>>vv<><<^><>v<^^<^>vv^^^^vv<<>vv<>v<>>>>^><>^<><>v<>><<>^^vvv>^^^<><>>vvv^v>><>vv^^^v^<<>^^v<><<^^v<>^^>^<^^v>>v^v^^>>v>>>^<<^<>^>^^v>>>>^v<<<^^vv><^>vv<>>vv^>v>>v^vvv^^>vv^<v^>>v^<>>><><<^^<^v>^>>>v>v>^v<>vv>v>^v<<<>><<><><>v^>>>v^>v^>>vv^^^<>>><^>v^<>^^>v<><<<>v^v>^>v<^<>v>v^^>>v>vv^v<>>^^^^<>v^>>>>>>>>^v<^<<>>><<<^<<^>^>v^<>^<<<>v>><^vv^>^>^>>>^v<<>^>^v^><>>v^>v^>^>>v<>vv^v<<>^^>>vv<>vv>>^v<^vv>^v>v<>v^<><>v^^><<<><>^>^v^<>>v^v>v<>>^^<<^<^^vv^<>>^vv^<>>^^^^v>v><^^^v^<<<>^<^<<>><>>v<<^v^>><>>^vv^v>vv>>>>>>^^<<>v^>v^v>^^>>>^v>>^^^<>><>v^<<v>v^^^>^v>^v<^<<><>vv>^^^<^^vv^^>vv>v<<^>^vv><^>^^^^v<v^<<^^>>^^vvvv^v^>vv>>v^vvv<>>^><>>v^^>>^<>>vvvv^>>>v<<^<<^>v^>><<v>v^>^v><>v<<>vv>>><^>>^^v>^>><>vv^><<>>vv<<<^<^^>^<<^>>>>>v>vv<^>^v><>>vv^vvvv>v^>>v><<^^^v>>vv^^>v>^v>^v^^>^<^vvvv<<^>>^<<^^>>^<^>v^><^vv>^^v>>><>v^v>^v<^><<<>vv>v<><>>v^<>^^>^<>^<<^>>vv^><^>v^>>v^>v>vv><>>v<^>><<vvv^vvv^vv^>^>v>>>>vv^>^<>v<^>^<^v>vv<^<<>>^<^<^^<>^<v<<>v>><^v<<^vvv>v>v<<^^<^^>v^^^>^>vv^^^vv>v<>>>vv>><><^><><<>vv>vv^v^>>><>v>>vv>^^vvv^>^^>^>^>^v<<^vv^>vvv^^vv><^>^v^>^><>v<^^vv<^<>>^^v^v>v^vv<>><^v>^<^v>^<>^v>>>><>>>v><^v^vv><<^v<<>^^<^v>vvv<><^^><<^v><>^<^v<^^<^vvvv^^>>>>vv>v>>>v<<<>v^>>vv^vvv<>vvv>>>><>>><>^v>><>>^vv<<^^vv><^v^vv^^^vv>^><^vvv<<>^vvv^>>>^<<<><<<<<^v<^^>>>>^>^v<<<^<^>>v^<<><<^^vvv^>v<>>^^>v>^v>>v>>>^<^<^>v^v^>><>^<<^vvv^^<>^v^>^^<<^>^vv>>v^v^>v>^<^^<>^>^>>>^^vvv^<<>v^<<>><>v<^<^>v^>^vv>^>>^<^v^<<<<^v^>v^><<<><^^^^>v>^^>v><>>^><<><^<>>^^>vv<^><^v^>>>vvv<^<>>^>>^v^<^^v>^^^v<^vv^>>^v><<^<><>>^>vv<<>^^^v^^><>>vv>v^>vvv^^v>^>>^>>v^<<v^<^v^vv^><^<^v<v>^v^<<^^>>^^^v>>>><^^v^>>^^>>^v^<^v>v^v^v^v^>v^vv<><>^^<>^><^^^<<<^v<<>^<^^^^^v^<^<<^^>^vv<>v^>><>>^>v>v<>^>v>><>^<>>>^>^>>v^>v><^vv^>v<v<><^><^v<<>v<>^^><<>v>vv<^vvv><><>vv^<<>^>^<^>>>^v>v<^v^^^vv<>>>^<<^>>><<^^v^>v^<^v>vvv>v^^vv>^^>>v<>^<<>^<><^^v^>><>^>v>>^^^<<^^v<>^^>^<>^>><^>^vvv><^>^<^>^>>vv<^>>^v>>^<>>^^>>>v^>v<>v^^vv>v><^v^^>v<<>v^^<><>^>vvv><^^^>^v^>v>>^vvv<^vv>^^>^>>v<>><<^v<^><>vv^<<^^vv>>^<^><^^v^<<>^v^^>v^>>^^^<^vv>v^>>>vv<<>v>>>^>v^^>v^<<>>vv<<^v>v<<^^>v>>v>v^>>^>>v>^><<^<<>^v>><^^<^<<^>vv<<>^<>^vv>^^^v<^v>vv>^^^^>v>v><<^<<<^vv><^<<<>>v<v>^v^v^<^<^vv>vvv<^^v<>v<<<<>v^<<><<<>v<^>^^v<^^v^>vv>vvv>v>>^><^>>v<v<<^^^v<<^v^^><><<<><<>v>^<<>v<<<^v>>v>><<^<><^v^^v^>^>vvvv<<><<>>^^^>v>v^><>>><^><<><<<^<>v^>>^v^>v^<>>v>^^><^<^v^>v>^vvv<>>v<>^vvvv><<<<<<<v<<<<^v<<><^<<>vv^<<>><^^<<>>>vv>>>>>>^v>v^v^^><<^v^^^<>^>>><>v^v^vvv^>>v>>>^^<<^^vv><<<^^^<<<^^>>>>vvv^v<^>^^>v<^<>v>>>^vv<<^^v^>^>^v>v>v^v^>v<><>>>>><<^v^<>^v<>vvv^>v>v^<><><>^>>><>^>^^<>v^^>^><>><>v^v^^v>>>>vv>>^v<<^v^<>^>v^^>^^<^><<<^^^v^^^^v<^<>v<^^<>vv^^v^<>^<<^>>v>v<<<^^^^vvv^<^<><>v<>><<><<^^^^vv><<>>>^v<<>^>>>v^>v>^^<>^<^>v>^>>>><>^^>v^^v>^vv^^v^><<<>>v<>v<<<>^<^<<>v>>>>^<vvv<^><^<<^>v>>v><>^>>>^v^v>v^^vv^>^<^^>>^><^vv^^vv^<>>^^^^<^^><>>^>>^>^vvv<^<^><>>>^^<><><<>>>>^<<>>>^<^v^>><<^>>>^<^>><>^^<>^v^^vv<><^>vv^^v^<^^^v^vvv^>><>>v<>^<^vvv<<^^>vv^^<<>>><^^vvv<<<^>^<><^>vv^><^<<>vv<>vv>v>v^<<>^^^^v^^^^^<<^><><^^v^>v>^>><^><<>v^>>^vvv>>^<^<>^^v^vv^^v><>>v<<<>v>^<>v<<>v^>^<<><<>v^>v<><^^>^<^v^^><^>vv>^>vvvv>^^><<>vv^>^v<<^<<^<<>vv>>^>>>>>v^v<^v>v>^^^vv^v<^<>v><>>vv>v><>v>^v<><<<<<>v^vv<<<<^<>^>><>^^vv>^<^<<>vv>>vv><>><^^><^<>^><>v^^^^v^^vv<>v<>v>^vv^>^<>^^^>v^>>>v><<^>>v<^v<>^^v<><<>v<^<^>v<>v^>v>^^<<<^^vv^<><<<>>v>^^<>v>>>><<>v^v<>v>><<<<^<<^>^>v^vv^><^v^^<>^^><>vv>^>vvv<^v^>>^>^>^^<<^>^>^v><>>^<^^v>^>>^^<><>>>^^>^^vvv>v<^^<>v^v^^v^v>><<^^^>>v>^vv>^>^^v<>^^<>v^^<>v^><<>vv<<^vvvv><<v>v^>v^<>v^>^^<>^>^^v<>><<<>^v^^v^v<<<^v^<>^<>v>^^>vv>^^<<<><<^>v<^^<^<<>^>>>>>^v^v<vvv<<>v>v>>^v^v^>><<<<>v^<<>>>^>>^>>< -------------------------------------------------------------------------------- /data/AoC2018_day2.txt: -------------------------------------------------------------------------------- 1 | krdmtuqjmwfoevnadixyclzspv 2 | yrdmtuqjiwfoevnabfxyclzsph 3 | kqjvtuqjgwfoevnabixyclzsph 4 | krdmtuqjgwjoevnaolxyclzsph 5 | krdmtnqjgwfoevnabiiyxlzsph 6 | lrymtuqjgwhoevnabixyclzsph 7 | krdmguqjgwfoevnabixkclzsah 8 | krdmtuqjgwfoevnibinyclzdph 9 | krdmtucjgwfoevnabhxyclzspv 10 | krdmtuqjgwfoevtabixyulzsuh 11 | krdmtuqqgwfoevnabixdblzsph 12 | krdmtuqjawfsevnabiyyclzsph 13 | krdmtuqjgwfoevnabzxccldsph 14 | krdmtcqegwfhevnabixyclzsph 15 | krdmtuqjgwforvnaxixycgzsph 16 | krdmtuqjgwfoqvnaxixyclzskh 17 | krdmtutjgwfoevyajixyclzsph 18 | krdmtuqmgwfoevnabixycxzspc 19 | krdptuqjgwhoevkabixyclzsph 20 | krdttuqjhwfoevnabixyclzspa 21 | krdmtuqjgwfoevnabibyhnzsph 22 | krdmtuqjywfoevntbidyclzsph 23 | krdmtojdgwfoevnabixyclzsph 24 | krdmtuqjgpfuevnauixyclzsph 25 | krdmtoqjgwfrevjabixyclzsph 26 | krdmtuqjgwfoyvndbixyclzyph 27 | krdmtxqjgwfomvnayixyclzsph 28 | crdmtuqjgwfoevnabixyoxzsph 29 | krdmtpqjgwfdevnabixycqzsph 30 | krdmtuqjgwfoevuabfxsclzsph 31 | krdmtuqjgwfoevnybixycdzskh 32 | krdmtusjgwfoevnabixxclzdph 33 | krdmtuqjgwfoevnaboxyglzjph 34 | zrdmtuqjgrfoevnalixyclzsph 35 | krdmtuqjclfoevnabixyclzsih 36 | kqdmtlqjgwfoevnabtxyclzsph 37 | krdmtuqggwpoevnabixyclzlph 38 | krdmtuqjgwfobwnrbixyclzsph 39 | krdmtuqjgwfoevwabkxycnzsph 40 | kldmtuqjgwfogvyabixyclzsph 41 | krdmtuqvgwfoevnabixtcrzsph 42 | krdmtuqjgwroevnabixyrlzspw 43 | krdmtuqjgjfoevnabixyelzrph 44 | krdmtuqjgffoevnaaixyclzspa 45 | krdmtuqjgwfoevxabifywlzsph 46 | krdmtuqjgwfoevlabixycrzsrh 47 | krdmtuqjgwfpevnabixocqzsph 48 | krdmtuqjgwfoevdabixycnhsph 49 | krdmtmqjgwfoevnajixyclvsph 50 | krdmtuqjjvfoevnabgxyclzsph 51 | krzmtuqjgwfoevnabioyckzsph 52 | kodmtwqjgwfoevnabieyclzsph 53 | ehdmthqjgwfoevnabixyclzsph 54 | krdmtuqjxwioevnabixyclbsph 55 | grdmkutjgwfoevnabixyclzsph 56 | krdutuqjgwfoebnabixaclzsph 57 | krdmtuqjgwfoebnabixyclcjph 58 | krdmteqjgwfoevnlbixycizsph 59 | krdmtegjgwhoevnabixyclzsph 60 | krdmtuqjgwfdrvnabixbclzsph 61 | krdmtuqjgyfoevidbixyclzsph 62 | krdmtubjawfoevnabixyclzuph 63 | krdmtuqjgwfoavjabixyclzssh 64 | krdmtuqjgwfoeonabixyclzsvo 65 | vrdmtuqjgwffevnabixpclzsph 66 | krdmtuqonwfoevnabixycfzsph 67 | krdmtumjgwfpevnubixyclzsph 68 | krdmtutjgwfoevnaciyyclzsph 69 | krdrtuqjgwfoevnwbixyglzsph 70 | krdmtuqjgwfoevbabixyclesdh 71 | krdmtuqcgwfoevnabixyqdzsph 72 | krdmtuqjgwfogvnabrxycezsph 73 | krdmujqkgwfoevnabixyclzsph 74 | krdmtuqjgtooevnabixyclzzph 75 | jrdntuqjgwfoevnabixyclrsph 76 | krdmtuqjgzfoevkebixyclzsph 77 | krdmtuqjgwfosvnaeixyclztph 78 | krdmtuqjgwfoevzabixydlzaph 79 | krdmtuqzgwfoavnabiqyclzsph 80 | krdmtuqvgwfoevnabixycwzspv 81 | krdmvuqjgwteevnabixyclzsph 82 | krdmtujjgwfoevgybixyclzsph 83 | kydmtuqjgwfoeunacixyclzsph 84 | krdmtuqjgifoqvnabicyclzsph 85 | krnltiqjgwfoevnabixyclzsph 86 | krdmtuqjgwfoevnabhxyclzsgi 87 | kfdmtuqjnwfowvnabixyclzsph 88 | kmdmtuljgwfoevnabixycvzsph 89 | krdmtxqjgwaoevvabixyclzsph 90 | kramduqjgwfoevnabixyclzwph 91 | krdutuqjgwfoennabixyclziph 92 | krdmvuqfgwfoevnacixyclzsph 93 | krdmtuqogwfoevnabmvyclzsph 94 | krdmfuqjgwfoyvnabixyclzseh 95 | krdmtuqjgweoelnabixyclzspd 96 | krdmtumjgwfoevnabixyclzypo 97 | krdmtuqjgkfoevhabixyclzsqh 98 | kjdmtuqjgwfoevgabixyclzsah 99 | krdmtuqjgwfoevnlbixyclzsbw 100 | mrdmtxqjgwfoevnabgxyclzsph 101 | krdmtuqpgwfoevnhbixycltsph 102 | krdmtuqjgwfmqvnabixyclzslh 103 | krqmtuqogwfoevnaqixyclzsph 104 | krdmtusjggfoevnabicyclzsph 105 | krcmtuljgwfoevlabixyclzsph 106 | krdmtuojgwfoeknabixyclzsrh 107 | krdmtuqjtwfoevnabiypclzsph 108 | krvmtupjgwfoevnabixycldsph 109 | krdmtuxjgwfoevaabxxyclzsph 110 | krdmtvqlgwfoehnabixyclzsph 111 | wrdmtuqjgwfoevnabixyclzdpy 112 | krdatuqlgwfoevnabixyclzsjh 113 | krdmtuqjgwfoevpabkxyclzsjh 114 | krdmtuqjgwqvsvnabixyclzsph 115 | krdmtwqjgwfoevnobixyclzspm 116 | krdmtuqjgssoevnabixyclgsph 117 | krdmtuqjgwfoevnafixyclzbpp 118 | krdmtuqjowfoevxabiuyclzsph 119 | krdmtuqrgwfoevntbixyclzspu 120 | krdmtucjgwfoevnabixcnlzsph 121 | krddtuojgwfoevnabixyclzzph 122 | krdmtuqjgwuoevnabiqycldsph 123 | kpdmpuqjgwfoevnabixyclzslh 124 | krdmtuqjgwfoewnabixyzxzsph 125 | krdmtuejswfoevhabixyclzsph 126 | krdmtuqggwfoevntbikyclzsph 127 | krdmtuqjgwfoevnabixydlhnph 128 | krdmtcqjglfoevnaxixyclzsph 129 | krumyuqjgwfoevnrbixyclzsph 130 | kgdmmuqjgwooevnabixyclzsph 131 | krdmteqjgwfqevwabixyclzsph 132 | krdmfuqjgwfpevnabixyclzspq 133 | erdmtycjgwfoevnabixyclzsph 134 | krdmcuqjgwfoevnabixjglzsph 135 | krdmtuqjgtfoeunabixiclzsph 136 | krdmtuqjgwfoevmqbixyclzspu 137 | krlmtuqjvwfoevnabikyclzsph 138 | krdotuqjgwfoevnagrxyclzsph 139 | krdmtuqbgwfoefnabixyclasph 140 | kwdmtuqjgwfosjnabixyclzsph 141 | kydmtuqjgwfoevcabixycezsph 142 | crdmtuqjgwloevnabixkclzsph 143 | krimtuqhgwfoevnbbixyclzsph 144 | krdmjuqagwfoevnabicyclzsph 145 | krdmtuqdgzfoevnabixydlzsph 146 | krdmtuqjgwwoevnaqixyclzspf 147 | krdmtuqjgwfoevnabdxyzvzsph 148 | krdmtuqjgwaofvnabixyclzsnh 149 | krdmturjgwfmevnabixyclzspn 150 | krdmvuqjgwboevnabixyolzsph 151 | krdmtuqjgwfomvnabijyclzspx 152 | bedmtuqjgwfoevnabixyslzsph 153 | krdmtenjgwfoevnabixyclzsqh 154 | krdmtuqugwfoevnabixpcdzsph 155 | krdmtuqjgiloevnabrxyclzsph 156 | krdmtupjcwfoevnabixyclwsph 157 | kremtuqjgwfoevnabixyyjzsph 158 | krdmtuqjgwnoovnabixyclzshh 159 | qrdmtuqjgwfoevnabixyciasph 160 | krdituqjgbfoevnagixyclzsph 161 | krdmnoqjgwfoqvnabixyclzsph 162 | krdmtuqegwfoevhkbixyclzsph 163 | krdmkucjgwfoevnabixnclzsph 164 | krdmtuqbnwpoevnabixyclzsph 165 | krdmttqjgwfoevnabixyclbspz 166 | srdmtubjgwfrevnabixyclzsph 167 | krdmruqjzwfoevnabixyclesph 168 | ardmtuqfgwwoevnabixyclzsph 169 | yrumtuqjgwhoevnabixyclzsph 170 | rrdmtuqjgwfoevnabsxycwzsph 171 | krpmtuqjgwfoevdabixyclzzph 172 | krdmuuqjgwfoevnabixyclriph 173 | krdmtuqjgwfobvnabixyvgzsph 174 | krdmbuujgwfoevnabixycczsph 175 | krhmtuwjgwfoeqnabixyclzsph 176 | krdwtuqjgwfoevnkbixyclzzph 177 | krdmtuqjgwkoeqnabixyvlzsph 178 | kadmtuqjgwfoednabcxyclzsph 179 | krdmtyqqgwfoevnabizyclzsph 180 | krdmtuqjgnfoevnabiyycmzsph 181 | krdmtuqjcwfouvnabixyclznph 182 | krdmtuqjjwfcevnqbixyclzsph 183 | krdmtuqfgbfoevgabixyclzsph 184 | kkdmtuqjgwfoevnapixyclzsth 185 | nrdmtuqjgwtoevnakixyclzsph 186 | krdmtuqjglfoevlabixdclzsph 187 | zrdmtuqjgwfoevndbixbclzsph 188 | krdmeuqjgwfoeenabixyclrsph 189 | krdmoaqjzwfoevnabixyclzsph 190 | krsmtuqjgwfoevnwbixyclzsfh 191 | kadmtuqjgwfoqdnabixyclzsph 192 | krsmtuqjgofoevnabixkclzsph 193 | krdmtuqjdwfoevnibixdclzsph 194 | mrdmtuqjgwfouvnabixyclzfph 195 | trdmtlqjgwfoevnabixyclzjph 196 | trdmyuqjgwfozvnabixyclzsph 197 | krdmtiqjgwroevnabixyclzspk 198 | erdmtutjgwftevnabixyclzsph 199 | krdwyuqjgwfoevnaaixyclzsph 200 | krdmthqbgwfoevnabixyclksph 201 | krdmttqjgwfoivnabixyclvsph 202 | krdmtuqjgwfoefnabixyflgsph 203 | khdmtuqjgwfoevnajixyvlzsph 204 | krdmtuqvgwfoevnasixyclzspt 205 | krdmtuqjgkwogvnabixyclzsph 206 | krdmtuqjgwfoevnaboxpglzjph 207 | kadmtuqjgwfoxvnabixyclziph 208 | krdmtuqjfwfoevnabaxycbzsph 209 | krdjtuqjgwfoevnabiryhlzsph 210 | krdvtuqjgpfoevnabcxyclzsph 211 | brdmtuqjgwfoevnafixyqlzsph 212 | krdmtuqjgwfoevnavixxcllsph 213 | krdhtuqjkwfoevfabixyclzsph 214 | krdmtuqjgjfoevnawixyclzsuh 215 | krddtuqjgwfoeqnabiwyclzsph 216 | krhmtuqjgwfnevnabinyclzsph 217 | kedmtuqjgzfmevnabixyclzsph 218 | qrdmtuqjgwfoevntbixyclzxph 219 | krdmtuqsgwfoevnabixvclzrph 220 | scdmtuqjgwfoevnabixtclzsph 221 | krymtuqjgjfolvnabixyclzsph 222 | krdmtuqjgwfkevnablxyclzskh 223 | krymtuqjswfoevnabixyclzvph 224 | krdmtuqjhwfoevnabixycwzspd 225 | krdmtuxjgwfoevnabyxyclzzph 226 | krdmtlqjgwfovvnabilyclzsph 227 | krdmtuqjgwfoevnaaijcclzsph 228 | krdatrqjgwfokvnabixyclzsph 229 | krdmtuqjgwfoevnaxifyclzkph 230 | krddtuqjgwfoevnabixccozsph 231 | krdmtuqngwfoevnabiyycxzsph 232 | krdmtumdgwfoevnqbixyclzsph 233 | krdmtuqjgwfoevnabixyxlmsch 234 | krdmtudzgwfoevnabixtclzsph 235 | krdmtuqjgwfoevnpbixyclhspl 236 | krdmtqqjgwjoevnabexyclzsph 237 | kydmtuqzgwfoevnabixyclwsph 238 | krdmeucjgwqoevnabixyclzsph 239 | krdmtuqjghfoevjabixyclzspp 240 | krdmtuqjgjfwevnabixyclzskh 241 | krdmkuhjgwfoevnabipyclzsph 242 | krdytuqjgwfoevnabibyclztph 243 | krdmtuqjgwfpevnabisyzlzsph 244 | kmdmtgqjgwfsevnabixyclzsph 245 | krdmtuqjgsfoevnabijyclzszh 246 | krdmtuqjgwfoevnabivyclzuuh 247 | krdstuqjgrfoevnabixyclzspu 248 | jrdmtuqjgwfotvnabixyclzspj 249 | krdmrumjgwfoevnabixeclzsph 250 | krpmtusjgwfoevnabioyclzsph 251 | -------------------------------------------------------------------------------- /Fad/Chapter1-Ex.lean: -------------------------------------------------------------------------------- 1 | import Fad.Chapter1 2 | 3 | namespace Chapter1 4 | 5 | /- # Exercicio 1.1 -/ 6 | 7 | def dropWhile (p : α → Bool) : (xs : List α) -> List α 8 | | [] => [] 9 | | (x :: xs) => if p x then dropWhile p xs else x :: xs 10 | 11 | 12 | /- # Exercicio 1.2 -/ 13 | 14 | def uncons {α : Type} (xs : List α) : Option (α × List α) := 15 | match xs with 16 | | [] => none 17 | | x :: xs => some (x, xs) 18 | 19 | 20 | /- # Exercicio 1.3 -/ 21 | 22 | def wrap {α : Type} (a : α) : List α := [a] 23 | 24 | example : wrap 0 = [0] := rfl 25 | example : wrap [42] = [[42]] := rfl 26 | 27 | def unwrap {α : Type} (a : List α) : Option α := 28 | match a with 29 | | [x] => some x 30 | | _ => none 31 | 32 | def unwrap! {α : Type} [Inhabited α] : (a : List α) → α 33 | | [x] => x 34 | | _ => panic! "unwrap!: empty list" 35 | 36 | example : unwrap [42] = some 42 := rfl 37 | example : unwrap [0, 1] = none := rfl 38 | example : unwrap (@List.nil Nat) = none := rfl 39 | 40 | def single {α : Type} (a : List α) : Bool := 41 | match a with 42 | | [_] => true 43 | | _ => false 44 | 45 | example : single [42] = true := rfl 46 | example : single [0, 1] = false := rfl 47 | example : single ([] : List Nat) = false := rfl 48 | 49 | theorem single_aux {x : Nat} {xs : List Nat} 50 | : single (x :: xs) = true ↔ xs = [] := by 51 | cases xs with 52 | | nil => simp [single] 53 | | cons a xs => simp [single] 54 | 55 | example : ∀ xs : List Nat, single xs = true ↔ xs.length = 1 := by 56 | intro xs 57 | induction xs with 58 | | nil => simp [single] 59 | | cons x xs _ => 60 | rw [single_aux] 61 | simp [List.length] 62 | 63 | 64 | /- # Exercicio 1.4 -/ 65 | 66 | def reverse₀ {α : Type} (a : List α) : List α := 67 | let rec helper (a : List α) (res : List α) : List α := 68 | match a with 69 | | [] => res 70 | | x :: xs => helper xs (x :: res) 71 | helper a [] 72 | 73 | def reverse₁ {a : Type} : List a → List a := 74 | List.foldl (flip List.cons) [] 75 | 76 | theorem aux_rev_append {α : Type} (as bs: List α) 77 | : List.foldl (flip List.cons) as bs = (List.foldl (flip List.cons) [] bs) ++ as := by 78 | induction bs generalizing as with 79 | | nil => rfl 80 | | cons c cs ih => 81 | rw [List.foldl, flip] 82 | rw [List.foldl, flip] 83 | rw [ih, ih [c]] 84 | simp 85 | 86 | theorem rev_cons : reverse₁ (x :: xs) = reverse₁ xs ++ [x] := by 87 | rw (occs := .pos [1]) [reverse₁] 88 | rw [List.foldl, flip] 89 | rw [aux_rev_append] 90 | rfl 91 | 92 | theorem rev_append {α : Type} (as bs: List α) : 93 | reverse₁ (as ++ bs) = reverse₁ bs ++ reverse₁ as := by 94 | induction as generalizing bs with 95 | | nil => simp; rfl 96 | | cons c cs ih => 97 | rw [List.cons_append] 98 | rw [rev_cons, rev_cons, ← List.append_assoc] 99 | rw [ih] 100 | 101 | theorem reverse_reverse {α : Type} (xs : List α) 102 | : reverse₁ (reverse₁ xs) = xs := by 103 | induction xs with 104 | | nil => rfl 105 | | cons a as ih => 106 | rw [rev_cons] 107 | rw [rev_append] 108 | rw [ih]; simp [reverse₁, flip] 109 | 110 | 111 | /- # Exercicio 1.5 -/ 112 | 113 | def map' {α β : Type} (f : α → β) (xs : List α) : List β := 114 | let op x xs := f x :: xs 115 | List.foldr op [] xs 116 | 117 | def filter' {α : Type} (p : α → Bool) (xs : List α) : List α := 118 | let op x xs := if p x then x :: xs else xs 119 | List.foldr op [] xs 120 | 121 | 122 | /- # Exercicio 1.6 -/ 123 | 124 | theorem foldr_filter_aux : 125 | (foldr f e ∘ filter p) ys = foldr f e (filter p ys) := by 126 | rfl 127 | 128 | example (f : α → β → β) 129 | : foldr f e ∘ filter p = foldr (λ x y => if p x then f x y else y) e 130 | := by 131 | funext xs 132 | induction xs with 133 | | nil => rfl 134 | | cons y ys ih => 135 | rw [Function.comp] 136 | rw [filter] 137 | by_cases h : p y = true 138 | rw [if_pos h] 139 | rw [foldr] 140 | rw [foldr] 141 | rw [if_pos h] 142 | rewrite [←foldr_filter_aux] 143 | exact congrArg (f y) ih 144 | rw [if_neg h] 145 | rw [foldr] 146 | rw [if_neg h] 147 | rewrite [←foldr_filter_aux] 148 | exact ih 149 | 150 | 151 | /- # Exercicio 1.7 -/ 152 | 153 | def takeWhile {α : Type} (p : α → Bool) : List α → List α := 154 | let op x acc := if p x then x :: acc else [] 155 | List.foldr op [] 156 | 157 | example : takeWhile (fun x => x % 2 = 0) [2, 3, 4, 5] = [2] := by 158 | rw [takeWhile] 159 | rw [List.foldr] 160 | rw [List.foldr] 161 | rw [List.foldr] 162 | rw [List.foldr] 163 | rw [List.foldr] 164 | rfl 165 | 166 | 167 | /- # Exercicio 1.8 -/ 168 | 169 | def dropWhileEnd {α : Type} (p : α → Bool) (xs : List α) : List α := 170 | let op x xs := if p x ∧ xs.isEmpty then [] else x :: xs 171 | xs.foldr op [] 172 | 173 | 174 | 175 | theorem map_equal (a : List α) (f : α → β): map f a = List.map f a := by 176 | induction a with 177 | | nil => rfl 178 | | cons a as ih => 179 | simp 180 | rw [map] 181 | exact congrArg (List.cons (f a)) ih 182 | 183 | 184 | example (f : α → β → α) : map (foldl f e) ∘ inits = scanl f e := by 185 | funext xs 186 | induction xs generalizing e with 187 | | nil => exact rfl 188 | | cons y ys ih => 189 | rw [Function.comp] 190 | rw [inits] 191 | rw [scanl] 192 | rw [map] 193 | simp [foldl] 194 | rw [←map_equal] 195 | rw [←map_compose] 196 | rw [foldl_comp] 197 | have h : map (foldl f (f e y)) (inits ys) = (map (foldl f (f e y)) ∘ inits) ys := by rfl 198 | rw [h] 199 | exact ih 200 | 201 | example (f : α → β → β) : map (foldr f e) ∘ tails = scanr f e := by 202 | funext xs 203 | induction xs with 204 | | nil => 205 | simp [Function.comp] 206 | simp [tails,map,scanr,foldr] 207 | | cons y ys ih => 208 | sorry 209 | 210 | 211 | /- # Exercicio 1.11 -/ 212 | 213 | def integer: List Nat → Nat := 214 | List.foldl shiftl 0 215 | where 216 | shiftl (n d : Nat) : Nat := 10 * n + d 217 | 218 | def fraction : List Nat → Float := 219 | List.foldr shiftr 0 220 | where 221 | shiftr (d : Nat) (n : Float) : Float := (d.toFloat + n)/10 222 | 223 | /- # Exercicio 1.13 -/ 224 | 225 | def apply {a : Type} : Nat → (a → a) → a → a 226 | | 0, _ => id 227 | | n + 1, f => f ∘ apply n f 228 | 229 | def apply₁ {a : Type} : Nat → (a → a) → a → a 230 | | 0, _ => id 231 | | n + 1, f => apply n f ∘ f 232 | 233 | 234 | /- # Exercicio 1.14 -/ 235 | 236 | def inserts₁ {a : Type} (x : a) (ys : List a) : List (List a) := 237 | let step y yss := 238 | (x :: y :: (yss.head!.tail)) :: yss.map (y :: ·) 239 | ys.foldr step [[x]] 240 | 241 | 242 | /- # Exercicio 1.15 -/ 243 | 244 | def remove {α : Type} [DecidableEq α] (x : α) : List α → List α 245 | | [] => [] 246 | | (y :: ys) => if x = y then ys else y :: remove x ys 247 | 248 | partial def perms₃ {α : Type} [DecidableEq α] : List α → List (List α) 249 | | [] => [[]] 250 | | as => 251 | as.flatMap (λ x => (perms₃ (remove x as)).map (λ ys => x :: ys)) 252 | 253 | 254 | /- # Exercicio 1.20 -/ 255 | 256 | def concat {α : Type} (xss : List (List α)) : List α := 257 | let op f (xs ys : List α) : List α := f (xs ++ ys) 258 | List.foldl op id xss [] 259 | 260 | example : concat [[1, 2], [3, 4]] = [1, 2, 3, 4] := by 261 | rw [concat] 262 | rewrite [List.foldl] 263 | rewrite [List.foldl] 264 | rewrite [List.foldl] 265 | rfl 266 | 267 | /- # Exercicio 1.21 -/ 268 | 269 | -- set_option trace.profiler true 270 | 271 | def steep₀ (xs : List Nat) : Bool := 272 | let sum (xs : List Nat) : Nat := 273 | xs.foldl (· + ·) 0 274 | match xs with 275 | | [] => true 276 | | x :: xs => x > sum xs ∧ steep₀ xs 277 | 278 | -- #eval steep₀ (List.iota 10000000) 279 | 280 | def steep₁ (xs : List Nat) : Bool := 281 | let rec sum : List Nat → Nat → Nat 282 | | [], s => s 283 | | x :: xs, s => sum xs (x + s) 284 | match xs with 285 | | [] => true 286 | | x :: xs => x > sum xs 0 ∧ steep₁ xs 287 | 288 | -- #eval steep₁ (List.iota 10000000) 289 | 290 | def steep₂ : List Nat → Bool := 291 | Prod.snd ∘ faststeep 292 | where 293 | faststeep : List Nat → (Nat × Bool) 294 | | [] => (0, true) 295 | | x :: xs => 296 | let (s, b) := faststeep xs 297 | (x + s, x > s ∧ b) 298 | 299 | -- #reduce steep₂ (List.range 100000) stack overflow 300 | 301 | def steep₃ : List Nat → Bool := 302 | Prod.snd ∘ faststeep 303 | where 304 | faststeep (xs : List Nat) : (Nat × Bool) := 305 | xs.reverse.foldl (λ t x => (x + t.1, x > t.1 ∧ t.2) ) (0, true) 306 | 307 | -- #eval steep₃ [8,5,2] 308 | -- #eval steep₃ (List.range 100000) 309 | 310 | example : steep₁ [8,4,2,1] = steep₂ [8,4,2,1] := rfl 311 | example : steep₁ [] = steep₂ [] := rfl 312 | 313 | example : ∀ xs, steep₁ xs = steep₂ xs := sorry 314 | 315 | 316 | end Chapter1 317 | -------------------------------------------------------------------------------- /Fad/Chapter8.lean: -------------------------------------------------------------------------------- 1 | import Fad.Chapter1 2 | import Fad.«Chapter1-Ex» 3 | import Fad.Chapter3 4 | import Fad.Chapter5 5 | import Fad.Chapter6 6 | import Fad.Chapter7 7 | 8 | namespace Chapter8 9 | 10 | namespace S1 11 | 12 | /- 8.1 Minimum-height trees -/ 13 | 14 | open Chapter5.S52 (halve length_halve_fst length_halve_snd pairWith) 15 | open Chapter1 (wrap unwrap single until' concatMap) 16 | open Chapter6 (foldl1) 17 | open Chapter7 (minWith) 18 | 19 | inductive Tree (α : Type) : Type where 20 | | leaf : α → Tree α 21 | | node : Tree α → Tree α → Tree α 22 | deriving Repr, Inhabited 23 | 24 | 25 | def Tree.size {α : Type} : Tree α → Nat 26 | | Tree.leaf _ => 1 27 | | Tree.node t₁ t₂ => t₁.size + t₂.size 28 | 29 | 30 | def Tree.height {α : Type} : Tree α → Nat 31 | | Tree.leaf _ => 0 32 | | Tree.node t₁ t₂ => Nat.max t₁.height t₂.height + 1 33 | 34 | 35 | def Tree.fringe {α : Type} : Tree α → List α 36 | | Tree.leaf x => [x] 37 | | Tree.node t₁ t₂ => t₁.fringe ++ t₂.fringe 38 | 39 | 40 | def mkTree {a : Type} [Inhabited a] : (as : List a) → Tree a 41 | | [] => .leaf default 42 | | x :: xs => 43 | if h : xs.length = 0 then 44 | .leaf x 45 | else 46 | let p := halve (x :: xs) 47 | have : (halve (x :: xs)).1.length < xs.length + 1 := 48 | by simp [length_halve_fst]; omega 49 | have : (halve (x :: xs)).2.length < xs.length + 1 := 50 | by simp [length_halve_snd]; omega 51 | .node (mkTree p.1) (mkTree p.2) 52 | termination_by xs => xs.length 53 | 54 | def mkTree₁ {a : Type} [Inhabited a] : List a → Tree a 55 | | [] => .leaf default 56 | | xs => 57 | unwrap (until' single (pairWith .node) (xs.map .leaf)) |>.getD default 58 | 59 | 60 | def cost : Tree Nat → Nat 61 | | Tree.leaf x => x 62 | | Tree.node t₁ t₂ => 1 + Nat.max (cost t₁) (cost t₂) 63 | 64 | 65 | def extend {a : Type} (x : a) : Tree a → List (Tree a) 66 | | Tree.leaf y => [.node (.leaf x) (.leaf y)] 67 | | Tree.node t₁ t₂ => 68 | [.node (.leaf x) (.node t₁ t₂)] ++ (extend x t₁).map (.node · t₂) 69 | 70 | 71 | def mkTrees {a : Type} [Inhabited a] : List a → List (Tree a) 72 | | [] => [] 73 | | [x] => [Tree.leaf x] 74 | | x :: xs => concatMap (extend x) (mkTrees xs) 75 | 76 | 77 | /- this should be defined only for nonempty lists -/ 78 | def foldrn {α β : Type} [Inhabited β] (f : α → β → β) (g : α → β) : List α → β 79 | | [] => default 80 | | [x] => g x 81 | | x :: xs => f x (foldrn f g xs) 82 | 83 | def mkTrees₁ {a : Type} [Inhabited a] : List a → List (Tree a) := 84 | foldrn (concatMap ∘ extend) (wrap ∘ .leaf) 85 | 86 | 87 | abbrev Forest (a : Type) := List (Tree a) 88 | 89 | 90 | def rollup [Inhabited (Tree a)] : List (Tree a) → Tree a := 91 | Chapter6.foldl1 .node 92 | 93 | def spine {a : Type} : Tree a → List (Tree a) 94 | | .leaf x => [.leaf x] 95 | | .node u v => spine u ++ [v] 96 | 97 | example [Inhabited a] (ts : List (Tree a)) : 98 | spine (rollup ([Tree.leaf x] ++ ts)) = [Tree.leaf x] ++ ts := by 99 | induction ts with 100 | | nil => 101 | simp [rollup, foldl1, spine] 102 | | cons t ts ih => 103 | simp [rollup, foldl1, List.foldl, spine] 104 | simp [rollup, foldl1] at ih 105 | sorry 106 | 107 | 108 | def extend₁ {a : Type} [Inhabited a] (x : a) (ts : Forest a) : List (Forest a) := 109 | (List.range' 1 ts.length).map 110 | (λ i => .leaf x :: rollup (ts.take i) :: ts.drop i) 111 | 112 | def mkForests {a : Type} [Inhabited a] : List a → List (Forest a) := 113 | foldrn (concatMap ∘ extend₁) (wrap ∘ wrap ∘ .leaf) 114 | 115 | def mkTrees₂ {a : Type} [Inhabited a] : List a → List (Tree a) := 116 | List.map rollup ∘ mkForests 117 | 118 | 119 | def mct₀ : List Nat → Tree Nat := 120 | minWith cost ∘ mkTrees₂ 121 | 122 | 123 | def scanl1 (f : a → a → a) : List a → List a 124 | | x :: xs => Chapter1.scanl f x xs 125 | | [] => [] 126 | 127 | def lcost : Tree Nat → List Nat := 128 | let op (x y : Nat) : Nat := 1 + Nat.max x y 129 | List.reverse ∘ scanl1 op ∘ List.map cost ∘ spine 130 | 131 | 132 | def add (x : Nat) (ts : List (Tree Nat)) : List (Tree Nat) := 133 | let rec join (x : Nat) : List (Tree Nat) → List (Tree Nat) 134 | | [u] => [u] 135 | | [] => [] -- not used 136 | | (u :: v :: ts) => 137 | if Nat.max x (cost u) < cost v then 138 | u :: v :: ts 139 | else 140 | join x (Tree.node u v :: ts) 141 | termination_by ts => ts.length 142 | (Tree.leaf x) :: join x ts 143 | 144 | def mct₁ : List Nat → Tree Nat := 145 | let gstep (x : Nat) : Tree Nat → Tree Nat := 146 | rollup ∘ add x ∘ spine 147 | foldrn gstep .leaf 148 | 149 | 150 | abbrev Pair := (Tree Nat × Nat) 151 | 152 | def node : Pair → Pair → Pair 153 | | (u, c), (v, d) => (Tree.node u v, 1 + Nat.max c d) 154 | 155 | def leaf : Nat → Pair 156 | | x => (Tree.leaf x, x) 157 | 158 | def join (x : Nat) : List Pair → List Pair 159 | | [u] => [u] 160 | | [] => [] -- not used 161 | | u :: v :: ts => 162 | if Nat.max x u.snd < v.snd then 163 | u :: v :: ts 164 | else 165 | join x (node u v :: ts) 166 | termination_by ts => ts.length 167 | 168 | def mct : List Nat → Tree Nat := 169 | let hstep (x : Nat) (ts : List Pair) : List Pair := 170 | leaf x :: join x ts 171 | rollup ∘ List.map Prod.fst ∘ foldrn hstep (wrap ∘ leaf) 172 | 173 | end S1 174 | 175 | 176 | /- 8.2 Huffman coding trees -/ 177 | 178 | namespace S2 179 | open S1 (Tree Forest) 180 | open Chapter1 (concatMap wrap unwrap unwrap! single until' single picks apply) 181 | open Chapter3.SL2 (SymList nilSL singleSL headSL headSL! snocSL nullSL tailSL) 182 | 183 | def depths : Tree a → List Nat := 184 | let rec frm (n : Nat) : Tree a → List Nat 185 | | Tree.leaf _ => [n] 186 | | Tree.node u v => frm (n + 1) u ++ frm (n + 1) v 187 | frm 0 188 | 189 | abbrev Weight := Nat 190 | abbrev Elem := Char × Weight 191 | abbrev Cost := Nat 192 | 193 | def cost (t : Tree Elem) : Cost := 194 | let t := t.fringe.zip (depths t) 195 | List.sum $ t.map (λ (c, d) => d * c.snd) 196 | 197 | def weight : Tree Elem → Nat 198 | | Tree.leaf (_, w) => w 199 | | Tree.node u v => weight u + weight v 200 | 201 | def cost₁ : Tree Elem → Cost 202 | | Tree.leaf _ => 0 203 | | Tree.node u v => cost u + cost v + weight u + weight v 204 | 205 | def pairs (xs : List α) : List ((α × α) × List α) := 206 | (picks xs).flatMap 207 | fun (x, ys) => 208 | (picks ys).map 209 | fun (y, zs) => ((x, y), zs) 210 | 211 | /- Exercise 8.11 -/ 212 | def insert (t₁ : Tree Elem) : Forest Elem → Forest Elem 213 | | [] => [t₁] 214 | | t₂ :: ts => 215 | if weight t₁ ≤ weight t₂ then 216 | t₁ :: t₂ :: ts 217 | else 218 | t₂ :: insert t₁ ts 219 | 220 | def combine (ts : Forest Elem) : List (Forest Elem) := 221 | (pairs ts).map fun (p, us) => 222 | insert (Tree.node p.1 p.2) us 223 | 224 | def mkForests : List (Tree Elem) → List (Forest Elem) := 225 | until' (flip List.all single) (concatMap combine) ∘ wrap 226 | 227 | def mkTrees : List Elem → List (Tree Elem) := 228 | List.map unwrap! ∘ mkForests ∘ List.map Tree.leaf 229 | 230 | def mkForests₁ (ts : List (Tree Elem)) : List (Forest Elem) := 231 | apply (ts.length - 1) (concatMap combine) [ts] 232 | 233 | def mkTrees₁ : List Elem → List (Tree Elem) := 234 | List.map unwrap! ∘ mkForests₁ ∘ List.map Tree.leaf 235 | 236 | 237 | /- quadractic version -/ 238 | 239 | def huffman₁ (es : List Elem) : Tree Elem := 240 | let gstep : Forest Elem → Forest Elem 241 | | t₁ :: t₂ :: ts => insert (Tree.node t₁ t₂) ts 242 | | [] => dbg_trace "error"; [] -- not used 243 | | t₁ :: ts => dbg_trace "error"; t₁ :: ts -- not used 244 | unwrap! (until' single gstep (List.map Tree.leaf es)) 245 | 246 | -- #eval huffman₁ [('a', 2), ('b', 3), ('c', 1), ('d', 20)] 247 | 248 | /- linear time version -/ 249 | 250 | abbrev Queue (α : Type) := SymList α 251 | abbrev Stack (α : Type) := List α 252 | abbrev SQ (α : Type) := Stack α × Queue α 253 | abbrev Pair := Tree Elem × Weight 254 | 255 | def leaf : Elem → Pair 256 | | (c, w) => (Tree.leaf (c, w), w) 257 | 258 | def node : Pair → Pair → Pair 259 | | (t₁, w₁), (t₂, w₂) => (Tree.node t₁ t₂, w₁ + w₂) 260 | 261 | def makeSQ (xs : List Pair) : SQ Pair := 262 | (xs, nilSL) 263 | 264 | def singleSQ (sq : SQ a) : Bool := 265 | sq.1.isEmpty ∧ singleSL sq.2 266 | 267 | def extractSQ (sq : SQ Pair) : Tree Elem := 268 | (headSL! sq.2).1 269 | 270 | 271 | def extractMin (ps : SQ Pair) : Pair × SQ Pair := 272 | let (xs, ys) := ps 273 | if nullSL ys then 274 | (xs.head!, (xs.tail, ys)) 275 | else if xs.isEmpty then 276 | (headSL! ys, (xs, tailSL ys)) 277 | else 278 | let x := xs.head! 279 | let y := headSL! ys 280 | if x.snd ≤ y.snd then 281 | (x, (xs.tail, ys)) 282 | else 283 | (y, (xs, tailSL ys)) 284 | 285 | def gstep (ps : SQ Pair) : SQ Pair := 286 | let add : Pair → SQ Pair → SQ Pair 287 | | y, (xs, ys) => (xs, snocSL y ys) 288 | let (p₁, qs) := extractMin ps 289 | let (p₂, rs) := extractMin qs 290 | add (node p₁ p₂) rs 291 | 292 | def huffman : List Elem → Tree Elem := 293 | extractSQ ∘ until' singleSQ gstep ∘ makeSQ ∘ List.map leaf 294 | 295 | -- #eval huffman [('a', 2), ('b', 3), ('c', 1), ('d', 20)] 296 | 297 | end S2 298 | end Chapter8 299 | -------------------------------------------------------------------------------- /Fad/Chapter4.lean: -------------------------------------------------------------------------------- 1 | 2 | import Fad.Chapter1 3 | 4 | namespace Chapter4 5 | 6 | /- # Section 4.1: a one-dimensional search problem -/ 7 | 8 | namespace D1 9 | 10 | def search₀ (f : Nat → Nat) (t : Nat) : List Nat := 11 | List.foldl (fun xs x => if t = f x then x :: xs else xs) [] 12 | (List.range <| t + 1) 13 | 14 | def search₁ (f : Nat → Nat) (t : Nat) : List Nat := 15 | seek (0, t) 16 | where 17 | acc xs x := if t = f x then x :: xs else xs 18 | seek : (Nat × Nat) → List Nat 19 | | (a, b) => List.foldl acc [] <| List.range' a (b - a + 1) 20 | 21 | 22 | def search₂ (f : Nat → Nat) (t : Nat) : List Nat := 23 | let rec seek (a b : Nat) : List Nat := 24 | let m := (a + b) / 2 25 | if h₁ : a > b then [] 26 | else if h₂ : t < f m then 27 | have : (a + b) / 2 - 1 - a < b - a := by 28 | rw [Nat.not_gt_eq] at h₁ 29 | sorry 30 | seek a (m - 1) 31 | else if h₃ : t = f m then [m] 32 | else 33 | have : b - ((a + b) / 2 + 1) < b - a := by 34 | sorry 35 | seek (m + 1) b 36 | termination_by (b - a) 37 | seek 0 t 38 | 39 | 40 | def bound (f : Nat → Nat) (t : Nat) : (Int × Nat) := 41 | if t ≤ f 0 then (-1, 0) else (b / 2, b) 42 | where 43 | b := Chapter1.until' done (· * 2) 1 44 | done b := t ≤ f b 45 | 46 | partial def smallest (f : Nat → Nat) (t : Nat) : (Int × Nat) → Nat 47 | | (a, b) => 48 | let m := (a + b) / 2 49 | if a + 1 = b then b 50 | else 51 | if t ≤ f m.toNat 52 | then 53 | smallest f t (a, m.toNat) 54 | else 55 | smallest f t (m, b) 56 | 57 | partial def search₃ (f : Nat → Nat) (t : Nat) : List Nat := 58 | if f x = t then [x] else [] 59 | where 60 | x := smallest f t (bound f t) 61 | 62 | -- #eval bound (fun x => dbg_trace "fun {x}"; x * x) 1024 63 | -- #eval search₁ (fun x => dbg_trace "fun {x}"; x * x) 1024 64 | -- #eval search₂ (fun x => dbg_trace "fun {x}"; x * x) 2025 65 | -- #eval search₃ (fun x => dbg_trace "fun {x}"; x * x) 2025 66 | 67 | end D1 68 | 69 | 70 | /- # Section 4.2 A two-dimensional search problem -/ 71 | 72 | namespace D2 73 | 74 | def search₀ (f : (Nat × Nat) → Nat) (t : Nat) : List (Nat × Nat) := 75 | ps.filter (λ p => t = f p) 76 | where 77 | as := (List.range $ t + 1) 78 | ps := Chapter1.concatMap (λ x => as.map (λ y => (x, y))) as 79 | 80 | -- #eval search₀ (λ p => dbg_trace "fun {p}"; p.1 + p.2) 5 81 | 82 | /- https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/.E2.9C.94.20proof.20termination -/ 83 | 84 | def search₁ (f : Nat × Nat → Nat) (t : Nat) : List (Nat × Nat) := 85 | searchIn 0 t [] 86 | where 87 | searchIn (x y : Nat) (sofar : List (Nat × Nat)) : List (Nat × Nat) := 88 | let z := f (x, y) 89 | if x > t then sofar 90 | else if z < t then 91 | searchIn (x + 1) y sofar 92 | else if z = t then 93 | match y with 94 | | 0 => (x,y) :: sofar 95 | | y' + 1 => searchIn (x + 1) y ((x, y) :: sofar) 96 | else 97 | match y with 98 | | 0 => sofar 99 | | y + 1 => searchIn x y sofar 100 | termination_by (t + 1 - x, y) 101 | 102 | -- #eval search₁ (λ (x, y) => dbg_trace "fun {x} {y}"; x + y) 5 103 | -- #eval search₁ (λ (x, y) => dbg_trace "fun {x} {y}"; x^2 + 3^y) 12 104 | -- #eval search₁ (λ (x, y) => x^2 + 3^y) 2223 105 | -- #eval search₁ (λ (x, y) => x^2 + 3^y) 20259 106 | 107 | 108 | partial def helper (t : Nat) (f : Nat × Nat → Nat) 109 | : (Nat × Nat) → (Nat × Nat) → List (Nat × Nat) 110 | | (x₁, y₁), (x₂, y₂) => 111 | let c := (x₁ + x₂) / 2 112 | let r := (y₁ + y₂) / 2 113 | let x := D1.smallest (λ x => f (x,r)) t (x₁ - 1, x₂) 114 | let y := D1.smallest (λ y => f (c,y)) t (y₂ - 1, y₁) 115 | if x₂ < x₁ ∨ y₁ < y₂ then 116 | [] 117 | else 118 | if y₁ - y₂ ≤ x₂ - x₁ then 119 | let z := f (x,r) 120 | if z < t then 121 | helper t f (x₁, y₁) (x₂, r + 1) 122 | else if z = t then 123 | (x, r) :: helper t f (x₁, y₁) (x - 1, r + 1) ++ helper t f (x + 1, r - 1) (x₂, y₂) 124 | else 125 | helper t f (x₁, y₁) (x - 1, r + 1) ++ helper t f (x, r - 1) (x₂, y₂) 126 | else 127 | let z := f (c, y) 128 | if z < t then 129 | helper t f (c + 1, y₁) (x₂, y₂) 130 | else if z = t then 131 | (c, y) :: helper t f (x₁, y₁) (c - 1, y + 1) ++ helper t f (c + 1, y - 1) (x₂, y₂) 132 | else 133 | helper t f (x₁, y₁) (c - 1, y) ++ helper t f (c + 1, y - 1) (x₂, y₂) 134 | 135 | partial def search₂ (f : Nat × Nat → Nat) (t : Nat) : List (Nat × Nat) := 136 | let p := D1.smallest (λ y => f (0, y)) t (-1, t) 137 | let q := D1.smallest (λ x => f (x, 0)) t (-1, t) 138 | helper t f (0, p) (q, 0) 139 | 140 | 141 | -- BUG #eval helper 12 (λ (x, y) => x^2 + 3^y) (0, 12) (12,0) 142 | 143 | 144 | /- https://kmill.github.io/informalization/ucsc_cse_talk.pdf -/ 145 | 146 | def scale (a : Array Int) (c : Int) : Array Int := Id.run do 147 | let mut b : Array Int := #[] 148 | for h: i in [0:a.size] do 149 | b := b.push (c * a[i]) 150 | return b 151 | 152 | -- #eval scale #[1,2,3,4] 4 153 | -- #eval for i in [0:12] do println! i 154 | 155 | def myhead₁ (xs : List a) (h : xs.length ≠ 0) : a := 156 | match xs with 157 | | [] => absurd rfl h 158 | | x :: _ => x 159 | 160 | def myhead₂ (xs : List a) (h : xs.length ≠ 0) : a := 161 | match xs, h with 162 | | x :: _, _ => x 163 | 164 | end D2 165 | 166 | 167 | /- # Section 4.3 Binary search trees -/ 168 | 169 | namespace BST1 170 | 171 | inductive Tree (α : Type) : Type 172 | | null : Tree α 173 | | node : (Tree α) → α → (Tree α) → Tree α 174 | deriving Inhabited 175 | 176 | open Std.Format in 177 | 178 | def Tree.toFormat [ToString α] : (t : Tree α) → Std.Format 179 | | .null => Std.Format.text "." 180 | | .node t₁ x t₂ => 181 | bracket "(" (f!"{x}" ++ 182 | line ++ nest 2 t₁.toFormat ++ line ++ nest 2 t₂.toFormat) ")" 183 | 184 | instance [ToString a] : Repr (Tree a) where 185 | reprPrec e _ := Tree.toFormat e 186 | 187 | def Tree.size : Tree a → Nat 188 | | null => 0 189 | | node t₁ _ t₂ => 1 + t₁.size + t₂.size 190 | 191 | def Tree.flatten : Tree a → List a 192 | | null => [] 193 | | node l x r => l.flatten ++ [x] ++ r.flatten 194 | 195 | 196 | def search (f : Nat → Nat) : Nat → Tree Nat → Option Nat 197 | | _, Tree.null => none 198 | | k, Tree.node l x r => 199 | if f x < k then 200 | search f k r 201 | else 202 | if f x = k then 203 | some x 204 | else 205 | search f k l 206 | 207 | def Tree.height : Tree a → Nat 208 | | null => 0 209 | | node l _ r => 1 + (max l.height r.height) 210 | 211 | 212 | def mkTree : List Nat → Tree Nat 213 | | [] => Tree.null 214 | | x :: xs => 215 | let p := xs.partition (· < x) 216 | Tree.node (mkTree p.1) x (mkTree p.2) 217 | termination_by l => l.length 218 | decreasing_by 219 | all_goals 220 | simp [List.partition_eq_filter_filter, 221 | List.length_filter_le, Nat.lt_add_one_of_le] 222 | 223 | end BST1 224 | 225 | namespace BST2 226 | 227 | inductive Tree (α : Type) : Type 228 | | null : Tree α 229 | | node : Nat → (Tree α) → α → (Tree α) → Tree α 230 | deriving Nonempty 231 | 232 | open Std.Format in 233 | 234 | def Tree.toFormat [ToString α] : (t : Tree α) → Std.Format 235 | | .null => Std.Format.text "." 236 | | .node _ t₁ x t₂ => 237 | bracket "(" (f!"{x}" ++ 238 | line ++ nest 2 t₁.toFormat ++ line ++ nest 2 t₂.toFormat) ")" 239 | 240 | instance [ToString a] : Repr (Tree a) where 241 | reprPrec e _ := Tree.toFormat e 242 | 243 | def Tree.height : (a : Tree α) -> Nat 244 | | Tree.null => 0 245 | | Tree.node x _ _ _ => x 246 | 247 | def Tree.flatten : Tree a → List a 248 | | null => [] 249 | | node _ l x r => l.flatten ++ [x] ++ r.flatten 250 | 251 | def node (l : Tree α) (x : α) (r : Tree α): Tree α := 252 | Tree.node h l x r 253 | where h := 1 + (max l.height r.height) 254 | 255 | def bias : Tree α → Int 256 | | .null => 0 257 | | .node _ l _ r => l.height - r.height 258 | 259 | def rotr : Tree α → Tree α 260 | | .null => .null 261 | | .node _ (.node _ ll y rl) x r => node ll y (node rl x r) 262 | | .node _ .null _ _ => .null 263 | 264 | def rotl : Tree α → Tree α 265 | | .null => .null 266 | | .node _ ll y (.node _ lrl z rrl) => node (node ll y lrl) z rrl 267 | | .node _ _ _ .null => .null 268 | 269 | def balance (t1 : Tree α) (x : α) (t2 : Tree α) : Tree α := 270 | if Int.natAbs (h1 - h2) ≤ 1 then 271 | node t1 x t2 272 | else if h1 == h2 + 2 then 273 | rotateR t1 x t2 274 | else 275 | rotateL t1 x t2 276 | where 277 | h1 := t1.height 278 | h2 := t2.height 279 | rotateR t1 x t2 := 280 | if 0 <= bias t1 then 281 | rotr (node t1 x t2) 282 | else rotr (node (rotl t1) x t2) 283 | rotateL t1 x t2 := 284 | if bias t2 <= 0 then 285 | rotl (node t1 x t2) 286 | else rotl (node t1 x (rotr t2)) 287 | 288 | 289 | def insert {α : Type} [LT α] [DecidableRel (α := α) (· < ·)] 290 | : (x : α) -> Tree α -> Tree α 291 | | x, .null => node .null x .null 292 | | x, .node h l y r => 293 | if x < y then balance (insert x l) y r else 294 | if x > y then balance l y (insert x r) else .node h l y r 295 | 296 | 297 | def mkTree [LT α] [DecidableRel (α := α) (· < ·)] : (xs : List α) → Tree α := 298 | Chapter1.foldr insert (.null : Tree α) 299 | 300 | 301 | def balanceR (t₁ : Tree α) (x : α) (t₂ : Tree α) : Tree α := 302 | match t₁ with 303 | | Tree.null => Tree.null 304 | | Tree.node _ l y r => 305 | if r.height ≥ t₂.height + 2 306 | then balance l y (balanceR r x t₂) 307 | else balance l y (node r x t₂) 308 | 309 | end BST2 310 | 311 | namespace DSet 312 | open BST2 (Tree insert node) 313 | 314 | abbrev Set a := Tree a 315 | 316 | def member [LT a] [DecidableRel (α := a) (· < ·)] (x : a) : Set a → Bool 317 | | .null => false 318 | | .node _ l y r => 319 | if x < y then member x l 320 | else if x > y then member x r 321 | else true 322 | 323 | end DSet 324 | 325 | end Chapter4 326 | -------------------------------------------------------------------------------- /data/AoC2018_day1.txt: -------------------------------------------------------------------------------- 1 | +2 2 | +11 3 | +4 4 | -16 5 | -4 6 | +15 7 | +19 8 | -8 9 | -17 10 | +12 11 | +8 12 | +8 13 | +18 14 | -10 15 | -17 16 | -6 17 | +10 18 | +10 19 | +19 20 | +11 21 | +12 22 | -17 23 | +4 24 | -1 25 | +12 26 | +6 27 | +13 28 | +14 29 | +6 30 | +19 31 | +2 32 | +19 33 | -7 34 | -15 35 | +6 36 | -12 37 | -15 38 | -18 39 | +17 40 | +8 41 | +4 42 | +14 43 | +1 44 | +15 45 | -6 46 | -6 47 | +13 48 | +9 49 | -2 50 | +1 51 | +15 52 | -12 53 | +18 54 | +7 55 | -14 56 | +15 57 | -16 58 | +6 59 | -12 60 | -11 61 | -12 62 | -1 63 | -15 64 | -8 65 | -18 66 | -15 67 | +11 68 | +13 69 | -7 70 | +15 71 | +19 72 | +2 73 | +17 74 | -9 75 | -15 76 | -11 77 | -8 78 | +16 79 | +17 80 | -6 81 | +13 82 | +16 83 | -20 84 | +19 85 | +8 86 | +14 87 | +5 88 | +11 89 | -5 90 | +6 91 | +10 92 | +3 93 | -7 94 | -5 95 | -14 96 | +9 97 | +6 98 | +13 99 | +4 100 | -3 101 | -13 102 | -6 103 | -18 104 | -23 105 | -2 106 | +16 107 | +12 108 | -4 109 | +12 110 | +3 111 | -4 112 | +9 113 | -14 114 | +1 115 | -17 116 | -21 117 | +11 118 | -8 119 | +7 120 | -16 121 | -20 122 | -3 123 | -10 124 | -17 125 | -11 126 | +6 127 | -15 128 | -11 129 | +17 130 | +12 131 | -13 132 | -11 133 | -12 134 | -18 135 | -1 136 | +10 137 | -16 138 | -1 139 | -8 140 | +18 141 | +4 142 | +2 143 | -11 144 | -18 145 | +8 146 | +18 147 | -17 148 | +7 149 | +3 150 | +17 151 | +7 152 | +1 153 | -16 154 | +10 155 | -6 156 | -10 157 | +31 158 | -5 159 | -14 160 | +15 161 | +19 162 | +6 163 | -1 164 | +17 165 | -4 166 | +16 167 | -6 168 | +9 169 | -26 170 | -18 171 | +17 172 | -9 173 | -5 174 | -19 175 | +2 176 | +13 177 | +41 178 | -18 179 | +10 180 | -26 181 | -12 182 | +4 183 | -3 184 | -23 185 | -9 186 | -29 187 | -13 188 | +15 189 | +4 190 | -21 191 | +14 192 | +23 193 | -24 194 | -4 195 | -12 196 | -8 197 | -18 198 | -16 199 | -14 200 | +17 201 | -8 202 | -16 203 | +13 204 | -15 205 | +7 206 | +9 207 | -17 208 | -4 209 | -7 210 | -12 211 | +4 212 | +10 213 | -13 214 | -6 215 | -8 216 | +10 217 | +1 218 | +9 219 | -15 220 | +14 221 | -15 222 | +11 223 | -18 224 | -18 225 | +2 226 | +10 227 | -8 228 | -10 229 | +5 230 | -4 231 | +12 232 | -4 233 | -2 234 | +15 235 | +17 236 | +16 237 | +19 238 | +17 239 | +13 240 | +8 241 | -10 242 | +13 243 | +8 244 | -13 245 | -14 246 | +5 247 | -3 248 | -15 249 | -16 250 | -9 251 | -5 252 | -10 253 | +8 254 | -2 255 | +1 256 | -14 257 | -7 258 | -3 259 | -12 260 | -3 261 | +10 262 | -18 263 | +22 264 | +8 265 | -13 266 | -3 267 | -18 268 | -12 269 | -9 270 | +1 271 | -10 272 | +8 273 | -13 274 | -12 275 | -9 276 | -2 277 | +12 278 | -14 279 | +8 280 | -19 281 | -6 282 | -11 283 | +12 284 | +15 285 | +17 286 | +7 287 | -14 288 | +11 289 | +9 290 | -10 291 | -21 292 | +3 293 | -19 294 | -6 295 | -6 296 | +7 297 | +15 298 | -13 299 | -8 300 | +10 301 | -18 302 | +15 303 | -9 304 | -19 305 | +5 306 | +11 307 | -7 308 | -14 309 | +7 310 | +9 311 | +6 312 | +6 313 | +13 314 | +8 315 | +13 316 | +19 317 | -8 318 | -3 319 | -4 320 | -18 321 | +12 322 | -2 323 | +18 324 | +13 325 | -1 326 | +17 327 | -7 328 | +12 329 | +8 330 | +2 331 | +19 332 | -11 333 | +22 334 | +14 335 | +9 336 | -12 337 | +24 338 | +1 339 | +5 340 | +20 341 | -18 342 | +17 343 | -9 344 | +12 345 | -4 346 | -15 347 | -10 348 | +4 349 | -7 350 | +11 351 | +13 352 | +10 353 | +21 354 | -14 355 | -1 356 | -13 357 | -6 358 | +14 359 | +7 360 | +7 361 | +10 362 | +11 363 | -22 364 | +6 365 | +17 366 | +26 367 | -7 368 | +8 369 | -5 370 | +29 371 | -54 372 | -16 373 | +13 374 | +10 375 | -1 376 | -23 377 | +12 378 | -20 379 | -20 380 | -27 381 | +1 382 | -2 383 | -4 384 | -26 385 | -2 386 | -36 387 | -18 388 | +14 389 | -3 390 | -18 391 | -1 392 | +17 393 | +15 394 | -12 395 | +9 396 | +1 397 | +23 398 | -22 399 | -6 400 | -20 401 | +19 402 | +3 403 | -17 404 | -6 405 | -41 406 | -5 407 | -5 408 | -5 409 | -19 410 | -9 411 | -13 412 | +3 413 | +2 414 | +10 415 | +5 416 | -6 417 | -18 418 | -19 419 | +10 420 | +17 421 | +6 422 | -17 423 | +5 424 | -7 425 | +10 426 | +6 427 | -5 428 | -14 429 | +1 430 | -17 431 | -19 432 | +7 433 | +14 434 | -19 435 | +6 436 | +12 437 | -16 438 | +13 439 | -1 440 | +16 441 | +14 442 | -13 443 | -4 444 | +2 445 | -17 446 | -1 447 | -7 448 | -5 449 | -3 450 | -14 451 | -3 452 | +5 453 | +11 454 | -12 455 | -5 456 | -14 457 | +2 458 | +2 459 | -15 460 | +12 461 | -3 462 | +15 463 | -2 464 | +10 465 | +7 466 | -16 467 | +12 468 | -15 469 | +1 470 | -3 471 | -4 472 | -19 473 | +7 474 | -5 475 | +22 476 | +8 477 | +17 478 | +16 479 | +15 480 | +11 481 | +3 482 | -16 483 | -10 484 | +7 485 | +12 486 | +16 487 | +13 488 | -10 489 | +15 490 | -1 491 | -15 492 | +22 493 | +1 494 | +3 495 | +20 496 | +24 497 | +12 498 | +17 499 | -19 500 | -12 501 | +7 502 | -11 503 | -19 504 | +4 505 | +4 506 | -21 507 | +22 508 | +1 509 | +45 510 | +3 511 | -9 512 | +41 513 | -1 514 | +94 515 | +2 516 | +7 517 | -37 518 | +3 519 | +217 520 | +13 521 | +10 522 | -14 523 | +5 524 | -3 525 | -14 526 | -26 527 | -39 528 | +80 529 | +33 530 | +9 531 | -13 532 | +7 533 | +7 534 | -13 535 | -14 536 | +1 537 | +37 538 | +33 539 | +7 540 | +14 541 | +1 542 | -5 543 | -11 544 | -8 545 | -12 546 | +4 547 | +14 548 | -17 549 | +1 550 | -10 551 | -20 552 | -25 553 | +27 554 | +31 555 | -8 556 | +1 557 | +53 558 | +22 559 | +9 560 | -14 561 | -49 562 | +13 563 | -52 564 | -64 565 | -100 566 | +34 567 | +198 568 | +605 569 | +76787 570 | -8 571 | +13 572 | +4 573 | -8 574 | +5 575 | +9 576 | -7 577 | -13 578 | -6 579 | -4 580 | -20 581 | +6 582 | +12 583 | -4 584 | +7 585 | +5 586 | -14 587 | +10 588 | +17 589 | +18 590 | +18 591 | +5 592 | +6 593 | -12 594 | -6 595 | -8 596 | -13 597 | -5 598 | -8 599 | -17 600 | +6 601 | +2 602 | +3 603 | +18 604 | -1 605 | +20 606 | +14 607 | -18 608 | +21 609 | -12 610 | +3 611 | +8 612 | +6 613 | +21 614 | -3 615 | +1 616 | +9 617 | +16 618 | +15 619 | -7 620 | +1 621 | -16 622 | +17 623 | -5 624 | -15 625 | -14 626 | +3 627 | +3 628 | -4 629 | +2 630 | +1 631 | +16 632 | +19 633 | +12 634 | -4 635 | +14 636 | -8 637 | +17 638 | -7 639 | -1 640 | -3 641 | -11 642 | -12 643 | +7 644 | +9 645 | -10 646 | -13 647 | +1 648 | -8 649 | +4 650 | -12 651 | +13 652 | -10 653 | -8 654 | -3 655 | -19 656 | -12 657 | +7 658 | +9 659 | -1 660 | -9 661 | -4 662 | -19 663 | -12 664 | +17 665 | -12 666 | +19 667 | +18 668 | -12 669 | -7 670 | -10 671 | +5 672 | -21 673 | +4 674 | -6 675 | +4 676 | -15 677 | -9 678 | +19 679 | -17 680 | +21 681 | +12 682 | +24 683 | +13 684 | +18 685 | -16 686 | -11 687 | +10 688 | +18 689 | +7 690 | +16 691 | -10 692 | -4 693 | -13 694 | +29 695 | -13 696 | -9 697 | +3 698 | -22 699 | -13 700 | +67 701 | -10 702 | +9 703 | +6 704 | +2 705 | +8 706 | +6 707 | +18 708 | -16 709 | -15 710 | +9 711 | +2 712 | +3 713 | -4 714 | +7 715 | +18 716 | +12 717 | -10 718 | -15 719 | +1 720 | +11 721 | -9 722 | -19 723 | +15 724 | +8 725 | +11 726 | +2 727 | +3 728 | +9 729 | +14 730 | -19 731 | +16 732 | -5 733 | -12 734 | +8 735 | +17 736 | +4 737 | +10 738 | -13 739 | +19 740 | +9 741 | -19 742 | -11 743 | +8 744 | +6 745 | +3 746 | +5 747 | -16 748 | +3 749 | +7 750 | +11 751 | -1 752 | +5 753 | -7 754 | -12 755 | +13 756 | -6 757 | +1 758 | +18 759 | +6 760 | +1 761 | +14 762 | -7 763 | -2 764 | -13 765 | +18 766 | -1 767 | +18 768 | -5 769 | +3 770 | -14 771 | -17 772 | +1 773 | +13 774 | +16 775 | +14 776 | +4 777 | -13 778 | -12 779 | -1 780 | +2 781 | +3 782 | +14 783 | -1 784 | -6 785 | +12 786 | +8 787 | -11 788 | +6 789 | -7 790 | +19 791 | -16 792 | +5 793 | +18 794 | +11 795 | -16 796 | -8 797 | -3 798 | +13 799 | +6 800 | +7 801 | -14 802 | +9 803 | +14 804 | -7 805 | +9 806 | -14 807 | +1 808 | -6 809 | +16 810 | -2 811 | -12 812 | -5 813 | -8 814 | +7 815 | -19 816 | +13 817 | +2 818 | +7 819 | +13 820 | -8 821 | +7 822 | +15 823 | +9 824 | +10 825 | -5 826 | +4 827 | +7 828 | -1 829 | +12 830 | +13 831 | +13 832 | +10 833 | +13 834 | +13 835 | -6 836 | +12 837 | +10 838 | -5 839 | +9 840 | +11 841 | -16 842 | +8 843 | -11 844 | -15 845 | -15 846 | +10 847 | -17 848 | +2 849 | -6 850 | -11 851 | -14 852 | -13 853 | -14 854 | -5 855 | -8 856 | -1 857 | -7 858 | -7 859 | +21 860 | -11 861 | -5 862 | +3 863 | +5 864 | -15 865 | +20 866 | +8 867 | -17 868 | +7 869 | -13 870 | +35 871 | +6 872 | -3 873 | -1 874 | -16 875 | +8 876 | -5 877 | -10 878 | +14 879 | +20 880 | +13 881 | +10 882 | -20 883 | -17 884 | +13 885 | -2 886 | +9 887 | +19 888 | -8 889 | +4 890 | -2 891 | +12 892 | -15 893 | +12 894 | -4 895 | +20 896 | -7 897 | +2 898 | -5 899 | -19 900 | -3 901 | -1 902 | +3 903 | +6 904 | +7 905 | -20 906 | +19 907 | +15 908 | +10 909 | +3 910 | -7 911 | +1 912 | +13 913 | +4 914 | +6 915 | +5 916 | +4 917 | +4 918 | +7 919 | -19 920 | +16 921 | +2 922 | +4 923 | -8 924 | -7 925 | +18 926 | +15 927 | +6 928 | +1 929 | -13 930 | +14 931 | +18 932 | -7 933 | +8 934 | -16 935 | -10 936 | +13 937 | -12 938 | +13 939 | +14 940 | +14 941 | -8 942 | -19 943 | +4 944 | -15 945 | -16 946 | -16 947 | -8 948 | +10 949 | +17 950 | -12 951 | +10 952 | +8 953 | +19 954 | -13 955 | +14 956 | +12 957 | +3 958 | -10 959 | +2 960 | +14 961 | +1 962 | -2 963 | +3 964 | +16 965 | -3 966 | -12 967 | +5 968 | +4 969 | -19 970 | +17 971 | -4 972 | -8 973 | +15 974 | -6 975 | +14 976 | +21 977 | -4 978 | -18 979 | +4 980 | -8 981 | +21 982 | -28 983 | +5 984 | -25 985 | -4 986 | -2 987 | +14 988 | -2 989 | -22 990 | -9 991 | -7 992 | -5 993 | -20 994 | -17 995 | -6 996 | +8 997 | -5 998 | -4 999 | -2 1000 | -15 1001 | -6 1002 | +13 1003 | +13 1004 | +13 1005 | -29 1006 | -16 1007 | +13 1008 | -38 1009 | +5 1010 | -16 1011 | +4 1012 | -15 1013 | +19 1014 | -31 1015 | +24 1016 | +6 1017 | -13 1018 | +20 1019 | -47 1020 | -6 1021 | +13 1022 | +14 1023 | -79 1024 | -14 1025 | +11 1026 | -4 1027 | +24 1028 | -18 1029 | -1 1030 | +20 1031 | -4 1032 | +24 1033 | -26 1034 | -25 1035 | -16 1036 | -6 1037 | -8 1038 | -7 1039 | +8 1040 | -77371 1041 | -------------------------------------------------------------------------------- /data/AoC2015_day6.txt: -------------------------------------------------------------------------------- 1 | turn off 660,55 through 986,197 2 | turn off 341,304 through 638,850 3 | turn off 199,133 through 461,193 4 | toggle 322,558 through 977,958 5 | toggle 537,781 through 687,941 6 | turn on 226,196 through 599,390 7 | turn on 240,129 through 703,297 8 | turn on 317,329 through 451,798 9 | turn on 957,736 through 977,890 10 | turn on 263,530 through 559,664 11 | turn on 158,270 through 243,802 12 | toggle 223,39 through 454,511 13 | toggle 544,218 through 979,872 14 | turn on 313,306 through 363,621 15 | toggle 173,401 through 496,407 16 | toggle 333,60 through 748,159 17 | turn off 87,577 through 484,608 18 | turn on 809,648 through 826,999 19 | toggle 352,432 through 628,550 20 | turn off 197,408 through 579,569 21 | turn off 1,629 through 802,633 22 | turn off 61,44 through 567,111 23 | toggle 880,25 through 903,973 24 | turn on 347,123 through 864,746 25 | toggle 728,877 through 996,975 26 | turn on 121,895 through 349,906 27 | turn on 888,547 through 931,628 28 | toggle 398,782 through 834,882 29 | turn on 966,850 through 989,953 30 | turn off 891,543 through 914,991 31 | toggle 908,77 through 916,117 32 | turn on 576,900 through 943,934 33 | turn off 580,170 through 963,206 34 | turn on 184,638 through 192,944 35 | toggle 940,147 through 978,730 36 | turn off 854,56 through 965,591 37 | toggle 717,172 through 947,995 38 | toggle 426,987 through 705,998 39 | turn on 987,157 through 992,278 40 | toggle 995,774 through 997,784 41 | turn off 796,96 through 845,182 42 | turn off 451,87 through 711,655 43 | turn off 380,93 through 968,676 44 | turn on 263,468 through 343,534 45 | turn on 917,936 through 928,959 46 | toggle 478,7 through 573,148 47 | turn off 428,339 through 603,624 48 | turn off 400,880 through 914,953 49 | toggle 679,428 through 752,779 50 | turn off 697,981 through 709,986 51 | toggle 482,566 through 505,725 52 | turn off 956,368 through 993,516 53 | toggle 735,823 through 783,883 54 | turn off 48,487 through 892,496 55 | turn off 116,680 through 564,819 56 | turn on 633,865 through 729,930 57 | turn off 314,618 through 571,922 58 | toggle 138,166 through 936,266 59 | turn on 444,732 through 664,960 60 | turn off 109,337 through 972,497 61 | turn off 51,432 through 77,996 62 | turn off 259,297 through 366,744 63 | toggle 801,130 through 917,544 64 | toggle 767,982 through 847,996 65 | turn on 216,507 through 863,885 66 | turn off 61,441 through 465,731 67 | turn on 849,970 through 944,987 68 | toggle 845,76 through 852,951 69 | toggle 732,615 through 851,936 70 | toggle 251,128 through 454,778 71 | turn on 324,429 through 352,539 72 | toggle 52,450 through 932,863 73 | turn off 449,379 through 789,490 74 | turn on 317,319 through 936,449 75 | toggle 887,670 through 957,838 76 | toggle 671,613 through 856,664 77 | turn off 186,648 through 985,991 78 | turn off 471,689 through 731,717 79 | toggle 91,331 through 750,758 80 | toggle 201,73 through 956,524 81 | toggle 82,614 through 520,686 82 | toggle 84,287 through 467,734 83 | turn off 132,367 through 208,838 84 | toggle 558,684 through 663,920 85 | turn on 237,952 through 265,997 86 | turn on 694,713 through 714,754 87 | turn on 632,523 through 862,827 88 | turn on 918,780 through 948,916 89 | turn on 349,586 through 663,976 90 | toggle 231,29 through 257,589 91 | toggle 886,428 through 902,993 92 | turn on 106,353 through 236,374 93 | turn on 734,577 through 759,684 94 | turn off 347,843 through 696,912 95 | turn on 286,699 through 964,883 96 | turn on 605,875 through 960,987 97 | turn off 328,286 through 869,461 98 | turn off 472,569 through 980,848 99 | toggle 673,573 through 702,884 100 | turn off 398,284 through 738,332 101 | turn on 158,50 through 284,411 102 | turn off 390,284 through 585,663 103 | turn on 156,579 through 646,581 104 | turn on 875,493 through 989,980 105 | toggle 486,391 through 924,539 106 | turn on 236,722 through 272,964 107 | toggle 228,282 through 470,581 108 | toggle 584,389 through 750,761 109 | turn off 899,516 through 900,925 110 | turn on 105,229 through 822,846 111 | turn off 253,77 through 371,877 112 | turn on 826,987 through 906,992 113 | turn off 13,152 through 615,931 114 | turn on 835,320 through 942,399 115 | turn on 463,504 through 536,720 116 | toggle 746,942 through 786,998 117 | turn off 867,333 through 965,403 118 | turn on 591,477 through 743,692 119 | turn off 403,437 through 508,908 120 | turn on 26,723 through 368,814 121 | turn on 409,485 through 799,809 122 | turn on 115,630 through 704,705 123 | turn off 228,183 through 317,220 124 | toggle 300,649 through 382,842 125 | turn off 495,365 through 745,562 126 | turn on 698,346 through 744,873 127 | turn on 822,932 through 951,934 128 | toggle 805,30 through 925,421 129 | toggle 441,152 through 653,274 130 | toggle 160,81 through 257,587 131 | turn off 350,781 through 532,917 132 | toggle 40,583 through 348,636 133 | turn on 280,306 through 483,395 134 | toggle 392,936 through 880,955 135 | toggle 496,591 through 851,934 136 | turn off 780,887 through 946,994 137 | turn off 205,735 through 281,863 138 | toggle 100,876 through 937,915 139 | turn on 392,393 through 702,878 140 | turn on 956,374 through 976,636 141 | toggle 478,262 through 894,775 142 | turn off 279,65 through 451,677 143 | turn on 397,541 through 809,847 144 | turn on 444,291 through 451,586 145 | toggle 721,408 through 861,598 146 | turn on 275,365 through 609,382 147 | turn on 736,24 through 839,72 148 | turn off 86,492 through 582,712 149 | turn on 676,676 through 709,703 150 | turn off 105,710 through 374,817 151 | toggle 328,748 through 845,757 152 | toggle 335,79 through 394,326 153 | toggle 193,157 through 633,885 154 | turn on 227,48 through 769,743 155 | toggle 148,333 through 614,568 156 | toggle 22,30 through 436,263 157 | toggle 547,447 through 688,969 158 | toggle 576,621 through 987,740 159 | turn on 711,334 through 799,515 160 | turn on 541,448 through 654,951 161 | toggle 792,199 through 798,990 162 | turn on 89,956 through 609,960 163 | toggle 724,433 through 929,630 164 | toggle 144,895 through 201,916 165 | toggle 226,730 through 632,871 166 | turn off 760,819 through 828,974 167 | toggle 887,180 through 940,310 168 | toggle 222,327 through 805,590 169 | turn off 630,824 through 885,963 170 | turn on 940,740 through 954,946 171 | turn on 193,373 through 779,515 172 | toggle 304,955 through 469,975 173 | turn off 405,480 through 546,960 174 | turn on 662,123 through 690,669 175 | turn off 615,238 through 750,714 176 | turn on 423,220 through 930,353 177 | turn on 329,769 through 358,970 178 | toggle 590,151 through 704,722 179 | turn off 884,539 through 894,671 180 | toggle 449,241 through 984,549 181 | toggle 449,260 through 496,464 182 | turn off 306,448 through 602,924 183 | turn on 286,805 through 555,901 184 | toggle 722,177 through 922,298 185 | toggle 491,554 through 723,753 186 | turn on 80,849 through 174,996 187 | turn off 296,561 through 530,856 188 | toggle 653,10 through 972,284 189 | toggle 529,236 through 672,614 190 | toggle 791,598 through 989,695 191 | turn on 19,45 through 575,757 192 | toggle 111,55 through 880,871 193 | turn off 197,897 through 943,982 194 | turn on 912,336 through 977,605 195 | toggle 101,221 through 537,450 196 | turn on 101,104 through 969,447 197 | toggle 71,527 through 587,717 198 | toggle 336,445 through 593,889 199 | toggle 214,179 through 575,699 200 | turn on 86,313 through 96,674 201 | toggle 566,427 through 906,888 202 | turn off 641,597 through 850,845 203 | turn on 606,524 through 883,704 204 | turn on 835,775 through 867,887 205 | toggle 547,301 through 897,515 206 | toggle 289,930 through 413,979 207 | turn on 361,122 through 457,226 208 | turn on 162,187 through 374,746 209 | turn on 348,461 through 454,675 210 | turn off 966,532 through 985,537 211 | turn on 172,354 through 630,606 212 | turn off 501,880 through 680,993 213 | turn off 8,70 through 566,592 214 | toggle 433,73 through 690,651 215 | toggle 840,798 through 902,971 216 | toggle 822,204 through 893,760 217 | turn off 453,496 through 649,795 218 | turn off 969,549 through 990,942 219 | turn off 789,28 through 930,267 220 | toggle 880,98 through 932,434 221 | toggle 568,674 through 669,753 222 | turn on 686,228 through 903,271 223 | turn on 263,995 through 478,999 224 | toggle 534,675 through 687,955 225 | turn off 342,434 through 592,986 226 | toggle 404,768 through 677,867 227 | toggle 126,723 through 978,987 228 | toggle 749,675 through 978,959 229 | turn off 445,330 through 446,885 230 | turn off 463,205 through 924,815 231 | turn off 417,430 through 915,472 232 | turn on 544,990 through 912,999 233 | turn off 201,255 through 834,789 234 | turn off 261,142 through 537,862 235 | turn off 562,934 through 832,984 236 | turn off 459,978 through 691,980 237 | turn off 73,911 through 971,972 238 | turn on 560,448 through 723,810 239 | turn on 204,630 through 217,854 240 | turn off 91,259 through 611,607 241 | turn on 877,32 through 978,815 242 | turn off 950,438 through 974,746 243 | toggle 426,30 through 609,917 244 | toggle 696,37 through 859,201 245 | toggle 242,417 through 682,572 246 | turn off 388,401 through 979,528 247 | turn off 79,345 through 848,685 248 | turn off 98,91 through 800,434 249 | toggle 650,700 through 972,843 250 | turn off 530,450 through 538,926 251 | turn on 428,559 through 962,909 252 | turn on 78,138 through 92,940 253 | toggle 194,117 through 867,157 254 | toggle 785,355 through 860,617 255 | turn off 379,441 through 935,708 256 | turn off 605,133 through 644,911 257 | toggle 10,963 through 484,975 258 | turn off 359,988 through 525,991 259 | turn off 509,138 through 787,411 260 | toggle 556,467 through 562,773 261 | turn on 119,486 through 246,900 262 | turn on 445,561 through 794,673 263 | turn off 598,681 through 978,921 264 | turn off 974,230 through 995,641 265 | turn off 760,75 through 800,275 266 | toggle 441,215 through 528,680 267 | turn off 701,636 through 928,877 268 | turn on 165,753 through 202,780 269 | toggle 501,412 through 998,516 270 | toggle 161,105 through 657,395 271 | turn on 113,340 through 472,972 272 | toggle 384,994 through 663,999 273 | turn on 969,994 through 983,997 274 | turn on 519,600 through 750,615 275 | turn off 363,899 through 948,935 276 | turn on 271,845 through 454,882 277 | turn off 376,528 through 779,640 278 | toggle 767,98 through 854,853 279 | toggle 107,322 through 378,688 280 | turn off 235,899 through 818,932 281 | turn on 445,611 through 532,705 282 | toggle 629,387 through 814,577 283 | toggle 112,414 through 387,421 284 | toggle 319,184 through 382,203 285 | turn on 627,796 through 973,940 286 | toggle 602,45 through 763,151 287 | turn off 441,375 through 974,545 288 | toggle 871,952 through 989,998 289 | turn on 717,272 through 850,817 290 | toggle 475,711 through 921,882 291 | toggle 66,191 through 757,481 292 | turn off 50,197 through 733,656 293 | toggle 83,575 through 915,728 294 | turn on 777,812 through 837,912 295 | turn on 20,984 through 571,994 296 | turn off 446,432 through 458,648 297 | turn on 715,871 through 722,890 298 | toggle 424,675 through 740,862 299 | toggle 580,592 through 671,900 300 | toggle 296,687 through 906,775 301 | -------------------------------------------------------------------------------- /Fad/Chapter5-Ex.lean: -------------------------------------------------------------------------------- 1 | import Fad.Chapter1 2 | import Fad.Chapter3 3 | import Fad.Chapter5 4 | 5 | namespace Chapter5 6 | 7 | /- 8 | # Exercicio 5.2 : qsort definitions 9 | -/ 10 | 11 | section 12 | open Quicksort 13 | 14 | theorem qsort₀_eq_qsort₁ [h₁ : LT β] [h₂ : DecidableRel (α := β) (· < ·)] 15 | (xs : List β) : qsort₀ xs = qsort₁ xs := by 16 | cases xs with 17 | | nil => 18 | rw [qsort₀, Function.comp, mkTree, Tree.flatten] 19 | rw [qsort₁] 20 | | cons a as => 21 | rw [qsort₀, Function.comp.eq_1 Tree.flatten mkTree] 22 | rw [mkTree, Tree.flatten] 23 | simp 24 | rw [← Function.comp.eq_1 Tree.flatten mkTree, ← qsort₀] 25 | rw [← Function.comp.eq_1 Tree.flatten mkTree, ← qsort₀] 26 | rw [qsort₁] 27 | simp 28 | have h₁ := qsort₀_eq_qsort₁ (List.filter (fun x => decide (x < a)) as) 29 | have h₂ := qsort₀_eq_qsort₁ (List.filter (not ∘ fun x => decide (x < a)) as) 30 | rw [h₁, h₂] 31 | termination_by xs.length 32 | decreasing_by 33 | all_goals simp 34 | all_goals rw [Nat.lt_add_one_iff] 35 | all_goals simp [List.length_filter_le] 36 | 37 | end 38 | 39 | 40 | /- # Exercicio 5.6 -/ 41 | 42 | namespace Quicksort 43 | 44 | partial def qsort₃ [LE α] [DecidableRel (α := α) (· ≤ ·)] (y : List α) : List α := 45 | match y with 46 | | [] => [] 47 | | x::xs => help x xs [] [] 48 | where 49 | help (x : α) (ys us vs: List α) : List α:= 50 | match ys with 51 | | [] => qsort₃ us ++ [x] ++ qsort₃ vs 52 | | y :: xs => 53 | if x ≤ y then 54 | help x xs us (y::vs) 55 | else 56 | help x xs (y::us) vs 57 | 58 | mutual 59 | partial def help₄ [LE α] [DecidableRel (α := α) (· ≤ ·)] 60 | (x : α) (ys us vs: List α) : List α:= 61 | dbg_trace "help₄ {ys.length} {us.length} {vs.length}" 62 | match ys with 63 | | [] => qsort₄ us ++ [x] ++ qsort₄ vs 64 | | y :: xs => 65 | if x ≤ y then 66 | help₄ x xs us (y::vs) 67 | else 68 | help₄ x xs (y::us) vs 69 | 70 | partial def qsort₄ [LE α] [DecidableRel (α := α) (· ≤ ·)] : List α → List α 71 | | [] => [] 72 | | x::xs => dbg_trace "qsort₄ {(x::xs).length}"; help₄ x xs [] [] 73 | 74 | end 75 | 76 | 77 | mutual 78 | def help₅ [LE α] [DecidableEq α] [DecidableRel (@LE.le α _)] 79 | (t : Nat) (x : α) (ys us vs: List α) : List α := 80 | if t = 0 then x :: ys else 81 | if _ : ys = [] then 82 | qsort₅ (t - 1 - vs.length) us ++ [x] ++ qsort₅ (t- 1 - us.length) vs 83 | else 84 | match ys with 85 | | y :: xs => 86 | if x ≤ y then 87 | help₅ t x xs us (y::vs) 88 | else 89 | help₅ t x xs (y::us) vs 90 | termination_by (t, ys) 91 | 92 | def qsort₅ [LE α] [DecidableEq α] [DecidableRel (α := α) (· ≤ ·)] 93 | (n : Nat) (ys : List α) : List α := 94 | if n = 0 then ys else 95 | if _ : ys.length = 0 then [] 96 | else 97 | match ys with 98 | | x :: xs => help₅ n x xs [] [] 99 | termination_by (n, ys) 100 | end 101 | 102 | 103 | end Quicksort 104 | 105 | 106 | /- # Exercicio 5.7 : Provar que T(m,n) ≤ m + n -/ 107 | 108 | def T (m n : Nat) : Nat := 109 | match m, n with 110 | | 0 , _ => 0 111 | | _ , 0 => 0 112 | | m + 1, n + 1 => 1 + max (T m (n + 1)) (T (m + 1) n) 113 | 114 | example (a b : Nat) : T a b ≤ a + b := by 115 | induction a generalizing b with 116 | | zero => 117 | rw [T, Nat.zero_add] 118 | exact Nat.zero_le b 119 | | succ a ha => 120 | induction b with 121 | | zero => 122 | rw [T, Nat.add_zero] 123 | exact Nat.zero_le (a+1) 124 | simp 125 | | succ b h2 => 126 | rw [T] 127 | have h1 : T a (b + 1) ≤ a + b + 1 := by 128 | exact ha (b+1) 129 | rw [Nat.succ_add, Nat.succ_add, Nat.zero_add] 130 | rw [Nat.succ_le_succ_iff] 131 | rw [← Nat.add_assoc] 132 | rw [Nat.max_le] 133 | rw [Nat.add_assoc, Nat.add_comm 1 b, ← Nat.add_assoc] at h2 134 | exact And.intro h1 h2 135 | 136 | 137 | /- # Exercicio 5.8 : see book -/ 138 | 139 | 140 | /- # Exercicio 5.9 -/ 141 | 142 | namespace S52 143 | 144 | example (xs : List Nat) : msort₂ xs = msort₃ xs := by 145 | induction xs with 146 | | nil => decide 147 | | cons x xs ih => 148 | simp [msort₂, msort₃] 149 | sorry 150 | 151 | end S52 152 | 153 | /- # Exercicio 5.10 -/ 154 | 155 | def expression₁ {α : Type} : List α → List α := 156 | flip (List.foldl (λ f x => (x :: ·) ∘ f) id) [] 157 | 158 | def expression₂ {α : Type} : List α → List α := 159 | flip (List.foldr (λ x f => f ∘ (x :: ·)) id) [] 160 | 161 | def reverse {α : Type} : List α → List α := 162 | List.foldl (flip List.cons) [] 163 | 164 | example (xs : List Nat) : expression₁ xs = reverse xs := by 165 | induction xs with 166 | | nil => rfl 167 | | cons x xs ih => 168 | simp [expression₁, reverse] at * 169 | simp [List.foldl_cons, flip] at * 170 | sorry 171 | 172 | example (xs : List Nat) : expression₂ xs = reverse xs := by 173 | induction xs with 174 | | nil => rfl 175 | | cons x xs ih => 176 | simp [expression₂, reverse] at * 177 | simp [List.foldr_cons, List.foldl_cons, flip] at * 178 | sorry 179 | 180 | 181 | 182 | /-- # Exercise 5.11 -/ 183 | 184 | structure Card where 185 | suit : Char 186 | rank : Char 187 | deriving Repr 188 | 189 | instance : Ord Card where 190 | compare a b := 191 | let posn seq r := seq.toList.findIdx (· = r) 192 | (compareOn (posn "SHDC" ·.suit) a b).then 193 | (compareOn (posn "AKQJT98765432" ·.rank) a b) 194 | 195 | 196 | /-- # Exercise 5.12 197 | 198 | sortBy é uma função de ordenação de listas parametrizada pela função de 199 | comparação. Precisamos adaptar merge para então basicamente renomear 200 | S52.msort₃ para sortBy parametrizando pela função de comparação. 201 | 202 | Como em Haskell, `compare` é definida para todo tipo instância de `Ord`. 203 | A função `compareOn` é equivalente a `comparing` do livro. 204 | 205 | -/ 206 | 207 | def merge₁ (f : a → a → Ordering) : List a → List a → List a 208 | | [], ys => ys 209 | | xs, [] => xs 210 | | (x :: xs), (y :: ys) => 211 | if f x y = Ordering.lt then 212 | x :: merge₁ f xs (y :: ys) 213 | else 214 | y :: merge₁ f (x :: xs) ys 215 | 216 | open Chapter1 (wrap unwrap single until') in 217 | open S52 in 218 | 219 | def sortBy (f : a → a → Ordering) : List a → List a 220 | | [] => [] 221 | | x::xs => 222 | unwrap (until' single (pairWith (merge₁ f)) (List.map wrap (x::xs))) |>.getD [] 223 | 224 | 225 | def sortOn₁ [Ord b] (f : a → b) : List a → List a := 226 | sortBy (compareOn f) 227 | 228 | def sortOn₂ [Ord b] (f : a → b) (xs : List a) : List a := 229 | sortBy (compareOn Prod.fst) ((xs.map f).zip xs) |>.map Prod.snd 230 | 231 | def sortOn₃ [Ord b] (f : a → b) : List a → List a := 232 | List.map Prod.snd ∘ sortBy (compareOn Prod.fst) ∘ List.map (λ x => (f x, x)) 233 | 234 | /- 235 | #eval sortOn₁ String.length ["aaa", "a", "aa", "aaaaaa", "aaaa"] 236 | 237 | #eval sortOn₂ (fun s => dbg_trace "fun {s}" 238 | match s.toList with 239 | | x :: y :: [] => Card.mk x y 240 | | _ => Card.mk ' ' ' ') 241 | ["H2","CA","CT","C7","C2", "SA","SQ","S9","S8", 242 | "S2","HK","H5","H3"] 243 | -/ 244 | 245 | /- # Exercicio 5.13 -/ 246 | 247 | namespace Heapsort 248 | 249 | def split [Inhabited a] [LE a] [DecidableRel (α := a) (· ≤ ·)] 250 | : List a → (a × List a × List a) 251 | | [] => (default, [], []) 252 | | x :: xs => 253 | let op x acc := 254 | if x ≤ acc.1 255 | then (x, acc.1 :: acc.2.2, acc.2.1) 256 | else (acc.1, x :: acc.2.2, acc.2.1) 257 | xs.foldr op (x, [], []) 258 | 259 | /-- Nn `split₁` the `where` makes `op` visible from outside. 260 | In `split`, `let` is defined only in the second equation of 261 | the pattern match. `let rec` would make `op` also visible. 262 | 263 | If `op` is not visible, in the `split_left_le` we would need 264 | `lift_lets ; intro op` -/ 265 | 266 | def split₁ [Inhabited a] [LE a] [DecidableRel (α := a) (· ≤ ·)] 267 | : List a → (a × List a × List a) 268 | | [] => (default, [], []) 269 | | x :: xs => 270 | xs.foldr op (x, [], []) 271 | where op x acc := 272 | if x ≤ acc.1 273 | then (x, acc.1 :: acc.2.2, acc.2.1) 274 | else (acc.1, x :: acc.2.2, acc.2.1) 275 | 276 | 277 | theorem split_left_le [Inhabited a] [LE a] [DecidableRel (α := a) (· ≤ ·)] 278 | (xs : List a) : (split₁ xs).2.1.length ≤ xs.length := by sorry 279 | 280 | partial def mkHeap [Inhabited a] [LE a] [DecidableRel (α := a) (· ≤ ·)] 281 | : List a → Tree a 282 | | [] => Tree.null 283 | | x :: xs => 284 | let p := split (x :: xs) 285 | Tree.node p.1 (mkHeap p.2.1) (mkHeap p.2.2) 286 | 287 | end Heapsort 288 | 289 | 290 | /- # Exercicio 5.15 -/ 291 | 292 | namespace Heapsort 293 | 294 | def mkPair₀ : Nat → (List a) → (Tree a × List a) 295 | | _, [] => (Tree.null, []) 296 | | n, x :: xs => 297 | if h₁ : n = 0 then 298 | (Tree.null, x :: xs) 299 | else 300 | let m := (n - 1) / 2 301 | let l_ys := mkPair₀ m xs 302 | let r_zs := mkPair₀ (n - 1 - m) l_ys.2 303 | (Tree.node x l_ys.1 r_zs.1, r_zs.2) 304 | 305 | /-- this is not necessary. Keeping only to preserve the proofs. -/ 306 | def mkPair : Nat → (List a) → (Tree a × List a) 307 | | _, [] => (Tree.null, []) 308 | | n, x :: xs => 309 | if h₁ : n = 0 then (Tree.null, x :: xs) else 310 | let m := (n - 1) / 2 311 | have h₂ : m < n := by 312 | have h₃ := Nat.ne_zero_iff_zero_lt.mp h₁ 313 | rw [Nat.div_lt_iff_lt_mul Nat.zero_lt_two] 314 | calc 315 | n - 1 < n := Nat.sub_one_lt_of_lt h₃ 316 | _ < n + n := Nat.lt_add_of_pos_right h₃ 317 | _ = n * 2 := Eq.symm (Nat.mul_two n) 318 | let y := mkPair m xs 319 | have : (n - 1 - m) < n := by exact Nat.sub_one_sub_lt_of_lt h₂ 320 | let z := mkPair (n - 1 - m) y.snd 321 | (Tree.node x y.fst z.fst, z.snd) 322 | 323 | def mkTree (xs : List a) : Tree a := (mkPair₀ xs.length xs).1 324 | 325 | 326 | end Heapsort 327 | 328 | 329 | /- # Exercicio 5.17 : qual a complexidade? -/ 330 | 331 | open List (replicate) in 332 | open Function (uncurry) in 333 | 334 | def csort (m : Nat) (xs : List Nat) : List Nat := 335 | let a := Chapter3.accumArray Nat.add 0 m (xs.map (·, 1)) 336 | a.zipWithIndex.toList.flatMap (uncurry replicate) 337 | 338 | 339 | /- # Exercicio 5.19 -/ 340 | 341 | def filter : (α → Bool) → List α → List α 342 | | _, [] => [] 343 | | p, x::xs => if p x then (x :: filter p xs) else (filter p xs) 344 | 345 | def remove_empty : List (List α) → List (List α) 346 | | [] => [] 347 | | []::xs => remove_empty xs 348 | | x::xs => x :: remove_empty xs 349 | 350 | def string_ptn : (String → Char) → List String → List (List String) 351 | | _, [] => [] 352 | | f, x::xs => 353 | let ms := "Aabcdefghijklmnopqrstuvwxyz".toList 354 | remove_empty (ms.map (fun m => filter (fun y => decide (f y = m)) (x::xs))) 355 | 356 | -- #eval string_ptn (flip String.get ⟨0⟩) ["abc", "def", "ghi", "acb"] 357 | 358 | def lists_concat : List (List α) → List α 359 | | [] => [] 360 | | x::xs => x ++ (lists_concat xs) 361 | 362 | def string_rsort : List (String → Char) → List String → List String 363 | | _, [] => [] 364 | | [], xs => xs 365 | | f::fs, xs => lists_concat (string_ptn f (string_rsort fs xs)) 366 | 367 | def string_incresing_order : Nat → List (String → Char) 368 | | sz => ((List.range sz).map (fun x => flip String.get ⟨x⟩)) 369 | 370 | 371 | -- #eval string_rsort (string_incresing_order 3) ["abc", "ghi"] 372 | 373 | 374 | end Chapter5 375 | -------------------------------------------------------------------------------- /data/AoC2017_day5.txt: -------------------------------------------------------------------------------- 1 | 0 2 | 1 3 | 0 4 | 1 5 | 0 6 | -1 7 | 0 8 | 1 9 | 2 10 | 2 11 | -8 12 | -7 13 | -3 14 | 1 15 | 0 16 | -2 17 | -6 18 | -7 19 | -11 20 | 2 21 | -11 22 | 0 23 | -18 24 | 0 25 | -18 26 | -1 27 | 1 28 | -16 29 | -3 30 | -28 31 | -10 32 | -6 33 | -11 34 | -6 35 | -17 36 | -20 37 | -15 38 | -31 39 | -37 40 | -34 41 | -14 42 | -35 43 | -34 44 | -17 45 | -28 46 | -20 47 | -12 48 | -41 49 | -29 50 | -8 51 | -1 52 | -50 53 | -46 54 | -26 55 | -41 56 | -33 57 | -17 58 | 0 59 | -28 60 | -52 61 | -38 62 | -28 63 | -29 64 | -60 65 | -23 66 | -60 67 | -55 68 | -28 69 | -43 70 | -57 71 | -66 72 | -35 73 | -48 74 | -71 75 | -25 76 | -6 77 | -27 78 | -47 79 | -77 80 | -68 81 | -21 82 | 2 83 | -39 84 | -82 85 | -2 86 | -59 87 | -61 88 | -67 89 | -26 90 | -11 91 | 0 92 | -68 93 | -85 94 | -10 95 | -62 96 | -49 97 | -28 98 | -15 99 | -34 100 | -55 101 | -92 102 | -92 103 | -37 104 | -82 105 | -49 106 | -86 107 | -25 108 | -24 109 | -81 110 | -86 111 | -6 112 | -48 113 | -79 114 | -22 115 | -30 116 | -1 117 | -63 118 | -77 119 | -64 120 | -70 121 | -86 122 | -118 123 | -36 124 | -44 125 | -50 126 | -70 127 | -76 128 | -5 129 | -72 130 | -72 131 | -84 132 | -1 133 | -104 134 | -116 135 | -18 136 | -69 137 | -78 138 | -23 139 | -99 140 | -69 141 | -32 142 | -26 143 | -4 144 | -134 145 | -22 146 | -18 147 | -70 148 | -95 149 | -13 150 | -136 151 | -73 152 | -131 153 | -24 154 | -101 155 | -136 156 | -29 157 | -132 158 | -154 159 | -108 160 | -127 161 | -48 162 | -134 163 | -122 164 | -162 165 | -2 166 | -61 167 | -9 168 | -4 169 | -126 170 | -146 171 | -161 172 | -157 173 | -116 174 | -95 175 | -83 176 | -36 177 | -86 178 | -57 179 | -42 180 | -103 181 | -73 182 | 1 183 | 0 184 | -28 185 | -156 186 | -67 187 | -178 188 | -36 189 | -169 190 | -46 191 | -16 192 | -97 193 | -86 194 | -112 195 | -186 196 | -111 197 | -69 198 | -158 199 | -37 200 | -75 201 | -109 202 | -186 203 | -16 204 | -84 205 | -73 206 | -83 207 | -139 208 | -54 209 | -89 210 | -191 211 | -126 212 | -15 213 | -158 214 | -19 215 | -116 216 | -73 217 | -13 218 | -184 219 | -121 220 | -14 221 | -116 222 | -167 223 | -174 224 | -103 225 | -66 226 | -128 227 | -156 228 | -5 229 | -174 230 | -220 231 | -213 232 | -96 233 | -139 234 | -22 235 | -102 236 | -33 237 | -118 238 | -163 239 | -184 240 | -17 241 | -76 242 | -72 243 | -96 244 | -106 245 | -203 246 | -55 247 | -181 248 | -207 249 | -40 250 | -235 251 | -139 252 | -5 253 | -127 254 | -21 255 | -155 256 | -183 257 | -51 258 | -54 259 | -38 260 | -247 261 | -218 262 | -56 263 | -34 264 | -173 265 | -241 266 | -187 267 | -38 268 | -13 269 | -172 270 | -2 271 | -235 272 | -167 273 | -191 274 | -250 275 | -150 276 | -34 277 | -151 278 | -183 279 | -119 280 | -90 281 | -21 282 | -93 283 | -275 284 | -168 285 | -160 286 | -97 287 | -100 288 | -25 289 | -273 290 | -245 291 | -44 292 | -223 293 | -201 294 | -156 295 | -12 296 | -55 297 | -189 298 | -181 299 | -10 300 | -92 301 | -152 302 | -90 303 | -217 304 | -68 305 | -81 306 | -76 307 | -86 308 | -48 309 | -287 310 | -281 311 | -63 312 | -83 313 | -66 314 | -50 315 | -49 316 | -310 317 | -254 318 | -121 319 | -294 320 | -132 321 | -53 322 | -30 323 | -223 324 | -85 325 | -297 326 | -264 327 | -58 328 | -51 329 | -294 330 | -283 331 | -3 332 | 0 333 | -262 334 | -33 335 | -136 336 | -14 337 | -238 338 | -6 339 | -312 340 | -17 341 | -328 342 | -299 343 | -245 344 | -266 345 | -6 346 | -330 347 | -117 348 | -172 349 | -260 350 | -224 351 | -139 352 | -156 353 | -165 354 | -13 355 | -243 356 | -173 357 | -42 358 | -67 359 | -7 360 | -148 361 | -1 362 | -105 363 | -205 364 | -223 365 | -122 366 | -82 367 | -221 368 | -317 369 | -330 370 | -240 371 | -189 372 | -12 373 | -268 374 | -243 375 | -177 376 | -120 377 | -320 378 | -127 379 | -351 380 | -178 381 | -219 382 | -351 383 | -128 384 | -28 385 | -227 386 | -188 387 | -195 388 | -205 389 | -204 390 | -283 391 | -316 392 | -276 393 | -319 394 | -312 395 | -337 396 | -318 397 | -136 398 | -33 399 | -307 400 | -397 401 | -387 402 | -303 403 | -12 404 | -347 405 | -112 406 | -171 407 | -222 408 | -358 409 | -215 410 | -71 411 | -99 412 | -108 413 | -24 414 | -291 415 | -344 416 | -97 417 | -99 418 | -6 419 | -270 420 | -327 421 | -32 422 | -387 423 | -402 424 | -13 425 | -175 426 | -243 427 | -374 428 | -422 429 | -382 430 | -152 431 | -420 432 | -266 433 | -326 434 | -37 435 | -215 436 | -357 437 | -423 438 | -16 439 | -272 440 | -357 441 | -87 442 | -184 443 | -21 444 | -351 445 | -300 446 | -219 447 | -390 448 | -12 449 | -15 450 | -78 451 | -69 452 | -35 453 | -308 454 | -303 455 | -300 456 | -265 457 | -440 458 | -19 459 | -117 460 | -87 461 | -218 462 | -163 463 | -317 464 | -42 465 | -55 466 | -185 467 | -245 468 | -196 469 | -183 470 | -327 471 | -467 472 | -102 473 | -432 474 | -162 475 | -202 476 | -39 477 | -179 478 | -301 479 | -237 480 | -299 481 | -33 482 | -198 483 | -127 484 | -138 485 | -454 486 | -46 487 | -87 488 | -362 489 | -448 490 | -382 491 | -42 492 | -358 493 | -475 494 | -350 495 | -50 496 | -380 497 | -316 498 | -380 499 | -463 500 | -108 501 | -405 502 | -139 503 | -480 504 | -30 505 | -212 506 | -308 507 | -239 508 | -223 509 | -306 510 | -81 511 | -89 512 | -172 513 | -304 514 | -87 515 | -380 516 | -394 517 | -507 518 | -392 519 | -98 520 | -403 521 | -155 522 | -13 523 | -197 524 | -66 525 | -244 526 | -401 527 | -278 528 | -391 529 | -64 530 | -460 531 | -368 532 | -178 533 | -145 534 | -440 535 | -49 536 | -369 537 | -418 538 | -332 539 | -200 540 | -294 541 | -495 542 | -104 543 | -5 544 | -261 545 | -168 546 | -392 547 | -230 548 | -154 549 | -472 550 | -404 551 | -472 552 | -307 553 | -256 554 | -169 555 | -330 556 | -500 557 | -365 558 | -146 559 | -133 560 | -84 561 | -336 562 | -405 563 | -555 564 | -74 565 | -68 566 | -354 567 | -552 568 | -108 569 | -80 570 | -406 571 | -164 572 | -119 573 | -487 574 | -151 575 | -113 576 | -244 577 | -471 578 | -80 579 | -312 580 | -495 581 | -556 582 | -76 583 | -24 584 | -546 585 | -493 586 | -340 587 | -464 588 | -328 589 | -7 590 | -474 591 | -246 592 | -237 593 | -40 594 | -199 595 | -346 596 | -330 597 | -139 598 | -284 599 | -435 600 | -83 601 | -210 602 | -423 603 | -361 604 | -56 605 | -271 606 | -140 607 | -162 608 | -232 609 | -391 610 | -42 611 | -99 612 | -590 613 | 2 614 | -271 615 | -101 616 | -114 617 | -117 618 | -310 619 | -502 620 | -287 621 | -319 622 | -323 623 | -362 624 | -551 625 | -439 626 | -533 627 | -183 628 | -404 629 | -401 630 | -343 631 | -36 632 | -89 633 | -454 634 | -128 635 | -611 636 | -6 637 | -619 638 | -110 639 | -389 640 | -290 641 | -270 642 | -375 643 | -283 644 | -472 645 | -65 646 | -195 647 | -129 648 | -61 649 | -548 650 | -151 651 | -74 652 | -612 653 | -156 654 | -371 655 | -42 656 | -447 657 | -565 658 | -394 659 | -550 660 | -476 661 | -592 662 | -262 663 | -96 664 | -529 665 | -395 666 | -204 667 | -491 668 | -167 669 | -186 670 | -527 671 | -508 672 | -245 673 | -455 674 | -552 675 | -672 676 | -338 677 | -269 678 | -104 679 | -240 680 | -77 681 | -303 682 | -227 683 | -453 684 | -126 685 | -294 686 | -572 687 | -8 688 | -527 689 | -361 690 | -438 691 | -457 692 | -513 693 | -560 694 | -442 695 | -649 696 | -321 697 | -123 698 | -52 699 | -166 700 | -320 701 | -301 702 | -570 703 | -684 704 | -325 705 | -515 706 | -547 707 | -52 708 | -221 709 | -488 710 | -182 711 | -618 712 | -109 713 | -497 714 | -167 715 | -288 716 | -358 717 | -334 718 | -313 719 | -288 720 | -102 721 | -409 722 | -143 723 | -204 724 | -216 725 | -681 726 | -512 727 | -245 728 | -301 729 | -35 730 | -262 731 | -239 732 | -405 733 | -682 734 | -715 735 | -438 736 | -314 737 | -179 738 | -611 739 | -667 740 | -622 741 | -511 742 | -463 743 | -370 744 | -338 745 | -434 746 | -580 747 | -637 748 | -201 749 | -213 750 | -357 751 | -443 752 | -382 753 | -315 754 | -483 755 | -399 756 | -624 757 | -318 758 | -226 759 | -652 760 | -638 761 | -743 762 | -330 763 | -647 764 | -146 765 | -138 766 | -698 767 | -511 768 | -173 769 | -663 770 | -333 771 | -564 772 | -160 773 | -239 774 | -243 775 | -91 776 | -65 777 | -468 778 | -256 779 | -197 780 | -210 781 | -575 782 | -420 783 | -715 784 | -681 785 | -454 786 | -226 787 | -226 788 | -339 789 | -473 790 | -737 791 | -62 792 | -149 793 | -351 794 | -770 795 | -313 796 | -216 797 | -491 798 | -511 799 | -269 800 | -628 801 | -391 802 | -429 803 | -110 804 | -199 805 | -409 806 | -516 807 | -7 808 | -433 809 | -405 810 | -792 811 | -685 812 | -615 813 | -287 814 | -385 815 | -627 816 | -527 817 | -426 818 | -626 819 | -164 820 | -767 821 | -794 822 | -115 823 | -483 824 | -323 825 | -371 826 | -679 827 | -772 828 | -808 829 | -2 830 | -16 831 | -459 832 | -749 833 | -569 834 | -139 835 | -7 836 | -555 837 | -161 838 | -613 839 | -230 840 | -771 841 | -825 842 | -241 843 | -579 844 | -710 845 | -73 846 | -790 847 | -653 848 | -655 849 | -394 850 | -218 851 | -711 852 | -467 853 | -774 854 | -694 855 | -664 856 | -357 857 | -29 858 | -121 859 | -643 860 | -742 861 | -388 862 | -633 863 | -440 864 | -755 865 | -581 866 | -661 867 | -653 868 | -536 869 | -596 870 | -10 871 | -796 872 | -230 873 | -813 874 | -125 875 | -540 876 | -584 877 | -389 878 | -144 879 | -346 880 | -213 881 | -444 882 | -205 883 | -712 884 | -651 885 | -670 886 | -139 887 | -60 888 | -620 889 | -49 890 | -284 891 | -212 892 | -452 893 | -520 894 | -243 895 | -356 896 | -348 897 | -442 898 | -585 899 | -202 900 | -207 901 | -222 902 | -47 903 | -49 904 | -408 905 | -571 906 | -154 907 | -695 908 | -802 909 | -524 910 | -523 911 | -617 912 | -615 913 | -571 914 | -92 915 | -344 916 | -675 917 | -613 918 | -759 919 | -29 920 | -833 921 | -662 922 | -223 923 | -46 924 | -156 925 | -373 926 | -412 927 | -848 928 | -93 929 | -695 930 | -250 931 | -810 932 | -477 933 | -150 934 | -282 935 | -789 936 | -193 937 | -443 938 | -193 939 | -159 940 | -840 941 | -755 942 | -508 943 | -404 944 | -307 945 | -80 946 | -320 947 | -14 948 | -245 949 | -746 950 | -610 951 | -855 952 | -552 953 | -323 954 | -366 955 | -45 956 | -16 957 | -335 958 | -852 959 | -46 960 | -459 961 | -461 962 | -537 963 | -547 964 | -180 965 | -842 966 | -213 967 | -447 968 | -712 969 | -633 970 | -362 971 | -953 972 | -407 973 | -47 974 | 0 975 | -466 976 | -107 977 | -648 978 | -528 979 | -413 980 | -828 981 | -217 982 | -484 983 | -969 984 | -121 985 | -858 986 | -208 987 | -618 988 | -384 989 | -16 990 | -91 991 | -662 992 | -348 993 | -675 994 | -63 995 | -713 996 | -966 997 | -678 998 | -293 999 | -827 1000 | -445 1001 | -387 1002 | -212 1003 | -763 1004 | -847 1005 | -756 1006 | -299 1007 | -443 1008 | -80 1009 | -286 1010 | -954 1011 | -521 1012 | -394 1013 | -357 1014 | -861 1015 | -530 1016 | -649 1017 | -671 1018 | -437 1019 | -884 1020 | -606 1021 | -73 1022 | -452 1023 | -354 1024 | -729 1025 | -927 1026 | -248 1027 | -2 1028 | -738 1029 | -521 1030 | -440 1031 | -435 1032 | -291 1033 | -104 1034 | -402 1035 | -375 1036 | -875 1037 | -686 1038 | -812 1039 | -539 1040 | -934 1041 | -536 1042 | -924 1043 | -924 1044 | -365 1045 | -------------------------------------------------------------------------------- /data/AoC2020_day3.txt: -------------------------------------------------------------------------------- 1 | ....#...##.#.........#....#.... 2 | #.......#...#...#.#............ 3 | #..#..#.#.##....#.#........#... 4 | ........##...................#. 5 | ........#...##...#.#.###....... 6 | ##............#...#.....#.##... 7 | ...........#....###...#.....#.. 8 | .......#......#..##..#.....#... 9 | ..#.#..#....#.........#...#..#. 10 | .........##......#.....##..##.. 11 | ........#.....#....#..##......# 12 | ..#..#.......#..............#.. 13 | .....#.#....................... 14 | .#.#....#.........#............ 15 | .......#.....#.###............. 16 | ......##......#...........#..#. 17 | .#...............##...#........ 18 | .....#..##........###.........# 19 | #...........#..#............#.. 20 | .........#....#..#.#......#.... 21 | .......#.........#..##......... 22 | .##.....#..................#... 23 | ....#............#.#....#.....# 24 | ..#....#...##....#...#.#...#... 25 | ..........#................#.#. 26 | #...#.#.#.####..#.#..........#. 27 | ..#...#.##......#...........#.. 28 | ..#.....#...#.......#......#..# 29 | ..............#.......#........ 30 | .#..#..........#.....#...#..#.# 31 | #........#...#......#.......#.. 32 | #..................#........... 33 | ..#...#........#...#..#........ 34 | ..............#.....#.....#..#. 35 | #.#.......#..............##.##. 36 | ....#.#.....##....#...........# 37 | ......#....#...#..#.......#.... 38 | ....#..#.#.....#..##.....#....# 39 | ...........#.......#.#.#....... 40 | #.......#..##........#..#...... 41 | .........#.##..#..............# 42 | ...........#............###.#.. 43 | ..#.....#.....##...#.........#. 44 | ....##............##........#.. 45 | .....###..........#......##.... 46 | #...##..#..#..........#........ 47 | ....#.....#.......#..#.#...##.. 48 | .#....#........#.#.........#.#. 49 | ##...#.#.....#......#.......... 50 | .....##.....#....#.....###.#..# 51 | ..............#..###..#...#..#. 52 | ....#...#....#.............#.#. 53 | .#.........#.....#........#.##. 54 | ....#.........#..........#..... 55 | .......#........#.#.#.......... 56 | #........##....#.........#..... 57 | ..##..........#....#.#...#....# 58 | #...#.#......#..##..........#.# 59 | .....#..#...#..#............... 60 | #...#..............#........... 61 | .#...#....#..##.....#....#.#... 62 | .#...#.......#...#..#.##....#.. 63 | #....#........#....#...#....... 64 | #..#......#.....#.....#..##.... 65 | ......#.#....##....##..#...#... 66 | ..#....#.#.###..............#.. 67 | .#.##.......#.#.#..#...#..#.... 68 | ..#..........#.#....#..#.#....# 69 | ..........#...#...#..........#. 70 | ..........#.....#.#..#..#....## 71 | .#.#...##...#...........####... 72 | ........##..#.#..........#.##.# 73 | #......###...........#...#..... 74 | ..#.#....##.........##....#.... 75 | #....#.##..##..#..#.....#.....# 76 | .##.....##....##....#.......#.. 77 | #...#.....##....#..........#... 78 | ............#.#.##....#....#... 79 | ....#............#.....#......# 80 | ....................#.......... 81 | ..#....................#..#.... 82 | ....#.....#........#..##...#... 83 | #.....#.#....................## 84 | .#....#.#.#...#..........#....# 85 | ....#...#......#...#.....##...# 86 | .....#......................... 87 | .......#..#.#...#...#...#.....# 88 | ...#......#.##.#...#..#...##.#. 89 | ...........................#..# 90 | ..#.#.....#........##.......... 91 | ....#...##........#.#.#..#...## 92 | ..##.....#..###.........##.##.. 93 | .#..#.....#...#.............#.. 94 | #..............##...#....##.... 95 | .##......#.#............#...... 96 | .............##...#.#.......#.. 97 | .........#..#..#............... 98 | ........##......#....##........ 99 | ...#.........#.#.#............. 100 | #..........#......#......#..#.. 101 | .............##.#.#..#.#.#...#. 102 | .....#.........#............... 103 | ..##.#..#.....##..#........#.#. 104 | .#..........#.#.......#......## 105 | .#........................#.... 106 | #....#....#...#..#......#...... 107 | ........#.......#......#.....#. 108 | .....#....##..#...###...#....#. 109 | ....#.........#....#......#.... 110 | .............#...#....#.......# 111 | .....#.........#..#.#.......... 112 | .........#..#........#.#.#..... 113 | ......#.##......#....#.#.##.#.. 114 | .#...#.#...#...#.#......#....## 115 | .#................#......#..... 116 | #.#.#...............#.......... 117 | .....#.#.......#...#........#.. 118 | #...#.#.#.##..#...........#..#. 119 | .............###.........#....# 120 | .#.....#.......##....##.......# 121 | ....#...#.......#.##.....#..... 122 | ...........##.........#...#.... 123 | ..............#.#..#.....#..#.. 124 | #.#...#..#.#.........#......#.# 125 | #.##.....##....#........#.#.#.# 126 | ##.#.###.........##.......#..#. 127 | #.....#.....................#.. 128 | .........##........#........... 129 | .###........##....#...#........ 130 | ....#.#........##...........#.. 131 | ..........#.....#..........#..# 132 | ......#..............#......#.. 133 | .....#...#......#...#...#...... 134 | ..........#.#..#....#...#..###. 135 | #..##........#................# 136 | ..#............................ 137 | .....#.........#.#............. 138 | ........#...#.....#...##......# 139 | ..#........#................#.. 140 | ......#....#..#......#......... 141 | ...........##....#..#.#........ 142 | .....#.............###......... 143 | #............#......#..#....... 144 | ..#..#.................#..#..## 145 | .......#......#.....#........#. 146 | ....................#..#.##...# 147 | .#..##...............##...#.... 148 | ...#...#....#........#......... 149 | .....##...#.....###............ 150 | .###.........#........#.....##. 151 | .............#...#............. 152 | ...#.#...............#..##..#.# 153 | ...#...............#..#.....#.. 154 | ....#.#..................#...#. 155 | ..........#...........#.#...### 156 | #...#......#................#.. 157 | ...#.#.......#...#......#.##... 158 | ......#..........#............. 159 | ##.......#.##.#...........#.... 160 | ......#...#.#.....#............ 161 | .#.....#.....#.....#.........#. 162 | ..................#............ 163 | .#.#.#.....#......#.##......... 164 | .......#..##.##......#..#....#. 165 | ...#.#.#......#...#........#... 166 | ..#............#......#.......# 167 | ..#......#........#.........#.. 168 | ..#..#.#.#.....#.............#. 169 | ..#.#..##......#...#...##...... 170 | .##...#....##.#.#...........#.. 171 | ..............#..#...#....#.... 172 | .......#.#........#............ 173 | .....##..###........#.......... 174 | ......................#........ 175 | ..##....#....#................. 176 | .##.#.###.#........#.##..#...#. 177 | ##................#...........# 178 | ....#..##.....##............... 179 | .#.....#..#............#.....#. 180 | #.........#..............#..... 181 | ...##.#......#...#............. 182 | ................#.............. 183 | ...#.....#....##...#..#....#... 184 | ..............##..#...#.##..#.. 185 | ......................#..#....# 186 | .......#....#..#.##.........#.# 187 | #...#........##.......#........ 188 | ...##...............#.....#.... 189 | .##...##...#................... 190 | .........##.#...#.........#.... 191 | ............#............#..#.. 192 | .............................#. 193 | ....#.#....#................... 194 | ......#......#...#..##......... 195 | #........#.#.#.#.#......#....#. 196 | .#.........#.#...#......#..#.#. 197 | ..............#....##.........# 198 | .#.......#..#....#.#.#....#.... 199 | ...###.#.#..#...#....#....#.... 200 | #........#....................# 201 | ......#...##.###..#..##...#.... 202 | .....#........#.......#........ 203 | #..#...........#.#............. 204 | ....##.#...#..##............##. 205 | #.#..##..#...#...#.....#....... 206 | ..#.............#.##..#...#.##. 207 | .#.....##.#..#...#...........#. 208 | ....#...#....................## 209 | ....##......#.###......#......# 210 | ...#...#.........#..#.##....#.. 211 | #......#..#....###.........#... 212 | #...........##.............#.#. 213 | #..............##....#......#.. 214 | .........#...#.#...#...#....... 215 | ....#....#............#.......# 216 | ........#...#....#......##..... 217 | ..........#.#..#.........#..... 218 | #........#.##....##......#..... 219 | ...#.......#................... 220 | ###...#...#..#.##....#.....#... 221 | ........##..........#.##..#.... 222 | .....#......#..#.....#.....#.#. 223 | ...#..#..##..###.....##.#...... 224 | #..#......##...#............#.. 225 | #............#....#..#......... 226 | #........#.......#......#..##.# 227 | ...#.#.........#.#............. 228 | #..............#..............# 229 | #.#......#..........##......... 230 | #..##...........#..##...#...... 231 | .....#.#.....#......#.....#.#.# 232 | .#.##...#...##...........#....# 233 | #.............#........#....... 234 | ..##.............#...#......... 235 | ....#.#......###....#.......... 236 | ...#..#.....#..##.#....#...#.#. 237 | .............##................ 238 | #.#............#........#..#.#. 239 | .#......#.....#...........#.... 240 | ...#.........#...........#.##.. 241 | .....#...#.....#..#..........#. 242 | ........#.#...............#.#.. 243 | .......#..#..#.....#.......##.. 244 | .#...#...#..#...##...#......... 245 | ..........##....#..#.##..#..... 246 | ....#.................#...#.... 247 | .........#...#......#....#....# 248 | .........#..#...#.##........##. 249 | #.#....##.......#.#............ 250 | ##.......##..................#. 251 | ......#...#......##............ 252 | ##.#...#.#...........#..#...... 253 | .........#.........#..#.#...#.. 254 | .#...#.......#.#...###......... 255 | ................#.#.....#...... 256 | ..#...#.....#........#......... 257 | .........##.###.#.#.....#...#.. 258 | #..#..........#....#.#...#...## 259 | ##.#.#....#..##.............#.# 260 | .###....#..#...............##.. 261 | ............#......#.#.#....#.. 262 | ........#...#..#...#........... 263 | ##.........#................#.. 264 | ...###...#.#..#...#..........## 265 | ...#......#......##........#... 266 | .......#............#.......... 267 | .....#.....##....#.....###..... 268 | .#...#...#.....#..#..#....#..#. 269 | #.#........#..#.......##...#.## 270 | .....#.....##..#.##........#..# 271 | .....#...#...........#......... 272 | ..#....#.#...#..#....##...#...# 273 | ...........#...##.........#.... 274 | ..#....#....##........#.####... 275 | #.............#.#.............# 276 | ...................#.....#.#..# 277 | .#....#.#.............#.#...... 278 | #...........#............#.#... 279 | ..#.........#.#....#.......##.. 280 | #....####......#...#......#.... 281 | ....##....#...................# 282 | ....#.##....#.............#.... 283 | .........##........#.....#..#.. 284 | ............#...#.............. 285 | ............#..##....#.....##.# 286 | ............#.....#......#..... 287 | ........#..#........##.#....... 288 | ...#.#........#..............#. 289 | ............#.........#..#.#... 290 | ................#.............# 291 | ..##..........##......#.#...... 292 | ..#..#.##....#.........#...#... 293 | ...........##...#.#.#.......... 294 | .#.#.......#.#...#.........#... 295 | .........#..#........#..#.#.... 296 | ..........##..#.##....#....#... 297 | ....#...............#.......#.. 298 | ##..........##................. 299 | ....#.#.#.....#..........##.#.. 300 | ..............#.##..........##. 301 | ##...............#...#..#...... 302 | ..#..#..........#......#....... 303 | #...#..##.#.#.................. 304 | ....#....##......##.#...#....## 305 | .#...#.#....##.............#..# 306 | ................#......###..... 307 | ..#..#.............#.#.......#. 308 | ..#..................#.......#. 309 | .....#.......#....#.##...#.##.. 310 | .....##.......#......#..#...... 311 | #..#.......#........#.......... 312 | ..#...#..#....#.........#...... 313 | #..#..#......##..#.##....####.. 314 | ......##.#.....#..#.......#.... 315 | .##...#.....#..#...#.#......... 316 | #.....#........###....#...#..#. 317 | .#....#.#..#......#............ 318 | .........#..#..#.....#........# 319 | ..#.......#..........#..#...... 320 | ......#.......##.#....#.#.#.... 321 | .#............#.....#.......#.. 322 | ...#..#...............#........ 323 | .....#......................... 324 | -------------------------------------------------------------------------------- /Fad/Chapter1.lean: -------------------------------------------------------------------------------- 1 | 2 | namespace Chapter1 3 | 4 | -- 1.1 Basic types and functions 5 | 6 | def map : (a → b) → List a → List b 7 | | _, [] => [] 8 | | f, (x :: xs) => f x :: map f xs 9 | 10 | example : map (· * 10) [1,2,3] = [10,20,30] := rfl 11 | example : map (λ x => x * 10) [1,2,3] = [10,20,30] := rfl 12 | 13 | def filter {a : Type} : (a → Bool) → List a → List a 14 | | _, [] => [] 15 | | p, (x :: xs) => if p x then x :: filter p xs else filter p xs 16 | 17 | example : filter (· > 5) [1,2,2,4,5,8,6] = [8,6] := rfl 18 | 19 | def foldr {a b : Type} : (a → b → b) → b → List a → b 20 | | _, e, [] => e 21 | | f, e, (x::xs) => f x (foldr f e xs) 22 | 23 | def foldl {a b : Type} : (b → a → b) → b → List a → b 24 | | _, e, [] => e 25 | | f, e, (x::xs) => foldl f (f e x) xs 26 | 27 | def length' (xs : List a) : Nat := 28 | foldr (fun _ y => y + 1) 0 xs 29 | 30 | def length : List a → Nat := foldr succ 0 31 | where succ _ n := n + 1 32 | 33 | example : length ["a", "b", "c"] = 3 := by 34 | unfold length 35 | unfold foldr 36 | unfold foldr 37 | unfold foldr 38 | rewrite [length.succ] 39 | rewrite [length.succ] 40 | rewrite [length.succ] 41 | rewrite [foldr.eq_1] 42 | rfl 43 | 44 | example : foldr Nat.add 0 [1,2,3] = 6 := by 45 | unfold foldr 46 | unfold foldr 47 | unfold foldr 48 | unfold foldr 49 | rewrite (config := {occs := .pos [3]}) [Nat.add] 50 | rewrite (config := {occs := .pos [2]}) [Nat.add] 51 | rewrite (config := {occs := .pos [1]}) [Nat.add] 52 | rfl 53 | 54 | example : foldl Nat.add 0 [1,2,3] = 6 := by 55 | unfold foldl 56 | unfold foldl 57 | unfold foldl 58 | unfold foldl 59 | rewrite (config := {occs := .pos [3]}) [Nat.add] 60 | rewrite (config := {occs := .pos [2]}) [Nat.add] 61 | rewrite (config := {occs := .pos [1]}) [Nat.add] 62 | rfl 63 | 64 | -- 1.2 Processing lists 65 | 66 | def concat1 {a : Type} : List (List a) → List a := 67 | List.foldr List.append [] 68 | 69 | def concat2 {a : Type} : List (List a) → List a := 70 | List.foldl List.append [] 71 | 72 | example : concat1 [[1,2,3,4], [5], [6]] = [1,2,3,4,5,6] := by 73 | unfold concat1 74 | unfold List.foldr 75 | rw [List.append.eq_2] 76 | rw [List.append.eq_2] 77 | rw [List.append.eq_2] 78 | rw [List.append.eq_2] 79 | rw [List.append.eq_1] 80 | unfold List.foldr 81 | rw [List.append.eq_2] 82 | rw [List.append.eq_1] 83 | unfold List.foldr 84 | rw [List.append.eq_2] 85 | rw [List.append.eq_1] 86 | unfold List.foldr 87 | rfl 88 | 89 | example : concat2 [[1,2,3,4], [5], [6]] = [1,2,3,4,5,6] := by 90 | unfold concat2 91 | unfold List.foldl 92 | rw [List.append.eq_1] 93 | unfold List.foldl 94 | rw [List.append.eq_2] 95 | rw [List.append.eq_2] 96 | rw [List.append.eq_2] 97 | rw [List.append.eq_2] 98 | rw [List.append.eq_1] 99 | unfold List.foldl 100 | rw [List.append.eq_2] 101 | rw [List.append.eq_2] 102 | rw [List.append.eq_2] 103 | rw [List.append.eq_2] 104 | rw [List.append.eq_2] 105 | rw [List.append.eq_1] 106 | unfold List.foldl 107 | rfl 108 | 109 | open List in 110 | example : ∀ xs : List Nat, foldr cons [] xs = xs := by 111 | intro xs 112 | induction xs with 113 | | nil => unfold foldr; rfl 114 | | cons a as ih => unfold foldr; rewrite [ih]; rfl 115 | 116 | 117 | 118 | def scanr₀ (f : a → b → b) (q₀ : b) (as : List a) : List b := 119 | let rec aux : List a → {l : List b // l ≠ []} 120 | | [] => Subtype.mk [q₀] (by simp) 121 | | (x :: xs) => 122 | let qs := aux xs 123 | Subtype.mk (f x (List.head qs qs.property) :: qs) (by simp) 124 | aux as 125 | 126 | def scanr : (a → b → b) → b → List a → List b 127 | | _, q₀, [] => [q₀] 128 | | f, q₀, (x :: xs) => 129 | match scanr f q₀ xs with 130 | | [] => [] 131 | | qs@(q :: _) => f x q :: qs 132 | 133 | /- 134 | #eval scanr Nat.add 0 [1,2,3,4] 135 | #eval scanr Nat.add 42 [] 136 | -/ 137 | 138 | def scanl : (b → a → b ) → b → List a → List b 139 | | _, e, [] => [e] 140 | | f, e, (x :: xs) => e :: scanl f (f e x) xs 141 | 142 | /- 143 | #eval scanl Nat.add 0 [1,2,3,4] 144 | #eval scanl Nat.add 42 [] 145 | #eval scanl (λ r n => n :: r) 146 | "foo".toList ['a', 'b', 'c', 'd'] |>.map List.asString 147 | -/ 148 | 149 | def inits {a : Type} : List a → List (List a) 150 | | [] => [[]] 151 | | (x :: xs) => [] :: (inits xs).map (fun ys => x :: ys) 152 | 153 | def tails {a : Type} : List a → List (List a) 154 | | [] => [[]] 155 | | (x :: xs) => (x :: xs) :: tails xs 156 | 157 | theorem map_compose {α β γ : Type} (f : β → γ) (g : α → β) (l : List α) : 158 | map (f ∘ g) l = map f (map g l) := by 159 | induction l with 160 | | nil => rfl 161 | | cons x xs ih => 162 | simp [map, ih] 163 | 164 | theorem foldl_comp {α β: Type} (y: α) (e : β) (f : β → α → β): 165 | foldl f e ∘ (fun x => y :: x) = foldl f (f e y) := by rfl 166 | 167 | theorem map_map {α : Type} (g : α -> α ) (a : List α): List.map g a = map g a := by 168 | induction a with 169 | | nil => rfl 170 | | cons a as ih => 171 | rw [map,List.map] 172 | rw [ih] 173 | done 174 | 175 | example {a b : Type} (f : b → a → b) (e : b) : 176 | map (foldl f e) ∘ inits = scanl f e := by 177 | funext xs 178 | induction xs generalizing e with 179 | | nil => simp [map, inits, foldl, scanl] 180 | | cons x xs ih => 181 | rw [Function.comp] 182 | rw [inits,map,foldl,map_map,←map_compose] 183 | rw [foldl_comp,scanl] 184 | have hx := ih (f e x) 185 | rw [← hx] 186 | simp 187 | 188 | -- 1.3 Inductive and recursive definitions 189 | 190 | def inserts {a : Type} : a → List a → List (List a) 191 | | x, [] => [[x]] 192 | | x, (y :: ys) => (x :: y :: ys) :: map (y :: ·) (inserts x ys) 193 | 194 | -- #eval inserts 1 [2,3,4] 195 | 196 | 197 | def concatMap (f : a → List b) : List a → List b := 198 | concat1 ∘ (List.map f) 199 | 200 | -- #eval concatMap (String.toList ·) ["aa", "bb", "cc"] 201 | 202 | 203 | def perm₀ : List a → List (List a) 204 | | [] => [[]] 205 | | (x :: xs) => concatMap (inserts x ·) (perm₀ xs) 206 | 207 | def perm₁ : List a → List (List a) := foldr step [[]] 208 | where 209 | step x xss := concatMap (inserts x) xss 210 | 211 | def perm₁' : List a → List (List a) := 212 | foldr (concatMap ∘ inserts) [[]] 213 | 214 | 215 | def picks {a : Type} : List a → List (a × List a) 216 | | [] => [] 217 | | (x :: xs) => 218 | (x, xs) :: ((picks xs).map (λ p => (p.1, x :: p.2))) 219 | 220 | partial def perm₂ : List a → List (List a) 221 | | [] => [[]] 222 | | xs => concatMap (λ p => (perm₂ p.2).map (p.1 :: ·)) (picks xs) 223 | 224 | 225 | theorem picks_less : 226 | p ∈ picks xs → p.2.length < xs.length := by 227 | induction xs with 228 | | nil => 229 | unfold picks 230 | intro h 231 | simp at h 232 | | cons x xs ih => 233 | intro h 234 | unfold picks at h 235 | simp at h 236 | cases h with 237 | | inl h => 238 | rw [h] 239 | simp 240 | | inr h => 241 | simp; rw [Nat.lt_succ_iff, Nat.le_iff_lt_or_eq]; left; apply ih 242 | apply Exists.elim h; intro q hq; sorry 243 | 244 | theorem perm_aux {a : Type} 245 | (v : a) (l : List a) 246 | (p : a × List a) (h : p ∈ picks (v :: l)) : 247 | p.2.length < (v :: l).length := by 248 | induction l with 249 | | nil => 250 | unfold picks at h 251 | rw [picks.eq_1] at h 252 | rw [List.map.eq_1] at h 253 | simp; simp at h; rw [h]; done 254 | | cons x xs ih => sorry 255 | 256 | def perm : List a → List (List a) 257 | | [] => [[]] 258 | | x :: xs => concatMap (fun ⟨p, hp⟩ ↦ 259 | have : p.2.length < (x :: xs).length := perm_aux x xs p hp 260 | (perm p.2).map (p.1 :: ·)) 261 | (picks (x :: xs)).attach 262 | termination_by xs => xs.length 263 | 264 | -- #eval perm [1,2,3] 265 | 266 | 267 | partial def until' (p: a → Bool) (f: a → a) (x : a) : a := 268 | if p x then x 269 | else until' p f (f x) 270 | 271 | partial def while' (p : a → Bool) := until' (not ∘ p) 272 | 273 | -- #eval until' (· > 10) (· + 1) 0 274 | 275 | 276 | -- 1.4 Fusion 277 | 278 | example {a b c : Type} (f : b → c) (g : a → b) : map f ∘ map g = map (f ∘ g) := by 279 | funext xs 280 | induction xs with 281 | | nil => rfl 282 | | cons x xs ih => simp [Function.comp, map]; rw [← ih]; rfl 283 | 284 | theorem append_left_nil : ∀ xs : List a, [] ++ xs = xs := by 285 | intro h1 286 | induction h1 with 287 | | nil => rfl 288 | | cons ha hs => simp [List.append]; done 289 | 290 | example (xs ys : List a) (f : a → b → b) (e : b) 291 | : foldr f e (xs ++ ys) = foldr f (foldr f e ys) xs := by 292 | have h1 := append_left_nil ys 293 | induction xs with 294 | | nil => rw [foldr.eq_1]; rewrite [h1]; rfl 295 | | cons x xs ih => simp [List.append, foldr]; rw [ih] 296 | 297 | theorem foldr_append {α β : Type} (f : α → β → β) (e : β) (xs ys : List α) : 298 | foldr f e (xs ++ ys) = foldr f (foldr f e ys) xs := by 299 | induction xs with 300 | |nil => rfl 301 | |cons x xs ih => 302 | simp [foldr, ih] 303 | 304 | example (f : a → a → a) 305 | : foldr f e ∘ concat1 = foldr (flip (foldr f)) e := by 306 | funext xs 307 | induction xs with 308 | | nil => 309 | rw [foldr.eq_1, Function.comp] 310 | simp [concat1, foldr.eq_1] 311 | | cons y ys ih => 312 | rw [Function.comp] 313 | simp [concat1] 314 | rw [←concat1] 315 | rw [foldr_append] 316 | rw [foldr] 317 | rw [flip] 318 | exact congrArg (λ x => foldr f x y) ih 319 | 320 | theorem fusion_th (g : a → b → b) (h : a → b) (h₁ : ∀ x y, h (f x y) = g x (h y)) 321 | : h (foldr f e xs) = foldr g (h e) xs := by 322 | induction xs with 323 | | nil => 324 | rewrite [foldr.eq_1] 325 | rewrite [foldr.eq_1] 326 | rfl 327 | | cons x xs ih => 328 | rewrite [foldr] 329 | rewrite [h₁ x (foldr f e xs)] 330 | rewrite [ih] 331 | rewrite [foldr] 332 | rfl 333 | 334 | 335 | -- 1.5 Accumulating and tupling 336 | 337 | def sum (xs : List Int) := xs.foldl Int.add 0 338 | 339 | partial def collapse₀ (xss : List (List Int)) : List Int := 340 | help [] xss 341 | where 342 | help : List Int → List (List Int) → List Int 343 | | xs, xss => 344 | if (sum xs) > 0 ∨ xss.isEmpty then xs 345 | else help (xs.append xss.head!) xss.tail 346 | 347 | def collapse₁ (xss : List (List Int)) : List Int := 348 | help [] xss 349 | where 350 | help : List Int → List (List Int) → List Int 351 | | xs, [] => xs 352 | | xs, (as :: bss) => 353 | if (sum xs) > 0 then xs 354 | else help (xs.append as) bss 355 | 356 | partial def collapse₂ (xss : List (List Int)) : List Int := 357 | help (0, []) (labelsum xss) 358 | where 359 | labelsum (xss : List (List Int)) : List (Int × List Int) := 360 | List.zip (map sum xss) xss 361 | help : (Int × List Int) → List (Int × List Int) → List Int 362 | | (_, xs), [] => xs 363 | | (s, xs), xss => if s > 0 then xs else help (cat (s, xs) xss.head!) xss.tail 364 | cat : (Int × List Int) → (Int × List Int) → (Int × List Int) 365 | | (s, xs), (t, ys) => (s + t, xs ++ ys) 366 | 367 | def collapse₃ (xss : List (List Int)) : List Int := 368 | help (0, id) (labelsum xss) [] 369 | where 370 | labelsum (xss : List (List Int)) : List (Int × List Int) := 371 | List.zip (map sum xss) xss 372 | help : 373 | let tf := List Int → List Int 374 | (Int × tf) → List (Int × List Int) → tf 375 | | (_, f), [] => f 376 | | (s, f), (as :: bs) => 377 | if s > 0 then f 378 | else help (s + as.1, f ∘ (as.2 ++ ·)) bs 379 | 380 | /- 381 | #eval collapse₃ [[1],[-3],[2,4]] 382 | #eval collapse₃ [[-2,1],[-3],[2,4]] 383 | #eval collapse₃ [[-2,1],[3],[2,4]] 384 | -/ 385 | 386 | def fib : Nat → Nat 387 | | 0 => 1 388 | | 1 => 1 389 | | n + 2 => fib (n + 1) + fib n 390 | 391 | example : fib 0 = 1 := rfl 392 | example : fib 1 = 1 := rfl 393 | example : fib (n + 2) = fib (n + 1) + fib n := rfl 394 | example : fib 7 = 21 := rfl 395 | 396 | def fibFast (n : Nat) : Nat := 397 | (loop n).2 398 | where 399 | loop : Nat → Nat × Nat 400 | | 0 => (0, 1) 401 | | n+1 => let p := loop n; (p.2, p.1 + p.2) 402 | 403 | /- 404 | #eval fibFast 100 405 | #reduce fib 100 -- try eval 406 | #print fib 407 | -/ 408 | 409 | example : fibFast 4 = 5 := by 410 | unfold fibFast 411 | unfold fibFast.loop 412 | unfold fibFast.loop 413 | unfold fibFast.loop 414 | unfold fibFast.loop 415 | unfold fibFast.loop 416 | rfl 417 | 418 | 419 | end Chapter1 420 | -------------------------------------------------------------------------------- /data/AoC2021_day2.txt: -------------------------------------------------------------------------------- 1 | forward 5 2 | forward 2 3 | forward 9 4 | down 2 5 | forward 9 6 | forward 3 7 | forward 2 8 | down 6 9 | forward 3 10 | forward 3 11 | down 3 12 | down 3 13 | forward 8 14 | down 5 15 | forward 7 16 | forward 9 17 | forward 9 18 | forward 6 19 | forward 9 20 | forward 3 21 | forward 3 22 | forward 1 23 | forward 7 24 | down 6 25 | forward 7 26 | forward 4 27 | down 3 28 | down 1 29 | forward 4 30 | down 7 31 | down 2 32 | down 8 33 | forward 9 34 | down 5 35 | down 2 36 | forward 6 37 | up 4 38 | down 3 39 | down 1 40 | down 6 41 | down 7 42 | forward 6 43 | up 2 44 | forward 2 45 | down 9 46 | down 3 47 | forward 7 48 | up 3 49 | up 7 50 | forward 8 51 | forward 7 52 | down 4 53 | up 8 54 | up 1 55 | forward 4 56 | down 6 57 | forward 9 58 | forward 3 59 | down 1 60 | down 1 61 | forward 2 62 | forward 4 63 | forward 3 64 | up 2 65 | forward 1 66 | down 8 67 | forward 4 68 | down 5 69 | forward 9 70 | up 1 71 | forward 3 72 | forward 6 73 | up 8 74 | forward 1 75 | forward 7 76 | up 9 77 | down 3 78 | up 7 79 | down 1 80 | forward 5 81 | forward 3 82 | forward 7 83 | down 5 84 | down 1 85 | down 2 86 | down 4 87 | down 5 88 | down 5 89 | down 8 90 | up 9 91 | down 7 92 | down 7 93 | down 6 94 | forward 3 95 | forward 5 96 | forward 1 97 | forward 8 98 | up 4 99 | down 8 100 | down 3 101 | down 1 102 | down 9 103 | down 3 104 | down 9 105 | down 8 106 | down 2 107 | forward 1 108 | forward 7 109 | forward 1 110 | down 3 111 | down 1 112 | up 3 113 | down 6 114 | forward 6 115 | forward 6 116 | down 8 117 | forward 3 118 | down 1 119 | forward 2 120 | down 4 121 | down 7 122 | up 8 123 | forward 4 124 | down 4 125 | up 1 126 | forward 6 127 | down 6 128 | forward 5 129 | forward 9 130 | up 5 131 | down 3 132 | up 9 133 | down 6 134 | up 3 135 | down 9 136 | down 4 137 | down 2 138 | forward 3 139 | down 6 140 | down 7 141 | down 9 142 | forward 7 143 | forward 2 144 | forward 5 145 | up 6 146 | down 8 147 | forward 1 148 | down 2 149 | forward 8 150 | down 5 151 | down 2 152 | up 4 153 | forward 9 154 | up 4 155 | down 4 156 | down 2 157 | forward 3 158 | up 2 159 | down 5 160 | down 9 161 | up 4 162 | forward 9 163 | down 5 164 | down 2 165 | down 2 166 | forward 9 167 | up 9 168 | forward 6 169 | up 7 170 | down 2 171 | forward 8 172 | down 1 173 | forward 2 174 | down 3 175 | up 6 176 | down 4 177 | forward 5 178 | up 4 179 | forward 5 180 | forward 9 181 | forward 9 182 | forward 5 183 | down 1 184 | down 7 185 | forward 6 186 | forward 9 187 | forward 3 188 | forward 4 189 | up 8 190 | down 1 191 | up 1 192 | down 3 193 | up 1 194 | down 5 195 | forward 6 196 | up 5 197 | up 6 198 | down 9 199 | forward 7 200 | down 7 201 | forward 1 202 | forward 2 203 | up 4 204 | forward 4 205 | down 9 206 | up 4 207 | down 6 208 | forward 1 209 | up 9 210 | forward 2 211 | down 8 212 | forward 6 213 | forward 6 214 | down 6 215 | forward 5 216 | forward 9 217 | up 8 218 | down 4 219 | forward 8 220 | up 6 221 | down 4 222 | up 9 223 | forward 2 224 | down 8 225 | down 6 226 | forward 2 227 | down 3 228 | forward 1 229 | forward 3 230 | forward 2 231 | up 4 232 | down 6 233 | up 6 234 | down 4 235 | down 2 236 | forward 1 237 | up 7 238 | forward 9 239 | forward 9 240 | forward 7 241 | down 1 242 | down 9 243 | forward 5 244 | forward 4 245 | forward 1 246 | forward 4 247 | down 5 248 | forward 6 249 | forward 8 250 | down 5 251 | down 7 252 | up 2 253 | up 1 254 | forward 5 255 | down 5 256 | down 1 257 | down 5 258 | down 8 259 | down 8 260 | down 5 261 | forward 7 262 | down 6 263 | down 2 264 | forward 9 265 | forward 1 266 | forward 3 267 | forward 4 268 | up 3 269 | down 4 270 | up 7 271 | forward 8 272 | forward 7 273 | up 4 274 | down 9 275 | forward 7 276 | forward 6 277 | up 1 278 | down 1 279 | up 6 280 | down 5 281 | up 1 282 | forward 2 283 | down 2 284 | forward 3 285 | down 6 286 | up 5 287 | up 4 288 | down 8 289 | down 5 290 | down 3 291 | down 4 292 | up 3 293 | down 3 294 | down 2 295 | down 7 296 | up 2 297 | down 8 298 | forward 5 299 | up 1 300 | forward 9 301 | down 6 302 | down 6 303 | down 8 304 | up 4 305 | forward 9 306 | forward 8 307 | up 7 308 | down 9 309 | down 4 310 | forward 9 311 | forward 9 312 | up 2 313 | down 1 314 | forward 1 315 | forward 4 316 | forward 2 317 | forward 9 318 | down 1 319 | down 3 320 | down 1 321 | down 3 322 | up 5 323 | down 2 324 | forward 4 325 | down 2 326 | forward 1 327 | down 6 328 | up 9 329 | down 3 330 | forward 1 331 | forward 5 332 | forward 8 333 | down 5 334 | down 6 335 | down 9 336 | forward 4 337 | down 7 338 | up 8 339 | forward 8 340 | down 9 341 | forward 6 342 | down 8 343 | up 3 344 | forward 4 345 | up 9 346 | down 7 347 | up 7 348 | forward 6 349 | forward 1 350 | up 9 351 | down 7 352 | up 7 353 | down 5 354 | forward 6 355 | up 7 356 | down 8 357 | down 8 358 | forward 4 359 | up 4 360 | forward 1 361 | forward 6 362 | down 4 363 | up 9 364 | forward 4 365 | up 1 366 | up 8 367 | up 6 368 | forward 9 369 | forward 4 370 | forward 7 371 | up 1 372 | down 5 373 | up 5 374 | up 5 375 | forward 4 376 | down 9 377 | up 8 378 | down 6 379 | down 3 380 | down 6 381 | forward 2 382 | up 1 383 | forward 3 384 | up 8 385 | down 1 386 | forward 5 387 | down 9 388 | forward 4 389 | up 5 390 | forward 3 391 | forward 2 392 | down 8 393 | down 9 394 | up 6 395 | down 9 396 | down 7 397 | forward 5 398 | forward 4 399 | forward 9 400 | up 8 401 | forward 3 402 | down 7 403 | forward 9 404 | down 8 405 | forward 4 406 | forward 8 407 | up 9 408 | up 9 409 | down 6 410 | forward 5 411 | forward 5 412 | forward 5 413 | up 2 414 | up 2 415 | up 1 416 | down 6 417 | forward 2 418 | forward 2 419 | down 8 420 | down 6 421 | up 2 422 | forward 1 423 | down 1 424 | up 5 425 | forward 7 426 | down 2 427 | forward 1 428 | forward 3 429 | down 5 430 | down 7 431 | forward 8 432 | forward 4 433 | forward 9 434 | up 1 435 | up 7 436 | up 9 437 | forward 3 438 | up 1 439 | forward 6 440 | forward 3 441 | forward 9 442 | up 9 443 | down 6 444 | forward 8 445 | up 6 446 | down 9 447 | forward 3 448 | forward 7 449 | down 9 450 | forward 4 451 | forward 5 452 | forward 7 453 | down 1 454 | down 4 455 | down 3 456 | forward 6 457 | down 3 458 | forward 7 459 | forward 8 460 | down 1 461 | forward 3 462 | down 4 463 | up 7 464 | forward 2 465 | forward 8 466 | down 6 467 | up 3 468 | down 2 469 | forward 9 470 | forward 5 471 | forward 7 472 | up 2 473 | up 6 474 | down 9 475 | forward 1 476 | up 5 477 | forward 1 478 | up 6 479 | up 2 480 | up 1 481 | forward 6 482 | down 8 483 | forward 7 484 | down 5 485 | forward 3 486 | down 9 487 | down 4 488 | forward 3 489 | down 1 490 | up 1 491 | up 7 492 | forward 4 493 | down 6 494 | forward 3 495 | forward 2 496 | down 8 497 | forward 9 498 | forward 6 499 | up 3 500 | down 3 501 | down 1 502 | down 7 503 | up 8 504 | up 2 505 | up 8 506 | forward 6 507 | forward 8 508 | forward 6 509 | forward 4 510 | down 6 511 | forward 6 512 | forward 6 513 | forward 1 514 | down 2 515 | forward 2 516 | forward 6 517 | down 1 518 | up 6 519 | forward 3 520 | forward 9 521 | forward 6 522 | down 2 523 | forward 2 524 | up 4 525 | down 2 526 | up 4 527 | forward 2 528 | forward 2 529 | forward 3 530 | up 1 531 | forward 8 532 | forward 3 533 | forward 3 534 | forward 1 535 | down 5 536 | down 9 537 | forward 4 538 | down 1 539 | forward 5 540 | forward 2 541 | down 5 542 | forward 6 543 | forward 3 544 | up 3 545 | forward 6 546 | forward 9 547 | forward 5 548 | down 2 549 | down 2 550 | down 7 551 | forward 8 552 | down 1 553 | down 5 554 | down 9 555 | up 3 556 | up 5 557 | up 4 558 | forward 3 559 | down 9 560 | down 2 561 | down 8 562 | down 5 563 | down 2 564 | forward 4 565 | up 3 566 | down 5 567 | up 3 568 | down 8 569 | down 7 570 | up 1 571 | forward 2 572 | forward 1 573 | down 2 574 | up 1 575 | up 5 576 | down 8 577 | down 3 578 | up 9 579 | forward 2 580 | down 8 581 | down 4 582 | down 3 583 | forward 3 584 | forward 7 585 | up 1 586 | down 9 587 | forward 1 588 | down 6 589 | up 3 590 | up 5 591 | down 6 592 | up 4 593 | forward 7 594 | up 4 595 | forward 9 596 | up 4 597 | forward 4 598 | down 7 599 | down 2 600 | down 8 601 | up 3 602 | down 7 603 | down 4 604 | up 5 605 | forward 8 606 | down 8 607 | down 1 608 | forward 7 609 | up 9 610 | down 5 611 | up 8 612 | down 1 613 | up 3 614 | forward 8 615 | up 4 616 | down 1 617 | up 1 618 | up 7 619 | forward 3 620 | forward 6 621 | forward 5 622 | forward 2 623 | down 4 624 | forward 2 625 | down 7 626 | up 7 627 | up 6 628 | down 4 629 | forward 4 630 | forward 9 631 | forward 7 632 | down 4 633 | down 6 634 | forward 5 635 | down 2 636 | down 6 637 | down 2 638 | down 2 639 | forward 9 640 | up 5 641 | forward 9 642 | down 3 643 | down 6 644 | down 1 645 | forward 5 646 | down 5 647 | forward 7 648 | forward 9 649 | up 8 650 | forward 6 651 | down 6 652 | down 2 653 | forward 8 654 | forward 5 655 | up 9 656 | up 4 657 | forward 1 658 | forward 2 659 | forward 2 660 | up 9 661 | down 6 662 | forward 6 663 | forward 8 664 | up 5 665 | up 7 666 | forward 4 667 | down 3 668 | forward 1 669 | up 6 670 | up 9 671 | forward 6 672 | up 8 673 | forward 5 674 | down 4 675 | forward 1 676 | down 3 677 | forward 1 678 | forward 6 679 | forward 2 680 | up 4 681 | down 6 682 | forward 9 683 | down 2 684 | forward 4 685 | down 4 686 | forward 3 687 | down 5 688 | down 2 689 | forward 2 690 | forward 2 691 | up 1 692 | forward 1 693 | down 7 694 | down 8 695 | up 6 696 | forward 8 697 | forward 5 698 | forward 8 699 | down 6 700 | forward 5 701 | down 3 702 | up 3 703 | forward 4 704 | up 9 705 | forward 8 706 | forward 4 707 | down 3 708 | forward 6 709 | up 6 710 | down 2 711 | down 7 712 | down 3 713 | down 2 714 | down 8 715 | forward 5 716 | down 9 717 | up 6 718 | down 6 719 | forward 8 720 | down 1 721 | forward 8 722 | down 1 723 | down 1 724 | forward 3 725 | forward 9 726 | down 6 727 | forward 7 728 | down 4 729 | forward 1 730 | forward 4 731 | forward 3 732 | down 6 733 | forward 5 734 | down 5 735 | forward 1 736 | forward 3 737 | forward 8 738 | down 9 739 | up 8 740 | up 6 741 | up 6 742 | forward 2 743 | forward 9 744 | down 4 745 | down 8 746 | forward 6 747 | up 1 748 | down 2 749 | down 6 750 | forward 6 751 | forward 2 752 | up 8 753 | forward 6 754 | down 9 755 | down 1 756 | forward 7 757 | forward 6 758 | forward 5 759 | forward 6 760 | down 6 761 | up 7 762 | down 9 763 | forward 9 764 | forward 3 765 | forward 5 766 | down 4 767 | down 1 768 | down 7 769 | up 3 770 | up 7 771 | forward 6 772 | forward 8 773 | down 7 774 | down 4 775 | forward 7 776 | down 6 777 | up 1 778 | forward 4 779 | down 2 780 | forward 4 781 | forward 3 782 | forward 4 783 | forward 4 784 | up 3 785 | down 8 786 | down 4 787 | down 1 788 | down 8 789 | down 3 790 | up 9 791 | down 4 792 | forward 7 793 | down 6 794 | up 2 795 | down 8 796 | up 9 797 | down 6 798 | forward 1 799 | down 3 800 | forward 9 801 | down 9 802 | forward 1 803 | down 5 804 | up 5 805 | up 1 806 | forward 8 807 | down 8 808 | down 9 809 | down 5 810 | down 2 811 | down 5 812 | forward 3 813 | down 9 814 | forward 4 815 | forward 4 816 | up 2 817 | forward 8 818 | forward 4 819 | forward 1 820 | down 5 821 | forward 5 822 | down 5 823 | forward 6 824 | forward 4 825 | up 5 826 | down 9 827 | up 3 828 | up 8 829 | forward 5 830 | forward 9 831 | up 6 832 | forward 6 833 | down 5 834 | forward 7 835 | down 8 836 | down 7 837 | down 9 838 | forward 4 839 | down 8 840 | forward 4 841 | down 5 842 | forward 6 843 | forward 4 844 | down 7 845 | down 5 846 | forward 4 847 | down 3 848 | up 4 849 | forward 3 850 | up 9 851 | down 8 852 | forward 9 853 | forward 6 854 | forward 9 855 | down 1 856 | forward 2 857 | up 5 858 | down 9 859 | down 2 860 | down 9 861 | up 8 862 | forward 7 863 | forward 8 864 | forward 4 865 | down 1 866 | up 8 867 | forward 8 868 | down 8 869 | down 4 870 | forward 9 871 | down 3 872 | forward 7 873 | forward 9 874 | down 5 875 | forward 7 876 | forward 1 877 | forward 5 878 | forward 2 879 | down 4 880 | forward 7 881 | down 6 882 | forward 3 883 | down 9 884 | forward 3 885 | down 5 886 | up 6 887 | up 3 888 | forward 1 889 | up 9 890 | down 1 891 | forward 2 892 | down 8 893 | down 7 894 | up 9 895 | up 2 896 | down 5 897 | up 9 898 | forward 9 899 | forward 9 900 | down 1 901 | forward 5 902 | up 5 903 | forward 1 904 | up 1 905 | down 3 906 | forward 3 907 | down 3 908 | forward 3 909 | up 5 910 | up 4 911 | down 7 912 | down 7 913 | down 4 914 | forward 7 915 | down 6 916 | forward 1 917 | up 1 918 | down 8 919 | forward 4 920 | down 3 921 | forward 9 922 | up 6 923 | forward 6 924 | forward 3 925 | up 9 926 | down 9 927 | forward 4 928 | up 5 929 | down 3 930 | down 8 931 | down 3 932 | down 3 933 | forward 4 934 | forward 6 935 | forward 4 936 | up 2 937 | up 3 938 | up 5 939 | down 5 940 | down 6 941 | forward 5 942 | forward 4 943 | down 1 944 | down 2 945 | up 8 946 | down 2 947 | down 1 948 | up 4 949 | forward 5 950 | forward 8 951 | forward 8 952 | forward 5 953 | down 3 954 | forward 4 955 | up 8 956 | forward 7 957 | forward 4 958 | down 9 959 | down 6 960 | forward 2 961 | down 7 962 | forward 6 963 | up 7 964 | up 1 965 | up 4 966 | forward 2 967 | forward 9 968 | forward 7 969 | up 5 970 | forward 2 971 | up 5 972 | forward 1 973 | forward 2 974 | forward 4 975 | down 6 976 | forward 2 977 | up 6 978 | up 7 979 | forward 3 980 | forward 2 981 | forward 6 982 | forward 4 983 | forward 9 984 | forward 6 985 | up 6 986 | forward 5 987 | up 7 988 | up 5 989 | down 6 990 | down 2 991 | down 1 992 | forward 7 993 | down 5 994 | down 1 995 | down 7 996 | forward 8 997 | forward 8 998 | forward 5 999 | down 9 1000 | forward 6 1001 | --------------------------------------------------------------------------------