├── .gitignore ├── .scripts ├── Main.hs └── screencasts.txt ├── 01-rust ├── .gitignore ├── Makefile ├── README.md ├── input.txt ├── main.rs └── sample.txt ├── 02-haskell ├── .gitignore ├── Main.hs ├── Makefile ├── README.md ├── input.txt └── sample.txt ├── 03-java ├── .gitignore ├── Main.java ├── Makefile ├── README.md ├── input.txt └── sample.txt ├── 04-d ├── .gitignore ├── Makefile ├── README.md ├── input.txt ├── main.d ├── sample-01.txt ├── sample-02.txt └── sample-03.txt ├── 05-c++ ├── .gitignore ├── Makefile ├── README.md ├── input.txt ├── main.cpp └── sample.txt ├── 06-python ├── .gitignore ├── Makefile ├── README.md └── main.py ├── 07-go ├── .gitignore ├── Makefile ├── README.md ├── input.txt ├── main.go ├── sample-01.txt └── sample-02.txt ├── 08-js ├── Makefile ├── README.md ├── input.txt ├── main.js └── sample.txt ├── 09-ocaml ├── .gitignore ├── Makefile ├── README.md ├── input.txt ├── main.ml └── sample.txt ├── 10-c ├── .gitignore ├── Makefile ├── README.md ├── input.txt ├── main.c ├── sample-01.txt └── sample-02.txt ├── 11-nim ├── .gitignore ├── Makefile ├── README.md ├── input.txt ├── main.nim └── sample.txt ├── 12-perl ├── Makefile ├── README.md ├── input.txt ├── main.pl └── sample.txt ├── 13-csharp ├── .gitignore ├── Main.cs ├── Makefile ├── README.md ├── input.txt └── sample.txt ├── 14-php ├── Makefile ├── README.md ├── index.php ├── input.txt ├── sample-01.txt └── sample-02.txt ├── 15-pascal ├── .gitignore ├── Makefile ├── README.md └── main.pas ├── 16-ruby ├── Makefile ├── README.md ├── input.txt ├── main.rb ├── sample-01.txt └── sample-02.txt ├── 17-scala ├── .gitignore ├── Main.scala ├── Makefile ├── README.md ├── input.txt └── sample.txt ├── 18-kotlin ├── .gitignore ├── Makefile ├── README.md ├── input.txt ├── main.kt └── sample.txt ├── 19-lua ├── Makefile ├── README.md ├── input-01.txt ├── input-02.txt ├── main.lua └── sample.txt ├── 20-julia ├── Makefile ├── README.md ├── input.txt ├── main.jl ├── output.txt └── sample.txt ├── 21-racket ├── Makefile ├── README.md ├── input.txt ├── main.rkt └── sample.txt ├── 22-groovy ├── Makefile ├── README.md ├── input.txt ├── main.groovy └── sample.txt ├── 23-dart ├── .gitignore ├── Makefile ├── README.md ├── input.txt ├── main.dart └── sample.txt ├── 24-ada ├── .gitignore ├── Makefile ├── README.md ├── day24.adb ├── input.txt └── sample.txt ├── 25-asm ├── .gitignore ├── Makefile ├── README.md ├── algo.txt └── main.asm ├── CONTRIBUTING.md ├── LICENSE ├── README.md └── thumbnail.png /.gitignore: -------------------------------------------------------------------------------- 1 | *~ -------------------------------------------------------------------------------- /.scripts/Main.hs: -------------------------------------------------------------------------------- 1 | module Main where 2 | 3 | import qualified Data.Text as T 4 | import qualified Data.Text.IO as T 5 | import System.Directory 6 | import Data.Functor 7 | import Control.Monad 8 | 9 | -- NOTE: Yt stands for YouTube. It's a YouTube link to the video 10 | type Yt = T.Text 11 | 12 | ytId :: Yt -> T.Text 13 | ytId = head . T.splitOn "&" . (!! 1) . T.splitOn "?v=" 14 | 15 | ytThumbnail :: Yt -> T.Text 16 | ytThumbnail yt = "http://i3.ytimg.com/vi/" <> ytId yt <> "/hqdefault.jpg" 17 | 18 | ytMarkdownLink :: Yt -> T.Text 19 | ytMarkdownLink yt = "[![screencast](" <> ytThumbnail yt <> ")](" <> yt <> ")" 20 | 21 | ytMarkdownSection :: Yt -> [T.Text] 22 | ytMarkdownSection yt = ["## Screencast", "", ytMarkdownLink yt] 23 | 24 | -- NOTE: Ep stands for Episode 25 | type Ep = T.Text 26 | 27 | epFolder :: Ep -> IO FilePath 28 | epFolder ep = head . filter (\dirPath -> ep `T.isPrefixOf` T.pack dirPath) <$> listDirectory "." 29 | 30 | epREADME :: Ep -> IO FilePath 31 | epREADME ep = (<> "/README.md") <$> epFolder ep 32 | 33 | -- NOTE: Sc stands for Screencast 34 | data Sc = Sc { scEp :: Ep, scYt :: Yt } deriving Show 35 | 36 | parseSc :: T.Text -> Sc 37 | parseSc s = Sc a b 38 | where [a, b] = T.splitOn ": " s 39 | 40 | addSc :: Sc -> IO () 41 | addSc (Sc ep yt) = do 42 | readmePath <- epREADME ep 43 | readme <- T.lines <$> T.readFile readmePath 44 | let section = ytMarkdownSection yt 45 | T.writeFile readmePath $ T.unlines $ [head readme, ""] <> section <> tail readme 46 | 47 | noSc :: Sc -> IO Bool 48 | noSc (Sc ep _) = 49 | epREADME ep >>= 50 | T.readFile <&> 51 | T.lines <&> 52 | filter (T.isPrefixOf "## Screencast") <&> 53 | null 54 | 55 | scs :: IO [Sc] 56 | scs = map parseSc . T.lines <$> T.readFile "./.scripts/screencasts.txt" 57 | 58 | main :: IO () 59 | main = scs >>= filterM noSc >>= mapM_ addSc 60 | -------------------------------------------------------------------------------- /.scripts/screencasts.txt: -------------------------------------------------------------------------------- 1 | 03: https://www.youtube.com/watch?v=mlth0Bn18Vs&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=700s 2 | 04: https://www.youtube.com/watch?v=cvvNfuS4BwI&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=755s 3 | 05: https://www.youtube.com/watch?v=vVjNyY2jkqg&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=829s 4 | 06: https://www.youtube.com/watch?v=vVjNyY2jkqg&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=3970s 5 | 07: https://www.youtube.com/watch?v=FXPmwFubyDE&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=713s 6 | 08: https://www.youtube.com/watch?v=8aYjw62hxTY&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=752s 7 | 09: https://www.youtube.com/watch?v=a8vogmPtlXE&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=778s 8 | 10: https://www.youtube.com/watch?v=lHzbWWjdloM&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=709s 9 | 11: https://www.youtube.com/watch?v=lHzbWWjdloM&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=4680s 10 | 12: https://www.youtube.com/watch?v=R00JE6QRbno&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=742s 11 | 13: https://www.youtube.com/watch?v=i2bya8kNXjw&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=708s 12 | 14: https://www.youtube.com/watch?v=95ooXiwVeMM&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P 13 | 15: https://www.youtube.com/watch?v=ech9Re2bi_E&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=791s 14 | 16: https://www.youtube.com/watch?v=xb-yZhokKSw&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=807s 15 | 17: https://www.youtube.com/watch?v=rKPG4bGQLGk&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=734s 16 | 18: https://www.youtube.com/watch?v=gGDuoey_TnE&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=777s 17 | 19: https://www.youtube.com/watch?v=6lFNgR5DkFg&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=740s 18 | 20: https://www.youtube.com/watch?v=vvvO8QQtW7Q&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=688s 19 | 21: https://www.youtube.com/watch?v=tP98VjF9Ljo&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=730s 20 | 22: https://www.youtube.com/watch?v=mq3ZIGRgZOA&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=709s 21 | 23: https://www.youtube.com/watch?v=p_wl80ugHSc&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=743s 22 | 24: https://www.youtube.com/watch?v=5ChNYG81tS0&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=671s 23 | 25: https://www.youtube.com/watch?v=6m7PSAkbelE&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=657s 24 | -------------------------------------------------------------------------------- /01-rust/.gitignore: -------------------------------------------------------------------------------- 1 | main -------------------------------------------------------------------------------- /01-rust/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: test 2 | test: main sample.txt input.txt 3 | ./main sample.txt 4 | ./main input.txt 5 | 6 | main: main.rs 7 | rustc main.rs 8 | -------------------------------------------------------------------------------- /01-rust/README.md: -------------------------------------------------------------------------------- 1 | # [Day 1](https://adventofcode.com/2020/day/1) solution in [Rust](https://www.rust-lang.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/uodcgWA8trs/hqdefault.jpg)](https://www.youtube.com/watch?v=uodcgWA8trs&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&index=1&t=615s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ rustc --version 11 | rustc 1.46.0 (04488afe3 2020-08-24) 12 | ``` 13 | 14 | ## Expected Result 15 | 16 | ```console 17 | Input file: input.txt 18 | Part 1: O(N²): 970816 19 | Part 2: O(N³): 96047280 20 | Part 1: O(NlogN): 970816 21 | Part 2: O(N²logN): 96047280 22 | ``` 23 | 24 | ## Quick Start 25 | 26 | - install [Rust](https://www.rust-lang.org/) and make sure `rustc` is available in `$PATH` 27 | - `$ make` 28 | -------------------------------------------------------------------------------- /01-rust/input.txt: -------------------------------------------------------------------------------- 1 | 1688 2 | 1463 3 | 1461 4 | 1842 5 | 1441 6 | 1838 7 | 1583 8 | 1891 9 | 1876 10 | 1551 11 | 1506 12 | 2005 13 | 1989 14 | 1417 15 | 1784 16 | 1975 17 | 1428 18 | 1485 19 | 1597 20 | 1871 21 | 105 22 | 788 23 | 1971 24 | 1892 25 | 1854 26 | 1466 27 | 1584 28 | 1565 29 | 1400 30 | 1640 31 | 1780 32 | 1774 33 | 360 34 | 1421 35 | 1368 36 | 1771 37 | 1666 38 | 1707 39 | 1627 40 | 1449 41 | 1677 42 | 1504 43 | 1721 44 | 1994 45 | 1959 46 | 1862 47 | 1768 48 | 1986 49 | 1904 50 | 1382 51 | 1969 52 | 1852 53 | 1917 54 | 1966 55 | 1742 56 | 1371 57 | 1405 58 | 1995 59 | 1906 60 | 1694 61 | 1735 62 | 1422 63 | 1719 64 | 1978 65 | 1641 66 | 1761 67 | 1567 68 | 1974 69 | 1495 70 | 1973 71 | 1958 72 | 1599 73 | 1770 74 | 1600 75 | 1465 76 | 1865 77 | 1479 78 | 1687 79 | 1390 80 | 1802 81 | 2008 82 | 645 83 | 1435 84 | 1589 85 | 1949 86 | 1909 87 | 1526 88 | 1667 89 | 1831 90 | 1864 91 | 1713 92 | 1718 93 | 1232 94 | 1868 95 | 1884 96 | 1825 97 | 1999 98 | 1590 99 | 1759 100 | 1391 101 | 1757 102 | 323 103 | 1612 104 | 1637 105 | 1727 106 | 1783 107 | 1643 108 | 1442 109 | 1452 110 | 675 111 | 1812 112 | 1604 113 | 1518 114 | 1894 115 | 1933 116 | 1801 117 | 1914 118 | 912 119 | 1576 120 | 1961 121 | 1970 122 | 1446 123 | 1985 124 | 1988 125 | 1563 126 | 1826 127 | 1409 128 | 1503 129 | 1539 130 | 1832 131 | 1698 132 | 1990 133 | 1689 134 | 1532 135 | 765 136 | 1546 137 | 1384 138 | 1519 139 | 1615 140 | 1556 141 | 1754 142 | 1983 143 | 1394 144 | 1763 145 | 1823 146 | 1788 147 | 1407 148 | 1946 149 | 1751 150 | 1837 151 | 1680 152 | 1929 153 | 1814 154 | 1948 155 | 1919 156 | 1953 157 | 55 158 | 1731 159 | 1516 160 | 1895 161 | 1795 162 | 1890 163 | 1881 164 | 1799 165 | 1536 166 | 1396 167 | 1942 168 | 1798 169 | 1767 170 | 1745 171 | 1883 172 | 2004 173 | 1550 174 | 1916 175 | 1650 176 | 1749 177 | 1991 178 | 1789 179 | 1740 180 | 1490 181 | 1873 182 | 1003 183 | 1699 184 | 1669 185 | 1781 186 | 2000 187 | 1728 188 | 1877 189 | 1733 190 | 1588 191 | 1168 192 | 1828 193 | 1848 194 | 1963 195 | 1928 196 | 1920 197 | 1493 198 | 1968 199 | 1564 200 | 1572 201 | -------------------------------------------------------------------------------- /01-rust/main.rs: -------------------------------------------------------------------------------- 1 | // Part 1 2 | // O(N²) 3 | fn part_1_n_squared(xs: &Vec) -> i32 { 4 | let n = xs.len(); 5 | for i in 0..n - 1 { 6 | for j in i + 1..n { 7 | if xs[i] + xs[j] == 2020 { 8 | return xs[i] * xs[j]; 9 | } 10 | } 11 | } 12 | unreachable!(); 13 | } 14 | 15 | // Part 2 16 | // O(N³) 17 | fn part_2_n_cubed(xs: &Vec) -> i32 { 18 | let n = xs.len(); 19 | for i in 0..n - 2 { 20 | for j in i + 1..n - 1 { 21 | for k in j + 1..n { 22 | if xs[i] + xs[j] + xs[k] == 2020 { 23 | return xs[i] * xs[j] * xs[k]; 24 | } 25 | } 26 | } 27 | } 28 | unreachable!(); 29 | } 30 | 31 | // Part 1 32 | // O(NlogN) 33 | fn part_1_n_logn(xs0: &Vec) -> i32 { 34 | let mut xs = xs0.clone(); 35 | let n = xs.len(); 36 | xs.sort(); 37 | for i in 0..n { 38 | if let Ok(j) = xs.binary_search(&(2020 - xs[i])) { 39 | if i != j { 40 | return xs[i] * xs[j]; 41 | } 42 | } 43 | } 44 | unreachable!(); 45 | } 46 | 47 | // Part 2 48 | // O(N²logN) 49 | fn part_2_n_squared_logn(xs0: &Vec) -> i32 { 50 | let mut xs = xs0.clone(); 51 | let n = xs.len(); 52 | xs.sort(); 53 | for i in 0..n - 1 { 54 | for j in i + 1..n { 55 | if let Ok(k) = xs.binary_search(&(2020 - xs[i] - xs[j])) { 56 | if j != k { 57 | return xs[i] * xs[j] * xs[k]; 58 | } 59 | } 60 | } 61 | } 62 | unreachable!(); 63 | } 64 | 65 | fn main() { 66 | use std::env::args; 67 | use std::fs::read_to_string; 68 | 69 | let input_file = 70 | args() 71 | .skip(1) 72 | .next() 73 | .expect("Input file is not provided"); 74 | let input = read_to_string(&input_file).unwrap(); 75 | let xs = 76 | input 77 | .split('\n') 78 | .filter(|s| !s.is_empty()) 79 | .map(|x| x.parse::().unwrap()) 80 | .collect::>(); 81 | 82 | println!("Input file: {}", input_file); 83 | println!("Part 1: O(N²): {}", part_1_n_squared(&xs)); 84 | println!("Part 2: O(N³): {}", part_2_n_cubed(&xs)); 85 | println!("Part 1: O(NlogN): {}", part_1_n_logn(&xs)); 86 | println!("Part 2: O(N²logN): {}", part_2_n_squared_logn(&xs)); 87 | } 88 | -------------------------------------------------------------------------------- /01-rust/sample.txt: -------------------------------------------------------------------------------- 1 | 1721 2 | 979 3 | 366 4 | 299 5 | 675 6 | 1456 7 | -------------------------------------------------------------------------------- /02-haskell/.gitignore: -------------------------------------------------------------------------------- 1 | Main 2 | *.hi 3 | *.o 4 | -------------------------------------------------------------------------------- /02-haskell/Main.hs: -------------------------------------------------------------------------------- 1 | module Main where 2 | 3 | import System.Environment 4 | import Text.Printf 5 | 6 | data Row = Row { rowChar :: Char 7 | , rowRange :: (Int, Int) 8 | , rowPassword :: String 9 | } deriving Show 10 | 11 | tail' :: [a] -> [a] 12 | tail' (_:xs) = xs 13 | tail' [] = [] 14 | 15 | splitOn :: Eq a => a -> [a] -> [[a]] 16 | splitOn _ [] = [] 17 | splitOn d s = x:splitOn d (tail' s') 18 | where (x, s') = span (/= d) s 19 | 20 | sample :: String 21 | sample = "1-3 a: abcde" 22 | 23 | rowFromString :: String -> Row 24 | rowFromString s = Row c (read low, read high) password 25 | where [policy, ' ':password] = splitOn ':' s 26 | [range, c:_] = splitOn ' ' policy 27 | [low, high] = splitOn '-' range 28 | 29 | isCorrectV1 :: Row -> Bool 30 | isCorrectV1 (Row c (low, high) password) = low <= n && n <= high 31 | where n = length $ filter (== c) password 32 | 33 | isCorrectV2 :: Row -> Bool 34 | isCorrectV2 (Row c (low, high) password) = (a == c) /= (b == c) 35 | where a = password !! (low - 1) 36 | b = password !! (high - 1) 37 | 38 | main :: IO () 39 | main = do 40 | args <- getArgs 41 | case args of 42 | (filePath:_) -> do 43 | printf "Input file: %s\n" filePath 44 | rows <- map rowFromString . lines <$> readFile filePath 45 | printf "Part 1: %d\n" $ length $ filter isCorrectV1 rows 46 | printf "Part 2: %d\n" $ length $ filter isCorrectV2 rows 47 | _ -> error "Input file is not provided" 48 | -------------------------------------------------------------------------------- /02-haskell/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: test 2 | test: Main sample.txt input.txt 3 | ./Main sample.txt 4 | ./Main input.txt 5 | 6 | Main: Main.hs 7 | ghc --make Main.hs 8 | -------------------------------------------------------------------------------- /02-haskell/README.md: -------------------------------------------------------------------------------- 1 | # [Day 2](https://adventofcode.com/2020/day/2) solution in [Haskell](https://www.haskell.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/C_P_KVsXeVk/hqdefault.jpg)](https://www.youtube.com/watch?v=C_P_KVsXeVk&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&index=2) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ ghc --version 11 | The Glorious Glasgow Haskell Compilation System, version 8.6.5 12 | ``` 13 | 14 | ## Expected Result 15 | 16 | ```console 17 | Input file: input.txt 18 | Part 1: 607 19 | Part 2: 321 20 | ``` 21 | 22 | ## Quick Start 23 | 24 | - install [GHC](https://www.haskell.org/ghc/) and make sure `ghc` is available in `$PATH` 25 | - `$ make` 26 | -------------------------------------------------------------------------------- /02-haskell/sample.txt: -------------------------------------------------------------------------------- 1 | 1-3 a: abcde 2 | 1-3 b: cdefg 3 | 2-9 c: ccccccccc 4 | -------------------------------------------------------------------------------- /03-java/.gitignore: -------------------------------------------------------------------------------- 1 | *.class -------------------------------------------------------------------------------- /03-java/Main.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.util.stream.*; 4 | 5 | public class Main { 6 | static long countTrees(String[] input, int dy, int dx) { 7 | final int height = input.length; 8 | final int width = input[0].length(); 9 | 10 | int y = dy; 11 | int x = dx % width; 12 | long result = 0; 13 | 14 | while (y < height) { 15 | if (input[y].charAt(x) == '#') { 16 | result += 1; 17 | } 18 | y = y + dy; 19 | x = (x + dx) % width; 20 | } 21 | 22 | return result; 23 | } 24 | 25 | static long part1(String[] input) { 26 | return countTrees(input, 1, 3); 27 | } 28 | 29 | static long part2(String[] input) { 30 | int slopes[][] = { 31 | {1, 1}, 32 | {1, 3}, 33 | {1, 5}, 34 | {1, 7}, 35 | {2, 1}, 36 | }; 37 | 38 | long result = 1; 39 | for (int i = 0; i < slopes.length; ++i) { 40 | result *= countTrees(input, slopes[i][0], slopes[i][1]); 41 | } 42 | return result; 43 | } 44 | 45 | public static void main(String[] args) throws IOException { 46 | if (args.length <= 0) { 47 | System.err.println("Input file is not provided"); 48 | System.exit(1); 49 | } 50 | 51 | var filepath = args[0]; 52 | 53 | String[] input = null; 54 | try(var file = new BufferedReader(new FileReader(filepath))) { 55 | input = file 56 | .lines() 57 | .collect(Collectors.toList()) 58 | .toArray(new String[0]); 59 | } 60 | 61 | System.out.printf("Input file: %s\n", filepath); 62 | System.out.printf("Part 1: %d\n", part1(input)); 63 | System.out.printf("Part 2: %d\n", part2(input)); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /03-java/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: test 2 | test: Main.class sample.txt input.txt 3 | java Main ./sample.txt 4 | java Main ./input.txt 5 | 6 | Main.class: Main.java 7 | javac Main.java 8 | -------------------------------------------------------------------------------- /03-java/README.md: -------------------------------------------------------------------------------- 1 | # [Day 3](https://adventofcode.com/2020/day/3) solution in [Java](https://www.oracle.com/java/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/mlth0Bn18Vs/hqdefault.jpg)](https://www.youtube.com/watch?v=mlth0Bn18Vs&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=700s) 6 | 7 | ## Tested on 8 | 9 | ``` 10 | $ java --version 11 | java 13.0.1 2019-10-15 12 | Java(TM) SE Runtime Environment (build 13.0.1+9) 13 | Java HotSpot(TM) 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing) 14 | ``` 15 | 16 | ## Expected Result 17 | 18 | ``` 19 | Input file: ./input.txt 20 | Part 1: 159 21 | Part 2: 6419669520 22 | ``` 23 | 24 | ## Quick Start 25 | 26 | - install [JDK](https://www.oracle.com/java/technologies/javase-downloads.html) and make sure `javac` and `java` are avaialble in `$PATH` 27 | - `$ make` 28 | -------------------------------------------------------------------------------- /03-java/input.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 | -------------------------------------------------------------------------------- /03-java/sample.txt: -------------------------------------------------------------------------------- 1 | ..##....... 2 | #...#...#.. 3 | .#....#..#. 4 | ..#.#...#.# 5 | .#...##..#. 6 | ..#.##..... 7 | .#.#.#....# 8 | .#........# 9 | #.##...#... 10 | #...##....# 11 | .#..#...#.# 12 | -------------------------------------------------------------------------------- /04-d/.gitignore: -------------------------------------------------------------------------------- 1 | main 2 | *.o 3 | -------------------------------------------------------------------------------- /04-d/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: test 2 | test: main sample-01.txt sample-02.txt sample-03.txt input.txt 3 | ./main ./sample-01.txt 4 | ./main ./sample-02.txt 5 | ./main ./sample-03.txt 6 | ./main ./input.txt 7 | 8 | main: main.d 9 | dmd main.d 10 | -------------------------------------------------------------------------------- /04-d/README.md: -------------------------------------------------------------------------------- 1 | # [Day 4](https://adventofcode.com/2020/day/4) solution in [D](https://dlang.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/cvvNfuS4BwI/hqdefault.jpg)](https://www.youtube.com/watch?v=cvvNfuS4BwI&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=755s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ dmd --version 11 | DMD64 D Compiler v2.094.0 12 | Copyright (C) 1999-2020 by The D Language Foundation, All Rights Reserved written by Walter Bright 13 | ``` 14 | 15 | ## Expected Result 16 | 17 | ```console 18 | Input file: ./input.txt 19 | Part 1: 222 20 | Part 2: 140 21 | ``` 22 | 23 | ## Quick Start 24 | 25 | - install [DMD](https://dlang.org/download.html#dmd) and make sure `dmd` is available in `$PATH` 26 | - `$ make` 27 | -------------------------------------------------------------------------------- /04-d/main.d: -------------------------------------------------------------------------------- 1 | import std.stdio; 2 | import std.file; 3 | import std.string; 4 | import std.array; 5 | import std.algorithm; 6 | import std.conv; 7 | import std.typecons; 8 | 9 | struct Field 10 | { 11 | string name; 12 | string value; 13 | }; 14 | 15 | bool value_between(string value, int low, int high) 16 | { 17 | assert(low <= high); 18 | try { 19 | auto x = value.to!int; 20 | return low <= x && x <= high; 21 | } catch (ConvException e) { 22 | return false; 23 | } 24 | } 25 | 26 | bool is_byr_valid(string value) 27 | { 28 | return value_between(value, 1920, 2002); 29 | } 30 | 31 | bool is_iyr_valid(string value) 32 | { 33 | return value_between(value, 2010, 2020); 34 | } 35 | 36 | bool is_eyr_valid(string value) 37 | { 38 | return value_between(value, 2020, 2030); 39 | } 40 | 41 | bool is_hgt_valid(string value) 42 | { 43 | if (value.endsWith("cm")) { 44 | return value_between(value[0..$-2], 150, 193); 45 | } 46 | 47 | if (value.endsWith("in")) { // KKoooooooona 48 | return value_between(value[0..$-2], 59, 76); 49 | } 50 | 51 | return false; 52 | } 53 | 54 | bool is_hcl_valid(string value) 55 | { 56 | if (value.length != 7) { 57 | return false; 58 | } 59 | 60 | if (value[0] != '#') { 61 | return false; 62 | } 63 | 64 | for (int i = 1; i < 7; ++i) { 65 | if (!('0' <= value[i] && value[i] <= '9') && 66 | !('a' <= value[i] && value[i] <= 'f')) 67 | { 68 | return false; 69 | } 70 | } 71 | 72 | return true; 73 | } 74 | 75 | bool is_ecl_valid(string value) 76 | { 77 | string[] colors = ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]; 78 | return colors.canFind(value); 79 | } 80 | 81 | bool is_pid_valid(string value) 82 | { 83 | if (value.length != 9) { 84 | return false; 85 | } 86 | 87 | for (int i = 0; i < 9; ++i) { 88 | if (!('0' <= value[i] && value[i] <= '9')) { 89 | return false; 90 | } 91 | } 92 | 93 | return true; 94 | } 95 | 96 | struct RequiredField 97 | { 98 | string name; 99 | bool function(string) is_valid; 100 | } 101 | 102 | bool is_valid_v1(Field[] fields) 103 | { 104 | string[] required_fields = [ "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid", ]; 105 | string[] field_names = fields.map!((field) => field.name).array; 106 | 107 | foreach (required_field; required_fields) { 108 | if (!field_names.canFind(required_field)) { 109 | return false; 110 | } 111 | } 112 | 113 | return true; 114 | } 115 | 116 | bool is_valid_v2(Field[] fields) 117 | { 118 | RequiredField[] required_fields = [ 119 | RequiredField("byr", &is_byr_valid), 120 | RequiredField("iyr", &is_iyr_valid), 121 | RequiredField("eyr", &is_eyr_valid), 122 | RequiredField("hgt", &is_hgt_valid), 123 | RequiredField("hcl", &is_hcl_valid), 124 | RequiredField("ecl", &is_ecl_valid), 125 | RequiredField("pid", &is_pid_valid) 126 | ]; 127 | 128 | foreach (required_field; required_fields) { 129 | bool exists = false; 130 | foreach (field; fields) { 131 | if (field.name == required_field.name) { 132 | exists = true; 133 | if (!required_field.is_valid(field.value)) { 134 | return false; 135 | } 136 | break; 137 | } 138 | } 139 | if (!exists) { 140 | return false; 141 | } 142 | } 143 | 144 | return true; 145 | } 146 | 147 | void parse_fields(ref Field[] fields, string line) 148 | { 149 | foreach (field; line.split(' ')) { 150 | auto keyvalue = field.split(':'); 151 | fields ~= Field(keyvalue[0], keyvalue[1]); 152 | } 153 | } 154 | 155 | int part_1(Field[][] passports) 156 | { 157 | int result = 0; 158 | foreach(passport; passports) { 159 | if (is_valid_v1(passport)) { 160 | result += 1; 161 | } 162 | } 163 | return result; 164 | } 165 | 166 | int part_2(Field[][] passports) 167 | { 168 | int result = 0; 169 | foreach(passport; passports) { 170 | if (is_valid_v2(passport)) { 171 | result += 1; 172 | } 173 | } 174 | return result; 175 | } 176 | 177 | int main(string[] args) 178 | { 179 | if (args.length < 2) { 180 | writeln("Input file is not provided"); 181 | return 1; 182 | } 183 | 184 | string filepath = args[1]; 185 | 186 | Field[][] passports = []; 187 | Field[] passport = []; 188 | foreach (line; readText(filepath).splitLines()) { 189 | if (line.empty) { 190 | passports ~= passport; 191 | passport.length = 0; 192 | } else { 193 | parse_fields(passport, line); 194 | } 195 | } 196 | passports ~= passport; 197 | 198 | writeln("Input file: ", filepath); 199 | writeln("Part 1: ", part_1(passports)); 200 | writeln("Part 2: ", part_2(passports)); 201 | 202 | return 0; 203 | } 204 | -------------------------------------------------------------------------------- /04-d/sample-01.txt: -------------------------------------------------------------------------------- 1 | ecl:gry pid:860033327 eyr:2020 hcl:#fffffd 2 | byr:1937 iyr:2017 cid:147 hgt:183cm 3 | 4 | iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884 5 | hcl:#cfa07d byr:1929 6 | 7 | hcl:#ae17e1 iyr:2013 8 | eyr:2024 9 | ecl:brn pid:760753108 byr:1931 10 | hgt:179cm 11 | 12 | hcl:#cfa07d eyr:2025 pid:166559648 13 | iyr:2011 ecl:brn hgt:59in 14 | -------------------------------------------------------------------------------- /04-d/sample-02.txt: -------------------------------------------------------------------------------- 1 | eyr:1972 cid:100 2 | hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926 3 | 4 | iyr:2019 5 | hcl:#602927 eyr:1967 hgt:170cm 6 | ecl:grn pid:012533040 byr:1946 7 | 8 | hcl:dab227 iyr:2012 9 | ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277 10 | 11 | hgt:59cm ecl:zzz 12 | eyr:2038 hcl:74454a iyr:2023 13 | pid:3556412378 byr:2007 14 | -------------------------------------------------------------------------------- /04-d/sample-03.txt: -------------------------------------------------------------------------------- 1 | pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980 2 | hcl:#623a2f 3 | 4 | eyr:2029 ecl:blu cid:129 byr:1989 5 | iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm 6 | 7 | hcl:#888785 8 | hgt:164cm byr:2001 iyr:2015 cid:88 9 | pid:545766238 ecl:hzl 10 | eyr:2022 11 | 12 | iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719 13 | -------------------------------------------------------------------------------- /05-c++/.gitignore: -------------------------------------------------------------------------------- 1 | main -------------------------------------------------------------------------------- /05-c++/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS=-Wall -Wextra -std=c++17 -pedantic 2 | INPUTS=sample.txt input.txt 3 | 4 | .PHONY: test 5 | test: main $(INPUTS) 6 | ./main $(INPUTS) 7 | 8 | main: main.cpp 9 | $(CXX) $(CXXFLAGS) -o main main.cpp 10 | -------------------------------------------------------------------------------- /05-c++/README.md: -------------------------------------------------------------------------------- 1 | # [Day 5](https://adventofcode.com/2020/day/5) solution in [C++](https://isocpp.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/vVjNyY2jkqg/hqdefault.jpg)](https://www.youtube.com/watch?v=vVjNyY2jkqg&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=829s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ clang++ --version 11 | clang version 7.0.1-8+deb10u2 (tags/RELEASE_701/final) 12 | Target: x86_64-pc-linux-gnu 13 | Thread model: posix 14 | InstalledDir: /usr/bin 15 | $ g++ --version 16 | g++ (Debian 8.3.0-6) 8.3.0 17 | Copyright (C) 2018 Free Software Foundation, Inc. 18 | This is free software; see the source for copying conditions. There is NO 19 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 20 | ``` 21 | 22 | ## Expected Result 23 | 24 | ```console 25 | Input file: input.txt 26 | Part 1: 27 | ... 28 | Answer: 855 29 | Part 2: 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 | ## Quick Start 161 | 162 | - install [GCC] or [Clang] and make sure `g++` or `clang++` are available in `$PATH` 163 | - `$ CXX=g++ make` in case of [GCC] 164 | - `$ CXX=clang++ make` in case of [Clang] 165 | 166 | [GCC]: https://gcc.gnu.org/ 167 | [Clang]: https://clang.llvm.org/ 168 | -------------------------------------------------------------------------------- /05-c++/input.txt: -------------------------------------------------------------------------------- 1 | FFFBFBFLRR 2 | FFBBFFFRLL 3 | FBFBFFBRLR 4 | FFFBBFBRRL 5 | BFFFBBFRRL 6 | FFBFBFFLLR 7 | FBFBBFFRLL 8 | FFBBFBBLLL 9 | BFFFBBFLLR 10 | FBBFFBBRLR 11 | FBFBBBBLLL 12 | BFFBBBFLLR 13 | BBFFBFBLRR 14 | FBBBFFBRRL 15 | FFFBFBBLRL 16 | FFBFBFBRLR 17 | FBBBBFFRRL 18 | FBBBBFBLLR 19 | BFBBBBFRRR 20 | BFBFFBBLRL 21 | FBBFBBFRLL 22 | FFBBBFBRLR 23 | FBBFFFBRLL 24 | FBFBBFBLRR 25 | FFFBBBFLLL 26 | FBFFBBFLRL 27 | BFFFBFFLLR 28 | FBFBBBBLRL 29 | FBBFBFFRLR 30 | FBFBBBBRLL 31 | FFBFFBFLLR 32 | BFFBFFBRLL 33 | FBBBBFFLRL 34 | BFFBFBBRLR 35 | FFBFFFFLLR 36 | BBFFBBFRLR 37 | BFBFBBBRRL 38 | BFFBBBBLRR 39 | FFBBBBBRLL 40 | FFFBBFFRLR 41 | BBFFFFBLRL 42 | BFBBFBFRRL 43 | BBFFBBFRRL 44 | FBFFFFBRLR 45 | FBBFBBBRLR 46 | BBFBFFFLLL 47 | BFBFBBBRLL 48 | BFFFFFFLRL 49 | FBFBFBFRRR 50 | BBFFFFFLRR 51 | FFFBBFBLLR 52 | FBFFBFBRLL 53 | BFBBFBFLLR 54 | FFBFBFFLRL 55 | BFBFFBFRRR 56 | FBBBBBFRLR 57 | FBBFFBBLLR 58 | FBFBBFFLRL 59 | FBFFFBFRRL 60 | BFFBFFFLRR 61 | FFBFBFBLRL 62 | FBFFBBBLRR 63 | BFBBFFFLLR 64 | FBFBBBBRLR 65 | FBFFFBBRRL 66 | FFFBBBFRRL 67 | FBBBBBBLRR 68 | FBBBFBBRRL 69 | FBBBFBBRRR 70 | BFFFBFBLRL 71 | FBBBFFBRLR 72 | BFBFFFBRRR 73 | BFBBFBBLRL 74 | BBFFBBFLRR 75 | FFFBFBFLLR 76 | FFBBBBFRRL 77 | BFBFBFFRRR 78 | FBFBBBBRRR 79 | BFFFBFFRLL 80 | BFBBFBBLLL 81 | FBFBFBBRLR 82 | BFFFBFBRLL 83 | FFBBFFBRLL 84 | FBFFBBFRLL 85 | FBFBFFFRRR 86 | FFBFBBBLLL 87 | BBFFBBBLRR 88 | FBBFFBBRLL 89 | BFFFFFBRRL 90 | FBBFFBFLLL 91 | FBFBFBFLRL 92 | BFFFFBBLRL 93 | BFFBBFBRLL 94 | BFBFFBFRRL 95 | BFBFFFFLRR 96 | FBFFFBFRRR 97 | BBFFBBBLLR 98 | FBFBFBFRRL 99 | BFFBFBBLRL 100 | FFFBFBBRLR 101 | BBFFFBFLRL 102 | BFBFFBFLLR 103 | BBFFBFBRRL 104 | FBBFFFBRRL 105 | FFBFFFFLLL 106 | BFBFBFFLLL 107 | BFBFFFBLRR 108 | FBBBFFBLRR 109 | FFBBFFBLLR 110 | BBFFFBBLRR 111 | BBFBFFBLRL 112 | BFFFBBFRLL 113 | FBFFBFFRRR 114 | FFBBBFBLRL 115 | FFBFBFFRRR 116 | BFFFFFFLLR 117 | BFFFFBBRRL 118 | FBFBBBFLRL 119 | BFFFFBFLRL 120 | FBFBBBFRLL 121 | FFBFBFBRRR 122 | FFFBFBBRRR 123 | FBBBFFBRLL 124 | FBFFBBFRLR 125 | FFBFBBFRLR 126 | FBBFFFBLLL 127 | BBFFBBBRLR 128 | FBBFFFFLRR 129 | BBFFFBFRLL 130 | FBBBBBBRLR 131 | BFBBFFBLLR 132 | BFBFBBBLRR 133 | FFBBFBBRLL 134 | FFFBFBBLLL 135 | BFBBBFFRLR 136 | FBFBFFFLRL 137 | BFFBBFBLLR 138 | BFBBFFFLRL 139 | BFBBFFBLRR 140 | FBFFFFBLRL 141 | FBFBFBFLRR 142 | FBBFFFFLRL 143 | FFBFBBBLLR 144 | FBFBFBBRRR 145 | BFBBBBFLRL 146 | BFBFBFBLRL 147 | BFFBFBFLRL 148 | BBFFFFBRRL 149 | FFBFFFBLRR 150 | BBFBFFBRRL 151 | FFBBBFFLRR 152 | BFFBBBBLRL 153 | FBBBFFFLLR 154 | FFFBBBFRLR 155 | BBFBFBFLLR 156 | BFBFBFFRRL 157 | FBFBFFBRRR 158 | FBFFBFFLRL 159 | FFBBFFFLLL 160 | BFFBBFBLLL 161 | BBFFFFFLRL 162 | FFBFBBBRRR 163 | FFBFFBFLLL 164 | FFBBBBFLRL 165 | FFBBFFFRLR 166 | BFBFFFBLLL 167 | FBFFFFFRRR 168 | FBBFFBBLRL 169 | BFFFFBFRRL 170 | BFFFFBFRLR 171 | FFBFBFFRLL 172 | BBFFBFFRRL 173 | FBFFBBBRRR 174 | FFBBBFFRRR 175 | BFFBBBBRLR 176 | FFBBFBBRLR 177 | BFBBFBBRLR 178 | FBFFBBBLLR 179 | BFFFFBBLRR 180 | FBBFBFBRRL 181 | FBBFBFBRLR 182 | FBBBBFFLRR 183 | FBFFFFBLRR 184 | FFBFBBFLLL 185 | FBBFFBBLRR 186 | FBFBFFFLLL 187 | FBFFFBBLRR 188 | FBBFBFFLLL 189 | FBBBFBFRLL 190 | FFBBFBBLRL 191 | BFBFFBBRRR 192 | BFFBBBBLLR 193 | FBFFBFBLRR 194 | FBBBBBBLLR 195 | FBBFBBFLRL 196 | BFBFBFBRLR 197 | FBBFFFFRLR 198 | BFFBFBBLLL 199 | BFBFFFBRLL 200 | BFFBBBBRLL 201 | BFBFBBFLLR 202 | FBFFBFFRLR 203 | BFFBBFBRRR 204 | FBBBFFFLLL 205 | BFFBFBFRRL 206 | FBFFBFFRLL 207 | FFBBBFFLRL 208 | FBFFBFFRRL 209 | BBFFBFBRLR 210 | FBBFBBBRLL 211 | FBFBBFBRRL 212 | FBFBBFFLLR 213 | BFFBFFBLRR 214 | FFBBFBFRRR 215 | BFFBBBFRRL 216 | FFBFBFBRRL 217 | BBFFBFFLLR 218 | FBBBBFFLLR 219 | FBBFBFBLLL 220 | BFBBBBFRLL 221 | FFBBBBFLLL 222 | FBBBFBBRLL 223 | BFFBFBFLLR 224 | FBFFFFFLLR 225 | FBBFBFBRLL 226 | FBFBBBFLLR 227 | FBFBFBBLLL 228 | FFFBBBBLLR 229 | FFBBBBFRRR 230 | FBBFFFBLRL 231 | BBFFBFBLLR 232 | BFBBFFFRLR 233 | BBFBFFBRLL 234 | BFBBBFBLRL 235 | BFBBBFFRRL 236 | FBFBFFBRLL 237 | BBFBFFFLRL 238 | BFBFBFBRRL 239 | BFFFBFFLLL 240 | FBFFFFFLLL 241 | FBFBBBBRRL 242 | FBFFBFBRRL 243 | BFBFBFFRLR 244 | FBFBFFFRRL 245 | BFBBBBFLLR 246 | FBBBBBFLRL 247 | FBBBFFBLLL 248 | FBBBFFFRRL 249 | BBFBFBFRRR 250 | FFBFFBBRRL 251 | FFBFBBFRRR 252 | BBFFFBFLLL 253 | BBFFBFBRRR 254 | BFFBBBFLRR 255 | FBFFFFBRLL 256 | FBFFBBBLLL 257 | FFFBBBFLLR 258 | BBFBFFBLLL 259 | FBFBFBFRLR 260 | BFBFBFBLLL 261 | BBFFFFBRRR 262 | FBBBBFBRRR 263 | FBFFFBBRLR 264 | FFFBBFFLLL 265 | FBBFFBFLRR 266 | BFFFBBBRLL 267 | FBBFBFBLRL 268 | FFBFFFFLRL 269 | FFBFFFBLLR 270 | FBBFBBBRRL 271 | BFBBFBFRLL 272 | FFBBBBBLRR 273 | BFFBBFFLRR 274 | FFBFFFBRRR 275 | BFFFFFFRLL 276 | BFFBFFBRRR 277 | FFFBBBFLRL 278 | FFBFBFBRLL 279 | BBFFBBBRRL 280 | BBFBFFFRRR 281 | BBFBFBFLRL 282 | FBBFBBBLRR 283 | FFBBFBFRLR 284 | BFBFBFBLRR 285 | FFFBBBBRRL 286 | BFFFFBBLLL 287 | FFFBBBFLRR 288 | FFBBFFBRRL 289 | FBFFBFFLRR 290 | FBFFBBFRRL 291 | BFBBFBBRRL 292 | BBFFBFBRLL 293 | FBBFFFFRRL 294 | FFBBFFBRLR 295 | BBFFFFBRLR 296 | BBFBFBFRLL 297 | FBBFBBBRRR 298 | BFFBBBBRRL 299 | BFFBBBBLLL 300 | BFBBFBBRLL 301 | BFFBBBFLRL 302 | FBFFBFBLRL 303 | BFFBBFFLLL 304 | BFBFBBFRLL 305 | FBBFFFBRRR 306 | FFBFFBFLRL 307 | FFBFFFFRLR 308 | BFBBFBBLLR 309 | BFBFBFBRRR 310 | FBBFFFFLLR 311 | FFBBFBBRRL 312 | BFFBFFFRRL 313 | FBFBBBFRRL 314 | FBBFFFFLLL 315 | BFFFFFFRRL 316 | BFBFFFBLLR 317 | BFBBFFBRRR 318 | BFFBFFFLLR 319 | FFBBBFBLLL 320 | BFFBBBFLLL 321 | FBFFFBBRRR 322 | FBBBBBFLLL 323 | BFBFFFFRRL 324 | FFBBFFBRRR 325 | BFBFBFFLLR 326 | FFFBFBBLLR 327 | BFBBFFBLRL 328 | FBFBBFFRRR 329 | BFBBBFFLLR 330 | FBBFFFBLLR 331 | BFFFBBFLRR 332 | BFFFFBBRLR 333 | FBFFFBBLRL 334 | BFBBBFBLRR 335 | FBFFBFBLLR 336 | BFBBFFFRLL 337 | FFBBFBFLLL 338 | FFBFFFBRRL 339 | FFBBFBBLRR 340 | FFBFBBFLRL 341 | FFFBBFFRRR 342 | BFBFFBFLRR 343 | BBFFFFBLRR 344 | FFBFBBFLRR 345 | FFFBBFFRLL 346 | FBBBFBBRLR 347 | BBFFFBBRRL 348 | FFFBFBFRRL 349 | FBBBBBBRRR 350 | FFBBBFBLRR 351 | FBBFBFFLRL 352 | FBBBFFFRRR 353 | FBBFFBBRRR 354 | BFFFFBBRRR 355 | FBBFFBFRLR 356 | BFFBFBFRLR 357 | BFBBBFFLRR 358 | FFBFFBFLRR 359 | BFBFFFBLRL 360 | BFFBFBFRLL 361 | FBBBBFBLLL 362 | BBFBFFFRRL 363 | FFBBFFBLLL 364 | FFBFFBBRLR 365 | FFBBBFBRRL 366 | FFBBFFFLRR 367 | BBFFBFFLRR 368 | BFFFBFFRRL 369 | FBFBBFBRRR 370 | FBFFFBFLRL 371 | BBFBFFBLRR 372 | BFFBFBFRRR 373 | BFFFFFFLLL 374 | FBBBFFBLRL 375 | FBBBFBFRRL 376 | BFFBFFBRLR 377 | BFBBBFFRRR 378 | BFFBFFFRRR 379 | FFFBBBBLLL 380 | FFBBBFBRRR 381 | FFBBFBFLLR 382 | BFBBBFBLLL 383 | FBBFBBBLLL 384 | BBFBFBFLLL 385 | BFBBBFBRRR 386 | BFBFBBFRLR 387 | FBFFBFBRLR 388 | BFFBFFFRLL 389 | BFBBFFBRRL 390 | FFBBFBFLRR 391 | FFBBBFBLLR 392 | BBFBFFFRLR 393 | FFBBFBBLLR 394 | FBFFFBBLLR 395 | FBBFBFBLLR 396 | FFBBFBFRLL 397 | BFFFFFBLLR 398 | BFBFBFBLLR 399 | BFBBFFBLLL 400 | FBBBBFBLRR 401 | BFBBBBFLLL 402 | BFFBBFFLLR 403 | FBFBFFFRLL 404 | FBBBBBFLLR 405 | BBFBFFBLLR 406 | BFBFBFFLRR 407 | FBBBBFFRLR 408 | FFFBFBBRRL 409 | FFFBBFBLLL 410 | BFBBBBFLRR 411 | BFBBBBBLRR 412 | FBBFBBFLLL 413 | BFFFBFBRLR 414 | FBBFBFFLRR 415 | FBFFFBBRLL 416 | BFFFFFBRRR 417 | BFFBBBFRLL 418 | FFBFBFBLLL 419 | BFBBFBFLRL 420 | FBBBBFFLLL 421 | FBBFBFFRRR 422 | FFFBBBBRLR 423 | BFBFBFFLRL 424 | FFBBBFFRRL 425 | BBFFBFFLRL 426 | BFFFBBBRRL 427 | FBFFFFFLRL 428 | BBFFBFFRLL 429 | BFFFBBFRLR 430 | BFBFFBBRLR 431 | FBFBFBBLLR 432 | FBBFFBBRRL 433 | BBFFBFFRLR 434 | FFBFFBBRRR 435 | BBFFFBBRLL 436 | BFBFBFBRLL 437 | FBFBBBBLLR 438 | FBFBFBFLLL 439 | FFBFBBBLRL 440 | BFFBFBBLRR 441 | BBFFFFFRRR 442 | FFFBBFFLRL 443 | BBFFFBFLRR 444 | FFBFFBFRLL 445 | FBFFBBFLLR 446 | FBFFFFBLLR 447 | FFBBBBFRLL 448 | FFFBBFFLRR 449 | BBFFFFFLLL 450 | BBFBFBFLRR 451 | FFBBBBFRLR 452 | BBFFFBBLLL 453 | FFBFBBFRLL 454 | BFFFFFFLRR 455 | FFBFFFBRLL 456 | FBFFFFFRRL 457 | BBFFFBBLLR 458 | FFFBBFBRLR 459 | BFFBBFFRRL 460 | FBBBBFFRLL 461 | BFBBFFBRLR 462 | BFFFBFBLRR 463 | BFBFFBBLLR 464 | BFFBFBBRRR 465 | BBFFFBFRRR 466 | FFBBBFFRLL 467 | BFBBFBBRRR 468 | FFBBBBBRLR 469 | BFFFBBBRLR 470 | BFFFBBFLLL 471 | FFBFFBBLRL 472 | FFBBFBFLRL 473 | FBFBFBBLRL 474 | BFFBBFBRLR 475 | FBBBBFBRRL 476 | FBFBBBFRRR 477 | BBFBFFBRRR 478 | BBFBFBFRRL 479 | FBBBBFBRLL 480 | FBFFFBBLLL 481 | BFBFBBFLLL 482 | FBBBBFFRRR 483 | FBBFBBFLRR 484 | FFBFFFFRRL 485 | FFFBFBFRLL 486 | FBFBBFBRLR 487 | FFBBBFFLLR 488 | FBFFBBFLLL 489 | FFFBBBBRRR 490 | BFFFFBFRRR 491 | BFBFFFFLRL 492 | FBFBFFBLLL 493 | BFFBBBBRRR 494 | BFFBFBFLLL 495 | FBFBBFFLLL 496 | FBBFBBFLLR 497 | FBBFBFFLLR 498 | BBFFBFFRRR 499 | FBBBBBFLRR 500 | FFBFBBBRLL 501 | FBBBBBBLLL 502 | BFBBFBBLRR 503 | FBFBFFFRLR 504 | FFBBBBBRRL 505 | FBFFFFBRRR 506 | FBBFFBFRRR 507 | FFFBBBBRLL 508 | FBBFFFFRRR 509 | BFFBFFBLRL 510 | FFFBBBBLRR 511 | BFBBBBFRRL 512 | BFFFFBBLLR 513 | BFFFFFFRLR 514 | BFFBBFFRLR 515 | BFFBBFBLRL 516 | BBFFFFBLLL 517 | BFFFFBBRLL 518 | FBBFFBFRLL 519 | FFFBBFBLRR 520 | BBFBFFBRLR 521 | BFFFBBBLRL 522 | BFBBFFFRRL 523 | BFBFFBFRLL 524 | BFFBFBBRRL 525 | FBFFBFFLLL 526 | BFBBFFBRLL 527 | BFBBBFFLRL 528 | FFFBFBFRLR 529 | BFFBFFBLLR 530 | BBFFFFBLLR 531 | FFBBFBBRRR 532 | FBBFBBFRRR 533 | FFBBBFFLLL 534 | BFBBFFFLRR 535 | FFBFFBFRRR 536 | FBBFBBFRLR 537 | FBFBFFBLRL 538 | BBFFBFBLRL 539 | BFFFFBFLRR 540 | FBBBFFFRLL 541 | FFFBBFFRRL 542 | FFBFBFFRLR 543 | FFBBBBBLLR 544 | BFFBBFBRRL 545 | BFFBFBFLRR 546 | FFFBFBBRLL 547 | FBFBBBBLRR 548 | BFBFFFBRRL 549 | FFBFBBBRRL 550 | FFBFBFFLRR 551 | FFBFBFBLRR 552 | BFFBBFBLRR 553 | FFFBFBFLRL 554 | BFFBFBBRLL 555 | BFFBFFFRLR 556 | FFBBFFBLRR 557 | FBBBBBFRLL 558 | FBFBFBFRLL 559 | BFBBBBBRRR 560 | BBFBFFFLLR 561 | FFBFFFFRLL 562 | FFFBBFFLLR 563 | BFBBFFFRRR 564 | FBBBFBFLLR 565 | BFBBBBBRRL 566 | FBBBBBBLRL 567 | FBBBFBFLLL 568 | BFFBFFBLLL 569 | BFFBFFBRRL 570 | FBFFBFBRRR 571 | FBFBFBBLRR 572 | BFFFBFFRLR 573 | FBFBFBBRRL 574 | BFBBBBBLLL 575 | FBFFBBFLRR 576 | BBFFFBBRRR 577 | FFFBFBBLRR 578 | FBBFFBBLLL 579 | BFFFFFBRLL 580 | BBFBFBFRLR 581 | BFFFBFFLRR 582 | FBFBFFBLRR 583 | BFBBBFBRLR 584 | BBFFBFBLLL 585 | FFBBBBFLLR 586 | FBBBFBFRLR 587 | FBBBBBFRRL 588 | FBBBFFBRRR 589 | FFBBBBFLRR 590 | BBFFBBBRRR 591 | BFFFFBFLLR 592 | FBFFBBFRRR 593 | BBFFFFFRRL 594 | BBFFBBBRLL 595 | FBFFFFBRRL 596 | FBFFBBBLRL 597 | BFFFFFFRRR 598 | BFFFBBBLLR 599 | FBFBBFBLRL 600 | BFBFBBBLRL 601 | FFBBFBFRRL 602 | FBFFBBBRLR 603 | BFFBBBFRLR 604 | FFBFBBBLRR 605 | BFFBFFFLLL 606 | FBBBFFBLLR 607 | BFBFBBFRRL 608 | FFBFFBBLRR 609 | BBFFFBBRLR 610 | FFBFFBBRLL 611 | FBFFFFFRLR 612 | FBBBFBBLRL 613 | BBFFFBFLLR 614 | FBBFFFFRLL 615 | BFFFBBFLRL 616 | BFBBBBBRLR 617 | BFFBFBBLLR 618 | FFBBFFBLRL 619 | BFBBBFBRLL 620 | BFBBFBFRRR 621 | FFBBFFFLRL 622 | FFBFFFBLLL 623 | FFBFFBBLLR 624 | BBFFFBFRRL 625 | BFFBBFFLRL 626 | FBBFFBFLRL 627 | BFFFBBBRRR 628 | FFBBFFFRRL 629 | FFBFBFFRRL 630 | FBBBBFBLRL 631 | BBFFBBFLLR 632 | FBBBBFBRLR 633 | BFBFFBFRLR 634 | FFBFFBBLLL 635 | FFBBBBBLLL 636 | BFBFBBFRRR 637 | BBFFFFFRLL 638 | FBFFFBFRLR 639 | BFBBBBFRLR 640 | FBBFFFBRLR 641 | BFFFFFBLRR 642 | FBFFBBBRRL 643 | FBBBFBFLRL 644 | BFFFBFFLRL 645 | BFBFBBFLRR 646 | BFBBFBFRLR 647 | FFBFFBFRLR 648 | BBFFFBBLRL 649 | BFBFFBFLLL 650 | BBFFBBFRLL 651 | FBBBBBBRRL 652 | FFBBFFFLLR 653 | FFFBBBBLRL 654 | BBFFBFFLLL 655 | FBBBFFFLRR 656 | FBFBFBFLLR 657 | FFBFBFFLLL 658 | FFBFFFBLRL 659 | BFBFFBBLLL 660 | FBFBFFFLRR 661 | BBFFBBFLRL 662 | BFFFBFFRRR 663 | FBFFBBBRLL 664 | FFFBBBFRLL 665 | FBFFFBFLLL 666 | BBFFFBFRLR 667 | BBFFBBFLLL 668 | BBFBFFFRLL 669 | BBFFBBBLLL 670 | FBFBBFBLLL 671 | FBBFBBBLRL 672 | FBFFFFFLRR 673 | FBFBBFBRLL 674 | FFFBBFBRLL 675 | FBBBFBFLRR 676 | FBFBFFFLLR 677 | FFBFFFFRRR 678 | BFFBBFFRLL 679 | BBFFFFBRLL 680 | BFBBBFBLLR 681 | BFBBFBFLLL 682 | FBBBBBFRRR 683 | FFBFBBBRLR 684 | BFFFFBFLLL 685 | BFFFFFBRLR 686 | BFBFFFFLLL 687 | BFBFFBBRRL 688 | FFBFFBFRRL 689 | FBBBFBBLRR 690 | BFBFFBFLRL 691 | FBBBBBBRLL 692 | FFFBBFBLRL 693 | FFBFBBFLLR 694 | BFBFBBBLLR 695 | FBFBBFFRLR 696 | FBFBFFBLLR 697 | BBFFBBBLRL 698 | BFBFFBBLRR 699 | FFBBFFFRRR 700 | FBFBBBFRLR 701 | BFBBFBFLRR 702 | FBBFBFFRRL 703 | FBFBBFFLRR 704 | FBBFBFBRRR 705 | FBFBBBFLRR 706 | BFBBBBBLLR 707 | FBFFFBFLLR 708 | FBFBBFBLLR 709 | FFBFFFFLRR 710 | BFBFBBBRLR 711 | FBFFFFFRLL 712 | FFBBBBBLRL 713 | BFBBBFFLLL 714 | BBFFFFFLLR 715 | BFFFBBFRRR 716 | FBFBFFBRRL 717 | FFFBBFBRRR 718 | FBBBFBBLLR 719 | BFFFFBFRLL 720 | BFBFBFFRLL 721 | BFBFBBFLRL 722 | FFBBBFFRLR 723 | BFBFBBBLLL 724 | BFFFBBBLRR 725 | BFBBFFFLLL 726 | FBFBFBBRLL 727 | BFFFFFBLLL 728 | BBFBFFFLRR 729 | BFBFFBBRLL 730 | FFBFBBFRRL 731 | FBFBBBFLLL 732 | FBFFFFBLLL 733 | FBFFBFFLLR 734 | FBBFBBFRRL 735 | BBFFBBFRRR 736 | BFFFBFBRRL 737 | BFBFFFBRLR 738 | FBBFFBFLLR 739 | BFBFFFFRLL 740 | BFBFBBBRRR 741 | BFBFFFFRLR 742 | FFBFBFBLLR 743 | FBFFFBFLRR 744 | FFBBBBBRRR 745 | FBBFFFBLRR 746 | FFBFFFBRLR 747 | BFFFBFBRRR 748 | FBBFBBBLLR 749 | BFBBBBBLRL 750 | FBBBFFFLRL 751 | BFFBBFFRRR 752 | BFBBBFFRLL 753 | BFBFFFFLLR 754 | BFFBFFFLRL 755 | BFBFFFFRRR 756 | BBFFFFFRLR 757 | FBFFFBFRLL 758 | FFFBBBFRRR 759 | BFFFBFBLLR 760 | FBBFBFFRLL 761 | FBBBFBBLLL 762 | BFBBBBBRLL 763 | BFFBBBFRRR 764 | FFFBFBFRRR 765 | BFBBBFBRRL 766 | FBFFBFBLLL 767 | FBBBFFFRLR 768 | FBFBBFFRRL 769 | FBBFFBFRRL 770 | FBBFBFBLRR 771 | BFFFFFBLRL 772 | FBBBFBFRRR 773 | BFFFBBBLLL 774 | FFBBBFBRLL 775 | -------------------------------------------------------------------------------- /05-c++/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | int row_of_pass(string pass) 11 | { 12 | assert(pass.size() == 10); 13 | 14 | int low = 0; 15 | int high = 127; 16 | 17 | for (int i = 0; i < 7; ++i) { 18 | const int middle = (high - low) / 2 + low; 19 | switch (pass[i]) { 20 | case 'F': 21 | high = middle; 22 | break; 23 | case 'B': 24 | low = middle + 1; 25 | break; 26 | default: 27 | assert(0 && "unreachable"); 28 | } 29 | } 30 | 31 | assert(low == high); 32 | 33 | return low; 34 | } 35 | 36 | int col_of_pass(string pass) 37 | { 38 | assert(pass.size() == 10); 39 | 40 | int low = 0; 41 | int high = 7; 42 | 43 | for (int i = 7; i < 10; ++i) { 44 | const int middle = (high - low) / 2 + low; 45 | switch (pass[i]) { 46 | case 'L': 47 | high = middle; 48 | break; 49 | case 'R': 50 | low = middle + 1; 51 | break; 52 | default: 53 | assert(0 && "unreachable"); 54 | } 55 | } 56 | 57 | assert(low == high); 58 | 59 | return low; 60 | } 61 | 62 | int id_of_pass(string pass) 63 | { 64 | return row_of_pass(pass) * 8 + col_of_pass(pass); 65 | } 66 | 67 | void part_1(const char *file_path) 68 | { 69 | ifstream fin(file_path); 70 | 71 | int ans = numeric_limits::min(); 72 | cout << "Part 1: " << endl; 73 | while (fin) { 74 | string pass; 75 | getline(fin, pass); 76 | if (pass.size() == 10) { 77 | auto id = id_of_pass(pass); 78 | ans = max(ans, id); 79 | cout << " " << pass << " -> " << id_of_pass(pass) << endl; 80 | } 81 | } 82 | cout << " Answer: " << ans << endl; 83 | } 84 | 85 | void part_2(const char *file_path) 86 | { 87 | ifstream fin(file_path); 88 | 89 | set ids_nuts; 90 | while (fin) { 91 | string pass; 92 | getline(fin, pass); 93 | if (pass.size() == 10) { 94 | auto id = id_of_pass(pass); 95 | ids_nuts.insert(id); 96 | } 97 | } 98 | 99 | std::cout << "Part 2: " << std::endl; 100 | for (int row = 0; row < 128; ++row) { 101 | cout << " "; 102 | for (int col = 0; col < 8; ++col) { 103 | if (ids_nuts.find(row * 8 + col) != ids_nuts.end()) { 104 | cout << "* "; 105 | } else { 106 | cout << ". "; 107 | } 108 | } 109 | cout << endl; 110 | } 111 | } 112 | 113 | void solve_file(const char *file_path) 114 | { 115 | cout << "Input file: " << file_path << endl; 116 | part_1(file_path); 117 | part_2(file_path); 118 | } 119 | 120 | int main(int argc, char **argv) 121 | { 122 | for (int i = 1; i < argc; ++i) { 123 | solve_file(argv[i]); 124 | } 125 | 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /05-c++/sample.txt: -------------------------------------------------------------------------------- 1 | BFFFBBFRRR 2 | FFFBBBFRRR 3 | BBFFBBFRLL 4 | -------------------------------------------------------------------------------- /06-python/.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | __pycache__/ 3 | *.txt -------------------------------------------------------------------------------- /06-python/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=sample.txt input.txt 2 | 3 | .PHONY: test 4 | test: main.py $(INPUTS) 5 | python3 main.py $(INPUTS) 6 | -------------------------------------------------------------------------------- /06-python/README.md: -------------------------------------------------------------------------------- 1 | # [Day 6](https://adventofcode.com/2020/day/6) solution in [Python](https://www.python.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/vVjNyY2jkqg/hqdefault.jpg)](https://www.youtube.com/watch?v=vVjNyY2jkqg&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=3970s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ python3 --version 11 | Python 3.7.3 12 | ``` 13 | 14 | ## Expected Result 15 | 16 | ```console 17 | Input file: input.txt 18 | Part 1: 6170 19 | Part 2: 2947 20 | ``` 21 | 22 | ## Quick Start 23 | 24 | - install [Python](https://www.python.org/downloads/) and make sure `python3` is available in `$PATH` 25 | - `$ make` 26 | -------------------------------------------------------------------------------- /06-python/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import sys 4 | 5 | def every_yesu(answers): 6 | result = {} 7 | for answer in answers: 8 | for x in answer: 9 | if x in result: 10 | result[x] += 1 11 | else: 12 | result[x] = 1 13 | return len([x for x in result.values() if x == len(answers)]) 14 | 15 | def any_yesu(answers): 16 | result = set() 17 | for answer in answers: 18 | for x in answer: 19 | result.add(x) 20 | return len(result) 21 | 22 | def solve_file(file_path): 23 | print("Input file: %s" % file_path) 24 | with open(file_path, 'r') as file: 25 | lines = map( 26 | lambda x: x.strip(), 27 | [line for line in file.readlines()]) 28 | answers = [] 29 | any_result = 0 30 | every_result = 0 31 | for line in lines: 32 | if line: 33 | answers.append(line) 34 | else: 35 | any_result += any_yesu(answers) 36 | every_result += every_yesu(answers) 37 | answers = [] 38 | any_result += any_yesu(answers) 39 | every_result += every_yesu(answers) 40 | print("Part 1: %d" % any_result) 41 | print("Part 2: %d" % every_result) 42 | 43 | if __name__ == "__main__": 44 | for file_path in sys.argv[1:]: 45 | solve_file(file_path) 46 | -------------------------------------------------------------------------------- /07-go/.gitignore: -------------------------------------------------------------------------------- 1 | *.dot 2 | *.png 3 | main -------------------------------------------------------------------------------- /07-go/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=sample-01.txt sample-02.txt input.txt 2 | 3 | all: test $(PNGS) 4 | 5 | .PHONY: test 6 | test: main $(INPUTS) 7 | ./main $(INPUTS) 8 | 9 | main: main.go 10 | go build -o main main.go 11 | -------------------------------------------------------------------------------- /07-go/README.md: -------------------------------------------------------------------------------- 1 | # [Day 7](https://adventofcode.com/2020/day/7) solution in [Go](https://golang.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/FXPmwFubyDE/hqdefault.jpg)](https://www.youtube.com/watch?v=FXPmwFubyDE&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=713s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ go version 11 | go version go1.13.5 linux/amd64 12 | ``` 13 | 14 | ## Expected Result 15 | 16 | ```console 17 | Input file: input.txt 18 | Part 1: 274 19 | Part 2: 158730 20 | ``` 21 | 22 | ## Quick Start 23 | 24 | - Install [go](https://golang.org/dl/) and make sure `go` is available in `$PATH` 25 | - `$ make` 26 | -------------------------------------------------------------------------------- /07-go/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | "regexp" 7 | "strconv" 8 | "os" 9 | "bufio" 10 | "container/list" 11 | ) 12 | 13 | type Color = string 14 | 15 | type Child struct { 16 | color Color 17 | amount int 18 | } 19 | 20 | type Rule struct { 21 | container Color 22 | children []Child 23 | } 24 | 25 | type Rules = map[Color][]Child 26 | 27 | func lineToRule(line string) Rule { 28 | a := strings.Split(line, "contain") 29 | container := strings.TrimRight(strings.Split(a[0], "bags")[0], " ") 30 | children := []Child{} 31 | 32 | r := regexp.MustCompile(" ([0-9]+) (.*) bags?") 33 | if a[1] != " no other bags." { 34 | for _, child := range strings.Split(a[1], ",") { 35 | b := r.FindStringSubmatch(child) 36 | amount, err := strconv.Atoi(b[1]) 37 | if err != nil { 38 | panic(err) 39 | } 40 | children = append(children, Child{ 41 | amount: amount, 42 | color: b[2], 43 | }) 44 | } 45 | } 46 | 47 | return Rule{ 48 | container: container, 49 | children: children, 50 | } 51 | } 52 | 53 | func findContainers(rules Rules, color Color) []Color { 54 | result := []Color{} 55 | for container, children := range rules { 56 | for _, child := range children { 57 | if child.color == color { 58 | result = append(result, container) 59 | break 60 | } 61 | } 62 | } 63 | return result 64 | } 65 | 66 | func rulesFromFile(filePath string) Rules { 67 | file, err := os.Open(filePath) 68 | if err != nil { 69 | panic(err) 70 | } 71 | defer file.Close() 72 | 73 | rules := map[Color][]Child{}; 74 | 75 | scanner := bufio.NewScanner(file) 76 | for scanner.Scan() { 77 | line := scanner.Text() 78 | rule := lineToRule(line); 79 | rules[rule.container] = rule.children; 80 | } 81 | 82 | if err := scanner.Err(); err != nil { 83 | panic(err) 84 | } 85 | 86 | return rules 87 | } 88 | 89 | func countBagsV1(rules Rules, color Color) int { 90 | visited := map[Color]bool{} 91 | queue := list.New() 92 | 93 | queue.PushBack(color); 94 | for queue.Len() > 0 { 95 | next := queue.Front() 96 | 97 | if _, ok := visited[next.Value.(Color)]; !ok { 98 | for _, container := range findContainers(rules, next.Value.(Color)) { 99 | queue.PushBack(container) 100 | } 101 | } 102 | 103 | visited[next.Value.(Color)] = true; 104 | queue.Remove(next) 105 | } 106 | 107 | return len(visited) - 1 108 | } 109 | 110 | func countBagsV2(rules Rules, color Color) int { 111 | result := 0 112 | 113 | if children, ok := rules[color]; ok { 114 | for _, child := range children { 115 | result += child.amount + child.amount * countBagsV2(rules, child.color) 116 | } 117 | } 118 | 119 | return result 120 | } 121 | 122 | func createDotFileForRules(rules Rules, filePath string) { 123 | file, err := os.Create(filePath) 124 | if err != nil { 125 | panic(err) 126 | } 127 | defer file.Close() 128 | 129 | fmt.Fprintf(file, "digraph D {\n"); 130 | for parent, children := range rules { 131 | for _, child := range children { 132 | fmt.Fprintf(file, " %#v -> %#v\n", parent, child.color); 133 | } 134 | } 135 | fmt.Fprintf(file, "}\n"); 136 | } 137 | 138 | func solveFile(filePath string) { 139 | rules := rulesFromFile(filePath) 140 | createDotFileForRules(rules, filePath + ".dot") 141 | fmt.Printf("Part 1: %d\n", countBagsV1(rules, "shiny gold")) 142 | fmt.Printf("Part 2: %d\n", countBagsV2(rules, "shiny gold")) 143 | } 144 | 145 | func main() { 146 | for _, filePath := range os.Args[1:] { 147 | fmt.Printf("Input file: %s\n", filePath) 148 | solveFile(filePath) 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /07-go/sample-01.txt: -------------------------------------------------------------------------------- 1 | light red bags contain 1 bright white bag, 2 muted yellow bags. 2 | dark orange bags contain 3 bright white bags, 4 muted yellow bags. 3 | bright white bags contain 1 shiny gold bag. 4 | muted yellow bags contain 2 shiny gold bags, 9 faded blue bags. 5 | shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags. 6 | dark olive bags contain 3 faded blue bags, 4 dotted black bags. 7 | vibrant plum bags contain 5 faded blue bags, 6 dotted black bags. 8 | faded blue bags contain no other bags. 9 | dotted black bags contain no other bags. 10 | -------------------------------------------------------------------------------- /07-go/sample-02.txt: -------------------------------------------------------------------------------- 1 | shiny gold bags contain 2 dark red bags. 2 | dark red bags contain 2 dark orange bags. 3 | dark orange bags contain 2 dark yellow bags. 4 | dark yellow bags contain 2 dark green bags. 5 | dark green bags contain 2 dark blue bags. 6 | dark blue bags contain 2 dark violet bags. 7 | dark violet bags contain no other bags. 8 | -------------------------------------------------------------------------------- /08-js/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=sample.txt input.txt 2 | 3 | .PHONY: test 4 | test: main.js $(INPUTS) 5 | node main.js $(INPUTS) 6 | -------------------------------------------------------------------------------- /08-js/README.md: -------------------------------------------------------------------------------- 1 | # [Day 8](https://adventofcode.com/2020/day/8) solution in JavaScript 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/8aYjw62hxTY/hqdefault.jpg)](https://www.youtube.com/watch?v=8aYjw62hxTY&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=752s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ node --version 11 | v13.5.0 12 | ``` 13 | 14 | ## Expected Result 15 | 16 | ```console 17 | Input file input.txt 18 | Part 1: 2051 19 | Part 2: 2304 20 | ``` 21 | 22 | ## Quick Start 23 | 24 | - install [node.js](https://nodejs.org/en/) and make sure `node` is available in `$PATH` 25 | - `$ make` 26 | -------------------------------------------------------------------------------- /08-js/input.txt: -------------------------------------------------------------------------------- 1 | jmp +583 2 | acc +9 3 | jmp +525 4 | jmp +302 5 | jmp +287 6 | jmp +412 7 | acc -16 8 | acc -19 9 | acc -19 10 | jmp +423 11 | acc -4 12 | nop +13 13 | acc -8 14 | jmp +37 15 | acc +0 16 | acc -5 17 | acc +48 18 | acc +0 19 | jmp +232 20 | acc +39 21 | acc +5 22 | jmp +69 23 | acc +31 24 | jmp +425 25 | acc +31 26 | jmp +93 27 | nop +166 28 | acc -7 29 | jmp +192 30 | acc +1 31 | jmp +391 32 | acc +11 33 | acc +20 34 | jmp +1 35 | acc +24 36 | acc +7 37 | acc +27 38 | jmp +162 39 | jmp +580 40 | acc +9 41 | acc +39 42 | acc -18 43 | jmp +283 44 | acc +28 45 | acc -14 46 | nop +464 47 | acc -12 48 | jmp +358 49 | jmp +523 50 | jmp +212 51 | acc +16 52 | acc -13 53 | acc +10 54 | acc +46 55 | jmp +207 56 | jmp +266 57 | jmp +1 58 | acc +36 59 | jmp -19 60 | jmp -3 61 | acc -16 62 | acc +3 63 | jmp +229 64 | acc +44 65 | acc +0 66 | acc -17 67 | acc -14 68 | jmp +132 69 | acc +38 70 | nop -30 71 | acc -12 72 | jmp +506 73 | jmp +511 74 | acc -15 75 | acc +4 76 | acc +29 77 | jmp +129 78 | jmp +419 79 | jmp +1 80 | jmp +45 81 | acc +14 82 | acc +20 83 | acc +11 84 | jmp +153 85 | jmp +78 86 | acc +32 87 | nop +212 88 | acc -7 89 | acc +42 90 | jmp -65 91 | acc -17 92 | jmp +458 93 | acc +10 94 | acc +18 95 | acc -11 96 | acc +8 97 | jmp +215 98 | acc +33 99 | acc +25 100 | jmp -21 101 | nop +92 102 | acc +0 103 | nop +353 104 | jmp +188 105 | acc +43 106 | jmp +82 107 | jmp +399 108 | acc +33 109 | acc +16 110 | acc -3 111 | jmp +174 112 | acc -12 113 | acc -3 114 | nop +171 115 | jmp +73 116 | nop +362 117 | jmp -48 118 | jmp +218 119 | acc +5 120 | jmp +486 121 | acc +43 122 | acc -1 123 | acc +0 124 | jmp +476 125 | acc +21 126 | jmp +44 127 | acc +7 128 | acc -6 129 | jmp +1 130 | acc +3 131 | jmp +95 132 | acc +12 133 | acc +38 134 | jmp +202 135 | acc +17 136 | acc -12 137 | jmp +114 138 | jmp -33 139 | jmp +367 140 | acc +45 141 | acc +40 142 | jmp -81 143 | acc -5 144 | acc +27 145 | acc +6 146 | jmp +374 147 | acc -6 148 | acc +10 149 | acc +19 150 | jmp +1 151 | jmp +171 152 | acc +8 153 | acc +46 154 | acc +12 155 | jmp +234 156 | acc +45 157 | acc +28 158 | jmp +337 159 | acc +8 160 | nop +10 161 | acc +17 162 | jmp +368 163 | acc +2 164 | acc -3 165 | acc +29 166 | jmp -160 167 | acc -7 168 | acc +11 169 | jmp +174 170 | acc +48 171 | acc -3 172 | acc +33 173 | jmp +6 174 | acc +3 175 | acc -10 176 | acc +9 177 | acc -13 178 | jmp +428 179 | acc -13 180 | acc +35 181 | nop -112 182 | jmp -147 183 | acc +13 184 | acc -4 185 | acc +50 186 | acc +46 187 | jmp -118 188 | acc +38 189 | acc +36 190 | jmp +375 191 | nop -3 192 | jmp +93 193 | acc +10 194 | acc -1 195 | jmp +211 196 | acc +6 197 | acc +38 198 | acc -12 199 | jmp -6 200 | jmp +1 201 | acc +41 202 | jmp -117 203 | acc -17 204 | acc -15 205 | jmp -120 206 | acc +17 207 | acc +48 208 | acc +37 209 | acc +0 210 | jmp +139 211 | acc +7 212 | acc -12 213 | acc +0 214 | jmp +98 215 | acc +47 216 | acc +3 217 | acc -18 218 | acc +26 219 | jmp +141 220 | jmp +236 221 | acc +18 222 | jmp +275 223 | acc -10 224 | acc -11 225 | jmp +12 226 | acc -19 227 | acc +17 228 | jmp +300 229 | acc +32 230 | nop +145 231 | jmp +84 232 | jmp +34 233 | acc -17 234 | acc +12 235 | acc +37 236 | jmp +182 237 | acc -7 238 | jmp +154 239 | nop +375 240 | acc -1 241 | jmp +108 242 | jmp +1 243 | acc +16 244 | acc +49 245 | jmp +355 246 | acc -16 247 | acc -19 248 | acc +47 249 | acc +26 250 | jmp -171 251 | jmp +285 252 | jmp +84 253 | acc +28 254 | acc -11 255 | acc +6 256 | jmp -252 257 | nop +228 258 | acc +10 259 | acc -17 260 | acc +42 261 | jmp -221 262 | acc +34 263 | acc +8 264 | jmp +201 265 | jmp -225 266 | acc +45 267 | nop +125 268 | acc +25 269 | acc -7 270 | jmp +318 271 | nop +348 272 | nop +40 273 | acc +42 274 | jmp +284 275 | acc -1 276 | acc +46 277 | jmp +1 278 | acc +41 279 | jmp +231 280 | acc +30 281 | acc +45 282 | acc +10 283 | acc +45 284 | jmp -227 285 | acc +25 286 | acc +13 287 | jmp +219 288 | acc -10 289 | acc +27 290 | acc +45 291 | jmp -186 292 | acc -18 293 | acc +50 294 | acc +31 295 | acc +19 296 | jmp +89 297 | nop -240 298 | jmp +173 299 | nop +65 300 | acc -8 301 | jmp +1 302 | nop -146 303 | jmp -156 304 | acc +27 305 | jmp +106 306 | acc +4 307 | acc +45 308 | jmp +35 309 | acc +44 310 | acc +47 311 | jmp -254 312 | jmp +57 313 | acc +1 314 | jmp -274 315 | acc +32 316 | acc +6 317 | acc +1 318 | nop +179 319 | jmp +122 320 | jmp +1 321 | jmp -310 322 | jmp -273 323 | acc +46 324 | acc +9 325 | jmp -187 326 | acc +36 327 | acc +0 328 | nop +47 329 | acc +17 330 | jmp -137 331 | nop -116 332 | acc -17 333 | acc -6 334 | acc -8 335 | jmp +106 336 | acc +36 337 | acc +50 338 | acc +3 339 | acc +22 340 | jmp +190 341 | acc +48 342 | jmp -336 343 | jmp -164 344 | jmp -32 345 | acc +44 346 | nop +242 347 | jmp -215 348 | jmp +7 349 | acc +36 350 | acc +3 351 | acc +27 352 | acc +24 353 | jmp -8 354 | jmp +156 355 | acc -5 356 | acc +42 357 | nop +37 358 | jmp +107 359 | jmp +247 360 | acc +12 361 | acc +30 362 | jmp +44 363 | jmp -306 364 | acc +36 365 | jmp -354 366 | nop +192 367 | nop +153 368 | jmp -106 369 | jmp -284 370 | jmp +1 371 | acc +33 372 | acc +18 373 | acc +13 374 | jmp +232 375 | acc -4 376 | nop -64 377 | acc +38 378 | acc +29 379 | jmp -349 380 | acc -7 381 | acc +44 382 | acc +4 383 | acc +48 384 | jmp -35 385 | acc +13 386 | jmp -144 387 | acc -7 388 | jmp +196 389 | acc -8 390 | jmp -316 391 | jmp -138 392 | jmp -381 393 | jmp -156 394 | acc +21 395 | jmp +189 396 | acc +30 397 | nop -85 398 | acc +34 399 | acc -13 400 | jmp -326 401 | jmp -7 402 | jmp +1 403 | acc +2 404 | acc +24 405 | jmp -56 406 | jmp +152 407 | acc +42 408 | acc +25 409 | acc -6 410 | jmp +174 411 | jmp -96 412 | jmp -86 413 | jmp +20 414 | acc +23 415 | nop -93 416 | acc +3 417 | jmp -42 418 | acc +0 419 | acc +6 420 | jmp +100 421 | jmp +20 422 | jmp -147 423 | acc +19 424 | nop -367 425 | jmp -80 426 | nop -318 427 | nop -289 428 | acc +45 429 | jmp -321 430 | nop -4 431 | acc +13 432 | jmp +74 433 | acc +0 434 | acc +15 435 | jmp +153 436 | acc -5 437 | acc +24 438 | acc +21 439 | jmp +1 440 | jmp -48 441 | jmp -262 442 | nop -317 443 | jmp +93 444 | acc +20 445 | jmp -32 446 | acc +44 447 | acc +50 448 | jmp -350 449 | acc -19 450 | acc +46 451 | jmp -431 452 | acc -11 453 | nop -227 454 | acc +48 455 | jmp +103 456 | acc +44 457 | acc +31 458 | acc -15 459 | jmp -15 460 | acc +0 461 | acc +34 462 | acc -3 463 | acc +38 464 | jmp +108 465 | acc +28 466 | nop -60 467 | acc +28 468 | acc +26 469 | jmp -20 470 | jmp -440 471 | acc +48 472 | jmp -257 473 | acc +11 474 | acc +8 475 | acc +14 476 | acc +30 477 | jmp +8 478 | acc +47 479 | jmp -323 480 | acc +15 481 | acc +10 482 | acc -15 483 | acc +13 484 | jmp -169 485 | acc -11 486 | acc -12 487 | acc +24 488 | acc +5 489 | jmp +125 490 | acc +34 491 | acc -17 492 | acc +2 493 | acc +32 494 | jmp -83 495 | jmp -120 496 | jmp -11 497 | acc +25 498 | nop -54 499 | jmp +1 500 | jmp -29 501 | acc +13 502 | acc +17 503 | acc +6 504 | acc +31 505 | jmp -420 506 | acc +25 507 | acc +13 508 | jmp +117 509 | jmp -3 510 | nop +68 511 | acc +32 512 | acc -11 513 | acc +31 514 | jmp -374 515 | acc -4 516 | acc +34 517 | acc +38 518 | acc +23 519 | jmp -113 520 | acc -19 521 | acc +50 522 | nop -216 523 | jmp -134 524 | nop -331 525 | acc -7 526 | acc +28 527 | jmp -357 528 | jmp -216 529 | jmp -128 530 | acc +34 531 | acc +16 532 | jmp -54 533 | acc -16 534 | acc +0 535 | jmp -64 536 | acc +7 537 | nop -322 538 | jmp -306 539 | nop -414 540 | acc +38 541 | acc +15 542 | jmp +77 543 | acc +18 544 | jmp +1 545 | acc +0 546 | acc -8 547 | jmp -248 548 | acc +26 549 | jmp -6 550 | acc +17 551 | acc +21 552 | acc +30 553 | jmp -142 554 | acc -13 555 | acc -18 556 | nop -330 557 | jmp +27 558 | acc -14 559 | jmp +60 560 | acc +31 561 | acc -6 562 | acc +49 563 | acc +16 564 | jmp -289 565 | acc +11 566 | acc +0 567 | nop -141 568 | acc +19 569 | jmp -143 570 | acc +44 571 | jmp -286 572 | acc +42 573 | jmp -209 574 | acc +34 575 | acc +10 576 | acc +3 577 | jmp -461 578 | acc +2 579 | jmp -358 580 | acc +42 581 | acc +0 582 | acc +26 583 | jmp -191 584 | acc +16 585 | jmp -230 586 | acc +31 587 | jmp -244 588 | nop -456 589 | acc +16 590 | nop -196 591 | jmp -475 592 | acc +24 593 | jmp -553 594 | acc +42 595 | acc +24 596 | acc +3 597 | jmp -572 598 | acc +31 599 | jmp +7 600 | jmp -144 601 | acc +20 602 | acc +23 603 | acc -14 604 | nop -506 605 | jmp -17 606 | acc +19 607 | nop -428 608 | jmp -286 609 | acc +2 610 | acc +6 611 | acc +28 612 | acc -13 613 | jmp -291 614 | jmp -377 615 | acc -3 616 | acc +32 617 | jmp +1 618 | jmp -205 619 | acc +18 620 | acc +32 621 | nop -523 622 | jmp -79 623 | acc +32 624 | jmp -30 625 | acc +40 626 | acc -17 627 | jmp +1 628 | acc +28 629 | jmp +1 630 | -------------------------------------------------------------------------------- /08-js/main.js: -------------------------------------------------------------------------------- 1 | const fs = require("fs"); 2 | const assert = require("assert"); 3 | 4 | function execute(program) { 5 | let accumulator = 0; 6 | let ip = 0; 7 | let visited = []; 8 | 9 | while (ip < program.length && !visited.includes(ip)) { 10 | visited.push(ip); 11 | switch (program[ip].instr) { 12 | case "nop": 13 | ip += 1; 14 | break; 15 | case "acc": 16 | accumulator += program[ip].arg; 17 | ip += 1; 18 | break; 19 | case "jmp": 20 | ip += program[ip].arg; 21 | break; 22 | default: 23 | assert(false, `unknown instructions ${program[ip].instr}`); 24 | } 25 | } 26 | 27 | return { 28 | accumulator: accumulator, 29 | visited: visited, 30 | terminated: ip >= program.length, 31 | }; 32 | } 33 | 34 | function part1(program) { 35 | return execute(program).accumulator; 36 | } 37 | 38 | function flip(instr) { 39 | switch(instr) { 40 | case "jmp": return "nop"; 41 | case "nop": return "jmp"; 42 | default: assert(false, `unexpected instruction ${instr}`); 43 | } 44 | } 45 | 46 | function part2(program) { 47 | let visited = execute(program).visited; 48 | 49 | for (let ip of visited) { 50 | if (program[ip].instr != "acc") { 51 | program[ip].instr = flip(program[ip].instr); 52 | let result = execute(program); 53 | if (result.terminated) { 54 | return result.accumulator; 55 | } 56 | program[ip].instr = flip(program[ip].instr); 57 | } 58 | } 59 | 60 | assert(false, "Could not find terminating solution"); 61 | } 62 | 63 | function solveFile(filePath) { 64 | let program = fs.readFileSync(filePath) 65 | .toString() 66 | .trim() 67 | .split('\n') 68 | .map(x => { 69 | let [instr, arg] = x.split(' '); 70 | return { 71 | instr: instr, 72 | arg: parseInt(arg) 73 | }; 74 | }); 75 | 76 | console.log(`Input file ${filePath}`); 77 | console.log(`Part 1: ${part1(program)}`); 78 | console.log(`Part 2: ${part2(program)}`); 79 | } 80 | 81 | for (let filePath of process.argv.slice(2)) { 82 | solveFile(filePath); 83 | } 84 | -------------------------------------------------------------------------------- /08-js/sample.txt: -------------------------------------------------------------------------------- 1 | nop +0 2 | acc +1 3 | jmp +4 4 | acc +3 5 | jmp -3 6 | acc -99 7 | acc +1 8 | jmp -4 9 | acc +6 10 | -------------------------------------------------------------------------------- /09-ocaml/.gitignore: -------------------------------------------------------------------------------- 1 | *.byte 2 | *.native 3 | *.cmi 4 | *.cmo 5 | *.cmx 6 | *.o -------------------------------------------------------------------------------- /09-ocaml/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=input.txt 2 | 3 | .PHONY: test 4 | test: main.native main.byte $(INPUTS) 5 | ./main.native $(INPUTS) 6 | 7 | main.native: main.ml 8 | ocamlopt -o main.native main.ml 9 | 10 | main.byte: main.ml 11 | ocamlc -o main.byte main.ml 12 | -------------------------------------------------------------------------------- /09-ocaml/README.md: -------------------------------------------------------------------------------- 1 | # [Day 9](https://adventofcode.com/2020/day/9) solution in [OCaml](https://ocaml.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/a8vogmPtlXE/hqdefault.jpg)](https://www.youtube.com/watch?v=a8vogmPtlXE&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=778s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ ocaml --version 11 | The OCaml toplevel, version 4.10.0 12 | ``` 13 | 14 | ## Expected Result 15 | 16 | ```console 17 | Part 1: 22406676 18 | Part 2: 2942387 19 | ``` 20 | 21 | ## Quick Start 22 | 23 | - install [OCaml](https://ocaml.org/learn/tutorials/up_and_running.html) and make sure `ocamlc` and `ocamlopt` is available in `$PATH` 24 | - `$ make` 25 | -------------------------------------------------------------------------------- /09-ocaml/input.txt: -------------------------------------------------------------------------------- 1 | 38 2 | 18 3 | 28 4 | 41 5 | 37 6 | 17 7 | 24 8 | 45 9 | 50 10 | 35 11 | 9 12 | 30 13 | 11 14 | 8 15 | 40 16 | 43 17 | 46 18 | 36 19 | 33 20 | 19 21 | 14 22 | 32 23 | 3 24 | 7 25 | 16 26 | 42 27 | 10 28 | 12 29 | 13 30 | 25 31 | 52 32 | 15 33 | 21 34 | 18 35 | 29 36 | 17 37 | 20 38 | 22 39 | 11 40 | 51 41 | 23 42 | 24 43 | 34 44 | 30 45 | 26 46 | 33 47 | 14 48 | 50 49 | 27 50 | 28 51 | 31 52 | 36 53 | 44 54 | 32 55 | 35 56 | 25 57 | 37 58 | 38 59 | 40 60 | 59 61 | 39 62 | 41 63 | 42 64 | 43 65 | 70 66 | 55 67 | 45 68 | 57 69 | 46 70 | 47 71 | 58 72 | 80 73 | 52 74 | 68 75 | 53 76 | 56 77 | 60 78 | 95 79 | 62 80 | 63 81 | 72 82 | 75 83 | 77 84 | 79 85 | 109 86 | 174 87 | 83 88 | 139 89 | 96 90 | 91 91 | 92 92 | 98 93 | 93 94 | 99 95 | 100 96 | 254 97 | 120 98 | 428 99 | 161 100 | 162 101 | 154 102 | 149 103 | 195 104 | 153 105 | 232 106 | 334 107 | 581 108 | 156 109 | 170 110 | 175 111 | 176 112 | 189 113 | 197 114 | 187 115 | 183 116 | 190 117 | 191 118 | 199 119 | 219 120 | 220 121 | 309 122 | 269 123 | 302 124 | 372 125 | 345 126 | 751 127 | 305 128 | 323 129 | 410 130 | 331 131 | 326 132 | 332 133 | 339 134 | 346 135 | 374 136 | 359 137 | 370 138 | 769 139 | 382 140 | 373 141 | 381 142 | 419 143 | 418 144 | 439 145 | 489 146 | 592 147 | 571 148 | 607 149 | 651 150 | 669 151 | 628 152 | 665 153 | 685 154 | 709 155 | 657 156 | 658 157 | 1068 158 | 792 159 | 705 160 | 1151 161 | 729 162 | 1060 163 | 754 164 | 763 165 | 791 166 | 1010 167 | 1342 168 | 857 169 | 928 170 | 1146 171 | 1163 172 | 1178 173 | 1297 174 | 1515 175 | 1648 176 | 1285 177 | 1438 178 | 1315 179 | 1362 180 | 1363 181 | 1387 182 | 1434 183 | 1459 184 | 1468 185 | 1483 186 | 1492 187 | 1517 188 | 1926 189 | 1620 190 | 1719 191 | 2091 192 | 2003 193 | 2020 194 | 3237 195 | 2309 196 | 2341 197 | 2475 198 | 3011 199 | 2927 200 | 4242 201 | 3640 202 | 2821 203 | 2893 204 | 2846 205 | 4540 206 | 2917 207 | 2902 208 | 4305 209 | 2985 210 | 6130 211 | 3009 212 | 4728 213 | 4094 214 | 3339 215 | 3722 216 | 4023 217 | 4312 218 | 4329 219 | 5294 220 | 4650 221 | 4816 222 | 5296 223 | 8142 224 | 5667 225 | 5714 226 | 5723 227 | 5902 228 | 6707 229 | 8625 230 | 5819 231 | 8708 232 | 8067 233 | 5994 234 | 6324 235 | 6348 236 | 10231 237 | 9006 238 | 9608 239 | 11115 240 | 11625 241 | 10653 242 | 8641 243 | 8979 244 | 9466 245 | 10112 246 | 10483 247 | 10963 248 | 11437 249 | 11381 250 | 11533 251 | 11542 252 | 12672 253 | 14635 254 | 16106 255 | 19949 256 | 17046 257 | 28483 258 | 15814 259 | 22914 260 | 17647 261 | 17620 262 | 17985 263 | 19604 264 | 18107 265 | 18445 266 | 39446 267 | 18753 268 | 19091 269 | 19578 270 | 30985 271 | 34091 272 | 33726 273 | 27251 274 | 24053 275 | 23075 276 | 24214 277 | 27307 278 | 30449 279 | 36137 280 | 36430 281 | 36092 282 | 41861 283 | 33434 284 | 36860 285 | 38669 286 | 66586 287 | 42679 288 | 37198 289 | 36552 290 | 54997 291 | 79211 292 | 37844 293 | 42166 294 | 42653 295 | 47128 296 | 47289 297 | 51360 298 | 48267 299 | 50382 300 | 59505 301 | 51521 302 | 60741 303 | 63883 304 | 69526 305 | 88558 306 | 74396 307 | 80523 308 | 69986 309 | 75221 310 | 148063 311 | 73750 312 | 79851 313 | 88719 314 | 80010 315 | 89294 316 | 99788 317 | 80497 318 | 84819 319 | 102158 320 | 94417 321 | 98649 322 | 131211 323 | 168409 324 | 101903 325 | 148702 326 | 112262 327 | 149996 328 | 137633 329 | 143276 330 | 252154 331 | 143736 332 | 184607 333 | 182248 334 | 148971 335 | 217026 336 | 175653 337 | 164829 338 | 160507 339 | 165316 340 | 259246 341 | 186722 342 | 179236 343 | 272769 344 | 193066 345 | 196320 346 | 200552 347 | 255998 348 | 375556 349 | 364416 350 | 249895 351 | 347564 352 | 292707 353 | 280909 354 | 309478 355 | 309052 356 | 304243 357 | 314287 358 | 342037 359 | 354889 360 | 325336 361 | 530804 362 | 339743 363 | 365958 364 | 526800 365 | 648795 366 | 554792 367 | 435234 368 | 465835 369 | 389386 370 | 859035 371 | 493259 372 | 604784 373 | 542602 374 | 554138 375 | 941502 376 | 573616 377 | 780122 378 | 885693 379 | 613295 380 | 1376736 381 | 963002 382 | 669176 383 | 1047397 384 | 665079 385 | 691294 386 | 1567786 387 | 1222411 388 | 755344 389 | 824620 390 | 928493 391 | 855221 392 | 901069 393 | 882645 394 | 931988 395 | 1334260 396 | 1147386 397 | 1096740 398 | 1207681 399 | 1127754 400 | 1282471 401 | 1186911 402 | 1304589 403 | 1278374 404 | 1334255 405 | 1446638 406 | 1356373 407 | 1360470 408 | 1420423 409 | 2010399 410 | 1579964 411 | 1610565 412 | 1951961 413 | 1656413 414 | 2011531 415 | 1737866 416 | 1756290 417 | 2660962 418 | 1814633 419 | 2214459 420 | 2224494 421 | 3076836 422 | 3804458 423 | 2628104 424 | 2314665 425 | 3184504 426 | 2638844 427 | 2915154 428 | 2612629 429 | 2690628 430 | 4208068 431 | 3531925 432 | 2780893 433 | 3000387 434 | 5691015 435 | 3190529 436 | 3266978 437 | 4537183 438 | 3394279 439 | 5315052 440 | 3552499 441 | 3570923 442 | 4029092 443 | 4039127 444 | 4438953 445 | 4539159 446 | 5708944 447 | 4927294 448 | 5527783 449 | 6661257 450 | 5251473 451 | 7833232 452 | 5781280 453 | 5303257 454 | 5471521 455 | 8718312 456 | 8505581 457 | 5971422 458 | 7610050 459 | 7229656 460 | 6457507 461 | 11162549 462 | 6946778 463 | 11385731 464 | 7123422 465 | 7581591 466 | 8478080 467 | 8068219 468 | 8578286 469 | 8978112 470 | 9466453 471 | 10779256 472 | 10554730 473 | 13849499 474 | 11222895 475 | 13084705 476 | 10774778 477 | 11084537 478 | 11274679 479 | 15841734 480 | 12428929 481 | 12918200 482 | 21777625 483 | 13404285 484 | 14525726 485 | 15601502 486 | 14070200 487 | 14528369 488 | 19752759 489 | 19532842 490 | 15649810 491 | 16546299 492 | 20550990 493 | 22870738 494 | 18444565 495 | 20021183 496 | 21329508 497 | 21639267 498 | 21859315 499 | 31251312 500 | 26488990 501 | 26926271 502 | 26988400 503 | 23703608 504 | 25347129 505 | 25833214 506 | 26322485 507 | 44200246 508 | 53248756 509 | 22406676 510 | 47692529 511 | 28598569 512 | 32196109 513 | 34094375 514 | 38185566 515 | 34990864 516 | 36567482 517 | 38465748 518 | 43188823 519 | 40083832 520 | 41350691 521 | 61082775 522 | 43498582 523 | 51005245 524 | 64298962 525 | 46110284 526 | 47753805 527 | 48239890 528 | 48729161 529 | 60824078 530 | 52155699 531 | 86425456 532 | 56501051 533 | 79888638 534 | 67186973 535 | 60794678 536 | 62692944 537 | 66290484 538 | 69085239 539 | 71558346 540 | 73456612 541 | 105881767 542 | 78549580 543 | 81434523 544 | 123487622 545 | 84849273 546 | 98265983 547 | 91738472 548 | 93864089 549 | 148239523 550 | 94350174 551 | 99909504 552 | 96969051 553 | 100884860 554 | 123688024 555 | 145014958 556 | 117295729 557 | 119193995 558 | 142581582 559 | 127085162 560 | 128983428 561 | 135375723 562 | 139747096 563 | 166054290 564 | 159984103 565 | 195235034 566 | 163398853 567 | 173172995 568 | 175298612 569 | 176587745 570 | 178713362 571 | 185602561 572 | 186088646 573 | 302401386 574 | 191319225 575 | 194259678 576 | 196878555 577 | 260868963 578 | 243466442 579 | 236489724 580 | 244380891 581 | 246279157 582 | 248177423 583 | 262460885 584 | 256068590 585 | 411576276 586 | 321464369 587 | 299731199 588 | 323382956 589 | 430469537 590 | 494966233 591 | 420054187 592 | 351886357 593 | 354011974 594 | 355301107 595 | 607954947 596 | 371691207 597 | 377407871 598 | 668231610 599 | 385578903 600 | 757270110 601 | 433368279 602 | 479956166 603 | 623114155 604 | 480870615 605 | 490660048 606 | 494456580 607 | 562192084 608 | 651617556 609 | 579451546 610 | 621195568 611 | 644847325 612 | 675269313 613 | 677394930 614 | 723577564 615 | 705898331 616 | 707187464 617 | 725703181 618 | 862351255 619 | 726992314 620 | 749099078 621 | 1108637592 622 | 818947182 623 | 971530663 624 | 1430765028 625 | 1371839639 626 | 913324445 627 | 970616214 628 | 1146074136 629 | 1322242255 630 | 985116628 631 | 1200647114 632 | 1513968811 633 | 1286639010 634 | 1224298871 635 | 2430879847 636 | 1320116638 637 | 1352664243 638 | 1383293261 639 | 1450569878 640 | 1413085795 641 | 1432890645 642 | 1719715292 643 | 2559207470 644 | 1568046260 645 | 2415881656 646 | 1732271627 647 | 1790477845 648 | 2131190764 649 | 2920362406 650 | 1883940659 651 | 1898441073 652 | 1955732842 653 | 2185763742 654 | 3415996475 655 | 2209415499 656 | 3241047723 657 | 2510937881 658 | 3905479034 659 | 4386612689 660 | 2672780881 661 | 2703409899 662 | 4056074142 663 | 6336358881 664 | 3018616138 665 | 2845976440 666 | 3000936905 667 | 3358524105 668 | 3300317887 669 | 3451986919 670 | 3616212286 671 | 6204500545 672 | 4069704401 673 | 3782381732 674 | 3839673501 675 | 4917057211 676 | 3854173915 677 | 4141496584 678 | 7441814471 679 | 7310752124 680 | 5450463222 681 | 5214347780 682 | 5376190780 683 | 6526954796 684 | 5673717786 685 | 5518757321 686 | 6844906483 687 | 7757708870 688 | 5846913345 689 | 5864592578 690 | 6146294327 691 | 11360642107 692 | 9504818432 693 | 8369044130 694 | 7068199205 695 | 7398594018 696 | 12444389985 697 | 7622055233 698 | 7636555647 699 | 7693847416 700 | 7995670499 701 | 17126873665 702 | 9355844364 703 | 13155312968 704 | 10590538560 705 | 14881009212 706 | 11192475107 707 | 10894948101 708 | 11365670666 709 | 22973787010 710 | 11383349899 711 | 11711505923 712 | 11993207672 713 | 12010886905 714 | 12932791783 715 | 18781943917 716 | 14466793223 717 | 14690254438 718 | 15063869704 719 | 14704754852 720 | 15020649251 721 | 25585202539 722 | 19059518082 723 | 19019905546 724 | 25958817805 725 | 18586209059 726 | 20739194263 727 | 24866818891 728 | 21973888459 729 | 21485486661 730 | 22087423208 731 | 25897229959 732 | 22905835006 733 | 22749020565 734 | 23094855822 735 | 23376557571 736 | 23704713595 737 | 29395009290 738 | 24943678688 739 | 29171548075 740 | 29710903689 741 | 35803063967 742 | 34040554797 743 | 39798712345 744 | 29725404103 745 | 37108072459 746 | 37606114605 747 | 48784922185 748 | 39325403322 749 | 43488214828 750 | 40071695720 751 | 60746742655 752 | 43572909869 753 | 44722909024 754 | 44234507226 755 | 92357832054 756 | 45654855571 757 | 52144029855 758 | 46453734160 759 | 46471413393 760 | 47081271166 761 | 48648392283 762 | 54115226763 763 | 62549793293 764 | 58882451764 765 | 91315778392 766 | 73298313972 767 | 63765958900 768 | 66833476562 769 | 76433475781 770 | 90377764595 771 | 89750144460 772 | 79397099042 773 | 97798885426 774 | 83559910548 775 | 94186922483 776 | 89889362797 777 | 87807417095 778 | 114693823148 779 | 112997678527 780 | 92108589731 781 | 92126268964 782 | 98597764015 783 | 227691501675 784 | 100586640156 785 | 95729663449 786 | 152927557888 787 | 116665020056 788 | 121432245057 789 | 159469091920 790 | 130599435462 791 | 140199434681 792 | 143163057942 793 | 156583621022 794 | 243749698098 795 | 183537080544 796 | 167204516137 797 | 247276509015 798 | 171367327643 799 | 173449273345 800 | 177696779892 801 | 179916006826 802 | 235271647673 803 | 184234858695 804 | 187838253180 805 | 222018885213 806 | 187855932413 807 | 395468158558 808 | 307403950818 809 | 320132074025 810 | 535667593239 811 | 238097265113 812 | 371393012957 813 | 261631679738 814 | 297803951599 815 | 270798870143 816 | 545501215931 817 | 365181943155 818 | 323788137159 819 | 351439374832 820 | 573164938450 821 | 338571843780 822 | 351146053237 823 | 448495650035 824 | 353365280171 825 | 365552712305 826 | 364150865521 827 | 375694185593 828 | 449469932918 829 | 739845051114 830 | 559435631337 831 | 511644069572 832 | 569035630556 833 | 499728944851 834 | 568602821742 835 | 642191883100 836 | 508896135256 837 | 532430549881 838 | 600203523518 839 | 662985894754 840 | 594587007302 841 | 662359980939 842 | 907607474336 843 | 1008905564255 844 | 689717897017 845 | 702722709301 846 | 995557163271 847 | 1564592793827 848 | 815022645223 849 | 717516145692 850 | 729703577826 851 | 887338255165 852 | 825164118511 853 | 1442567760415 854 | 1008625080107 855 | 1324290585128 856 | 1214366778873 857 | 1685275060288 858 | 1032159494732 859 | 1231588716496 860 | 1041326685137 861 | 1103483142558 862 | 1948934159473 863 | 1194790530820 864 | 1256946988241 865 | 1365082690240 866 | 2880065591108 867 | 2240213796603 868 | 1392440606318 869 | 2379615439050 870 | 1420238854993 871 | 1544726223049 872 | 1447219723518 873 | 2812302413758 874 | 1542680264203 875 | 1554867696337 876 | 3705229709619 877 | 1833789198618 878 | 2298273673378 879 | 2040784574839 880 | 2550702866076 881 | 2812679461311 882 | 2811814684578 883 | 2678808440014 884 | 2272915401633 885 | 2615029385813 886 | 5426844070391 887 | 2677185843234 888 | 3376469462821 889 | 2839660329836 890 | 4259899184829 891 | 2867458578511 892 | 3853141369715 893 | 5397867592764 894 | 6320259095432 895 | 3281008922136 896 | 2989899987721 897 | 5679760992269 898 | 3097547960540 899 | 3583464839042 900 | 8593174497065 901 | 3874573773457 902 | 9554334765726 903 | 5321793496975 904 | 7636368647650 905 | 5292215229047 906 | 4887944787446 907 | 5829560317557 908 | 6864473761178 909 | 4950101244867 910 | 5454689715649 911 | 8707852992527 912 | 5516846173070 913 | 6742032351968 914 | 6573364826763 915 | 5965006539051 916 | 5857358566232 917 | 6087447948261 918 | 11208203882878 919 | 8954906526772 920 | 8614394133610 921 | 6972121733997 922 | 8047649205407 923 | 6681012799582 924 | 9166789002504 925 | 8824675018324 926 | 17781183136114 927 | 9838046032313 928 | 10180160016493 929 | 10404790960516 930 | 10242316473914 931 | 10342634503095 932 | 10466947417937 933 | 12197858972652 934 | 14812265093004 935 | 10971535888719 936 | 11374204739302 937 | 11481852712121 938 | 11822365105283 939 | 11944806514493 940 | 17214438207911 941 | 12538371365814 942 | 12768460747843 943 | 13653134533579 944 | 20709263891851 945 | 21554364755795 946 | 17085803760098 947 | 14728662004989 948 | 21724169186035 949 | 18662721050637 950 | 19004835034817 951 | 20018206048806 952 | 20080362506227 953 | 24908822021482 954 | 20809581921032 955 | 20584950977009 956 | 21314170391814 957 | 21438483306656 958 | 39472302971669 959 | 32291434633153 960 | 22345740628021 961 | 42309120163044 962 | 23304217817404 963 | 23767171619776 964 | 36219273242728 965 | 38743083556864 966 | 25306832113657 967 | 26421595281422 968 | 28381796538568 969 | 35538243926021 970 | 31814465765087 971 | 33391383055626 972 | 45387194619884 973 | 39023041083623 974 | 38680927099443 975 | 39085197541044 976 | 45649958445425 977 | 78108238624667 978 | 41394532898041 979 | 42023434283665 980 | 41899121368823 981 | 42752653698470 982 | 87044491343466 983 | 70495392864530 984 | 47071389437180 985 | 59986444862504 986 | 49725813098826 987 | 48611049931061 988 | 59812978337048 989 | 51728427395079 990 | 67352709691108 991 | 75032645212483 992 | 54803391819990 993 | 67062723638011 994 | 81433580797913 995 | 88465922335221 996 | 75414817339291 997 | 77766124640487 998 | 84735155986469 999 | 80984318909867 1000 | 80479730439085 1001 | -------------------------------------------------------------------------------- /09-ocaml/main.ml: -------------------------------------------------------------------------------- 1 | let read_whole_file filename = 2 | let ch = open_in filename in 3 | let s = really_input_string ch (in_channel_length ch) in 4 | close_in ch; 5 | s 6 | 7 | let preamble_size = 25 8 | 9 | let first_invalid (xs: int array): int = 10 | let exception Result of int in 11 | let n = Array.length xs in 12 | try 13 | for i = preamble_size to n - 1 do 14 | let x = Array.get xs i in 15 | let is_valid (): bool = 16 | try 17 | for a = i - preamble_size to i - 2 do 18 | for b = a + 1 to i - 1 do 19 | let a' = Array.get xs a in 20 | let b' = Array.get xs b in 21 | if a' + b' == x then 22 | raise_notrace Exit 23 | done 24 | done; 25 | false 26 | with 27 | Exit -> true 28 | in 29 | if not (is_valid ()) then 30 | raise_notrace (Result x) 31 | done; 32 | assert false (* unreachable *) 33 | with 34 | Result x -> x 35 | 36 | let part_1 = first_invalid 37 | 38 | let part_2 (xs: int array): int = 39 | let exception Result of int in 40 | let n = Array.length xs in 41 | let fi = first_invalid xs in 42 | let ys = Array.make n 0 in 43 | Array.set ys 0 (Array.get xs 0); 44 | for i = 1 to n - 1 do 45 | Array.set ys i (Array.get ys (i - 1) + Array.get xs i) 46 | done; 47 | try 48 | for i = 0 to n - 2 do 49 | for j = i + 1 to n - 1 do 50 | let s = 51 | if i == 0 52 | then Array.get ys j 53 | else Array.get ys j - Array.get ys (i - 1) 54 | in 55 | if s == fi then 56 | let mx = ref Int.min_int in 57 | let mn = ref Int.max_int in 58 | for k = i to j do 59 | mx := max (Array.get xs k) !mx; 60 | mn := min (Array.get xs k) !mn 61 | done; 62 | raise_notrace (Result (!mx + !mn)) 63 | done 64 | done; 65 | assert false 66 | with 67 | Result x -> x 68 | 69 | let solve_file (file_path: string) = 70 | let xs = 71 | file_path 72 | |> read_whole_file 73 | |> String.split_on_char '\n' 74 | |> List.map (String.trim) 75 | |> List.filter (fun s -> String.length s > 0) 76 | |> List.map int_of_string 77 | |> Array.of_list 78 | in 79 | Printf.printf "Input file: %s\n" file_path; 80 | Printf.printf "Part 1: %d\n" (part_1 xs); 81 | Printf.printf "Part 2: %d\n" (part_2 xs) 82 | 83 | let () = 84 | Sys.argv 85 | |> Array.to_list 86 | |> List.tl 87 | |> List.iter solve_file 88 | -------------------------------------------------------------------------------- /09-ocaml/sample.txt: -------------------------------------------------------------------------------- 1 | 35 2 | 20 3 | 15 4 | 25 5 | 47 6 | 40 7 | 62 8 | 55 9 | 65 10 | 95 11 | 102 12 | 117 13 | 150 14 | 182 15 | 127 16 | 219 17 | 299 18 | 277 19 | 309 20 | 576 21 | -------------------------------------------------------------------------------- /10-c/.gitignore: -------------------------------------------------------------------------------- 1 | main -------------------------------------------------------------------------------- /10-c/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-Wall -Wextra -std=c11 -pedantic 2 | INPUTS=sample-01.txt sample-02.txt input.txt 3 | 4 | .PHONY: test 5 | test: main $(INPUTS) 6 | ./main $(INPUTS) 7 | 8 | main: main.c 9 | $(CC) $(CFLAGS) -o main main.c 10 | -------------------------------------------------------------------------------- /10-c/README.md: -------------------------------------------------------------------------------- 1 | # [Day 10](https://adventofcode.com/2020/day/10) solution in C 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/lHzbWWjdloM/hqdefault.jpg)](https://www.youtube.com/watch?v=lHzbWWjdloM&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=709s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ clang --version 11 | clang version 7.0.1-8+deb10u2 (tags/RELEASE_701/final) 12 | Target: x86_64-pc-linux-gnu 13 | Thread model: posix 14 | InstalledDir: /usr/bin 15 | $ gcc --version 16 | gcc (Debian 8.3.0-6) 8.3.0 17 | Copyright (C) 2018 Free Software Foundation, Inc. 18 | This is free software; see the source for copying conditions. There is NO 19 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 20 | ``` 21 | 22 | ## Expected Result 23 | 24 | ```console 25 | Input file: input.txt 26 | Part 1: 2368 27 | Part 2: 1727094849536 28 | ``` 29 | 30 | ## Quick Start 31 | 32 | - install [GCC] or [Clang] and make sure `gcc` or `clang` 33 | are available in `$PATH` 34 | - `$ CC=gcc make` in case of [GCC] 35 | - `$ CC=clang make` in case of [Clang] 36 | 37 | 38 | [GCC]: https://gcc.gnu.org/ 39 | [Clang]: https://clang.llvm.org/ 40 | -------------------------------------------------------------------------------- /10-c/input.txt: -------------------------------------------------------------------------------- 1 | 30 2 | 73 3 | 84 4 | 136 5 | 132 6 | 117 7 | 65 8 | 161 9 | 49 10 | 68 11 | 139 12 | 46 13 | 21 14 | 127 15 | 109 16 | 153 17 | 163 18 | 160 19 | 18 20 | 22 21 | 131 22 | 146 23 | 62 24 | 113 25 | 172 26 | 150 27 | 171 28 | 98 29 | 93 30 | 130 31 | 170 32 | 59 33 | 1 34 | 110 35 | 2 36 | 55 37 | 37 38 | 44 39 | 148 40 | 102 41 | 40 42 | 28 43 | 35 44 | 43 45 | 56 46 | 169 47 | 33 48 | 5 49 | 141 50 | 83 51 | 15 52 | 105 53 | 142 54 | 36 55 | 116 56 | 11 57 | 45 58 | 82 59 | 10 60 | 17 61 | 159 62 | 140 63 | 12 64 | 108 65 | 29 66 | 72 67 | 121 68 | 52 69 | 91 70 | 166 71 | 88 72 | 97 73 | 118 74 | 99 75 | 124 76 | 149 77 | 16 78 | 9 79 | 143 80 | 104 81 | 57 82 | 79 83 | 123 84 | 58 85 | 96 86 | 24 87 | 162 88 | 23 89 | 92 90 | 69 91 | 147 92 | 156 93 | 25 94 | 133 95 | 34 96 | 8 97 | 85 98 | 76 99 | 103 100 | 122 101 | -------------------------------------------------------------------------------- /10-c/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define N 1000 7 | 8 | unsigned long xs[N + 10]; 9 | int xs_size = 0; 10 | unsigned long dp[N + 10]; 11 | 12 | int compare_int(const void *x, const void *y) 13 | { 14 | return *(const unsigned long *)x - *(const unsigned long *)y; 15 | } 16 | 17 | unsigned long part_1(void) 18 | { 19 | int one = 0; 20 | int three = 0; 21 | for (int i = 1; i < xs_size; ++i) { 22 | int d = xs[i] - xs[i - 1]; 23 | switch (d) { 24 | case 1: 25 | one += 1; 26 | break; 27 | case 3: 28 | three += 1; 29 | break; 30 | default: 31 | assert(0 && "unreachable"); 32 | } 33 | } 34 | 35 | return one * three; 36 | } 37 | 38 | unsigned long part_2(void) 39 | { 40 | dp[0] = 1; 41 | for (int i = 1; i < xs_size; ++i) { 42 | dp[i] = 0; 43 | for (int j = i - 1; j >= 0 && xs[i] - xs[j] <= 3; --j) { 44 | dp[i] += dp[j]; 45 | } 46 | } 47 | 48 | return dp[xs_size - 1]; 49 | } 50 | 51 | void solve_file(const char *file_path) 52 | { 53 | printf("Input file: %s\n", file_path); 54 | 55 | xs_size = 0; 56 | 57 | FILE *f = fopen(file_path, "r"); 58 | xs[xs_size++] = 0; 59 | while (!feof(f)) { 60 | unsigned long x = 0; 61 | int n = fscanf(f, "%lu", &x); 62 | if (n == 1) { 63 | xs[xs_size++] = x; 64 | } 65 | } 66 | fclose(f); 67 | qsort(xs, xs_size, sizeof(xs[0]), compare_int); 68 | xs[xs_size] = xs[xs_size - 1] + 3; 69 | xs_size += 1; 70 | 71 | printf("Part 1: %lu\n", part_1()); 72 | printf("Part 2: %lu\n", part_2()); 73 | } 74 | 75 | int main(int argc, char *argv[]) 76 | { 77 | for (int i = 1; i < argc; ++i) { 78 | solve_file(argv[i]); 79 | } 80 | 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /10-c/sample-01.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 10 3 | 15 4 | 5 5 | 1 6 | 11 7 | 7 8 | 19 9 | 6 10 | 12 11 | 4 12 | -------------------------------------------------------------------------------- /10-c/sample-02.txt: -------------------------------------------------------------------------------- 1 | 28 2 | 33 3 | 18 4 | 42 5 | 31 6 | 14 7 | 46 8 | 20 9 | 48 10 | 47 11 | 24 12 | 23 13 | 49 14 | 45 15 | 19 16 | 38 17 | 39 18 | 11 19 | 1 20 | 32 21 | 25 22 | 35 23 | 8 24 | 17 25 | 7 26 | 9 27 | 4 28 | 2 29 | 34 30 | 10 31 | 3 32 | -------------------------------------------------------------------------------- /11-nim/.gitignore: -------------------------------------------------------------------------------- 1 | main -------------------------------------------------------------------------------- /11-nim/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=sample.txt input.txt 2 | 3 | .PHONY: test 4 | test: main $(INPUTS) 5 | ./main $(INPUTS) 6 | 7 | main: main.nim 8 | nim c main.nim 9 | -------------------------------------------------------------------------------- /11-nim/README.md: -------------------------------------------------------------------------------- 1 | # [Day 11](https://adventofcode.com/2020/day/11) solution in [Nim](https://nim-lang.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/lHzbWWjdloM/hqdefault.jpg)](https://www.youtube.com/watch?v=lHzbWWjdloM&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=4680s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ nim --version 11 | Nim Compiler Version 1.0.4 [Linux: amd64] 12 | Compiled at 2019-11-27 13 | Copyright (c) 2006-2019 by Andreas Rumpf 14 | 15 | active boot switches: -d:release 16 | ``` 17 | 18 | ## Expected Result 19 | 20 | ```console 21 | Input file: input.txt 22 | Part 1: 2093 23 | Part 2: 1862 24 | ``` 25 | 26 | ## Quick Start 27 | 28 | - install [Nim](https://nim-lang.org/install.html) and make sure `nim` is available in `$PATH` 29 | - `$ make` 30 | -------------------------------------------------------------------------------- /11-nim/input.txt: -------------------------------------------------------------------------------- 1 | LLLLL.LLLL.LL..LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL.L.LLLLLLLLL.LLLLLLL.LLLL.LLLLLLLL 2 | LLLLLLLL.LLLLL.LLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLL.LLLLLLLLLLLLLLL.LL.LLL.LLLL.LLLLLLLL 3 | LLLLLLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLLLLL..LLLLLL.LLLLLLLLLL..LLLLLLLL.LLLLLLLLL.LLLLLLLLLLL 4 | LLLLL.LLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLLLL..LLLLL.LLLLL.LLLLLLLLLLLLLLL.L.LLLLLLLLL.LLL.LLLL 5 | LLLLL.LLL.L..L.LLLLL.LLL.LLLLLLLLLLLLLLL.LL.LLLLLLL.L.LLL..LLLLLLLLL.LLLLLLL.LLLL.LLLLLLLL 6 | LLLLL.LLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLL.LLLLLLLLLL.LLLLLL.LL.LLLLLLLLLLLL.LLLL.LLL 7 | LLLLL.LLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLL.L.LLLLLLLLLLLLL 8 | L.L..LLL.......LL.LL.L.L....L..............L..LL.......L...L....LL....L.L..LL.....L.L..... 9 | LLLLL.LLLLLLL.LLLLLLLLLL.LLLLLL.LLLLL..L.LLLLLL.LLLLL.LLLL.LLLL.LLLL.LLLLLLL.LLLL.L.LLLLLL 10 | LLLLL.LLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.L.LLL.LLLL.LLLLLLLLL.LLLLLLL.LLLLL.LL.LLLL 11 | LLLLL.LLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLL.LLLL.LLLLLLLLLLLL..LLLLLLLL.LLLLLLL.LLLL.LLLLLLLL 12 | LLLLLLLLLLLLLLLL.LLLLLLL.LLLL.L.LLLLLLLLLLLLLLL.LLLLLLLLLL.LLLLLLLLLL.LLLLL.LLLLL.LL.LLLLL 13 | L.LLL.LLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLL.L.LL.L.LLL.LLLL.LLLLLLLLLLLLLLLLL.L.LL.LLLLLLLL 14 | LLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLL.LLL.LLLLLL.LLLLL.LLLL.LLLLLLLLL.LLLLLLL.LLLL.LLLLLLLL 15 | LLLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLL.LL.LL..LLLLL.LLLL.LLLLL.LLL.L.LLL.L.LLLL.L.LLLLLL 16 | LLLLL.LLLLLLLL.LLLLLLLLL.LLLL.LLLLLLLLLL.LLLLLL.LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLL.LLLLLLLL 17 | .LLL..LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLL.LLLL.L.LLLLL.L.LLLLLLLLLLLL.LLLLLLLL 18 | L.L.L.....LLL...L...L..LL...L.LL......LLLL.L.LL..LLL.L..L.......L..L.L....LLL.....L.L...L. 19 | LLLLL.LLLL.LLLLLLLLLLLLL.LLLLLL.LL.LLLLL.LLLLLLL.LLLL.LLLL.LLLL.LLL.LLLLLLLL.LLLLLLLLLLLLL 20 | LLLLL.L.LLLLLLL.LL.LLLLL.L.LLLLLLLLLLLLL.LLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLL..LLL.LLLLLLLL 21 | LLLLL.LLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLL.LLLLL.L..L.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLL. 22 | LLLLL.LLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLL.L.L.LLL.LLLL.LLLLL.LLL.LLLLLLLLLLLL.LLLLLLLL 23 | LLL.L.LLLLLL.L.LL.LLLLLL.LLLLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLL.L 24 | LL.LL.L.LLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLL.LLLLL.LLLL.LLLLLLLLL.L.LLLLLLLLLL.LLLLLLL. 25 | LLLLL.LLLL.LLLLLLLL.LL.L.LLLLLLLLLLLLLLL.LL.LLL.LLLLL.LLLL.LLLLLLLLL.LLLLLLL.LLLL.LL.LLLLL 26 | LL.........LLL..LL.L...L...L.....L...LL.....L..LLL........LL.L.LL.L......L...........L.LL. 27 | LLLLLLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLL..LLLLL.LLLLL.LLLL.LLLLLLLLL.LLLLLLL.LLLL.LLLLLL.L 28 | LLLLLLLLLLLLLL.LLLLLLLL..LLLLLL.LLLLL.LL.LLLLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLLLLLL 29 | LLLL..LLL.LLLLLLLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLL.LLLL.LLLLLLLLL.L.LLLLL.LLL..LLLLLLLL 30 | LLLLLLLLLLLL.L.LLLLLLLLL.LLLL.L.LL.LLLLL.LLLLLL.LLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLL 31 | .LLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLL.L.LLLL.LLLLL.LLLL.LLLLL.LLLLLLLLLLLLLLLL.LLLLLLLL 32 | LLLLL.LL.LLLLL.LLLL.LLLL.LLL.LL.LLLLLLLLLLLLLLLLLLLLL.LLLL.LLLL.LLLL.LLLLLLL.LLLL.LLLLLLLL 33 | LLLLL.LLLLLLLLLLLLLLLLLL.LLLLLLLLLL.LL.L.LLLLLL.LL.LL.LLLL.LLLLLLLLLLLLLLLLL.LLLLL.LLLLLL. 34 | LLLLL.LLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLL.L.LL...LLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLL 35 | ...LL....L.L.L........L.L..LL..LL.L..L...LLL..L.LL.LL.....L.....LL.L..LLL.LL.LL..LL...L... 36 | LLLLLLLLLLLLLL.LLLLLLLLL.LLL.LL.LLLLLLLL.LLLLLLLLLLLLLLLL..LLLLLLLLLLLLLLLL..LLLL.LLLLLLLL 37 | LLLLL..LLLLLLL.LLLLLLL.LL..LLLL.LLLLLLLLLLLL.LL.LLLLL.LLLL.LLLLLL.LLLLLLLLLL.LLLLLLLLLLLLL 38 | LLLLLLLLLLLLL.LLLLLLLLL..LLLLLL.LLLLLLLLLLLL.LL.LLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLL.LLLLLLLL 39 | LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLL.LLLLLLLLL.LLLLLLL.L.LL.LL.LLLLL 40 | LLLLL.LLLLL.LLLLLLLLLLLL.LLLL.L.LLLLLLLL.LLLLLL.LL.LL.LLLLLLLLLLLLL...LLLLLL.LLLLLLLLLLLLL 41 | LLLLLL.LLLLL.L.LLL.LLLLL.LLLL.L.LLLLL.LL.LLLLLL..LLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLL.LLLLLLL. 42 | LLLLL.LLLLLLL..LLLLLLLLL..LLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLL.LLLLLLLLL.LLLLLLL.L.LLLLLLLLLLL 43 | LLLLL.LLLLLLLL.LLLLLLLLL..LLLL..LLLLLLLL.LLLLLL.LL.LL.LLLLLL.LLLL.L..LLLLLLL.LL.L.LLLLLLLL 44 | .LL..L....L..LLL.L......LL..L.L.....LLL.....L.LLL.....L...L.......L...LLL...LL...L........ 45 | LLLLLLLLLLLLLLLLLLL.LLLL.LLLLLL.LLLL.LLL.LLLLLL.LLLLL.LLLL.L.LLLLLLL.LLLLLLL.LLL..LLLLLLLL 46 | LLLLL.LLLL.LLL.LLLLLLLLL.LLLLLL.LLLLLLLL.LL.LLLLLLLLL.LLLL..LLL.LLLL.LLLLLLL.LLLL.LLLLLLLL 47 | LL.LL.LLLLLLLL.LLLLLLLLL.LLLLLL..LLLLL.L.LLLLLL..LLLL.LLLL.LL.LLLL.LLLLLLLLLLLLLL.LLLLLLL. 48 | LLLLL.LLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLL.LL.L.LLLLLLLLLLLLLLLLL.LLLLLLLLLLLLL 49 | L...L.L....L..L...LLL.L..LLLLL.........LL.....LL...L....L..............L.L.L...LL.LL...LL. 50 | LLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL...LLLLLLLLLLL.LLLL.LL.LLLLL..LLLLLLL.LLLL.LLLLLLLL 51 | LLLLL.LL.LLLLL.LLLLLLLLL.LLLLLL..LLLLLLLLLLL.LL.LLLLL.LLLL.LLLLLLLLLLLLLLLLL.LLLL.LL.LLLLL 52 | LLLLL.LLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL 53 | LLLLLLLLLL.LLL.LL.LLLLLL.LLLLLLLLLLLLLLL.L.LLLL.LLLLL.LLLL.LLLLLLLLLLLLLLLLL.LLLL.L.LLLLLL 54 | LLL.L.LLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLL.LLL.LLLL 55 | LLLLL.LLLLLLL..LLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLL.LLLLL.LL.L.LLLLLLLLLLLLLL.LLLLLLLLLLLLLL.L 56 | ...LL.......LLLL.L..LL..L.LL.L.L..L.........L.L..LLL............L.L.LL..L.LL.L..LL........ 57 | LLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLLL.LLLL.LLL.LLLLL.LLLLLLLLLLLL.LLLLLLLL 58 | L.LLLLLLLLL.LLLLLLLLLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLL.LLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLLLL.L 59 | LLLLL.LLLLLLLL.LLLL.LLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLL.LLLLLLL.LLLL.LLLLLLLL 60 | LLLLL.LLLLLLLL.LLLLLL.LL.LLLLLL.LLLLLLLLLLLLLLL.LLLLL.LLLL.LLLLLLLLL.LLLLLLL.LLLL.LLLLLLLL 61 | LLLLL.LLLL.LLL..LLLLLLLLLLLLLLL.LLLLLLLL.LLLLLL.LLLL..LLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL 62 | LLLLL.LLLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLLLLLLL.LLLLL.LLLL.L.LLLLLLLLL.LLLLL.LLLLLLLLLLL.L 63 | LLLLL.LLLLLLLLLLLLLLLLL..LLLLLLLLLLL.LLL.LLLLLL.LLL.LLLLLL.LLLLLLLLLL.LLLLLL.LLLL.LL.LLLLL 64 | ..L..L....LL.L............L....LL.........LLLL.L.L........L..L...LLL.....L.......LL..L..LL 65 | LLLLL.L.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLL.LLLLL.LLLL.LLLLLLLLL.LLLLLLL.LLLL.LLLLLLLL 66 | LLLLLLLLLLLL.LLLLLLLLL.L.LLLLLL.LLLLLLLL.L.LLLL.LLLLL.LLLL..LLLLLLLL.LLLLLLL.LLLLLLLLLLLLL 67 | LLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLL.LLLLL..L.LLL..LLLLLLLLLLLLL.LLLLLLL.LLLL.LLLLLLLL 68 | LLLLL.LLLLLLLLLLLLLLLLLL.LLLLLL..LLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLL.L.LLLLLLLLLLLLLLLLLLL 69 | .L..L...L.....LL...........L.LL..LL...LL.LLL..........L.L.LL..L.LLL...L....LL.LL....LLL.LL 70 | LLLLL.L.LLLL.LLLLLLLLLLLLLLLLLL.LL.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLLL.LLLLL 71 | LLLLL.LLLLLLLLLLL.L.LLLLLLLLLLLL.LLLLLLL.LLLLL..L.LLL.LLLLLLL.LLLLLL.LLLLLLL..LLL.LLLLLLL. 72 | LLLLLLLLLLLLLL.L.LLLLLLL.LLLLLLLLLL.LLLL.LLLLLL.LLLLL.LLLLLLLLLLLL.LLLLLLLLLLL.LLL.LLLLLLL 73 | LLLLL.LL.LLLLLLLLLLLLLLL.LLLLLL.LLLLL.LL.LLLLLL.LLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLL.LLLLLLLL 74 | LL..L.LLLLLLLL.LLLLLLL.L.LLLL.LLLLL.LLLLLLLLLLLL.LLL..LLLL.LLLLLLLL..LLLL.L..LLLLLLLLLLL.L 75 | LLLLLLLLLLLLLLLLLLLLLLLL.LLL.LL.LLLL.LLL.LLLLLL.LLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLL.LLLLLLLL 76 | LLLLLLLLLLLLLLLLLLLLLLLL.LLLL.L.LLLLLLLLLLLLLLL.L.LLLLLLLL..LLLLLLLL.LLLLLLLLLLLLLLLLLLLLL 77 | LLLLL.LLLLLLLL.L.LLLLLLL.LLLLLL..LLL.LLL.LLLLLL.LLLLL.LLLL.LLLLLLLLLLLLLLLLL.LLLLLLLLLLLLL 78 | .L.L.L.....LL.LL.LL...LL.L..LL........LL.......L..LLLL..L.L.LL....L...L....L.L.L...L..L... 79 | LLLLL..LLLLLLL.LLLLLLLLL.LLLLLL.LLLLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLL..LLLLLLL.LLLL.LLLLLLLL 80 | LLLLL.LLLL.LLL.LLLLLLLLL.LLLLLLLLLLLLLL..LLLLLLLLLLLL.LLLLLLLLLLLLLL.LLLLLLLLLLLL.LLL.LLLL 81 | .LLLLLLLLLLLLL..LLLLLLLLLLLLLLL.LLLLLLLL.LLLLLL.L.LLL.LLLLLLLLLLLLLLLLLLLLLL.LLLL.LLLLLLLL 82 | LLLLL.LLLLLLLL.LLLLLLLLL.LLLLL..LL.LLLL.LLLLLLL.LL.LL.LLLL.LLLLLLLLLLLLLL..L.L..L.LLLLLLLL 83 | LLLLLLLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLL.L..LLLLLLLLLLLL.LLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLLLLLL 84 | LLLLL.LLLL.LLLL.LLL.LLLL.LLLLLL.LLLLLLLL.LLLLLL.LLLLL.LLLLLLLLLLLLLL..LLLLLL.LLLLLLLLLLLLL 85 | LLLLLLLLLLLLLL..LLLL.LLL.LLLLLL.LLLLLLLL.LLLLLL.LLLLL..LLL.LLLLLLLLL.LLLLLLLLL.L..LLLLLLLL 86 | ..LLL..L..L.LL..LLL....L..L...L.L.L.L.L.L.L.LLL..LLL..L...L.......LLL..L.....LL...LL....L. 87 | LLLLL.LLLLLLLL.LLLLLLLLL.LLLLLLLLLLLL.LLLLL.LLLLLLLLL.LLLL.LLLLLLLLL.LLLL.LLLLLLL.LLLLLLLL 88 | LLLLL.LLLLLLLLLLLLLL.LLL.LLLLLL.LLLLLLLL.LLLLLLLLL.LL.LLLL.LLLLLLL.L.LLLLLLLLLLLL.LLLLLLLL 89 | LLLLL.LLLL.LLL.LLLLLLLLL.LLLLLLLLLLLLLLL..LLLLL.LLLLL.LLLL..LL..LLLL.LLLLLLL.LLLL.LLLLLLLL 90 | LLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLLLL..LLLL.LL..LLLLLLL.LLLLLLLLLLLLL 91 | LLLLLLLLLLLLLL.LL.LLLLLL.LLLL.L..LLLLLLL.LLL.LL.LLLLLLLLLL.LLLLLLLLL.L.LLLLL.LLLL.LLLLLL.L 92 | -------------------------------------------------------------------------------- /11-nim/main.nim: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | type NborsCounter = proc(board: seq[string], row0, col0: int): int 4 | 5 | proc countNborsV1(board: seq[string], row0, col0: int): int = 6 | let h = board.len 7 | let w = board[0].len 8 | for drow in -1..1: 9 | for dcol in -1..1: 10 | if drow != 0 or dcol != 0: 11 | let row = row0 + drow 12 | let col = col0 + dcol 13 | if (0 <= row and row < h) and (0 <= col and col < w): 14 | if board[row][col] == '#': 15 | result += 1 16 | 17 | proc countNborsV2(board: seq[string], row0, col0: int): int = 18 | let h = board.len 19 | let w = board[0].len 20 | for drow in -1..1: 21 | for dcol in -1..1: 22 | if drow != 0 or dcol != 0: 23 | var row = row0 + drow 24 | var col = col0 + dcol 25 | while (0 <= row and row < h) and (0 <= col and col < w): 26 | case board[row][col] 27 | of '#': 28 | result += 1 29 | break 30 | of 'L': 31 | break 32 | else: 33 | row += drow 34 | col += dcol 35 | 36 | proc nextRound(board: seq[string], 37 | tolerance: int, 38 | countNbors: NborsCounter): seq[string] = 39 | for row in 0..board.len - 1: 40 | result.add(board[row]) 41 | for col in 0..board[row].len - 1: 42 | let nbors = countNbors(board, row, col) 43 | case board[row][col]: 44 | of 'L': 45 | if nbors == 0: 46 | result[row][col] = '#' 47 | of '#': 48 | if nbors >= tolerance: 49 | result[row][col] = 'L' 50 | else: 51 | discard 52 | 53 | proc solveBoard(board: seq[string], 54 | tolerance: int, 55 | countNbors: NborsCounter): int = 56 | var input = board 57 | var input1 = nextRound(input, tolerance, countNbors) 58 | while (input1 != input): 59 | input = input1 60 | input1 = nextRound(input, tolerance, countNbors) 61 | 62 | for row in input: 63 | for seat in row: 64 | if seat == '#': 65 | result += 1 66 | 67 | proc part1(board: seq[string]): int = 68 | result = solveBoard(board, 4, countNborsV1) 69 | 70 | proc part2(board: seq[string]): int = 71 | result = solveBoard(board, 5, countNborsV2) 72 | 73 | proc solveFile(filePath: string) = 74 | var board: seq[string] = @[] 75 | for line in filePath.lines: 76 | board.add(line) 77 | 78 | echo "Input file: ", filePath 79 | echo "Part 1: ", part1(board) 80 | echo "Part 2: ", part2(board) 81 | 82 | proc main() = 83 | for i in 1..paramCount(): 84 | solveFile(paramStr(i)) 85 | 86 | main() 87 | -------------------------------------------------------------------------------- /11-nim/sample.txt: -------------------------------------------------------------------------------- 1 | L.LL.LL.LL 2 | LLLLLLL.LL 3 | L.L.L..L.. 4 | LLLL.LL.LL 5 | L.LL.LL.LL 6 | L.LLLLL.LL 7 | ..L.L..... 8 | LLLLLLLLLL 9 | L.LLLLLL.L 10 | L.LLLLL.LL 11 | -------------------------------------------------------------------------------- /12-perl/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=sample.txt input.txt 2 | 3 | .PHONY: test 4 | test: main.pl $(INPUTS) 5 | ./main.pl $(INPUTS) 6 | -------------------------------------------------------------------------------- /12-perl/README.md: -------------------------------------------------------------------------------- 1 | # [Day 12](https://adventofcode.com/2020/day/12) solution in [Perl](https://www.perl.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/R00JE6QRbno/hqdefault.jpg)](https://www.youtube.com/watch?v=R00JE6QRbno&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=742s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ perl --version 11 | 12 | This is perl 5, version 28, subversion 1 (v5.28.1) built for x86_64-linux-gnu-thread-multi 13 | (with 65 registered patches, see perl -V for more detail) 14 | 15 | Copyright 1987-2018, Larry Wall 16 | 17 | Perl may be copied only under the terms of either the Artistic License or the 18 | GNU General Public License, which may be found in the Perl 5 source kit. 19 | 20 | Complete documentation for Perl, including FAQ lists, should be found on 21 | this system using "man perl" or "perldoc perl". If you have access to the 22 | Internet, point your browser at http://www.perl.org/, the Perl Home Page. 23 | ``` 24 | 25 | ## Expected Result 26 | 27 | ```console 28 | Input file: input.txt 29 | Part 1: 1148 30 | Part 2: 52203 31 | ``` 32 | 33 | ## Quick Start 34 | 35 | - install [Perl](https://www.perl.org/get.html) and make sure `perl` is available in `$PATH` 36 | - `$ make` 37 | -------------------------------------------------------------------------------- /12-perl/input.txt: -------------------------------------------------------------------------------- 1 | N3 2 | L90 3 | F63 4 | W5 5 | F46 6 | E3 7 | F22 8 | N2 9 | R90 10 | F68 11 | E4 12 | W3 13 | R90 14 | S3 15 | W4 16 | R180 17 | E1 18 | S5 19 | F90 20 | N4 21 | E3 22 | N1 23 | R90 24 | F74 25 | R90 26 | E2 27 | R90 28 | W1 29 | S3 30 | W4 31 | F5 32 | S1 33 | E5 34 | S1 35 | E4 36 | R90 37 | E5 38 | L90 39 | E4 40 | R90 41 | E2 42 | F57 43 | N1 44 | L90 45 | F59 46 | R90 47 | N1 48 | W3 49 | S2 50 | L90 51 | N3 52 | E1 53 | F56 54 | L180 55 | S3 56 | R90 57 | F88 58 | E3 59 | F59 60 | W1 61 | N2 62 | F52 63 | W4 64 | F69 65 | W2 66 | F10 67 | W1 68 | R180 69 | W1 70 | R90 71 | F14 72 | L90 73 | W1 74 | S5 75 | L90 76 | S3 77 | R90 78 | E3 79 | F35 80 | R90 81 | E3 82 | S3 83 | F45 84 | E2 85 | R90 86 | F86 87 | E1 88 | E4 89 | F35 90 | L180 91 | S1 92 | L90 93 | N2 94 | F71 95 | L180 96 | W3 97 | S4 98 | R90 99 | N5 100 | F93 101 | W4 102 | F74 103 | L180 104 | E2 105 | R180 106 | F11 107 | S5 108 | F28 109 | S3 110 | F93 111 | W2 112 | N4 113 | F26 114 | R90 115 | S4 116 | L90 117 | N1 118 | L90 119 | E2 120 | L90 121 | F3 122 | E4 123 | F43 124 | R90 125 | W4 126 | R90 127 | E3 128 | S1 129 | R180 130 | L90 131 | F62 132 | L90 133 | E5 134 | R90 135 | W3 136 | L180 137 | F40 138 | F20 139 | N2 140 | L270 141 | E1 142 | F14 143 | W3 144 | S5 145 | R90 146 | F3 147 | S2 148 | L90 149 | W5 150 | L270 151 | W1 152 | R90 153 | F11 154 | R90 155 | E3 156 | N1 157 | E3 158 | F19 159 | S5 160 | L180 161 | N4 162 | E2 163 | R180 164 | E5 165 | S2 166 | W4 167 | S3 168 | W1 169 | F4 170 | L90 171 | S2 172 | W4 173 | S5 174 | F21 175 | L180 176 | W4 177 | S3 178 | L90 179 | S4 180 | L90 181 | E1 182 | F28 183 | L180 184 | S3 185 | E2 186 | N3 187 | L180 188 | W3 189 | L90 190 | F99 191 | S2 192 | F63 193 | E2 194 | N3 195 | R90 196 | E3 197 | L90 198 | E5 199 | L90 200 | N4 201 | F39 202 | R180 203 | S3 204 | R90 205 | N3 206 | F7 207 | E3 208 | S2 209 | E2 210 | F98 211 | S1 212 | F87 213 | E1 214 | S3 215 | F49 216 | N1 217 | W2 218 | F4 219 | L270 220 | F91 221 | L90 222 | E1 223 | S4 224 | R180 225 | F43 226 | S3 227 | E3 228 | R90 229 | F46 230 | W2 231 | R90 232 | W5 233 | F13 234 | R180 235 | F52 236 | N4 237 | F28 238 | N3 239 | R90 240 | E5 241 | S3 242 | F82 243 | R90 244 | W3 245 | L90 246 | F33 247 | S5 248 | R90 249 | R90 250 | S5 251 | F24 252 | R90 253 | N4 254 | F89 255 | W1 256 | S4 257 | F80 258 | W3 259 | L270 260 | F11 261 | L90 262 | W2 263 | N3 264 | F18 265 | R90 266 | W2 267 | R90 268 | E1 269 | R270 270 | N3 271 | R180 272 | S4 273 | F36 274 | S3 275 | L90 276 | N2 277 | L90 278 | N2 279 | E1 280 | F48 281 | E5 282 | L180 283 | S3 284 | F81 285 | E4 286 | L90 287 | W3 288 | F31 289 | E5 290 | R90 291 | F66 292 | S4 293 | W3 294 | L90 295 | E3 296 | N4 297 | F85 298 | L90 299 | F58 300 | E5 301 | L90 302 | S1 303 | W3 304 | F79 305 | S4 306 | F60 307 | N2 308 | F42 309 | S3 310 | W3 311 | R90 312 | E1 313 | N1 314 | L90 315 | F15 316 | E4 317 | F98 318 | L90 319 | R90 320 | S4 321 | E1 322 | F19 323 | E2 324 | S4 325 | R90 326 | W2 327 | L180 328 | N3 329 | E2 330 | S3 331 | F34 332 | S4 333 | S4 334 | L180 335 | S1 336 | R90 337 | S4 338 | S1 339 | L90 340 | E3 341 | F28 342 | R90 343 | W1 344 | N2 345 | E5 346 | F48 347 | E4 348 | S1 349 | W2 350 | F95 351 | W2 352 | N2 353 | L90 354 | E2 355 | L90 356 | W3 357 | S2 358 | L270 359 | W4 360 | L90 361 | N4 362 | R90 363 | E4 364 | R270 365 | W4 366 | F6 367 | W2 368 | N1 369 | E1 370 | F19 371 | W2 372 | N1 373 | F54 374 | W2 375 | L90 376 | S1 377 | L90 378 | F80 379 | E1 380 | S5 381 | E5 382 | F80 383 | R90 384 | L270 385 | E4 386 | F93 387 | N4 388 | E5 389 | S1 390 | E1 391 | R90 392 | F63 393 | N3 394 | R90 395 | E1 396 | N2 397 | L90 398 | W5 399 | R90 400 | R270 401 | N1 402 | E4 403 | L180 404 | E4 405 | F19 406 | L90 407 | F27 408 | W2 409 | S2 410 | W5 411 | S1 412 | F54 413 | S4 414 | R90 415 | F85 416 | W2 417 | F13 418 | R90 419 | F73 420 | S5 421 | E2 422 | S2 423 | F12 424 | W5 425 | F23 426 | N1 427 | E1 428 | F38 429 | N2 430 | W2 431 | N3 432 | E2 433 | L270 434 | F7 435 | L90 436 | S3 437 | L90 438 | S3 439 | F86 440 | E5 441 | R90 442 | E1 443 | F52 444 | L180 445 | S4 446 | L180 447 | W4 448 | F41 449 | R90 450 | E3 451 | F70 452 | R270 453 | N3 454 | F32 455 | S2 456 | E5 457 | R180 458 | F20 459 | W3 460 | F54 461 | E2 462 | F34 463 | F61 464 | S5 465 | W1 466 | L90 467 | S5 468 | N5 469 | W2 470 | R180 471 | W2 472 | L90 473 | E5 474 | S4 475 | L90 476 | S4 477 | L180 478 | F84 479 | S1 480 | W1 481 | L90 482 | F92 483 | F46 484 | N1 485 | F22 486 | F24 487 | L90 488 | N5 489 | W4 490 | R270 491 | F79 492 | N1 493 | W1 494 | F68 495 | R90 496 | W5 497 | R180 498 | N5 499 | L90 500 | L180 501 | S1 502 | W4 503 | N1 504 | L180 505 | S1 506 | N4 507 | E4 508 | R90 509 | E1 510 | E4 511 | F58 512 | S4 513 | E5 514 | F49 515 | N1 516 | E2 517 | S4 518 | L90 519 | W2 520 | F67 521 | E2 522 | N5 523 | W1 524 | L90 525 | E5 526 | F82 527 | N5 528 | F91 529 | W5 530 | R90 531 | F17 532 | W5 533 | S2 534 | R90 535 | N2 536 | R90 537 | N5 538 | E4 539 | L90 540 | N1 541 | F26 542 | N3 543 | E3 544 | F19 545 | L270 546 | R90 547 | E3 548 | F21 549 | L180 550 | S4 551 | F50 552 | S4 553 | W2 554 | F56 555 | F49 556 | N2 557 | E3 558 | R180 559 | E4 560 | F5 561 | F17 562 | E2 563 | R90 564 | N3 565 | F96 566 | L180 567 | E4 568 | F64 569 | W5 570 | R90 571 | W5 572 | S5 573 | F92 574 | E5 575 | F10 576 | N1 577 | W1 578 | F94 579 | R90 580 | W4 581 | F22 582 | S1 583 | W4 584 | F38 585 | W1 586 | F17 587 | E3 588 | L90 589 | F3 590 | S1 591 | L90 592 | F27 593 | W4 594 | F31 595 | S5 596 | W4 597 | N2 598 | E5 599 | F44 600 | W2 601 | E4 602 | F54 603 | L180 604 | E5 605 | L90 606 | N1 607 | E5 608 | N4 609 | L180 610 | L270 611 | W3 612 | F80 613 | S2 614 | F49 615 | E4 616 | F46 617 | E2 618 | E5 619 | L270 620 | F12 621 | F63 622 | L90 623 | N2 624 | E5 625 | N3 626 | F85 627 | R270 628 | S3 629 | F71 630 | N4 631 | E5 632 | F36 633 | N5 634 | F23 635 | L90 636 | N2 637 | E3 638 | F93 639 | S5 640 | F1 641 | S2 642 | F29 643 | L90 644 | F17 645 | R180 646 | S4 647 | R90 648 | E2 649 | S3 650 | W5 651 | R90 652 | S3 653 | R90 654 | W4 655 | F62 656 | L180 657 | S4 658 | L90 659 | N2 660 | F46 661 | N3 662 | R180 663 | E1 664 | R90 665 | F73 666 | S5 667 | F12 668 | L180 669 | F47 670 | L90 671 | F79 672 | N4 673 | R270 674 | W3 675 | N1 676 | W1 677 | N3 678 | F63 679 | S2 680 | F50 681 | R90 682 | F30 683 | N3 684 | F7 685 | N4 686 | L90 687 | S4 688 | N1 689 | E5 690 | S5 691 | F9 692 | L90 693 | L90 694 | F7 695 | N1 696 | R90 697 | F52 698 | E3 699 | L90 700 | N3 701 | F50 702 | L90 703 | F83 704 | E3 705 | F74 706 | L90 707 | N1 708 | L90 709 | F4 710 | N1 711 | F28 712 | E4 713 | F9 714 | E4 715 | S2 716 | W4 717 | L270 718 | S1 719 | W4 720 | F23 721 | E1 722 | F52 723 | E1 724 | L180 725 | E2 726 | N5 727 | L90 728 | W5 729 | L90 730 | S1 731 | E3 732 | R90 733 | E4 734 | L90 735 | S1 736 | W2 737 | N4 738 | W1 739 | S4 740 | E2 741 | L90 742 | E5 743 | S2 744 | L180 745 | F91 746 | N5 747 | W4 748 | N5 749 | F14 750 | S5 751 | R90 752 | S5 753 | L90 754 | F78 755 | N2 756 | W3 757 | R90 758 | F17 759 | N5 760 | W1 761 | F53 762 | W2 763 | F33 764 | R90 765 | E2 766 | F15 767 | L90 768 | E5 769 | F77 770 | L90 771 | S1 772 | F33 773 | -------------------------------------------------------------------------------- /12-perl/main.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | 3 | use strict; 4 | use warnings; 5 | 6 | sub part_1($) { 7 | my ($file_path) = @_; 8 | 9 | my @dirs = ( 10 | [1, 0], # east 11 | [0, 1], # north 12 | [-1, 0], # west 13 | [0, -1] # south 14 | ); 15 | 16 | my %names = ( 17 | 'E' => 0, 18 | 'N' => 1, 19 | 'W' => 2, 20 | 'S' => 3 21 | ); 22 | 23 | my %turns = ( 24 | 'L' => 1, 25 | 'R' => -1 26 | ); 27 | 28 | my $x = 0; 29 | my $y = 0; 30 | my $d = 0; 31 | 32 | open(FH, '<', $file_path) or die $!; 33 | while () { 34 | if (m/([NSEWLRF])([0-9]+)/) { 35 | my $cmd = $1; 36 | my $arg = int($2); 37 | 38 | if ($cmd =~ m/[NSEW]/) { 39 | my $dx = @{$dirs[$names{$cmd}]}[0]; 40 | my $dy = @{$dirs[$names{$cmd}]}[1]; 41 | $x += $dx * $arg; 42 | $y += $dy * $arg; 43 | } elsif ($cmd =~ m/[LR]/) { 44 | $d = ($d + $turns{$cmd} * ($arg / 90)) % 4; 45 | } elsif ($cmd =~ m/[F]/) { 46 | my $dx = @{$dirs[$d]}[0]; 47 | my $dy = @{$dirs[$d]}[1]; 48 | $x += $dx * $arg; 49 | $y += $dy * $arg; 50 | } 51 | } else { 52 | die "Invalid command $_\n"; 53 | } 54 | } 55 | close(FH); 56 | 57 | return abs($x) + abs($y); 58 | } 59 | 60 | sub part_2($) { 61 | my ($file_path) = @_; 62 | 63 | my %names = ( 64 | 'E' => [1, 0], 65 | 'N' => [0, 1], 66 | 'W' => [-1, 0], 67 | 'S' => [0, -1] 68 | ); 69 | 70 | my $ship_x = 0; 71 | my $ship_y = 0; 72 | my $wp_x = 10; 73 | my $wp_y = 1; 74 | 75 | my @sin = (0, 1, 0, -1,); 76 | my @cos = (1, 0, -1, 0,); 77 | 78 | open(FH, '<', $file_path) or die $!; 79 | # print("ship_x: $ship_x, ship_y: $ship_y, wp_x: $wp_x, wp_y: $wp_y\n"); 80 | while () { 81 | if (m/([NSEWLRF])([0-9]+)/) { 82 | my $cmd = $1; 83 | my $arg = int($2); 84 | if ($cmd =~ m/[NSEW]/) { 85 | $wp_x += $names{$cmd}->[0] * $arg; 86 | $wp_y += $names{$cmd}->[1] * $arg; 87 | } elsif ($cmd =~ m/[LR]/) { 88 | if ($cmd eq 'R') { 89 | $arg = -$arg; 90 | } 91 | 92 | my $angle = $arg / 90; 93 | my $new_wp_x = $cos[$angle] * $wp_x - $sin[$angle] * $wp_y; 94 | my $new_wp_y = $sin[$angle] * $wp_x + $cos[$angle] * $wp_y; 95 | $wp_x = $new_wp_x; 96 | $wp_y = $new_wp_y; 97 | } elsif ($cmd =~ m/[F]/) { 98 | $ship_x += $wp_x * $arg; 99 | $ship_y += $wp_y * $arg; 100 | } 101 | } 102 | # print("ship_x: $ship_x, ship_y: $ship_y, wp_x: $wp_x, wp_y: $wp_y\n"); 103 | } 104 | close(FH); 105 | 106 | return abs($ship_x) + abs($ship_y); 107 | } 108 | 109 | sub solve_file($) { 110 | my ($file_path) = @_; 111 | print("Input file: $file_path\n"); 112 | printf("Part 1: %d\n", part_1($file_path)); 113 | printf("Part 2: %d\n", part_2($file_path)); 114 | } 115 | 116 | for (@ARGV) { 117 | solve_file($_) 118 | } 119 | -------------------------------------------------------------------------------- /12-perl/sample.txt: -------------------------------------------------------------------------------- 1 | F10 2 | N3 3 | F7 4 | R90 5 | F11 6 | -------------------------------------------------------------------------------- /13-csharp/.gitignore: -------------------------------------------------------------------------------- 1 | *.exe -------------------------------------------------------------------------------- /13-csharp/Main.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | 5 | class Bus { 6 | public ulong Id; 7 | public ulong Offset; 8 | public Bus(ulong Id, ulong Offset) { 9 | this.Id = Id; 10 | this.Offset = Offset; 11 | } 12 | } 13 | 14 | class MainClass 15 | { 16 | static int Part1(string filePath) 17 | { 18 | string[] lines = System.IO.File.ReadAllLines(filePath); 19 | var start = int.Parse(lines[0]); 20 | int answerWait = int.MaxValue; 21 | int answerBusId = -1; 22 | foreach (var bus in lines[1].Split(',')) { 23 | if (bus != "x") { 24 | var busId = int.Parse(bus); 25 | var wait = start % busId == 0 ? 0 : busId - start % busId; 26 | if (wait < answerWait) { 27 | answerWait = wait; 28 | answerBusId = busId; 29 | } 30 | } 31 | } 32 | return answerWait * answerBusId; 33 | } 34 | 35 | static ulong nextLineUp(List buses, int n, ulong start, ulong step) 36 | { 37 | ulong t = start; 38 | bool found = false; 39 | while (!found) { 40 | found = true; 41 | for (int i = 0; found && i < n; ++i) { 42 | if ((t + buses[i].Offset) % buses[i].Id != 0) { 43 | found = false; 44 | } 45 | } 46 | 47 | if (!found) { 48 | t += step; 49 | } 50 | } 51 | 52 | return t; 53 | } 54 | 55 | static ulong Part2(string filePath) 56 | { 57 | string[] lines = System.IO.File.ReadAllLines(filePath); 58 | List buses = new List(); 59 | 60 | ulong offset = 0; 61 | foreach (var bus in lines[1].Split(',')) { 62 | if (bus != "x") { 63 | buses.Add(new Bus(ulong.Parse(bus), offset)); 64 | } 65 | offset += 1; 66 | } 67 | 68 | buses.Sort(delegate(Bus bus1, Bus bus2) { 69 | return bus2.Id.CompareTo(bus1.Id); 70 | }); 71 | 72 | ulong step = 1; 73 | int n = 2; 74 | ulong t = 0; 75 | while (n < buses.Count) { 76 | ulong t0 = nextLineUp(buses, n, t, step); 77 | ulong t1 = nextLineUp(buses, n, t0 + step, step); 78 | step = t1 - t0; 79 | t = t0; 80 | n += 1; 81 | } 82 | 83 | return nextLineUp(buses, n, t, step); 84 | } 85 | 86 | static void SolveFile(string filePath) 87 | { 88 | Console.WriteLine("Input file: {0}", filePath); 89 | Console.WriteLine("Part 1: {0}", Part1(filePath)); 90 | Console.WriteLine("Part 2: {0}", Part2(filePath)); 91 | } 92 | 93 | static void Main(string[] args) 94 | { 95 | foreach (var filePath in args) { 96 | SolveFile(filePath); 97 | } 98 | } 99 | } 100 | 101 | -------------------------------------------------------------------------------- /13-csharp/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=sample.txt input.txt 2 | 3 | test: Main.exe $(INPUTS) 4 | ./Main.exe $(INPUTS) 5 | 6 | Main.exe: Main.cs 7 | mcs Main.cs 8 | -------------------------------------------------------------------------------- /13-csharp/README.md: -------------------------------------------------------------------------------- 1 | # [Day 13](https://adventofcode.com/2020/day/13) solution in [C#](https://csharp.net/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/i2bya8kNXjw/hqdefault.jpg)](https://www.youtube.com/watch?v=i2bya8kNXjw&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=708s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ mcs --version 11 | Mono C# compiler version 5.18.0.240 12 | ``` 13 | 14 | ## Expected Result 15 | 16 | ```console 17 | Input file: input.txt 18 | Part 1: 4315 19 | Part 2: 556100168221141 20 | ``` 21 | 22 | ## Quick Start 23 | 24 | - install [Mono](https://www.mono-project.com/) and make sure `mcs` is available in `$PATH` 25 | - `$ make` 26 | -------------------------------------------------------------------------------- /13-csharp/input.txt: -------------------------------------------------------------------------------- 1 | 1001938 2 | 41,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,431,x,x,x,x,x,x,x,23,x,x,x,x,13,x,x,x,17,x,19,x,x,x,x,x,x,x,x,x,x,x,863,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,29 3 | -------------------------------------------------------------------------------- /13-csharp/sample.txt: -------------------------------------------------------------------------------- 1 | 939 2 | 7,13,x,x,59,x,31,19 3 | -------------------------------------------------------------------------------- /14-php/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=sample-02.txt input.txt 2 | 3 | .PHONY: test 4 | test: index.php $(INPUTS) 5 | php index.php $(INPUTS) 6 | -------------------------------------------------------------------------------- /14-php/README.md: -------------------------------------------------------------------------------- 1 | # [Day 14](https://adventofcode.com/2020/day/14) solution in [PHP](https://www.php.net/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/95ooXiwVeMM/hqdefault.jpg)](https://www.youtube.com/watch?v=95ooXiwVeMM&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ php --version 11 | PHP 7.3.19-1~deb10u1 (cli) (built: Jul 5 2020 06:46:45) ( NTS ) 12 | Copyright (c) 1997-2018 The PHP Group 13 | Zend Engine v3.3.19, Copyright (c) 1998-2018 Zend Technologies 14 | with Zend OPcache v7.3.19-1~deb10u1, Copyright (c) 1999-2018, by Zend Technologies 15 | ``` 16 | 17 | ## Expected Result 18 | 19 | ```console 20 | Input file: input.txt 21 | Part 1: 12135523360904 22 | Part 2: 2741969047858 23 | ``` 24 | 25 | ## Quick Start 26 | 27 | - install [PHP 7.x](https://www.php.net/downloads.php#v7.4.13) and make sure `php` is available in `$PATH` 28 | - `$ make` 29 | -------------------------------------------------------------------------------- /14-php/index.php: -------------------------------------------------------------------------------- 1 | 0) { 36 | $op = explode("=", $line); 37 | $name = trim($op[0]); 38 | $arg = trim($op[1]); 39 | if ($name === "mask") { 40 | $xmask = make_xmask($arg); 41 | $smask = make_smask($arg); 42 | } else { 43 | $addr = intval(substr($name, 4, strlen($name) - 5)); 44 | $value = intval($arg); 45 | $mem[$addr] = ($value & $xmask) | $smask; 46 | } 47 | } 48 | } 49 | 50 | $result = 0; 51 | foreach ($mem as $byte) { 52 | $result += $byte; 53 | } 54 | 55 | return $result; 56 | } 57 | 58 | function count_ones($mask) { 59 | $result = 0; 60 | while ($mask > 0) { 61 | if ($mask & 1 == 1) { 62 | $result += 1; 63 | } 64 | $mask = $mask >> 1; 65 | } 66 | return $result; 67 | } 68 | 69 | function spread_xmask($xmask, $x) { 70 | $rev_result = 0; 71 | 72 | // XX0XX0 73 | // 001101 74 | 75 | for ($i = 0; $i < 36; ++$i) { 76 | $rev_result = $rev_result << 1; 77 | 78 | if (($xmask & 1) == 1) { 79 | $rev_result = $rev_result | ($x & 1); 80 | $x = $x >> 1; 81 | } 82 | 83 | $xmask = $xmask >> 1; 84 | } 85 | 86 | $result = 0; 87 | for ($i = 0; $i < 36; ++$i) { 88 | $result = ($result << 1) | ($rev_result & 1); 89 | $rev_result = $rev_result >> 1; 90 | } 91 | 92 | return $result; 93 | } 94 | 95 | function part_2($file_path) { 96 | $lines = explode("\n", file_get_contents($file_path)); 97 | $xmask = 0; 98 | $smask = 0; 99 | $n = 0; 100 | $mem = array(); 101 | 102 | foreach ($lines as $line) { 103 | if (strlen($line) > 0) { 104 | $op = explode("=", $line); 105 | $name = trim($op[0]); 106 | $arg = trim($op[1]); 107 | if ($name === "mask") { 108 | $xmask = make_xmask($arg); 109 | $smask = make_smask($arg); 110 | $n = pow(2, count_ones($xmask)); 111 | } else { 112 | $addr = intval(substr($name, 4, strlen($name) - 5)); 113 | $value = intval($arg); 114 | $eaddr = ($addr | $smask) & (~$xmask); 115 | 116 | for ($x = 0; $x < $n; ++$x) { 117 | $mem[$eaddr | spread_xmask($xmask, $x)] = $value; 118 | } 119 | } 120 | } 121 | } 122 | 123 | $result = 0; 124 | foreach ($mem as $byte) { 125 | $result += $byte; 126 | } 127 | 128 | return $result; 129 | } 130 | 131 | function solve_file($file_path) { 132 | echo("Input file: $file_path\n"); 133 | echo("Part 1: " . part_1($file_path) . "\n"); 134 | echo("Part 2: " . part_2($file_path) . "\n"); 135 | } 136 | 137 | foreach (array_slice($argv, 1) as $file_path) { 138 | solve_file($file_path); 139 | } 140 | -------------------------------------------------------------------------------- /14-php/sample-01.txt: -------------------------------------------------------------------------------- 1 | mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X 2 | mem[8] = 11 3 | mem[7] = 101 4 | mem[8] = 0 5 | -------------------------------------------------------------------------------- /14-php/sample-02.txt: -------------------------------------------------------------------------------- 1 | mask = 000000000000000000000000000000X1001X 2 | mem[42] = 100 3 | mask = 00000000000000000000000000000000X0XX 4 | mem[26] = 1 5 | -------------------------------------------------------------------------------- /15-pascal/.gitignore: -------------------------------------------------------------------------------- 1 | main 2 | *.o -------------------------------------------------------------------------------- /15-pascal/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: test 2 | test: main 3 | ./main 4 | 5 | main: main.pas 6 | fpc main.pas 7 | -------------------------------------------------------------------------------- /15-pascal/README.md: -------------------------------------------------------------------------------- 1 | # [Day 15](https://adventofcode.com/2020/day/15) solution in [Pascal](https://en.wikipedia.org/wiki/Pascal_(unit)) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/ech9Re2bi_E/hqdefault.jpg)](https://www.youtube.com/watch?v=ech9Re2bi_E&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=791s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ fpc -version 11 | [0.008] Free Pascal Compiler version 3.0.4+dfsg-22 [2019/01/24] for x86_64 12 | [0.008] Copyright (c) 1993-2017 by Florian Klaempfl and others 13 | ``` 14 | 15 | ## Expected Result 16 | 17 | ```console 18 | Sample: 6,3,15,13,1,0 19 | 30000000th: 51358 20 | ``` 21 | 22 | ## Quick Start 23 | 24 | - install [Free Pascal](https://www.freepascal.org/) and make sure `fpc` is available in `$PATH` 25 | - `$ make` 26 | -------------------------------------------------------------------------------- /15-pascal/main.pas: -------------------------------------------------------------------------------- 1 | program Aoc2020Day15; 2 | 3 | uses SysUtils; 4 | 5 | const 6 | Capacity = 30 * 1000 * 1000 + 10; 7 | 8 | var 9 | input : array[0..0] of String = ( 10 | {'0,3,6', 11 | '1,3,2', 12 | '2,1,3', 13 | '1,2,3', 14 | '2,3,1', 15 | '3,2,1', 16 | '3,1,2', 17 | } 18 | '6,3,15,13,1,0' 19 | ); 20 | sample : array of Int64; 21 | hashtable : array of Int64; 22 | i, j, sampleSize : Int64; 23 | 24 | procedure SavePrevPos(value: Int64; pos: Int64); 25 | begin 26 | Assert(Value < Capacity); 27 | hashtable[value] := pos; 28 | end; 29 | 30 | function GetPrevPos(value: Int64): Int64; 31 | begin 32 | Assert(Value < Capacity); 33 | GetPrevPos := hashtable[Value]; 34 | end; 35 | 36 | { 37 | function FindPrev(size, prev: Int64): Int64; 38 | begin 39 | for i := size - 1 downto 0 do 40 | if sample[i] = prev then 41 | Exit(i); 42 | Exit(-1); 43 | end; 44 | } 45 | 46 | procedure SolveSample(n: Int64); 47 | var 48 | prevIndex : Int64; 49 | begin 50 | while sampleSize < n do 51 | begin 52 | prevIndex := GetPrevPos(sample[sampleSize - 1]); 53 | SavePrevPos(sample[sampleSize - 1], sampleSize - 1); 54 | if prevIndex < 0 then 55 | sample[sampleSize] := 0 56 | else 57 | sample[sampleSize] := sampleSize - (prevIndex + 1); 58 | 59 | inc(sampleSize); 60 | end; 61 | 62 | WriteLn(n, 'th: ', sample[sampleSize - 1]); 63 | end; 64 | 65 | procedure InitSample(input : String); 66 | var 67 | start, finish : Int64; 68 | begin 69 | start := 1; 70 | finish := 1; 71 | sampleSize := 0; 72 | while start <= Length(input) do 73 | begin 74 | while (finish <= Length(input)) and (input[finish] <> ',') do 75 | begin 76 | inc(finish); 77 | end; 78 | 79 | if sampleSize > 0 then 80 | SavePrevPos(sample[sampleSize - 1], sampleSize - 1); 81 | 82 | sample[sampleSize] := StrToInt64(copy(input, start, finish - start)); 83 | inc(sampleSize); 84 | 85 | start := finish + 1; 86 | finish := start; 87 | end; 88 | end; 89 | 90 | begin 91 | setLength(sample, Capacity); 92 | setLength(hashtable, Capacity); 93 | for i := 0 to Length(input) - 1 do 94 | begin 95 | WriteLn('Sample: ', input[i]); 96 | for j := 0 to Capacity - 1 do hashtable[j] := -1; 97 | InitSample(input[i]); 98 | {SolveSample(2020);} 99 | SolveSample(30 * 1000 * 1000); 100 | end; 101 | end. 102 | -------------------------------------------------------------------------------- /16-ruby/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=sample-01.txt sample-02.txt input.txt 2 | 3 | test: main.rb $(INPUTS) 4 | ruby main.rb $(INPUTS) 5 | -------------------------------------------------------------------------------- /16-ruby/README.md: -------------------------------------------------------------------------------- 1 | # [Day 16](https://adventofcode.com/2020/day/16) solution in [Ruby](https://www.ruby-lang.org/en/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/xb-yZhokKSw/hqdefault.jpg)](https://www.youtube.com/watch?v=xb-yZhokKSw&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=807s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ ruby --version 11 | ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux-gnu] 12 | ``` 13 | 14 | ## Expected result 15 | 16 | ```console 17 | Input file: input.txt 18 | Part 1: 23925 19 | Part 2: 964373157673 20 | ``` 21 | 22 | ## Quick Start 23 | 24 | - install [Ruby](https://www.ruby-lang.org/en/downloads/) and make sure `ruby` is available in `$PATH` 25 | - `$ make` 26 | -------------------------------------------------------------------------------- /16-ruby/main.rb: -------------------------------------------------------------------------------- 1 | Field = Struct.new(:name, :ranges) 2 | 3 | def parse_field(s) 4 | name, raw_ranges = s.split(": ") 5 | Field.new(name, raw_ranges 6 | .split(" or ") 7 | .map { |r| r.split("-").map(&:to_i) }) 8 | end 9 | 10 | def field_value_valid?(value, field) 11 | for low, high in field.ranges do 12 | if low <= value && value <= high then 13 | return true 14 | end 15 | end 16 | return false 17 | end 18 | 19 | def first_invalid_field(ticket, fields) 20 | for value in ticket do 21 | valid = false 22 | for field in fields do 23 | if field_value_valid?(value, field) then 24 | valid = true 25 | break 26 | end 27 | end 28 | 29 | if !valid then 30 | return value 31 | end 32 | end 33 | 34 | return -1 35 | end 36 | 37 | def ticket_valid?(ticket, fields) 38 | first_invalid_field(ticket, fields) < 0 39 | end 40 | 41 | def part_1(input) 42 | result = 0 43 | for ticket in input.nearby do 44 | x = first_invalid_field(ticket, input.fields) 45 | if x >= 0 then 46 | result += x 47 | end 48 | end 49 | result 50 | end 51 | 52 | def valid_fields_for_value(value, fields) 53 | fields.select do |field| 54 | valid = false 55 | for low,high in field.ranges do 56 | if low <= value && value <= high then 57 | valid = true 58 | break 59 | end 60 | end 61 | valid 62 | end 63 | end 64 | 65 | def part_2(input) 66 | result = input.nearby.select do 67 | |ticket| ticket_valid?(ticket, input.fields) 68 | end.map do |ticket| 69 | ticket.map do |value| 70 | valid_fields_for_value(value, input.fields).map{|f| f.name} 71 | end 72 | end.transpose.each_with_index.map do |col, index| 73 | result = col[0] 74 | col.each do |x| 75 | result = result & x 76 | end 77 | Struct.new(:index, :names).new(index, result) 78 | end.sort_by {|set| set.names.length} 79 | 80 | for i in 0..result.length - 2 do 81 | for j in i+1..result.length - 1 do 82 | result[j].names.delete(result[i].names[0]) 83 | end 84 | end 85 | 86 | result.select do |x| 87 | x.names[0].start_with?("departure") 88 | end.map do |x| 89 | input.my[x.index] 90 | end.reduce(:*) 91 | end 92 | 93 | def solve_file(file_path) 94 | content = IO.read(file_path) 95 | .split("\n") 96 | 97 | fields = content 98 | .take_while{|s| !s.empty?} 99 | .map{|x| parse_field(x)} 100 | content = content.drop_while{|s| !s.empty?}.drop(1) 101 | 102 | my = content 103 | .take_while{|s| !s.empty?} 104 | .drop(1)[0] 105 | .split(",") 106 | .map(&:to_i) 107 | content = content.drop_while{|s| !s.empty?}.drop(1) 108 | 109 | nearby = content.take_while{|s| !s.empty?} 110 | .drop(1) 111 | .map{|s| s.split(",").map(&:to_i)} 112 | 113 | input = Struct 114 | .new(:fields, :my, :nearby) 115 | .new(fields, my, nearby) 116 | 117 | puts "Input file: #{file_path}" 118 | puts "Part 1: #{part_1(input)}" 119 | puts "Part 2: #{part_2(input)}" 120 | end 121 | 122 | for file_path in ARGV 123 | solve_file(file_path) 124 | end 125 | -------------------------------------------------------------------------------- /16-ruby/sample-01.txt: -------------------------------------------------------------------------------- 1 | class: 1-3 or 5-7 2 | row: 6-11 or 33-44 3 | seat: 13-40 or 45-50 4 | 5 | your ticket: 6 | 7,1,14 7 | 8 | nearby tickets: 9 | 7,3,47 10 | 40,4,50 11 | 55,2,20 12 | 38,6,12 13 | -------------------------------------------------------------------------------- /16-ruby/sample-02.txt: -------------------------------------------------------------------------------- 1 | class: 0-1 or 4-19 2 | row: 0-5 or 8-19 3 | seat: 0-13 or 16-19 4 | 5 | your ticket: 6 | 11,12,13 7 | 8 | nearby tickets: 9 | 3,9,18 10 | 15,1,5 11 | 5,14,9 12 | -------------------------------------------------------------------------------- /17-scala/.gitignore: -------------------------------------------------------------------------------- 1 | *.class -------------------------------------------------------------------------------- /17-scala/Main.scala: -------------------------------------------------------------------------------- 1 | import scala.io._ 2 | 3 | object Part1 { 4 | type Cell = (Int, Int, Int) 5 | 6 | def nbors(cell: Cell): Seq[Cell] = { 7 | val (x, y, z) = cell 8 | for ( 9 | dx <- -1 to 1; 10 | dy <- -1 to 1; 11 | dz <- -1 to 1; 12 | if dx != 0 || dy != 0 || dz != 0 13 | ) yield (x + dx, y + dy, z + dz) 14 | } 15 | 16 | def countActiveNbors(cell: Cell, pocket: Set[Cell]): Int = 17 | nbors(cell) 18 | .filter(pocket.contains(_)) 19 | .size 20 | 21 | def pocketFromFile(filePath: String): Set[Cell] = 22 | Source 23 | .fromFile(filePath) 24 | .getLines() 25 | .zipWithIndex 26 | .flatMap { 27 | case (line, y) => 28 | line 29 | .zipWithIndex 30 | .filter(_._1 == '#') 31 | .map(p => (p._2, y, 0)) 32 | .toList 33 | } 34 | .toSet 35 | 36 | def next(pocket: Set[Cell]): Set[Cell] = 37 | pocket 38 | .flatMap(nbors) 39 | .filter { 40 | case cell => { 41 | val n = countActiveNbors(cell, pocket) 42 | if (pocket.contains(cell)) { 43 | 2 <= n && n <= 3 44 | } else { 45 | n == 3 46 | } 47 | } 48 | } 49 | } 50 | 51 | object Part2 { 52 | type Cell = (Int, Int, Int, Int) 53 | 54 | def nbors(cell: Cell): Seq[Cell] = { 55 | val (x, y, z, w) = cell 56 | for ( 57 | dx <- -1 to 1; 58 | dy <- -1 to 1; 59 | dz <- -1 to 1; 60 | dw <- -1 to 1; 61 | if dx != 0 || dy != 0 || dz != 0 || dw != 0 62 | ) yield (x + dx, y + dy, z + dz, w + dw) 63 | } 64 | 65 | def countActiveNbors(cell: Cell, pocket: Set[Cell]): Int = 66 | nbors(cell) 67 | .filter(pocket.contains(_)) 68 | .size 69 | 70 | def pocketFromFile(filePath: String): Set[Cell] = 71 | Source 72 | .fromFile(filePath) 73 | .getLines() 74 | .zipWithIndex 75 | .flatMap { 76 | case (line, y) => 77 | line 78 | .zipWithIndex 79 | .filter(_._1 == '#') 80 | .map(p => (p._2, y, 0, 0)) 81 | .toList 82 | } 83 | .toSet 84 | 85 | def next(pocket: Set[Cell]): Set[Cell] = 86 | pocket 87 | .flatMap(nbors) 88 | .filter { 89 | case cell => { 90 | val n = countActiveNbors(cell, pocket) 91 | if (pocket.contains(cell)) { 92 | 2 <= n && n <= 3 93 | } else { 94 | n == 3 95 | } 96 | } 97 | } 98 | } 99 | 100 | object Main { 101 | def part1(filePath: String): Int = 102 | LazyList.iterate(Part1.pocketFromFile(filePath))(Part1.next)(6).size 103 | 104 | def part2(filePath: String): Int = 105 | LazyList.iterate(Part2.pocketFromFile(filePath))(Part2.next)(6).size 106 | 107 | def solveFile(filePath: String) { 108 | println(s"Input file: $filePath") 109 | println(s"Part 1: ${part1(filePath)}") 110 | println(s"Part 2: ${part2(filePath)}") 111 | } 112 | 113 | def main(args: Array[String]) = 114 | args.foreach(solveFile) 115 | } 116 | -------------------------------------------------------------------------------- /17-scala/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=sample.txt input.txt 2 | 3 | .PHONY: test $(INPUTS) 4 | test: Main.class $(INPUTS) 5 | java -cp "$(SCALA_HOME)/lib/scala-library.jar:." Main $(INPUTS) 6 | 7 | Main.class: Main.scala 8 | scalac Main.scala 9 | -------------------------------------------------------------------------------- /17-scala/README.md: -------------------------------------------------------------------------------- 1 | # [Day 17](https://adventofcode.com/2020/day/17) solution in [Scala](https://www.scala-lang.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/rKPG4bGQLGk/hqdefault.jpg)](https://www.youtube.com/watch?v=rKPG4bGQLGk&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=734s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ java -version 11 | java version "13.0.1" 2019-10-15 12 | Java(TM) SE Runtime Environment (build 13.0.1+9) 13 | Java HotSpot(TM) 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing) 14 | $ scala --version 15 | Scala code runner version 2.13.4 -- Copyright 2002-2020, LAMP/EPFL and Lightbend, Inc. 16 | ``` 17 | 18 | ## Expected Result 19 | 20 | ```console 21 | Input file: input.txt 22 | Part 1: 313 23 | Part 2: 2640 24 | ``` 25 | 26 | ## Quick Start 27 | 28 | - install [JDK](https://www.oracle.com/java/technologies/javase-downloads.html) and make sure `java` is available in `$PATH` 29 | - install [Scala](https://downloads.lightbend.com/scala/2.13.4/scala-2.13.4.tgz) 30 | - point `SCALA_HOME` environment variable to scala distribution 31 | - make sure `scalac` is available in `$PATH` 32 | - `$ make` 33 | -------------------------------------------------------------------------------- /17-scala/input.txt: -------------------------------------------------------------------------------- 1 | #####... 2 | .#..##.. 3 | ##.##.## 4 | ...####. 5 | #.#...## 6 | .##...#. 7 | .#.#.### 8 | #.#.#..# 9 | -------------------------------------------------------------------------------- /17-scala/sample.txt: -------------------------------------------------------------------------------- 1 | .#. 2 | ..# 3 | ### 4 | -------------------------------------------------------------------------------- /18-kotlin/.gitignore: -------------------------------------------------------------------------------- 1 | *.jar -------------------------------------------------------------------------------- /18-kotlin/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=sample.txt input.txt 2 | 3 | .PHONY: test 4 | test: main.jar $(INPUTS) 5 | java -jar main.jar $(INPUTS) 6 | 7 | main.jar: main.kt 8 | kotlinc main.kt -include-runtime -d main.jar 9 | -------------------------------------------------------------------------------- /18-kotlin/README.md: -------------------------------------------------------------------------------- 1 | # [Day 18](https://adventofcode.com/2020/day/18) solution in [Kotlin](https://kotlinlang.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/gGDuoey_TnE/hqdefault.jpg)](https://www.youtube.com/watch?v=gGDuoey_TnE&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=777s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ java -version 11 | java version "13.0.1" 2019-10-15 12 | Java(TM) SE Runtime Environment (build 13.0.1+9) 13 | Java HotSpot(TM) 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing) 14 | $ kotlin -version 15 | Kotlin version 1.4.20-release-308 (JRE 13.0.1+9) 16 | ``` 17 | 18 | ## Expected Result 19 | 20 | ```console 21 | Input file: input.txt 22 | Part 1: 18213007238947 23 | Part 2: 388966573054664 24 | ``` 25 | 26 | ## Quick Start 27 | 28 | - install [JDK](https://www.oracle.com/java/technologies/javase-downloads.html) and make sure `java` is available in `$PATH` 29 | - install [Kotlin](https://github.com/JetBrains/kotlin/releases/download/v1.4.20/kotlin-compiler-1.4.20.zip) and make sure `kotlinc` is available in `$PATH` 30 | - `$ make` 31 | -------------------------------------------------------------------------------- /18-kotlin/main.kt: -------------------------------------------------------------------------------- 1 | import java.io.File 2 | 3 | enum class TokenType { 4 | OpenParen, 5 | CloseParen, 6 | Plus, 7 | Mult, 8 | Number 9 | } 10 | 11 | data class Token(val type: TokenType, val text: String) 12 | 13 | fun String.tokenize(): List { 14 | @OptIn(kotlin.ExperimentalStdlibApi::class) 15 | return buildList { 16 | var input = this@tokenize.trimStart() 17 | while (input.isNotEmpty()) { 18 | val token: Token = when (input[0]) { 19 | '(' -> Token(TokenType.OpenParen, "(") 20 | ')' -> Token(TokenType.CloseParen, ")") 21 | '+' -> Token(TokenType.Plus, "+") 22 | '*' -> Token(TokenType.Mult, "*") 23 | else -> { 24 | Token(TokenType.Number, input.takeWhile(Char::isDigit)) 25 | } 26 | } 27 | add(token) 28 | input = input.drop(token.text.length).trimStart() 29 | } 30 | } 31 | } 32 | 33 | object Part1 { 34 | fun evalSeq(tokens: List): Pair> { 35 | var result = evalExpr(tokens) 36 | var acc = result.first.toLong() 37 | var input = result.second 38 | 39 | while (input.isNotEmpty() && input.first().type != TokenType.CloseParen) { 40 | result = evalExpr(input.drop(1)); 41 | when (input.first().type) { 42 | TokenType.Plus -> { 43 | acc += result.first.toLong() 44 | } 45 | 46 | TokenType.Mult -> { 47 | acc *= result.first.toLong() 48 | } 49 | } 50 | 51 | input = result.second; 52 | } 53 | 54 | return Pair(acc, input) 55 | } 56 | 57 | fun evalExpr(tokens: List): Pair> { 58 | return when (tokens.first().type) { 59 | TokenType.Number -> { 60 | Pair(tokens.first().text.toLong(), tokens.drop(1)) 61 | } 62 | 63 | TokenType.OpenParen -> { 64 | val result = evalSeq(tokens.drop(1)) 65 | 66 | if (result.second.first().type != TokenType.CloseParen) { 67 | throw Exception("Unexpected token ${result.second.first().text}") 68 | } 69 | 70 | Pair(result.first, result.second.drop(1)) 71 | } 72 | 73 | else -> { 74 | throw Exception("Unexpected token ${tokens.first().text}. Expected Number or OpenParen") 75 | } 76 | } 77 | } 78 | 79 | fun evalLine(line: String): Long { 80 | return evalSeq(line.tokenize()).first 81 | } 82 | 83 | fun evalFile(filePath: String): Long { 84 | var acc = 0L 85 | File(filePath).forEachLine { 86 | acc += evalLine(it) 87 | } 88 | return acc 89 | } 90 | } 91 | 92 | object Part2 { 93 | fun evalPrimary(tokens: List): Pair> { 94 | return when (tokens.first().type) { 95 | TokenType.Number -> { 96 | Pair(tokens.first().text.toLong(), tokens.drop(1)) 97 | } 98 | 99 | TokenType.OpenParen -> { 100 | val result = evalMult(tokens.drop(1)) 101 | 102 | if (result.second.first().type != TokenType.CloseParen) { 103 | throw Exception("Unexpected token ${result.second.first().text}. Expected ClosedParen. ${result.second.map({ it.text })}") 104 | } 105 | 106 | Pair(result.first, result.second.drop(1)) 107 | } 108 | 109 | else -> { 110 | throw Exception("Unexpected token ${tokens.first().text}. Expected Number or OpenParen") 111 | } 112 | } 113 | } 114 | 115 | fun evalPlus(tokens: List): Pair> { 116 | var result = evalPrimary(tokens) 117 | var acc = result.first 118 | var input = result.second 119 | 120 | while (input.isNotEmpty() && input.first().type == TokenType.Plus) { 121 | result = evalPrimary(input.drop(1)) 122 | 123 | acc += result.first 124 | input = result.second 125 | } 126 | 127 | return Pair(acc, input) 128 | } 129 | 130 | fun evalMult(tokens: List): Pair> { 131 | var result = evalPlus(tokens) 132 | var acc = result.first 133 | var input = result.second 134 | 135 | while (input.isNotEmpty() && input.first().type == TokenType.Mult) { 136 | result = evalPlus(input.drop(1)) 137 | 138 | acc *= result.first 139 | input = result.second 140 | } 141 | 142 | return Pair(acc, input) 143 | } 144 | 145 | fun evalLine(line: String): Long { 146 | return evalMult(line.tokenize()).first 147 | } 148 | 149 | fun evalFile(filePath: String): Long { 150 | var acc = 0L 151 | File(filePath).forEachLine { 152 | val result = evalLine(it) 153 | acc += result 154 | } 155 | return acc 156 | } 157 | } 158 | 159 | fun solveFile(filePath: String) { 160 | println("Input file: $filePath") 161 | println("Part 1: ${Part1.evalFile(filePath)}") 162 | println("Part 2: ${Part2.evalFile(filePath)}") 163 | } 164 | 165 | fun main(args: Array) = 166 | args.forEach(::solveFile) 167 | -------------------------------------------------------------------------------- /18-kotlin/sample.txt: -------------------------------------------------------------------------------- 1 | 1 + (2 * 3) + (4 * (5 + 6)) 2 | 2 * 3 + (4 * 5) 3 | 5 + (8 * 3 + 9 + 3 * 4 * 3) 4 | 5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4)) 5 | ((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 6 | -------------------------------------------------------------------------------- /19-lua/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=sample.txt input-01.txt input-02.txt #input-03.txt 2 | 3 | .PHONY: test 4 | test: main.lua $(INPUTS) 5 | lua main.lua $(INPUTS) 6 | -------------------------------------------------------------------------------- /19-lua/README.md: -------------------------------------------------------------------------------- 1 | # [Day 19](https://adventofcode.com/2020/day/19) solution in [Lua](https://www.lua.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/6lFNgR5DkFg/hqdefault.jpg)](https://www.youtube.com/watch?v=6lFNgR5DkFg&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=740s) 6 | 7 | ## Test on 8 | 9 | ```console 10 | $ lua -v 11 | Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio 12 | ``` 13 | 14 | ## Expected Result 15 | 16 | ```console 17 | Input file: input-01.txt 18 | 102 19 | Input file: input-02.txt 20 | 318 21 | ``` 22 | 23 | ## Quick Start 24 | 25 | - install [Lua](https://www.lua.org/download.html) and make sure `lua` is available in `$PATH` 26 | - `$ make` 27 | -------------------------------------------------------------------------------- /19-lua/main.lua: -------------------------------------------------------------------------------- 1 | -- Stolen from https://www.luafaq.org/#T1.15 2 | function dump(o) 3 | if type(o) == 'table' then 4 | local s = '{ ' 5 | for k,v in pairs(o) do 6 | if type(k) ~= 'number' then k = '"'..k..'"' end 7 | s = s .. '['..k..'] = ' .. dump(v) .. ',' 8 | end 9 | return s .. '} ' 10 | else 11 | return tostring(o) 12 | end 13 | end 14 | 15 | function my_split(s, d) 16 | local result = {} 17 | 18 | while #s > 0 do 19 | i, j = string.find(s, d) 20 | 21 | if i then 22 | table.insert(result, string.sub(s, 1, i - 1)) 23 | s = string.sub(s, i + #d) 24 | else 25 | table.insert(result, s) 26 | s = "" 27 | end 28 | end 29 | 30 | return result 31 | end 32 | 33 | function parse_def(def) 34 | local match = string.match(def, "\"(.*)\"") 35 | if match then 36 | return {kind = "match", value = match} 37 | else 38 | local result = {} 39 | for _, subrule in pairs(my_split(def, " | ")) do 40 | local seq = {} 41 | for _, rule in pairs(my_split(subrule, " ")) do 42 | table.insert(seq, tonumber(rule)) 43 | end 44 | table.insert(result, {kind = "seq", value = seq}) 45 | end 46 | return {kind = "alt", value = result} 47 | end 48 | end 49 | 50 | function parse_rule(line) 51 | local result = {} 52 | for index, def in string.gmatch(line, "(.+): (.+)") do 53 | result = { 54 | index = tonumber(index), 55 | def = parse_def(def) 56 | } 57 | end 58 | return result 59 | end 60 | 61 | function match_rule_seq(line, rules, seq) 62 | local inputs = {line} 63 | assert(seq.kind == "seq") 64 | for _, subindex in pairs(seq.value) do 65 | if #inputs == 0 then 66 | break 67 | end 68 | 69 | local more_inputs = {} 70 | for _, input in pairs(inputs) do 71 | for _, rest in pairs(match_rule_index(input, rules, subindex), _) do 72 | table.insert(more_inputs, rest) 73 | end 74 | end 75 | 76 | inputs = more_inputs 77 | end 78 | return inputs 79 | end 80 | 81 | function match_rule_index(line, rules, index) 82 | local kind = rules[index].kind 83 | local value = rules[index].value 84 | if kind == "match" then 85 | if string.sub(line, 1, 1) == value then 86 | return {string.sub(line, 2)} 87 | else 88 | return {} 89 | end 90 | elseif kind == "seq" then 91 | return match_rule_seq(line, rules, value) 92 | elseif kind == "alt" then 93 | local result = {} 94 | 95 | for _, seq in pairs(value) do 96 | assert(seq.kind == "seq") 97 | for _, input in pairs(match_rule_seq(line, rules, seq)) do 98 | table.insert(result, input) 99 | end 100 | end 101 | 102 | return result 103 | else 104 | assert(false, "Unknown kind of rule `" .. kind .. "`") 105 | end 106 | end 107 | 108 | function fully_parsed(inputs) 109 | local result = false 110 | 111 | for _, input in ipairs(inputs) do 112 | if #input == 0 then 113 | result = true 114 | break; 115 | end 116 | end 117 | 118 | return result 119 | end 120 | 121 | function part_1(file_path) 122 | local rules = {} 123 | local parsing_rules = true 124 | local result = 0 125 | 126 | for line in io.lines(file_path) do 127 | if parsing_rules then 128 | if #line == 0 then 129 | parsing_rules = false 130 | goto continue 131 | end 132 | 133 | local rule = parse_rule(line) 134 | rules[rule.index] = rule.def 135 | else 136 | local inputs = match_rule_index(line, rules, 0) 137 | if #inputs > 0 and fully_parsed(inputs) then 138 | result = result + 1 139 | end 140 | end 141 | ::continue:: 142 | end 143 | 144 | return result 145 | end 146 | 147 | function solve_file(file_path) 148 | print("Input file:", file_path) 149 | print(part_1(file_path)) 150 | end 151 | 152 | for i=1,#arg do 153 | solve_file(arg[i]) 154 | end 155 | -------------------------------------------------------------------------------- /19-lua/sample.txt: -------------------------------------------------------------------------------- 1 | 0: 4 1 5 2 | 1: 2 3 | 3 2 3 | 2: 4 4 | 5 5 4 | 3: 4 5 | 5 4 5 | 4: "a" 6 | 5: "b" 7 | 8 | ababbb 9 | bababa 10 | abbbab 11 | aaabbb 12 | aaaabbb 13 | -------------------------------------------------------------------------------- /20-julia/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=input.txt 2 | 3 | .PHONY: test 4 | test: main.jl $(INPUTS) 5 | julia main.jl $(INPUTS) 6 | -------------------------------------------------------------------------------- /20-julia/README.md: -------------------------------------------------------------------------------- 1 | # [Day 20](https://adventofcode.com/2020/day/20) solution in [Julia](https://julialang.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/vvvO8QQtW7Q/hqdefault.jpg)](https://www.youtube.com/watch?v=vvvO8QQtW7Q&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=688s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ julia --version 11 | julia version 1.0.3 12 | ``` 13 | 14 | ## Expected Result 15 | 16 | ```console 17 | Input file: input.txt 18 | Part 1: 63187742854073 19 | Part 2: 2152 20 | ``` 21 | 22 | ## Quick Start 23 | 24 | - install [Julia](https://julialang.org/downloads/) and make sure `julia` is available in `$PATH` 25 | - `$ make` 26 | -------------------------------------------------------------------------------- /20-julia/main.jl: -------------------------------------------------------------------------------- 1 | TOP = 1 2 | RIGHT = 2 3 | BOTTOM = 3 4 | LEFT = 4 5 | 6 | struct Tile 7 | id :: Int 8 | rows :: Array{String, 1} 9 | sides :: Array{String, 1} 10 | end 11 | 12 | function sides_of_rows(rows :: Array{String, 1}) :: Array{String, 1} 13 | sides = Array{Char,1}[] 14 | 15 | for _ in 1:4 16 | push!(sides, Array{Char, 1}()) 17 | end 18 | 19 | for i in 1:10 20 | push!(sides[TOP], rows[1][i]) 21 | push!(sides[RIGHT], rows[i][10]) 22 | push!(sides[BOTTOM], rows[10][i]) 23 | push!(sides[LEFT], rows[i][1]) 24 | end 25 | 26 | result = String[] 27 | for i in 1:4 28 | push!(result, String(sides[i])) 29 | end 30 | return result 31 | end 32 | 33 | function rotate_lines(lines :: Array{String, 1}) :: Array{String, 1} 34 | h = length(lines) 35 | w = length(lines[1]) 36 | 37 | result = String[] 38 | for i in 1:w 39 | row = Char[] 40 | for j in 1:h 41 | push!(row, lines[h - j + 1][i]) 42 | end 43 | push!(result, String(row)) 44 | end 45 | return result 46 | end 47 | 48 | function rotate(tile :: Tile) :: Tile 49 | rows = rotate_lines(tile.rows) 50 | # rows = String[] 51 | # for i in 1:10 52 | # row = Char[] 53 | # for j in 1:10 54 | # push!(row, tile.rows[10 - j + 1][i]) 55 | # end 56 | # push!(rows, String(row)) 57 | # end 58 | 59 | return Tile(tile.id, rows, sides_of_rows(rows)) 60 | end 61 | 62 | # 1 2 3 3 2 1 63 | # 4 5 6 => 6 5 4 64 | # 7 8 9 9 8 7 65 | # 66 | # 7 8 9 67 | # 4 5 6 68 | # 1 2 3 69 | 70 | function flip_lines(lines :: Array{String, 1}) :: Array{String, 1} 71 | rows = String[] 72 | h = length(lines) 73 | for i in 1:h 74 | push!(rows, lines[h - i + 1]) 75 | end 76 | return rows 77 | end 78 | 79 | function flip(tile :: Tile) :: Tile 80 | # rows = String[] 81 | # for i in 1:10 82 | # push!(rows, tile.rows[10 - i + 1]) 83 | # end 84 | rows = flip_lines(tile.rows) 85 | return Tile(tile.id, rows, sides_of_rows(rows)) 86 | end 87 | 88 | function parse_file(file_path :: String) :: Array{Tile, 1} 89 | tiles = Tile[] 90 | open(file_path) do file 91 | parsing_tile = true 92 | 93 | id = 0 94 | rows = String[] 95 | for line in eachline(file) 96 | if parsing_tile 97 | id = parse(Int, split(split(line, " ")[2], ":")[1]) 98 | parsing_tile = false 99 | else 100 | if length(line) > 0 101 | push!(rows, line) 102 | else 103 | push!(tiles, Tile(id, rows, sides_of_rows(rows))) 104 | rows = String[] 105 | parsing_tile = true 106 | end 107 | end 108 | end 109 | push!(tiles, Tile(id, rows, sides_of_rows(rows))) 110 | end 111 | return tiles 112 | end 113 | 114 | function find_right_bottom(left :: Tile, top :: Tile, tiles :: Array{Tile, 1}) :: Tile 115 | for tile in tiles 116 | if top.id != tile.id && left.id != tile.id 117 | result = tile 118 | 119 | for i in 1:4 120 | if top.sides[BOTTOM] == result.sides[TOP] && left.sides[RIGHT] == result.sides[LEFT] 121 | return result 122 | end 123 | result = rotate(result) 124 | end 125 | 126 | result = flip(tile) 127 | 128 | for i in 1:4 129 | if top.sides[BOTTOM] == result.sides[TOP] && left.sides[RIGHT] == result.sides[LEFT] 130 | return result 131 | end 132 | result = rotate(result) 133 | end 134 | end 135 | end 136 | 137 | @assert(false, "Could not find solution for find_right_bottom") 138 | end 139 | 140 | function find_bottom(top :: Tile, tiles :: Array{Tile, 1}) :: Tile 141 | for tile in tiles 142 | if top.id != tile.id 143 | bottom = tile 144 | 145 | for i in 1:4 146 | if top.sides[BOTTOM] == bottom.sides[TOP] 147 | return bottom 148 | end 149 | bottom = rotate(bottom) 150 | end 151 | 152 | bottom = flip(tile) 153 | 154 | for i in 1:4 155 | if top.sides[BOTTOM] == bottom.sides[TOP] 156 | return bottom 157 | end 158 | bottom = rotate(bottom) 159 | end 160 | end 161 | end 162 | 163 | @assert(false, "Could not find solution for find_right") 164 | end 165 | 166 | function find_right(left :: Tile, tiles :: Array{Tile, 1}) :: Tile 167 | for tile in tiles 168 | if left.id != tile.id 169 | right = tile 170 | for i in 1:4 171 | if left.sides[RIGHT] == right.sides[LEFT] 172 | return right 173 | end 174 | right = rotate(right) 175 | end 176 | 177 | right = flip(tile) 178 | 179 | for i in 1:4 180 | if left.sides[RIGHT] == right.sides[LEFT] 181 | return right 182 | end 183 | right = rotate(right) 184 | end 185 | end 186 | end 187 | 188 | @assert(false, "Could not find solution for find_right") 189 | end 190 | 191 | function side_matches(candidate :: Tile, tiles :: Array{Tile, 1}) :: Array{Int, 1} 192 | cs = fill(0, (4)) 193 | for tile in tiles 194 | if candidate.id != tile.id 195 | for left in 1:4 196 | for right in 1:4 197 | if candidate.sides[left] == tile.sides[right] 198 | cs[left] += 1 199 | end 200 | if candidate.sides[left] == reverse(tile.sides[right]) 201 | cs[left] += 1 202 | end 203 | end 204 | end 205 | end 206 | end 207 | 208 | # SIDES: 209 | # TOP = 1 210 | # RIGHT = 2 211 | # BOTTOM = 3 212 | # LEFT = 4 213 | ############# 214 | # CORNERS: 215 | # 0 1 216 | # 217 | # 3 2 218 | 219 | return cs 220 | end 221 | 222 | function is_corner(candidate :: Tile, tiles :: Array{Tile, 1}) :: Bool 223 | cs = side_matches(candidate, tiles) 224 | 225 | res = 0 226 | for c in cs 227 | if c == 0 228 | res += 1 229 | end 230 | end 231 | 232 | return res == 2 233 | end 234 | 235 | function part_1(tiles :: Array{Tile, 1}) :: Int 236 | result = 1 237 | for tile in tiles 238 | if is_corner(tile, tiles) 239 | result *= tile.id 240 | end 241 | end 242 | return result 243 | end 244 | 245 | function count_hash(lines :: Array{String, 1}) :: Int 246 | result = 0 247 | for line in lines 248 | for x in line 249 | if x == '#' 250 | result += 1 251 | end 252 | end 253 | end 254 | return result 255 | end 256 | 257 | 258 | monster = [" # ", 259 | "# ## ## ###", 260 | " # # # # # # "] 261 | 262 | function contains_monster(drow :: Int, dcol :: Int, picture :: Array{String, 1}) :: Bool 263 | for row0 in 0:(3 - 1) 264 | for col0 in 0:(20 - 1) 265 | if monster[row0 + 1][col0 + 1] == '#' 266 | if picture[row0 + drow][col0 + dcol] != '#' 267 | return false 268 | end 269 | end 270 | end 271 | end 272 | return true 273 | end 274 | 275 | function count_monsters(picture :: Array{String, 1}) :: Int 276 | result = 0 277 | for drow in 1:(96 - (3)) 278 | for dcol in 1:(96 - (20)) 279 | if contains_monster(drow, dcol, picture) 280 | result += 1 281 | end 282 | end 283 | end 284 | return result 285 | end 286 | 287 | function part_2(tiles :: Array{Tile, 1}) :: Int 288 | puzzle = Array{Array{Tile, 1}, 1}() 289 | 290 | push!(puzzle, Array{Tile, 1}()) 291 | for tile in tiles 292 | if side_matches(tile, tiles) == [0, 1, 1, 0] 293 | push!(puzzle[1], tile) 294 | break 295 | end 296 | end 297 | 298 | for i in 2:12 299 | right = find_right(puzzle[1][i - 1], tiles) 300 | push!(puzzle[1], right) 301 | end 302 | 303 | for i in 2:12 304 | push!(puzzle, Array{Tile, 1}()) 305 | bottom = find_bottom(puzzle[i - 1][1], tiles) 306 | push!(puzzle[i], bottom) 307 | 308 | for j in 2:12 309 | right_bottom = find_right_bottom(puzzle[i][j - 1], 310 | puzzle[i - 1][j], 311 | tiles) 312 | push!(puzzle[i], right_bottom) 313 | end 314 | end 315 | 316 | picture = Array{String, 1}() 317 | 318 | for row in puzzle 319 | for i in 2:9 320 | picture_row = Array{String, 1}() 321 | for tile in row 322 | push!(picture_row, tile.rows[i][2:9]) 323 | end 324 | push!(picture, join(picture_row)) 325 | end 326 | end 327 | 328 | picture = flip_lines(picture) 329 | 330 | return count_hash(picture) - count_monsters(picture) * count_hash(monster) 331 | end 332 | 333 | function solve_file(file_path :: String) 334 | println("Input file: $(file_path)") 335 | tiles = parse_file(file_path) 336 | println("Part 1: $(part_1(tiles))") 337 | println("Part 2: $(part_2(tiles))") 338 | end 339 | 340 | for file_path in ARGS 341 | solve_file(file_path) 342 | end 343 | -------------------------------------------------------------------------------- /20-julia/output.txt: -------------------------------------------------------------------------------- 1 | 3209 2 | 1193 3 | 2441 4 | 2063 5 | 3931 6 | 3581 7 | 3727 8 | 1987 9 | 1163 10 | 3733 11 | 2963 12 | 3803 13 | 2237 14 | 1409 15 | 2549 16 | 1663 17 | 2417 18 | 3067 19 | 2381 20 | 2731 21 | 2729 22 | 1993 23 | 2389 24 | 2161 25 | 3767 26 | 1997 27 | 3877 28 | 3673 29 | 2087 30 | 2711 31 | 3307 32 | 1123 33 | 3761 34 | 3793 35 | 3347 36 | 1231 37 | 2777 38 | 3719 39 | 1153 40 | 1361 41 | 3257 42 | 2749 43 | 2347 44 | 1559 45 | 3541 46 | 2017 47 | 2383 48 | 2473 49 | 2351 50 | 3517 51 | 1979 52 | 1933 53 | 3407 54 | 2131 55 | 2377 56 | 1607 57 | 1489 58 | 2411 59 | 1613 60 | 1321 61 | 1039 62 | 2917 63 | 2203 64 | 2767 65 | 3607 66 | 1693 67 | 2819 68 | 3301 69 | 3191 70 | 1453 71 | 2897 72 | 1823 73 | 2281 74 | 3457 75 | 3449 76 | 2879 77 | 3571 78 | 1117 79 | 3911 80 | 3389 81 | 1237 82 | 2707 83 | 2531 84 | 1279 85 | 2251 86 | 3511 87 | 3691 88 | 3323 89 | 3163 90 | 1171 91 | 3259 92 | 2083 93 | 1481 94 | 1777 95 | 1033 96 | 3079 97 | 2137 98 | 1753 99 | 3559 100 | 1801 101 | 1427 102 | 2647 103 | 1187 104 | 2969 105 | 2297 106 | 1259 107 | 1721 108 | 1031 109 | 3329 110 | 3547 111 | 3943 112 | 2677 113 | 2789 114 | 2267 115 | 1181 116 | 2851 117 | 2539 118 | 3709 119 | 1787 120 | 1609 121 | 2659 122 | 1291 123 | 3169 124 | 1973 125 | 2593 126 | 1657 127 | 2113 128 | 2591 129 | 1901 130 | 3499 131 | 1699 132 | 1109 133 | 3701 134 | 2521 135 | 3167 136 | 3181 137 | 3947 138 | 3023 139 | 2579 140 | 2633 141 | 2239 142 | 3989 143 | 2269 144 | 1399 145 | -------------------------------------------------------------------------------- /20-julia/sample.txt: -------------------------------------------------------------------------------- 1 | Tile 2311: 2 | ..##.#..#. 3 | ##..#..... 4 | #...##..#. 5 | ####.#...# 6 | ##.##.###. 7 | ##...#.### 8 | .#.#.#..## 9 | ..#....#.. 10 | ###...#.#. 11 | ..###..### 12 | 13 | Tile 1951: 14 | #.##...##. 15 | #.####...# 16 | .....#..## 17 | #...###### 18 | .##.#....# 19 | .###.##### 20 | ###.##.##. 21 | .###....#. 22 | ..#.#..#.# 23 | #...##.#.. 24 | 25 | Tile 1171: 26 | ####...##. 27 | #..##.#..# 28 | ##.#..#.#. 29 | .###.####. 30 | ..###.#### 31 | .##....##. 32 | .#...####. 33 | #.##.####. 34 | ####..#... 35 | .....##... 36 | 37 | Tile 1427: 38 | ###.##.#.. 39 | .#..#.##.. 40 | .#.##.#..# 41 | #.#.#.##.# 42 | ....#...## 43 | ...##..##. 44 | ...#.##### 45 | .#.####.#. 46 | ..#..###.# 47 | ..##.#..#. 48 | 49 | Tile 1489: 50 | ##.#.#.... 51 | ..##...#.. 52 | .##..##... 53 | ..#...#... 54 | #####...#. 55 | #..#.#.#.# 56 | ...#.#.#.. 57 | ##.#...##. 58 | ..##.##.## 59 | ###.##.#.. 60 | 61 | Tile 2473: 62 | #....####. 63 | #..#.##... 64 | #.##..#... 65 | ######.#.# 66 | .#...#.#.# 67 | .######### 68 | .###.#..#. 69 | ########.# 70 | ##...##.#. 71 | ..###.#.#. 72 | 73 | Tile 2971: 74 | ..#.#....# 75 | #...###... 76 | #.#.###... 77 | ##.##..#.. 78 | .#####..## 79 | .#..####.# 80 | #..#.#..#. 81 | ..####.### 82 | ..#.#.###. 83 | ...#.#.#.# 84 | 85 | Tile 2729: 86 | ...#.#.#.# 87 | ####.#.... 88 | ..#.#..... 89 | ....#..#.# 90 | .##..##.#. 91 | .#.####... 92 | ####.#.#.. 93 | ##.####... 94 | ##..#.##.. 95 | #.##...##. 96 | 97 | Tile 3079: 98 | #.#.#####. 99 | .#..###### 100 | ..#....... 101 | ######.... 102 | ####.#..#. 103 | .#...#.##. 104 | #.#####.## 105 | ..#.###... 106 | ..#....... 107 | ..#.###... 108 | -------------------------------------------------------------------------------- /21-racket/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=input.txt 2 | 3 | .PHONY: test 4 | test: main.rkt $(INPUTS) 5 | racket main.rkt $(INPUTS) 6 | -------------------------------------------------------------------------------- /21-racket/README.md: -------------------------------------------------------------------------------- 1 | # [Day 21](https://adventofcode.com/2020/day/21) solution in [Racket](https://racket-lang.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/tP98VjF9Ljo/hqdefault.jpg)](https://www.youtube.com/watch?v=tP98VjF9Ljo&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=730s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ racket --version 11 | Welcome to Racket v7.2. 12 | ``` 13 | 14 | ## Expected Result 15 | 16 | ```console 17 | Input file: input.txt 18 | Part 1: 2614 19 | Part 2: #((wheat . #) (soy . #) (shellfish . #) (eggs . #) (sesame . #) (nuts . #) (peanuts . #) (fish . #)) 20 | ``` 21 | 22 | ## Quick Start 23 | 24 | - install [Racket](https://download.racket-lang.org/) and make sure `racket` is available in `$PATH` 25 | - `$ make` 26 | -------------------------------------------------------------------------------- /21-racket/main.rkt: -------------------------------------------------------------------------------- 1 | #lang racket 2 | 3 | (require racket/hash) 4 | 5 | (define (parse-food line) 6 | (match-let ([(list ings allegs) (string-split line " (contains ")]) 7 | `#hash((ings . ,(list->set (string-split ings))) 8 | (allegs . ,(list->set 9 | (string-split 10 | (first (string-split allegs ")")) 11 | ", ")))))) 12 | 13 | (define (parse-file file-path) 14 | (for/vector ([line (file->lines file-path)]) 15 | (parse-food line))) 16 | 17 | (define (count-ing-alleg ing alleg foods) 18 | (length 19 | (for/list ([food foods] 20 | #:when 21 | (and 22 | (set-member? (hash-ref food 'ings) ing) 23 | (set-member? (hash-ref food 'allegs) alleg))) 24 | food))) 25 | 26 | (define (potential-allegs-2 foods) 27 | (let ([uniq-allegs (for*/set ([food foods] 28 | [alleg (hash-ref food 'allegs)]) 29 | alleg)]) 30 | (for/vector ([alleg uniq-allegs]) 31 | (cons alleg (for/set ([ing (let ([ings (for/list 32 | ([food foods] 33 | #:when 34 | (set-member? (hash-ref food 'allegs) alleg)) 35 | (hash-ref food 'ings))]) 36 | (foldl set-intersect (car ings) (cdr ings)))]) 37 | ing))))) 38 | 39 | (define (potential-allegs foods) 40 | (let ([uniq-allegs (for*/set ([food foods] 41 | [alleg (hash-ref food 'allegs)]) 42 | alleg)]) 43 | (for*/set ([alleg uniq-allegs] 44 | [ing (let ([ings (for/list 45 | ([food foods] 46 | #:when 47 | (set-member? (hash-ref food 'allegs) alleg)) 48 | (hash-ref food 'ings))]) 49 | (foldl set-intersect (car ings) (cdr ings)))]) 50 | ing))) 51 | 52 | (define (not-allegs foods) 53 | (let ([uniq-ings (for*/set ([food foods] 54 | [ing (hash-ref food 'ings)]) 55 | ing)]) 56 | (set-subtract 57 | uniq-ings 58 | (potential-allegs foods)))) 59 | 60 | (define (part-1 foods) 61 | (length 62 | (for*/list ([not-alleg (not-allegs foods)] 63 | [food foods] 64 | [ing (hash-ref food 'ings)] 65 | #:when (equal? not-alleg ing)) 66 | ing))) 67 | 68 | (define (part-2 foods) 69 | (let* ([xs (vector-sort 70 | (potential-allegs-2 foods) 71 | #:key (lambda (x) (set-count (cdr x))) <)] 72 | [n (vector-length xs)]) 73 | ;; (for ([i (in-range 0 (- n 1))]) 74 | ;; (for ([j (in-range (+ i 1) n)]) 75 | ;; (let ([x (car (set->list (vector-ref xs i)))]) 76 | ;; (vector-set! xs j 77 | ;; (set-remove (vector-ref xs j) x))))) 78 | xs) 79 | ) 80 | 81 | (define (solve-file file-path) 82 | (printf "Input file: ~a\n" file-path) 83 | (let ([foods (parse-file file-path)]) 84 | (printf "Part 1: ~a\n" (part-1 foods)) 85 | (printf "Part 2: ~a\n" (part-2 foods)))) 86 | 87 | (for ([arg (current-command-line-arguments)]) 88 | (solve-file arg)) 89 | 90 | -------------------------------------------------------------------------------- /21-racket/sample.txt: -------------------------------------------------------------------------------- 1 | mxmxvkd kfcds sqjhc nhms (contains dairy, fish) 2 | trh fvjkl sbzzf mxmxvkd (contains dairy) 3 | sqjhc fvjkl (contains soy) 4 | sqjhc mxmxvkd sbzzf (contains fish) 5 | -------------------------------------------------------------------------------- /22-groovy/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=sample.txt input.txt 2 | 3 | .PHONY: test 4 | test: main.groovy $(INPUTS) 5 | groovy main.groovy $(INPUTS) 6 | -------------------------------------------------------------------------------- /22-groovy/README.md: -------------------------------------------------------------------------------- 1 | # [Day 22](https://adventofcode.com/2020/day/22) solution in [Groovy](https://groovy-lang.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/mq3ZIGRgZOA/hqdefault.jpg)](https://www.youtube.com/watch?v=mq3ZIGRgZOA&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=709s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ java --version 11 | java 13.0.1 2019-10-15 12 | Java(TM) SE Runtime Environment (build 13.0.1+9) 13 | Java HotSpot(TM) 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing) 14 | $ groovy --version 15 | Groovy Version: 3.0.7 JVM: 13.0.1 Vendor: Oracle Corporation OS: Linux 16 | ``` 17 | 18 | ## Expected Result 19 | 20 | ```console 21 | Input file: input.txt 22 | Part 1: 35397 23 | Part 2: 31120 24 | ``` 25 | 26 | ## Quick Start 27 | 28 | - install [JDK](https://www.oracle.com/java/technologies/javase-downloads.html) and make sure `java` is available in `$PATH` 29 | - install [Groovy](https://groovy.apache.org/download.html) 30 | - point `GROOVY_HOME` environment variable to the groovy distribution 31 | - make sure `groovy` is available in `$PATH` 32 | - `$ make` 33 | -------------------------------------------------------------------------------- /22-groovy/input.txt: -------------------------------------------------------------------------------- 1 | Player 1: 2 | 5 3 | 20 4 | 28 5 | 30 6 | 48 7 | 7 8 | 41 9 | 24 10 | 29 11 | 8 12 | 37 13 | 32 14 | 16 15 | 17 16 | 34 17 | 27 18 | 46 19 | 43 20 | 14 21 | 49 22 | 35 23 | 11 24 | 6 25 | 38 26 | 1 27 | 28 | Player 2: 29 | 22 30 | 18 31 | 50 32 | 31 33 | 12 34 | 13 35 | 33 36 | 39 37 | 45 38 | 21 39 | 19 40 | 26 41 | 44 42 | 10 43 | 42 44 | 3 45 | 4 46 | 15 47 | 36 48 | 2 49 | 40 50 | 47 51 | 9 52 | 23 53 | 25 54 | -------------------------------------------------------------------------------- /22-groovy/main.groovy: -------------------------------------------------------------------------------- 1 | class Decks implements Cloneable { 2 | List player1 = [] 3 | List player2 = [] 4 | 5 | Decks(player1, player2) { 6 | this.player1 = player1 7 | this.player2 = player2 8 | } 9 | 10 | @Override 11 | String toString() { 12 | return "{ ${player1}, ${player2} }" 13 | } 14 | 15 | @Override 16 | Decks clone() throws CloneNotSupportedException { 17 | return new Decks(player1.clone(), player2.clone()); 18 | } 19 | 20 | @Override 21 | boolean equals(Object o) { 22 | if (o instanceof Decks) { 23 | def that = o as Decks; 24 | 25 | return this.player1.equals(that.player1) && 26 | this.player2.equals(that.player2); 27 | } 28 | 29 | return false; 30 | } 31 | 32 | @Override 33 | int hashCode() { 34 | int result = player1.hashCode(); 35 | result = 31 * result + player2.hashCode(); 36 | return result; 37 | } 38 | } 39 | 40 | def parseFile(filePath) { 41 | def decks = new Decks([], []) 42 | def lines = new File(filePath).readLines() 43 | def index = 1 44 | 45 | while (lines[index].length() > 0) { 46 | decks.player1.add(lines[index].toInteger()) 47 | index += 1 48 | } 49 | 50 | index += 2 51 | 52 | while (index < lines.size()) { 53 | decks.player2.add(lines[index].toInteger()) 54 | index += 1 55 | } 56 | 57 | return decks 58 | } 59 | 60 | def recWinnerCondition(decks, visited) { 61 | if (visited.contains(decks)) { 62 | return 1 63 | } else if (decks.player1.size() == 0 && decks.player2.size() == 0) { 64 | assert false : "Unreachable. Both players somehow ended up with no cards" 65 | } else if (decks.player1.size() > 0 && decks.player2.size() == 0) { 66 | return 1; 67 | } else if (decks.player2.size() > 0 && decks.player1.size() == 0) { 68 | return 2; 69 | } else { 70 | return 0; 71 | } 72 | } 73 | 74 | def simulateGameRec(decks, game) { 75 | def winner = 0; 76 | def round = 1; 77 | def visited = new HashSet() 78 | while ((winner = recWinnerCondition(decks, visited)) == 0) { 79 | // println "DEBUG: Round ${round} (Game ${game})" 80 | // println "DEBUG: ${decks}" 81 | 82 | visited.add(decks.clone()) 83 | def a1 = decks.player1.pop() 84 | def a2 = decks.player2.pop() 85 | 86 | if (decks.player1.size() >= a1 && decks.player2.size() >= a2) { 87 | def subWinner = simulateGameRec( 88 | new Decks(decks.player1.take(a1), decks.player2.take(a2)), 89 | game + 1) 90 | 91 | // println "DEBUG: Winner: $subWinner" 92 | 93 | switch (subWinner) { 94 | case 1: 95 | decks.player1.add(a1) 96 | decks.player1.add(a2) 97 | break; 98 | case 2: 99 | decks.player2.add(a2) 100 | decks.player2.add(a1) 101 | break; 102 | default: 103 | assert false : "Unexpected sub game results: ${subWinner}" 104 | } 105 | } else if (a1 > a2) { 106 | decks.player1.add(a1) 107 | decks.player1.add(a2) 108 | // println "DEBUG: Winner: 1" 109 | } else if (a1 < a2) { 110 | decks.player2.add(a2) 111 | decks.player2.add(a1) 112 | // println "DEBUG: Winner: 2" 113 | } else { 114 | assert false : "Unreachable state of the game. Players drew equal cards" 115 | } 116 | 117 | round += 1; 118 | } 119 | 120 | return winner; 121 | } 122 | 123 | def simulateGame(decks) { 124 | while (decks.player1.size() != 0 && decks.player2.size() != 0) { 125 | def a1 = decks.player1.pop() 126 | def a2 = decks.player2.pop() 127 | 128 | if (a1 > a2) { 129 | decks.player1.add(a1) 130 | decks.player1.add(a2) 131 | } else if (a1 < a2) { 132 | decks.player2.add(a2) 133 | decks.player2.add(a1) 134 | } else { 135 | assert false : "unreachable state of the game" 136 | } 137 | } 138 | } 139 | 140 | def winnerScore(decks) { 141 | def deck = decks.player1.size() > 0 ? 142 | decks.player1 : 143 | decks.player2 144 | def n = deck.size() 145 | def result = 0; 146 | 147 | for (int i = 1; i <= n; ++i) { 148 | result += i * deck.removeLast() 149 | } 150 | return result; 151 | } 152 | 153 | def part1(decks) { 154 | simulateGame(decks) 155 | return winnerScore(decks) 156 | } 157 | 158 | def part2(decks) { 159 | simulateGameRec(decks, 1); 160 | return winnerScore(decks); 161 | } 162 | 163 | def solveFile(filePath) { 164 | println "Input file: ${filePath}" 165 | def decks = parseFile(filePath) 166 | println "Part 1: ${part1(decks.clone())}" 167 | println "Part 2: ${part2(decks.clone())}" 168 | } 169 | 170 | for (filePath in args) { 171 | solveFile(filePath) 172 | } 173 | -------------------------------------------------------------------------------- /22-groovy/sample.txt: -------------------------------------------------------------------------------- 1 | Player 1: 2 | 9 3 | 2 4 | 6 5 | 3 6 | 1 7 | 8 | Player 2: 9 | 5 10 | 8 11 | 4 12 | 7 13 | 10 14 | -------------------------------------------------------------------------------- /23-dart/.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | -------------------------------------------------------------------------------- /23-dart/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=input.txt 2 | 3 | .PHONY: test 4 | test: main.exe $(INPUTS) 5 | ./main.exe $(INPUTS) 6 | 7 | main.exe: main.dart 8 | dart compile exe main.dart 9 | -------------------------------------------------------------------------------- /23-dart/README.md: -------------------------------------------------------------------------------- 1 | # [Day 23](https://adventofcode.com/2020/day/23) solution in [Dart](https://dart.dev/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/p_wl80ugHSc/hqdefault.jpg)](https://www.youtube.com/watch?v=p_wl80ugHSc&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=743s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ dart --version 11 | Dart SDK version: 2.10.4 (stable) (Wed Nov 11 13:35:58 2020 +0100) on "linux_x64" 12 | ``` 13 | 14 | ## Expected Result 15 | 16 | ```console 17 | Input file: input.txt 18 | Part 1: 95648732 19 | Part 2: 192515314252 20 | ``` 21 | 22 | ## Quick Start 23 | 24 | - install [Dart](https://storage.googleapis.com/dart-archive/channels/stable/release/2.10.4/sdk/dartsdk-linux-ia32-release.zip) and make sure `dart` is available in `$PATH` 25 | - `$ make` 26 | -------------------------------------------------------------------------------- /23-dart/input.txt: -------------------------------------------------------------------------------- 1 | 368195742 2 | -------------------------------------------------------------------------------- /23-dart/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | String part1(String current) { 4 | int getDestIndex(String current, int max) { 5 | int t = int.parse(current[0]) - 1; 6 | int result = current.indexOf(t.toString()); 7 | while (result < 0) { 8 | t -= 1; 9 | if (t < 1) t = max; 10 | result = current.indexOf(t.toString()); 11 | } 12 | return result; 13 | } 14 | 15 | for (int i = 0; i < 100; ++i) { 16 | // print(current); 17 | var pick3 = current.substring(1, 4); 18 | var smol = current.split(pick3).join(); 19 | var destIndex = getDestIndex(smol, 9); 20 | current = smol.substring(1, destIndex + 1) + pick3 + smol.substring(destIndex + 1) + smol[0]; 21 | } 22 | 23 | return current.split("1").reversed.join(); 24 | } 25 | 26 | class Node { 27 | int value; 28 | int next; 29 | 30 | Node(this.value, this.next); 31 | 32 | @override 33 | String toString() { 34 | return "(value: ${value}, next: ${next})"; 35 | } 36 | } 37 | 38 | int part2(String content) { 39 | var nodes = new List(); 40 | var addrs = new List(1000 * 1000 + 10); 41 | 42 | for (int i = 0; i < content.length; ++i) { 43 | var value = int.parse(content[i]); 44 | nodes.add(new Node(value, i + 1)); 45 | addrs[value] = i; 46 | } 47 | 48 | for (int value = 10; value <= 1000 * 1000; ++value) { 49 | int addr = value - 1; 50 | nodes.add(new Node(value, addr + 1)); 51 | addrs[value] = addr; 52 | } 53 | 54 | nodes[1000 * 1000 - 1].next = 0; 55 | 56 | int current = 0; 57 | 58 | int jump(int addr, int n) { 59 | for (int i = 0; i < n; ++i) { 60 | addr = nodes[addr].next; 61 | } 62 | return addr; 63 | } 64 | 65 | bool isValueRemoved(int value, int addr, int n) { 66 | for (int i = 0; i < n; ++i) { 67 | if (nodes[addr].value == value) { 68 | return true; 69 | } 70 | addr = nodes[addr].next; 71 | } 72 | return false; 73 | } 74 | 75 | int getDestAddr(int pick3) { 76 | int result = nodes[current].value - 1; 77 | if (result < 1) result = 1000 * 1000; 78 | 79 | while (isValueRemoved(result, pick3, 3)) { 80 | result -= 1; 81 | if (result < 1) result = 1000 * 1000; 82 | } 83 | 84 | return addrs[result]; 85 | } 86 | 87 | for (int i = 0; i < 10 * 1000 * 1000; ++i) { 88 | var pick3 = nodes[current].next; 89 | nodes[current].next = jump(current, 4); 90 | var dest = getDestAddr(pick3); 91 | // print(jump(pick3, 2)); 92 | nodes[jump(pick3, 2)].next = nodes[dest].next; 93 | nodes[dest].next = pick3; 94 | current = nodes[current].next; 95 | } 96 | 97 | return nodes[jump(addrs[1], 1)].value * nodes[jump(addrs[1], 2)].value; 98 | } 99 | 100 | void solveFile(String filePath) { 101 | new File(filePath).readAsString() 102 | .then((String content) { return content.trim(); }) 103 | .then((String content) { 104 | print('Input file: ${filePath}'); 105 | print("Part 1: ${part1(content)}"); 106 | print('Part 2: ${part2(content)}'); 107 | }); 108 | } 109 | 110 | void main(List args) { 111 | for (String filePath in args) { 112 | solveFile(filePath); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /23-dart/sample.txt: -------------------------------------------------------------------------------- 1 | 389125467 2 | -------------------------------------------------------------------------------- /24-ada/.gitignore: -------------------------------------------------------------------------------- 1 | day24 2 | *.ali 3 | *.o 4 | -------------------------------------------------------------------------------- /24-ada/Makefile: -------------------------------------------------------------------------------- 1 | INPUTS=input.txt 2 | 3 | .PHONY: test 4 | test: day24 $(INPUTS) 5 | ./day24 $(INPUTS) 6 | 7 | day24: day24.adb 8 | gnatmake day24.adb 9 | -------------------------------------------------------------------------------- /24-ada/README.md: -------------------------------------------------------------------------------- 1 | # [Day 24](https://adventofcode.com/2020/day/24) solution in [Ada](https://www.adaic.org/) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/5ChNYG81tS0/hqdefault.jpg)](https://www.youtube.com/watch?v=5ChNYG81tS0&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=671s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ gnat --version 11 | GNAT 8.3.0 12 | Copyright (C) 1996-2018, Free Software Foundation, Inc. 13 | This is free software; see the source for copying conditions. 14 | There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 15 | ``` 16 | 17 | ## Expected Result 18 | 19 | ```console 20 | Input file: input.txt 21 | Part 1: 360 22 | Part 2: 3924 23 | ``` 24 | 25 | ## Quick Start 26 | 27 | - install [GNAT](https://gcc.gnu.org/wiki/GNAT) and make sure `gnatmake` is available in `$PATH` 28 | - `$ make` 29 | -------------------------------------------------------------------------------- /24-ada/day24.adb: -------------------------------------------------------------------------------- 1 | with Ada.Text_IO; use Ada.Text_IO; 2 | with Ada.Command_Line; use Ada.Command_Line; 3 | with Ada.Strings.Unbounded; use Ada.Strings.Unbounded; 4 | with System.Assertions; use System.Assertions; 5 | with Ada.Containers.Hashed_Maps; use Ada.Containers; 6 | with Ada.Characters.Handling; use Ada.Characters.Handling; 7 | 8 | procedure Day24 is 9 | 10 | type Tile is record 11 | X : Integer; 12 | Y : Integer; 13 | Z : Integer; 14 | end record; 15 | 16 | function "+"(T1: Tile; T2: Tile) return Tile is 17 | begin 18 | return (T1.X + T2.X, T1.Y + T2.Y, T1.Z + T2.Z); 19 | end; 20 | 21 | type Dir_Kind is (E, SE, SW, W, NW, NE); 22 | 23 | Dir_To_Delta : constant array (Dir_Kind) of Tile := ( 24 | E => (X => 1, Y => -1, Z => 0), 25 | SE => (X => 0, Y => -1, Z => 1), 26 | SW => (X => -1, Y => 0, Z => 1), 27 | W => (X => -1, Y => 1, Z => 0), 28 | NW => (X => 0, Y => 1, Z => -1), 29 | NE => (X => 1, Y => 0, Z => -1) 30 | ); 31 | 32 | function Tile_Hash(T: Tile) return Hash_Type is 33 | Result : Long_Integer := Long_Integer(T.X); 34 | begin 35 | Result := Result * 31 + Long_Integer(T.Y); 36 | Result := Result * 97 + Long_Integer(T.Z); 37 | return Hash_Type(Result mod 1_000_000_000); 38 | end; 39 | 40 | package Floor is new Ada.Containers.Hashed_Maps 41 | (Key_Type => Tile, 42 | Element_Type => Boolean, 43 | Hash => Tile_Hash, 44 | Equivalent_Keys => "="); 45 | 46 | procedure Flip_Tile(F: in out Floor.Map; T: Tile) is 47 | D: Floor.Cursor := Floor.Find(F, T); 48 | begin 49 | if not Floor.Has_Element(D) then 50 | Floor.Insert(F, T, True); 51 | else 52 | Floor.Replace_Element(F, D, not Floor.Element(D)); 53 | end if; 54 | end; 55 | 56 | function Is_Black(F: in Floor.Map; T: Tile) return Boolean is 57 | C: Floor.Cursor := Floor.Find(F, T); 58 | begin 59 | return Floor.Has_Element(C) and then Floor.Element(C); 60 | end; 61 | 62 | function Count_Neighbours(F: in Floor.Map; T: Tile) return Integer is 63 | Result: Integer := 0; 64 | begin 65 | for Dir of Dir_To_Delta loop 66 | if Is_Black(F, T + Dir) then 67 | Result := Result + 1; 68 | end if; 69 | end loop; 70 | return Result; 71 | end; 72 | 73 | procedure Set_Tile(F: in out Floor.Map; T: Tile; Value: Boolean) is 74 | C: Floor.Cursor := Floor.Find(F, T); 75 | begin 76 | if Floor.Has_Element(C) then 77 | Floor.Replace_Element(F, C, Value); 78 | else 79 | Floor.Insert(F, T, Value); 80 | end if; 81 | end; 82 | 83 | procedure Next_Floor(F1: in Floor.Map; F2: out Floor.Map) is 84 | T : Tile; 85 | Neighbours : Integer; 86 | begin 87 | Floor.Clear(F2); 88 | for C in Floor.Iterate(F1) loop 89 | for Dir of Dir_To_Delta loop 90 | T := Floor.Key(C) + Dir; 91 | Neighbours := Count_Neighbours(F1, T); 92 | if Is_Black(F1, T) then 93 | Set_Tile(F2, T, not (Neighbours = 0 or Neighbours > 2)); 94 | else 95 | Set_Tile(F2, T, Neighbours = 2); 96 | end if; 97 | end loop; 98 | end loop; 99 | end; 100 | 101 | function Count_Black(F: in Floor.Map) return Integer is 102 | C: Floor.Cursor := Floor.First(F); 103 | Result: Integer := 0; 104 | begin 105 | while Floor.Has_Element(C) loop 106 | if Floor.Element(C) then 107 | Result := Result + 1; 108 | end if; 109 | C := Floor.Next(C); 110 | end loop; 111 | return Result; 112 | end; 113 | 114 | function Tile_Image(T: Tile) return String is 115 | begin 116 | return "(" & Integer'Image(T.X) & ", " & Integer'Image(T.Y) & ", " & Integer'Image(T.Z) & ")"; 117 | end; 118 | 119 | function Next_Dir(Desc: String) return Dir_Kind is 120 | begin 121 | for Dir in Dir_Kind loop 122 | declare 123 | Dir_Str : constant String := To_Lower (Dir'Img); 124 | begin 125 | if Dir_Str'Length <= Desc'Length then 126 | if Desc (Desc'First .. Desc'First + Dir_Str'Length - 1) = Dir_Str then 127 | return Dir; 128 | end if; 129 | end if; 130 | end; 131 | end loop; 132 | 133 | Raise_Assert_Failure("Unreachable. Could not get the next direction"); 134 | end Next_Dir; 135 | 136 | function Parse_Tile(Desc: Unbounded_String) return Tile is 137 | Result : Tile := (X => 0, Y => 0, Z => 0); 138 | Dir: Dir_Kind; 139 | Input: Unbounded_String := Desc; 140 | begin 141 | while Length(Input) > 0 loop 142 | Dir := Next_Dir(To_String (Input)); 143 | Input := Unbounded_Slice(Input, Dir'Img'Length + 1, Length(Input)); 144 | Result := Result + Dir_To_Delta (Dir); 145 | end loop; 146 | 147 | return Result; 148 | end Parse_Tile; 149 | 150 | procedure Floor_From_File(File_Path: String; F: out Floor.Map) is 151 | File : File_Type; 152 | T : Tile; 153 | begin 154 | Open(File => File, 155 | Mode => In_File, 156 | Name => File_Path); 157 | while not End_Of_File(File) loop 158 | T := Parse_Tile(To_Unbounded_String(Get_Line(File))); 159 | Flip_Tile(F, T); 160 | end loop; 161 | Close(File); 162 | end; 163 | 164 | function Part1(File_Path: String) return Integer is 165 | F : Floor.Map; 166 | begin 167 | Floor_From_File(File_Path, F); 168 | return Count_Black(F); 169 | end; 170 | 171 | function Part2(File_Path: String) return Integer is 172 | F : array (0..1) of Floor.Map; 173 | Current : Integer := 0; 174 | begin 175 | Floor_From_File(File_Path, F(Current)); 176 | for i in 1..100 loop 177 | Next_Floor(F(Current), F(1 - Current)); 178 | Current := 1 - Current; 179 | end loop; 180 | return Count_Black(F(Current)); 181 | end; 182 | 183 | procedure Solve_File(File_Path: String) is 184 | begin 185 | Put_Line("Input file: " & File_Path); 186 | Put_Line(" Part 1:" & Integer'Image(Part1(File_Path))); 187 | Put_Line(" Part 2:" & Integer'Image(Part2(File_Path))); 188 | end Solve_File; 189 | 190 | begin 191 | Put_Line("Amount of args: " & Integer'Image(Argument_Count)); 192 | for Arg in 1..Argument_Count loop 193 | Solve_File(Argument(Arg)); 194 | end loop; 195 | end Day24; 196 | -------------------------------------------------------------------------------- /24-ada/sample.txt: -------------------------------------------------------------------------------- 1 | sesenwnenenewseeswwswswwnenewsewsw 2 | neeenesenwnwwswnenewnwwsewnenwseswesw 3 | seswneswswsenwwnwse 4 | nwnwneseeswswnenewneswwnewseswneseene 5 | swweswneswnenwsewnwneneseenw 6 | eesenwseswswnenwswnwnwsewwnwsene 7 | sewnenenenesenwsewnenwwwse 8 | wenwwweseeeweswwwnwwe 9 | wsweesenenewnwwnwsenewsenwwsesesenwne 10 | neeswseenwwswnwswswnw 11 | nenwswwsewswnenenewsenwsenwnesesenew 12 | enewnwewneswsewnwswenweswnenwsenwsw 13 | sweneswneswneneenwnewenewwneswswnese 14 | swwesenesewenwneswnwwneseswwne 15 | enesenwswwswneneswsenwnewswseenwsese 16 | wnwnesenesenenwwnenwsewesewsesesew 17 | nenewswnwewswnenesenwnesewesw 18 | eneswnwswnwsenenwnwnwwseeswneewsenese 19 | neswnwewnwnwseenwseesewsenwsweewe 20 | wseweeenwnesenwwwswnew 21 | -------------------------------------------------------------------------------- /25-asm/.gitignore: -------------------------------------------------------------------------------- 1 | main 2 | *.o -------------------------------------------------------------------------------- /25-asm/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: test 2 | test: main 3 | ./main 4 | 5 | main: main.o 6 | ld -o main main.o 7 | 8 | main.o: main.asm 9 | nasm -felf64 main.asm 10 | -------------------------------------------------------------------------------- /25-asm/README.md: -------------------------------------------------------------------------------- 1 | # [Day 25](https://adventofcode.com/2020/day/25) solution in [Assembly](https://en.wikipedia.org/wiki/Assembly_language) 2 | 3 | ## Screencast 4 | 5 | [![screencast](http://i3.ytimg.com/vi/6m7PSAkbelE/hqdefault.jpg)](https://www.youtube.com/watch?v=6m7PSAkbelE&list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P&t=657s) 6 | 7 | ## Tested on 8 | 9 | ```console 10 | $ nasm --version 11 | NASM version 2.14 12 | $ ld --version 13 | GNU ld (GNU Binutils for Debian) 2.31.1 14 | Copyright (C) 2018 Free Software Foundation, Inc. 15 | This program is free software; you may redistribute it under the terms of 16 | the GNU General Public License version 3 or (at your option) a later version. 17 | This program has absolutely no warranty. 18 | ``` 19 | 20 | ## Expected Result 21 | 22 | ```console 23 | 711945 24 | ``` 25 | 26 | ## Quick Start 27 | 28 | - install [nasm](https://www.nasm.us/pub/nasm/releasebuilds/?C=M;O=D) and make sure `nasm` and `ld` are available in `$PATH` 29 | - `$ make` 30 | -------------------------------------------------------------------------------- /25-asm/algo.txt: -------------------------------------------------------------------------------- 1 | // (subject_number ^ loop_size) % 20201227 2 | transform(subject_number, loop_size) { 3 | x = 1 4 | for i in 0..loop_size { 5 | x = x * subject_number 6 | x = x % 20201227 7 | } 8 | } 9 | 10 | card_public_key = transform(7, card_loop_size); 11 | door_public_key = transform(7, door_loop_size); 12 | 13 | transform(door_public_key, card_loop_size) == 14 | transform(card_public_key, door_loop_size) 15 | -------------------------------------------------------------------------------- /25-asm/main.asm: -------------------------------------------------------------------------------- 1 | BITS 64 2 | 3 | %define SYS_READ 0 4 | %define SYS_WRITE 1 5 | %define SYS_OPEN 2 6 | %define SYS_CLOSE 3 7 | %define SYS_EXIT 60 8 | 9 | %define STDIN 0 10 | %define STDOUT 1 11 | %define STDERR 2 12 | 13 | %define O_RDONLY 0 14 | 15 | %define BUFFER_CAPACITY 256 16 | 17 | global _start 18 | section .text 19 | 20 | ;; rdi - number 21 | itoa: 22 | mov rbx, 10 23 | mov rsi, buffer_end - 1 24 | .begin: 25 | test rdi, rdi 26 | jz .end 27 | mov rax, rdi 28 | mov rdx, 0 29 | div rbx 30 | mov rdi, rax 31 | add rdx, '0' 32 | mov [rsi], dl 33 | dec rsi 34 | jmp .begin 35 | .end: 36 | mov rax, rsi 37 | inc rax 38 | ret 39 | 40 | ;; rdi - subject_number 41 | ;; rsi - state 42 | ;; -- 43 | ;; rax - next state 44 | transform_step: 45 | mov rax, rsi 46 | mov rcx, 20201227 47 | mul rdi 48 | mov rdx, 0 49 | div rcx 50 | mov rax, rdx 51 | ret 52 | 53 | ;; rdi - subject_number 54 | ;; rsi - loop_size 55 | transform: 56 | mov rax, 1 57 | mov rcx, 20201227 58 | .begin: 59 | test rsi, rsi 60 | jz .end 61 | mul rdi 62 | mov rdx, 0 63 | div rcx 64 | mov rax, rdx 65 | dec rsi 66 | jmp .begin 67 | .end: 68 | ret 69 | 70 | ;; rdi - subject_number 71 | ;; rsi - public_key 72 | ;; ---- 73 | ;; rax - loop_size 74 | crack_loop_size: 75 | mov rax, 1 76 | mov rbx, 1 ;state 77 | .begin: 78 | push rax 79 | push rbx 80 | push rdi 81 | push rsi 82 | 83 | mov rsi, rbx 84 | call transform_step 85 | 86 | pop rsi 87 | pop rdi 88 | pop rbx 89 | 90 | cmp rax, rsi 91 | je .end 92 | mov rbx, rax 93 | 94 | pop rax 95 | inc rax 96 | jmp .begin 97 | .end: 98 | pop rax 99 | ret 100 | 101 | ; %rdi %rsi %rdx %r10 %r8 %r9 102 | 103 | _start: 104 | mov rdi, 7 105 | mov rsi, [card_public_key] 106 | call crack_loop_size 107 | 108 | ;; rdi - subject_number 109 | mov rdi, [door_public_key] 110 | ;; rsi - loop_size 111 | mov rsi, rax 112 | call transform 113 | 114 | mov rdi, rax 115 | call itoa 116 | 117 | mov rsi, rax 118 | mov rdx, buffer_end 119 | sub rdx, rsi 120 | mov rdi, STDOUT 121 | mov rax, SYS_WRITE 122 | syscall 123 | 124 | mov rax, SYS_WRITE 125 | mov rdi, STDOUT 126 | mov rsi, nl 127 | mov rdx, 1 128 | syscall 129 | 130 | mov rax, SYS_EXIT 131 | mov rdi, 0 132 | syscall 133 | 134 | section .data 135 | ;; Sample 136 | ; card_public_key: dq 5764801 137 | ; door_public_key: dq 17807724 138 | ;; Input 139 | card_public_key: dq 11239946 140 | door_public_key: dq 10464955 141 | 142 | nl: db 10 143 | 144 | section .bss 145 | buffer: 146 | resb BUFFER_CAPACITY 147 | buffer_end: 148 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # PLEASE DON'T SUBMIT ANY PULL REQUESTS! 2 | 3 | This repo is reserved only for solutions created by [@rexim](https://github.com/rexim) during his live streams on [Tsoding](https://twitch.tv/tsoding) Twitch channel. Anyone else's solutions are not accepted here, sorry. 4 | 5 | Thank you for understanding! 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2020 Alexey Kutepov 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to 5 | deal in the Software without restriction, including without limitation the 6 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | sell copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Solutions for [Advent of Code 2020](https://adventofcode.com/2020) from Tsoding 2 | 3 | ![thumbnail](./thumbnail.png) 4 | 5 | - The problems were solved live on [https://twitch.tv/tsoding](https://twitch.tv/tsoding). 6 | - The playlist of archived Advent of Code solutions is available on YouTube: [https://www.youtube.com/playlist?list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P](https://www.youtube.com/playlist?list=PLpM-Dvs8t0Vba3v-9lweHuomr0DPhdX6P) 7 | 8 | ## Environment 9 | 10 | ```console 11 | $ neofetch 12 | _,met$$$$$gg. rexim@rexim-B590 13 | ,g$$$$$$$$$$$$$$$P. ---------------- 14 | ,g$$P" """Y$$.". OS: Debian GNU/Linux 10 (buster) x86_64 15 | ,$$P' `$$$. Host: 20208 Lenovo B590 16 | ',$$P ,ggs. `$$b: Kernel: 4.19.0-12-amd64 17 | `d$$' ,$P"' . $$$ Uptime: 19 days, 12 hours, 34 mins 18 | $$P d$' , $$P Packages: 2541 (dpkg) 19 | $$: $$. - ,d$$' Shell: bash 5.0.3 20 | $$; Y$b._ _,d$P' Resolution: 1366x768 21 | Y$$. `.`"Y$$$$P"' WM: i3 22 | `$$b "-.__ CPU: Intel i5-3230M (4) @ 3.200GHz 23 | `Y$$ GPU: NVIDIA GeForce 610M/710M/810M/820M / GT 620M/625M/630M/720M 24 | `Y$$. GPU: Intel 3rd Gen Core processor Graphics Controller 25 | `$$b. Memory: 2283MiB / 7816MiB 26 | `Y$$b. 27 | `"Y$b._ 28 | `""" 29 | ``` 30 | -------------------------------------------------------------------------------- /thumbnail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tsoding/aoc-2020/b2800286973a96d4797637ca8b38713de775ce5c/thumbnail.png --------------------------------------------------------------------------------