├── .clang-format ├── .editorconfig ├── .gitignore ├── 2015-c ├── .clangd ├── .gitignore ├── 01-not-quite-lisp │ ├── README.txt │ └── main.c ├── 02-i-was-told-there-would-be-no-math │ ├── README.txt │ └── main.c ├── 03-perfectly-spherical-houses-in-a-vacuum │ ├── README.txt │ └── main.c ├── 04-md5 │ ├── README.txt │ ├── main.c │ └── md5.c ├── 05-nice-strings │ ├── README.txt │ └── main.c ├── 06-probably-a-fire-hazard │ ├── README.txt │ └── main.c ├── 07-some-assembly-required │ ├── README.txt │ └── main.c ├── 08-matchsticks │ ├── README.txt │ └── main.c ├── 09-all-in-a-single-night │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 10-elves-look-elves-say │ ├── README.txt │ └── main.c ├── 11-corporate-policy │ ├── README.txt │ └── main.c ├── 12-jsabacusframework-io │ ├── README.txt │ └── main.c ├── 13-knights-of-the-dinner-table │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 14-reindeer-olympics │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 15-science-for-hungry-people │ ├── README.txt │ ├── arrays.c │ ├── input_test.txt │ └── main.c ├── 16-aunt-sue │ ├── README.txt │ └── main.c ├── 17-no-such-thing-as-too-much │ ├── README.txt │ └── main.c ├── 18-like-a-gif-for-your-yard │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 19-medicine-for-rudolph │ ├── README.txt │ ├── input_test.txt │ ├── main.c │ └── main.py ├── 20-infinite-elves-and-infinite-houses │ ├── README.txt │ └── main.c ├── 21-rpg-simulator-20xx │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 22-wizard-simulator-20xx │ ├── README.txt │ ├── index.html │ ├── input_test.txt │ └── main.c ├── 23-opening-the-turing-lock │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 24-it-hangs-in-the-balance │ ├── README.txt │ ├── input_test.txt │ ├── main.c │ └── main.cpp ├── 25-let-it-snow │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── README.md ├── adventofcode.h ├── hashmap.h ├── memcheck-all.sh └── run.sh ├── 2016-c ├── .clangd ├── 01-no-time-for-a-taxicab │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 02-bathroom-security │ └── main.c ├── 03-squares-with-three-sides │ └── main.c ├── 04-security-through-obscurity │ ├── input_test.txt │ └── main.c ├── 05-how-about-a-nice-game-of-chess │ ├── input_test.txt │ └── main.c ├── 06-signals-and-noise │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 07-internet-protocol-version-7 │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 08-two-factor-authentication │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 09-explosives-in-cyberspace │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── README.md ├── adventofcode.h ├── new.sh ├── run.sh └── template.c ├── 2016-cpp ├── .clangd ├── 01.cpp ├── 02.cpp ├── 03.cpp ├── 04.cpp ├── 05.cpp ├── 06.cpp ├── 07.cpp ├── 08.cpp ├── 09.cpp ├── 10.cpp ├── 11.cpp ├── 12.cpp ├── 13.cpp ├── 14.cpp ├── 15.cpp ├── 16.cpp ├── 17.cpp ├── 18.cpp ├── 19.cpp ├── 20.cpp ├── 21.cpp ├── 22.cpp ├── 23.cpp ├── 24.cpp ├── 25.cpp ├── Makefile ├── README.md ├── adventofcode.h ├── new.sh ├── run.sh └── template.cpp ├── 2017-c ├── .clangd ├── 01-inverse-captcha │ ├── README.txt │ └── main.c ├── 02-corruption-checksum │ ├── README.txt │ └── main.c ├── 03-spiral-memory │ ├── README.txt │ └── main.c ├── 04-high-entropy-passphrases │ ├── README.txt │ └── main.c ├── 05-a-maze-of-twisty-trampolines-all-alike │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 06-memory-reallocation │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 07-recursive-circus │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 08-i-heard-you-like-registers │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 09-stream-processing │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 10-knot-hash │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── 11-hex-ed │ ├── README.txt │ ├── input_test.txt │ └── main.c ├── README.md ├── adventofcode.h ├── hashmap.h └── run.sh ├── 2017-cpp ├── .clangd ├── 01.cpp ├── 02.cpp ├── 03.cpp ├── 04.cpp ├── 05.cpp ├── 06.cpp ├── 07.cpp ├── 08.cpp ├── 09.cpp ├── 10.cpp ├── 11.cpp ├── 12.cpp ├── 13.cpp ├── 14.cpp ├── 15.cpp ├── 16.cpp ├── 17.cpp ├── 18.cpp ├── 19.cpp ├── 20.cpp ├── 21.cpp ├── 22.cpp ├── 23.cpp ├── 24.cpp ├── 25.cpp ├── Makefile ├── README.md ├── new.sh ├── run.sh └── template.cpp ├── 2018-c ├── .clangd ├── .gitignore ├── 01.c ├── 02.c ├── 03.c ├── 04.c ├── 05.c ├── 06.c ├── 07.c ├── 08.c ├── 09.c ├── 10.c ├── 11.c ├── 12.c ├── 13.c ├── 14.c ├── 16.c ├── 17.c ├── 18.c ├── 19.c ├── Makefile ├── README.md ├── adventofcode.h ├── new.sh ├── run.sh └── template.c ├── 2019-rust ├── .gitignore ├── README.md ├── day-01 │ ├── .gitignore │ ├── Cargo.lock │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── day-02 │ ├── Cargo.lock │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── day-03 │ ├── .gitignore │ ├── Cargo.lock │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── day-04 │ ├── .gitignore │ ├── Cargo.lock │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── day-05 │ ├── .gitignore │ ├── Cargo.lock │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── day-12 │ ├── .gitignore │ ├── Cargo.lock │ ├── Cargo.toml │ ├── input_test.txt │ └── src │ │ └── main.rs ├── day06 │ ├── Cargo.lock │ ├── Cargo.toml │ ├── README.txt │ └── src │ │ └── main.rs ├── day07 │ ├── Cargo.lock │ ├── Cargo.toml │ ├── README.txt │ └── src │ │ └── main.rs ├── day08 │ ├── Cargo.lock │ ├── Cargo.toml │ ├── README.txt │ └── src │ │ └── main.rs ├── day09 │ ├── Cargo.lock │ ├── Cargo.toml │ ├── README.txt │ └── src │ │ └── main.rs ├── day10 │ ├── Cargo.lock │ ├── Cargo.toml │ ├── README.txt │ └── src │ │ └── main.rs ├── new.sh ├── run-all.sh └── template.rs ├── 2020-c ├── .clangd ├── 01.c ├── 02.c ├── 03.c ├── 04.c ├── 05.c ├── 06.c ├── 07.c ├── 08.c ├── 09.c ├── 10.c ├── 11.c ├── 12.c ├── 13.c ├── 14.c ├── 15.c ├── 16.c ├── 17.c ├── 18.c ├── 19.c ├── 20.c ├── 21.c ├── 22.c ├── 23.c ├── 24.c ├── 25.c ├── Makefile ├── README.md ├── aoc2020.c ├── compare.txt ├── ec2.sh ├── ht.c ├── ht.h ├── hugemem.c ├── hugemem.h ├── inputs │ ├── 01.h │ ├── 01.txt │ ├── 02.h │ ├── 02.txt │ ├── 03.h │ ├── 03.txt │ ├── 04.h │ ├── 04.txt │ ├── 05.h │ ├── 05.txt │ ├── 06.h │ ├── 06.txt │ ├── 07.h │ ├── 07.txt │ ├── 08.h │ ├── 08.txt │ ├── 08_test.h │ ├── 08_test.txt │ ├── 09.h │ ├── 09.txt │ ├── 09_test.h │ ├── 09_test.txt │ ├── 10.h │ ├── 10.txt │ ├── 10_test.h │ ├── 10_test.txt │ ├── 11.h │ ├── 11.txt │ ├── 11_test.h │ ├── 11_test.txt │ ├── 12.h │ ├── 12.txt │ ├── 12_test.h │ ├── 12_test.txt │ ├── 14.h │ ├── 14.txt │ ├── 14_test.h │ ├── 14_test.txt │ ├── 16.h │ ├── 16.txt │ ├── 16_test.h │ ├── 16_test.txt │ ├── 17.h │ ├── 17.txt │ ├── 17_test.h │ ├── 17_test.txt │ ├── 18.h │ ├── 18.txt │ ├── 19.h │ ├── 19.txt │ ├── 19_test.h │ ├── 19_test.txt │ ├── 20.h │ ├── 20.txt │ ├── 20_test.h │ ├── 20_test.txt │ ├── 21.h │ ├── 21.txt │ ├── 21_test.h │ ├── 21_test.txt │ ├── 22.h │ ├── 22.txt │ ├── 22_test.h │ ├── 22_test.txt │ ├── 24.h │ ├── 24.txt │ ├── 24_test.h │ ├── 24_test.txt │ └── dump.sh ├── intmap.c ├── intmap.h ├── new.sh └── pgo.sh ├── 2021-py ├── .gitignore ├── 01-sonar-sweep │ ├── README.txt │ └── main.py ├── 02-dive │ ├── README.txt │ └── main.py ├── 03-binary-diagnostic │ ├── README.txt │ ├── main.py │ └── test_input.txt ├── 04-giant-squid │ ├── README.txt │ └── main.py ├── 05-hydrothermal-venture │ ├── README.txt │ └── main.py ├── 06-lanternfish │ ├── README.txt │ └── main.py ├── 07-treachery-of-whales │ ├── README.txt │ └── main.py ├── 08-seven-segment-search │ ├── README.txt │ └── main.py ├── 09-smoke-basin │ ├── README.txt │ └── main.py ├── 10-syntax-scoring │ ├── README.txt │ └── main.py ├── 11-dumbo-octopus │ ├── README.txt │ └── main.py ├── 12-passage-pathing │ ├── README.txt │ ├── example1.txt │ ├── example2.txt │ ├── example3.txt │ └── main.py ├── 13-transparent-origami │ ├── README.txt │ ├── input_test.txt │ └── main.py ├── 14-extend-polymerization │ ├── README.txt │ └── main.py ├── 15-chiton │ ├── README.txt │ ├── main.py │ └── point.py ├── 16-packet-decoder │ ├── README.txt │ ├── input_test.txt │ └── main.py ├── 17-trick-shot │ ├── README.txt │ ├── input_test.txt │ └── main.py ├── 18-snailfish │ ├── README.txt │ ├── example1.txt │ ├── example2.txt │ ├── main.py │ └── test.py ├── 19-beacon-scanner │ ├── README.txt │ ├── input_test.txt │ ├── main.py │ └── test.py ├── 20-trench-map │ ├── README.txt │ ├── input_test.txt │ ├── main.py │ └── test.py ├── 21-dirac-dice │ ├── README.txt │ ├── input_test.txt │ ├── main.py │ └── test.py ├── 22-reactor-reboot │ ├── README.txt │ ├── input_test.txt │ ├── input_test_2.txt │ ├── input_test_3.txt │ ├── main.py │ └── test.py ├── 23-amphipod │ ├── README.txt │ ├── input_test.txt │ ├── main.py │ ├── part1.txt │ └── part2.txt ├── 24-arithmetic-logic-unit │ ├── README.txt │ ├── input_test.txt │ ├── main.py │ ├── monad.ods │ └── out.txt ├── 25-sea-cucumber │ ├── README.txt │ ├── input_test.txt │ ├── main.py │ └── test.py ├── README.md ├── new ├── requirements.txt ├── run-all.sh ├── template_main.py └── template_test.py ├── 2022-py ├── .gitignore ├── 01-calorie-counting │ ├── README.txt │ └── main.py ├── 02-rock-paper-scissors │ ├── README.txt │ ├── input_test.txt │ └── main.py ├── 03-rucksack-reoganization │ ├── README.txt │ ├── input_test.txt │ └── main.py ├── 04-camp-cleanup │ ├── README.txt │ ├── input_test.txt │ └── main.py ├── 05-supply-stacks │ ├── README.txt │ ├── input_test.txt │ └── main.py ├── 06-tuning-trouble │ ├── README.txt │ ├── input_test.txt │ ├── main.py │ ├── main2.py │ └── test.py ├── 07-no-space-left-on-device │ ├── README.txt │ ├── input_test.txt │ └── main.py ├── 08-treetop-tree-house │ ├── README.txt │ ├── input_test.txt │ └── main.py ├── 09-rope-bridge │ ├── README.txt │ ├── input_test.txt │ ├── main.py │ └── main.rs ├── 10-cathode-ray-tube │ ├── README.txt │ ├── input_test.txt │ └── main.py ├── 11-monkey-in-the-middle │ ├── .gitignore │ ├── README.txt │ ├── input_test.txt │ ├── main.py │ └── main.rs ├── 12-hill-climbing-algorithm │ ├── README.txt │ ├── input_test.txt │ └── main.py ├── 13-distress-signal │ ├── README.md │ ├── input_test.txt │ └── main.py ├── 14-regolith-reservoir │ ├── README.md │ ├── input_test.txt │ └── main.py ├── 15-beacon-exclusion-zone │ ├── README.txt │ ├── input_test.txt │ ├── main.py │ └── main.rs ├── 16-proboscidea-volcanium │ ├── README.txt │ ├── input_test.txt │ ├── main.py │ ├── main2.py │ └── test.py ├── README.md ├── new ├── run_all.sh ├── template_main.py └── template_test.py ├── 2023-go ├── 01-trebuchet │ ├── README.txt │ ├── input_test.txt │ ├── input_test2.txt │ ├── main.c │ ├── main.go │ └── main_test.go ├── 02-cube-conundrum │ ├── 02-cube-conundrum │ ├── README.txt │ ├── input_test.txt │ ├── main.go │ └── main_test.go ├── 03-gear-ratios │ ├── README.txt │ ├── input_test.txt │ └── main.go ├── 04-scratchcards │ ├── README.txt │ ├── input_test.txt │ └── main.go ├── 05-if-you-give-a-seed-a-fertilizer │ ├── README.txt │ ├── input_test.txt │ ├── main.go │ └── out.txt ├── 06-wait-for-it │ ├── README.txt │ ├── input_test.txt │ └── main.go ├── 07-camel-cards │ ├── README.txt │ ├── input_test.txt │ └── main.go ├── 08-haunted-wasteland │ ├── README.txt │ ├── input_test.txt │ ├── main.go │ └── main_test.go ├── 09-mirage-maintenance │ ├── README.txt │ ├── input_test.txt │ ├── main.go │ └── main_test.go ├── 10-pipe-maze │ ├── README.txt │ ├── input_test.txt │ └── main.go ├── 11-cosmic-expansion │ ├── README.txt │ ├── input_test.txt │ └── main.go ├── 12-hot-springs │ ├── README.txt │ ├── input_2.txt │ ├── input_test.txt │ ├── main.go │ ├── solution.py │ └── test.txt ├── 13-point-of-incidence │ ├── README.txt │ ├── input_test.txt │ ├── main.go │ └── main_test.go ├── 14-parabolic-reflector-dish │ ├── README.txt │ ├── input_test.txt │ ├── main.go │ └── main_test.go ├── 15-lens-library │ ├── README.txt │ ├── input_test.txt │ └── main.go ├── 16-the-floor-will-be-lava │ ├── README.txt │ ├── input_test.txt │ └── main.go ├── 17-clumsy-crucible │ ├── README.txt │ ├── input_test.txt │ ├── main.go │ └── pt1.go.bak ├── 18-lavaduct-lagoon │ ├── README.txt │ ├── input_test.txt │ └── main.go ├── 19-aplenty │ ├── README.txt │ ├── input_test.txt │ └── main.go ├── 20-pulse-propagation │ ├── README.txt │ ├── input_test.txt │ ├── input_test_2.txt │ └── main.go ├── 21-step-counter │ ├── README.txt │ ├── input_test.txt │ ├── main.go │ └── main.py ├── 22-sand-slabs │ ├── README.txt │ ├── input_test.txt │ ├── main.go │ └── main.go.bak ├── 23-a-long-walk │ ├── README.txt │ ├── input_test.txt │ ├── main.go │ ├── main_test.go │ └── test.js ├── 24-never-tell-me-the-odds │ ├── README.txt │ ├── input_test.txt │ ├── main.go │ └── main.py ├── 25-snowverload │ ├── README.txt │ ├── input_test.txt │ ├── input_test_2.txt │ └── main.go ├── README.md ├── go.mod ├── go.sum ├── new.sh ├── run.sh ├── template_main.go └── template_main_test.go ├── 2024-php ├── .gitignore ├── 01.php ├── 02.php ├── 03.php ├── 04.php ├── 05.php ├── 06.php ├── 07.php ├── 08.php ├── 09.php ├── 10.php ├── 11.php ├── 12.php ├── 13.php ├── 14.php ├── 15.php ├── 16.php ├── 17.php ├── 18.php ├── 19.php ├── 20.php ├── 22.php ├── 23.php ├── 24.php ├── 25.php ├── README.md ├── new.sh ├── run.sh └── template.php ├── README.md └── UNLICENSE /.clang-format: -------------------------------------------------------------------------------- 1 | TabWidth: 2 2 | UseTab: Never 3 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # top-most EditorConfig file 2 | root = true 3 | 4 | [*] 5 | trim_trailing_whitespace = true 6 | insert_final_newline = true 7 | 8 | [*.c] 9 | charset = utf-8 10 | indent_size = 2 11 | indent_style = space 12 | 13 | [*.cpp] 14 | charset = utf-8 15 | indent_size = 2 16 | indent_style = space 17 | 18 | [Makefile] 19 | indent_style = tab 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | a.out 2 | aoc2020 3 | aoc2020debug 4 | **/vgcore* 5 | **/gmon.out 6 | **/prof_output 7 | **/cachegrind.out* 8 | **/*.gcda 9 | .idea 10 | .vscode 11 | input.txt 12 | *.sublime-* 13 | perf.data 14 | perf.data.old 15 | 16 | -------------------------------------------------------------------------------- /2015-c/.clangd: -------------------------------------------------------------------------------- 1 | CompileFlags: 2 | Add: [-std=c99, -Wall, -Wextra, -Wpedantic ] 3 | -------------------------------------------------------------------------------- /2015-c/.gitignore: -------------------------------------------------------------------------------- 1 | */a.out 2 | */main 3 | -------------------------------------------------------------------------------- /2015-c/01-not-quite-lisp/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 1: Not Quite Lisp --- 2 | 3 | Santa was hoping for a white Christmas, but his weather machine's "snow" function is powered by stars, and he's fresh out! To save Christmas, he needs you to collect fifty stars by December 25th. 4 | 5 | https://adventofcode.com/2015/day/1 6 | -------------------------------------------------------------------------------- /2015-c/01-not-quite-lisp/main.c: -------------------------------------------------------------------------------- 1 | #include "../adventofcode.h" 2 | #include 3 | #include 4 | 5 | int main(void) { 6 | clock_t start_t = clock_time(); 7 | char input[64 * 1024] = ""; 8 | int ninput = (int)read_input_file(input, 64 * 1024, "input.txt"); 9 | 10 | int v = 0; 11 | int pos = -1; 12 | for (int i = 0; i < ninput; i++) { 13 | if (input[i] == '(') { 14 | v++; 15 | } else if (input[i] == ')') { 16 | v--; 17 | } 18 | 19 | if (pos == -1 && v == -1) { 20 | pos = i + 1; 21 | } 22 | } 23 | 24 | fprintf(stdout, "--- Day 1: Not Quite Lisp ---\n"); 25 | fprintf(stdout, "Part 1: %d\n", v); 26 | fprintf(stdout, "Part 2: %d\n", pos); 27 | printf("Time: %.2f ms\n", clock_time_since(start_t)); 28 | return EXIT_SUCCESS; 29 | } 30 | -------------------------------------------------------------------------------- /2015-c/02-i-was-told-there-would-be-no-math/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 2: I Was Told There Would Be No Math --- 2 | 3 | The elves are running low on wrapping paper, and so they need to submit an order for more. They have a list of the dimensions (length l, width w, and height h) of each present, and only want to order exactly as much as they need. 4 | 5 | https://adventofcode.com/2015/day/2 6 | -------------------------------------------------------------------------------- /2015-c/03-perfectly-spherical-houses-in-a-vacuum/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 3: Perfectly Spherical Houses in a Vacuum --- 2 | 3 | Santa is delivering presents to an infinite two-dimensional grid of houses. 4 | 5 | https://adventofcode.com/2015/day/3 6 | -------------------------------------------------------------------------------- /2015-c/04-md5/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 4: The Ideal Stocking Stuffer --- 2 | 3 | Santa needs help mining some AdventCoins (very similar to bitcoins) to use as gifts for all the economically forward-thinking little girls and boys. 4 | 5 | https://adventofcode.com/2015/day/4 6 | -------------------------------------------------------------------------------- /2015-c/05-nice-strings/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 5: Doesn't He Have Intern-Elves For This? --- 2 | 3 | Santa needs help figuring out which strings in his text file are naughty or nice. 4 | 5 | https://adventofcode.com/2015/day/5 6 | -------------------------------------------------------------------------------- /2015-c/06-probably-a-fire-hazard/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 6: Probably a Fire Hazard --- 2 | 3 | Because your neighbors keep defeating you in the holiday house decorating contest year after year, you've decided to deploy one million lights in a 1000x1000 grid. 4 | 5 | https://adventofcode.com/2015/day/6 6 | -------------------------------------------------------------------------------- /2015-c/07-some-assembly-required/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 7: Some Assembly Required --- 2 | 3 | This year, Santa brought little Bobby Tables a set of wires and bitwise logic gates! Unfortunately, little Bobby is a little under the recommended age range, and he needs help assembling the circuit. 4 | 5 | https://adventofcode.com/2015/day/7 6 | -------------------------------------------------------------------------------- /2015-c/08-matchsticks/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 8: Matchsticks --- 2 | 3 | Space on the sleigh is limited this year, and so Santa will be bringing his list as a digital copy. He needs to know how much space it will take up when stored. 4 | 5 | https://adventofcode.com/2015/day/8 6 | -------------------------------------------------------------------------------- /2015-c/08-matchsticks/main.c: -------------------------------------------------------------------------------- 1 | #include "../adventofcode.h" 2 | #include 3 | 4 | int main(void) { 5 | clock_t start_t = clock_time(); 6 | char input[32 * 1024]; 7 | read_input_file(input, 32 * 1024, "input.txt"); 8 | 9 | char *s = input; 10 | int pt1 = 0; 11 | int pt2 = 0; 12 | 13 | while (*s != '\0') { 14 | // skip opening " 15 | s++; 16 | 17 | // opening and closing "" 18 | pt1 += 2; 19 | pt2 += 4; 20 | 21 | while (*s != '"') { 22 | if (*s == '\\') { 23 | // hexadecimal ASCII 24 | if (*(s + 1) == 'x') { 25 | s += 4; 26 | pt1 += 3; 27 | pt2 += 1; 28 | } else { 29 | // single escaped character 30 | s += 2; 31 | pt1 += 1; 32 | pt2 += 2; 33 | } 34 | 35 | continue; 36 | } 37 | 38 | // normal character 39 | s++; 40 | } 41 | 42 | // skip closing " 43 | s++; 44 | 45 | // skip newline 46 | s++; 47 | } 48 | 49 | printf("--- Day 8: Matchsticks ---\n"); 50 | printf("Part 1: %d %s\n", pt1, pt1 == 1342 ? "✔" : ""); 51 | printf("Part 2: %d %s\n", pt2, pt2 == 2074 ? "✔" : ""); 52 | printf("Time: %.2f ms\n", clock_time_since(start_t)); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /2015-c/09-all-in-a-single-night/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 9: All in a Single Night --- 2 | 3 | Every year, Santa manages to deliver all of his presents in a single night. 4 | 5 | https://adventofcode.com/2015/day/9 6 | -------------------------------------------------------------------------------- /2015-c/09-all-in-a-single-night/input_test.txt: -------------------------------------------------------------------------------- 1 | London to Dublin = 464 2 | London to Belfast = 518 3 | Dublin to Belfast = 141 4 | -------------------------------------------------------------------------------- /2015-c/10-elves-look-elves-say/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 10: Elves Look, Elves Say --- 2 | 3 | Today, the Elves are playing a game called look-and-say. They take turns making sequences by reading aloud the previous sequence and using that reading as the next sequence. For example, 211 is read as "one two, two ones", which becomes 1221 (1 2, 2 1s). 4 | 5 | https://adventofcode.com/2015/day/10 6 | -------------------------------------------------------------------------------- /2015-c/10-elves-look-elves-say/main.c: -------------------------------------------------------------------------------- 1 | #include "../adventofcode.h" 2 | #include 3 | #include 4 | #include 5 | 6 | int apply(char *dst, char *in) { 7 | char *s = in; 8 | char *o = dst; 9 | int d; 10 | 11 | while (*s != '\0') { 12 | d = 1; 13 | for (; s[d] == *s; d++) 14 | ; 15 | 16 | *o++ = (char)(d + '0'); 17 | *o++ = *s; 18 | s += d; 19 | } 20 | 21 | *o = '\0'; 22 | return (int)(o - dst); 23 | } 24 | 25 | int main(void) { 26 | clock_t start_t = clock_time(); 27 | 28 | // single malloc call for 16MB of memory 29 | char *input = malloc_or_die(1024 * 1024 * 16); 30 | read_input_file(input, 64, "input.txt"); 31 | 32 | char *buf = input + (1024 * 1024 * 8 * sizeof(char)); 33 | char *tmp; 34 | 35 | int pt1; 36 | for (int i = 0; i < 40; i++) { 37 | pt1 = apply(buf, input); 38 | tmp = input; 39 | input = buf; 40 | buf = tmp; 41 | } 42 | 43 | int pt2; 44 | for (int i = 0; i < 10; i++) { 45 | pt2 = apply(buf, input); 46 | tmp = input; 47 | input = buf; 48 | buf = tmp; 49 | } 50 | 51 | printf("--- Day 10: Elves Look, Elves Say ---\n"); 52 | printf("Part 1: %d %s\n", pt1, pt1 == 492982 ? "✔" : ""); 53 | printf("Part 2: %d %s\n", pt2, pt2 == 6989950 ? "✔" : ""); 54 | printf("Time: %.2f ms\n", clock_time_since(start_t)); 55 | free(input); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /2015-c/11-corporate-policy/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 11: Corporate Policy --- 2 | 3 | Santa's previous password expired, and he needs help choosing a new one. 4 | 5 | https://adventofcode.com/2015/day/11 6 | -------------------------------------------------------------------------------- /2015-c/12-jsabacusframework-io/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 12: JSAbacusFramework.io --- 2 | 3 | Santa's Accounting-Elves need help balancing the books after a recent order. Unfortunately, their accounting software uses a peculiar storage format. That's where you come in. 4 | 5 | https://adventofcode.com/2015/day/12 6 | -------------------------------------------------------------------------------- /2015-c/13-knights-of-the-dinner-table/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 9: All in a Single Night --- 2 | 3 | Every year, Santa manages to deliver all of his presents in a single night. 4 | 5 | https://adventofcode.com/2015/day/9 6 | -------------------------------------------------------------------------------- /2015-c/13-knights-of-the-dinner-table/input_test.txt: -------------------------------------------------------------------------------- 1 | Alice would gain 54 happiness units by sitting next to Bob. 2 | Alice would lose 79 happiness units by sitting next to Carol. 3 | Alice would lose 2 happiness units by sitting next to David. 4 | Bob would gain 83 happiness units by sitting next to Alice. 5 | Bob would lose 7 happiness units by sitting next to Carol. 6 | Bob would lose 63 happiness units by sitting next to David. 7 | Carol would lose 62 happiness units by sitting next to Alice. 8 | Carol would gain 60 happiness units by sitting next to Bob. 9 | Carol would gain 55 happiness units by sitting next to David. 10 | David would gain 46 happiness units by sitting next to Alice. 11 | David would lose 7 happiness units by sitting next to Bob. 12 | David would gain 41 happiness units by sitting next to Carol. -------------------------------------------------------------------------------- /2015-c/14-reindeer-olympics/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 15: Science for Hungry People --- 2 | 3 | Today, you set out on the task of perfecting your milk-dunking cookie recipe. All you have to do is find the right balance of ingredients. 4 | 5 | https://adventofcode.com/2015/day/15 6 | -------------------------------------------------------------------------------- /2015-c/14-reindeer-olympics/input_test.txt: -------------------------------------------------------------------------------- 1 | Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds. 2 | Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds. -------------------------------------------------------------------------------- /2015-c/15-science-for-hungry-people/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 15: Science for Hungry People --- 2 | 3 | Today, you set out on the task of perfecting your milk-dunking cookie recipe. All you have to do is find the right balance of ingredients. 4 | 5 | https://adventofcode.com/2015/day/15 6 | -------------------------------------------------------------------------------- /2015-c/15-science-for-hungry-people/input_test.txt: -------------------------------------------------------------------------------- 1 | Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8 2 | Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3 3 | -------------------------------------------------------------------------------- /2015-c/16-aunt-sue/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 16: Aunt Sue --- 2 | 3 | Your Aunt Sue has given you a wonderful gift, and you'd like to send her a thank you card. However, there's a small problem: she signed it "From, Aunt Sue". 4 | 5 | https://adventofcode.com/2015/day/16 6 | -------------------------------------------------------------------------------- /2015-c/17-no-such-thing-as-too-much/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 17: No Such Thing as Too Much --- 2 | 3 | The elves bought too much eggnog again - 150 liters this time. To fit it all into your refrigerator, you'll need to move it into smaller containers. You take an inventory of the capacities of the available containers. 4 | 5 | https://adventofcode.com/2015/day/17 6 | -------------------------------------------------------------------------------- /2015-c/18-like-a-gif-for-your-yard/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 18: Like a GIF For Your Yard --- 2 | 3 | After the million lights incident, the fire code has gotten stricter: now, at most ten thousand lights are allowed. You arrange them in a 100x100 grid. 4 | 5 | https://adventofcode.com/2015/day/18 6 | -------------------------------------------------------------------------------- /2015-c/18-like-a-gif-for-your-yard/input_test.txt: -------------------------------------------------------------------------------- 1 | .#.#.# 2 | ...##. 3 | #....# 4 | ..#... 5 | #.#..# 6 | ####.. -------------------------------------------------------------------------------- /2015-c/19-medicine-for-rudolph/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 19: Medicine for Rudolph --- 2 | 3 | Rudolph the Red-Nosed Reindeer is sick! His nose isn't shining very brightly, and he needs medicine. 4 | 5 | https://adventofcode.com/2015/day/19 6 | -------------------------------------------------------------------------------- /2015-c/19-medicine-for-rudolph/input_test.txt: -------------------------------------------------------------------------------- 1 | e => H 2 | e => O 3 | H => HO 4 | H => OH 5 | O => HH 6 | 7 | HOH -------------------------------------------------------------------------------- /2015-c/19-medicine-for-rudolph/main.py: -------------------------------------------------------------------------------- 1 | 2 | def parse(): 3 | with open(0) as f: 4 | [replacements, medicine] = f.read().strip().split("\n\n") 5 | replacements = replacements.splitlines() 6 | replacements = [r.split(" => ") for r in replacements] 7 | return replacements, medicine 8 | 9 | start = "e" 10 | replacements, medicine = parse() 11 | print(medicine, replacements) 12 | 13 | steps = 0 14 | while medicine != "e": 15 | for r in replacements: 16 | if r[1] in medicine: 17 | medicine = medicine.replace(r[1], r[0], 1) 18 | steps += 1 19 | 20 | print(steps) -------------------------------------------------------------------------------- /2015-c/20-infinite-elves-and-infinite-houses/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 19: Medicine for Rudolph --- 2 | 3 | Rudolph the Red-Nosed Reindeer is sick! His nose isn't shining very brightly, and he needs medicine. 4 | 5 | https://adventofcode.com/2015/day/20 6 | -------------------------------------------------------------------------------- /2015-c/21-rpg-simulator-20xx/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 21: RPG Simulator 20XX --- 2 | 3 | Little Henry Case got a new video game for Christmas. It's an RPG, and he's stuck on a boss. He needs to know what equipment to buy at the shop. He hands you the controller. 4 | 5 | https://adventofcode.com/2015/day/21 -------------------------------------------------------------------------------- /2015-c/21-rpg-simulator-20xx/input_test.txt: -------------------------------------------------------------------------------- 1 | Hit Points: 12 2 | Damage: 7 3 | Armor: 2 -------------------------------------------------------------------------------- /2015-c/22-wizard-simulator-20xx/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 21: RPG Simulator 20XX --- 2 | 3 | Little Henry Case got a new video game for Christmas. It's an RPG, and he's stuck on a boss. He needs to know what equipment to buy at the shop. He hands you the controller. 4 | 5 | https://adventofcode.com/2015/day/21 -------------------------------------------------------------------------------- /2015-c/22-wizard-simulator-20xx/index.html: -------------------------------------------------------------------------------- 1 | 2 |
3 |
Minimaal 7 tekens
4 |
1 hoofdletter
5 |
6 | -------------------------------------------------------------------------------- /2015-c/22-wizard-simulator-20xx/input_test.txt: -------------------------------------------------------------------------------- 1 | Hit Points: 14 2 | Damage: 8 3 | -------------------------------------------------------------------------------- /2015-c/23-opening-the-turing-lock/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 23: Opening the Turing Lock --- 2 | 3 | Little Jane Marie just got her very first computer for Christmas from some unknown benefactor. It comes with instructions and an example program, but the computer itself seems to be malfunctioning. She's curious what the program does, and would like you to help her run it. 4 | 5 | https://adventofcode.com/2015/day/23 6 | -------------------------------------------------------------------------------- /2015-c/23-opening-the-turing-lock/input_test.txt: -------------------------------------------------------------------------------- 1 | inc a 2 | jio a, +2 3 | tpl a 4 | inc a 5 | -------------------------------------------------------------------------------- /2015-c/24-it-hangs-in-the-balance/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 25: Let It Snow --- 2 | 3 | Merry Christmas! Santa is booting up his weather machine; looks like you might get a white Christmas after all. 4 | 5 | https://adventofcode.com/2015/day/25 -------------------------------------------------------------------------------- /2015-c/24-it-hangs-in-the-balance/input_test.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | 4 5 | 5 6 | 7 7 | 8 8 | 9 9 | 10 10 | 11 -------------------------------------------------------------------------------- /2015-c/24-it-hangs-in-the-balance/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using std::stoi; 8 | using std::string; 9 | using std::vector; 10 | 11 | int main() { 12 | vector weights; 13 | string line; 14 | while (getline(std::cin, line)) { 15 | weights.emplace_back(stoi(line)); 16 | } 17 | 18 | // for part 1, a greedy algorithm works 19 | std::sort(weights.begin(), weights.end(), std::greater()); 20 | int sum = std::accumulate(weights.begin(), weights.end(), 0); 21 | int group_sum = sum / 3; 22 | uint64_t qe = 1; 23 | for (int s = 0, i = 0; i < weights.size() && s < group_sum; i++) { 24 | if (s + weights[i] < group_sum) { 25 | s += weights[i]; 26 | qe *= weights[i]; 27 | } 28 | } 29 | 30 | std::cout << qe << std::endl; 31 | } 32 | -------------------------------------------------------------------------------- /2015-c/25-let-it-snow/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 25: Let It Snow --- 2 | 3 | Merry Christmas! Santa is booting up his weather machine; looks like you might get a white Christmas after all. 4 | 5 | https://adventofcode.com/2015/day/25 -------------------------------------------------------------------------------- /2015-c/25-let-it-snow/input_test.txt: -------------------------------------------------------------------------------- 1 | To continue, please consult the code grid in the manual. Enter the code at row 2981, column 3075. -------------------------------------------------------------------------------- /2015-c/25-let-it-snow/main.c: -------------------------------------------------------------------------------- 1 | #include "../adventofcode.h" 2 | #include 3 | #include 4 | #include 5 | 6 | void parse(const char *s, int *row, int *col) { 7 | s = skip_until_digit(s); 8 | s = parse_int(row, s); 9 | s = skip_until_digit(s); 10 | s = parse_int(col, s); 11 | } 12 | 13 | unsigned long value_at(int row, int col) { 14 | int r = 6; 15 | int c = 6; 16 | unsigned long v = 27995004; 17 | 18 | for (; r < row || c < col;) { 19 | if (r == 1) { 20 | r = c + 1; 21 | c = 1; 22 | } else { 23 | r -= 1; 24 | c += 1; 25 | } 26 | 27 | v = (v * 252533) % 33554393; 28 | } 29 | return v; 30 | } 31 | 32 | int main(void) { 33 | clock_t start_t = clock_time(); 34 | 35 | char input[1024]; 36 | read_input_file(input, 1024, "input.txt"); 37 | int row, col; 38 | parse(input, &row, &col); 39 | 40 | unsigned long a1 = value_at(row, col); 41 | printf("--- Day 25: Let It Snow ---\n"); 42 | printf("Part 1: %ld\n", a1); 43 | printf("Time: %.2f ms\n", clock_time_since(start_t)); 44 | return EXIT_SUCCESS; 45 | } 46 | -------------------------------------------------------------------------------- /2015-c/memcheck-all.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | for d in */; do 6 | cd "$d" 7 | cc -O2 -Wall -Werror -Wpedantic -g -fsanitize=undefined main.c -lcrypto 8 | valgrind -s --track-origins=yes --leak-check=full ./a.out 9 | echo "" 10 | read -n 1 -p "Next? [Any key to proceed]" 11 | cd .. 12 | done 13 | -------------------------------------------------------------------------------- /2015-c/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | CC="${CC:=gcc}" 4 | CFLAGS="$CFLAGS 5 | -O2 6 | -march=native 7 | -mtune=native 8 | -Wall 9 | -Wextra 10 | -Wpedantic 11 | -std=c99 12 | -Wformat=2 13 | -Wconversion 14 | -Wtrampolines 15 | -Wimplicit-fallthrough 16 | " 17 | 18 | $CC --version 19 | TIME_LINES="" 20 | for d in */; do 21 | cd "$d" 22 | $CC $CFLAGS main.c -lcrypto 23 | OUT=$(./a.out) 24 | echo -e "$OUT\n" 25 | 26 | TIME_LINES+=$(echo -e "$OUT" | grep "Time: ") 27 | TIME_LINES+="\n" 28 | 29 | cd .. 30 | done 31 | 32 | echo -e "$TIME_LINES" | awk 'BEGIN {sum=0.0} { gsub(/(Time: | ms)/,"", $2); sum += $2; } END { printf "Total time: %.3f ms\n", sum }' 33 | -------------------------------------------------------------------------------- /2016-c/.clangd: -------------------------------------------------------------------------------- 1 | CompileFlags: 2 | Add: [-std=c11, -Wall, -Wextra, -Wpedantic ] 3 | -------------------------------------------------------------------------------- /2016-c/01-no-time-for-a-taxicab/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 1: No Time for a Taxicab --- 2 | 3 | Santa's sleigh uses a very high-precision clock to guide its movements, and the clock's oscillator is regulated by stars. Unfortunately, the stars have been stolen... by the Easter Bunny. To save Christmas, Santa needs you to retrieve all fifty stars by December 25th. 4 | 5 | https://adventofcode.com/2016/day/1 6 | -------------------------------------------------------------------------------- /2016-c/01-no-time-for-a-taxicab/input_test.txt: -------------------------------------------------------------------------------- 1 | R8, R4, R4, R8 2 | -------------------------------------------------------------------------------- /2016-c/01-no-time-for-a-taxicab/main.c: -------------------------------------------------------------------------------- 1 | #include "../adventofcode.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() { 8 | clock_t start_t = clock_time(); 9 | 10 | char input[64 * 1024]; 11 | read_input_file(input, 64 * 1024, "input.txt"); 12 | 13 | const char *s = input; 14 | int pos[] = {0, 0}; 15 | int dirs[][2] = { 16 | {0, 1}, // N 17 | {1, 0}, // E 18 | {0, -1}, // S 19 | {-1, 0}, // W 20 | }; 21 | int cdir = 0; // N 22 | 23 | char dir; 24 | int amount; 25 | char seen[256 * 256] = {0}; 26 | int pt2 = -1; 27 | 28 | while (*s != '\0') { 29 | dir = *s++; 30 | s = parse_int(&amount, s); 31 | 32 | // skip comma and space 33 | while (*s == ',' || *s == ' ' || *s == '\n') 34 | s++; 35 | 36 | if (dir == 'R') { 37 | cdir = (cdir == 3) ? 0 : cdir + 1; 38 | } else { 39 | cdir = (cdir == 0) ? 3 : cdir - 1; 40 | } 41 | 42 | while (amount > 0) { 43 | pos[0] += dirs[cdir][0]; 44 | pos[1] += dirs[cdir][1]; 45 | amount--; 46 | 47 | if (pt2 == -1) { 48 | int idx = (pos[0] + 128) * 256 + (pos[1] + 128); 49 | if (seen[idx] == 1) { 50 | pt2 = abs(pos[0]) + abs(pos[1]); 51 | } 52 | 53 | seen[idx] = 1; 54 | } 55 | } 56 | } 57 | 58 | printf("--- Day 1: No Time for a Taxicab ---\n"); 59 | printf("Part 1: %d\n", abs(pos[0]) + abs(pos[1])); 60 | printf("Part 2: %d\n", pt2); 61 | printf("Time: %.2fms\n", clock_time_since(start_t)); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /2016-c/04-security-through-obscurity/input_test.txt: -------------------------------------------------------------------------------- 1 | aaaaa-bbb-z-y-x-123[abxyz] 2 | a-b-c-d-e-f-g-h-987[abcde] 3 | not-a-real-room-404[oarel] 4 | totally-real-room-200[decoy] 5 | -------------------------------------------------------------------------------- /2016-c/05-how-about-a-nice-game-of-chess/input_test.txt: -------------------------------------------------------------------------------- 1 | aaaaa-bbb-z-y-x-123[abxyz] 2 | a-b-c-d-e-f-g-h-987[abcde] 3 | not-a-real-room-404[oarel] 4 | totally-real-room-200[decoy] 5 | -------------------------------------------------------------------------------- /2016-c/05-how-about-a-nice-game-of-chess/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void solve(char *doorid, char *password, char *password2) { 7 | int p1 = 0; 8 | int p2 = 0; 9 | int pos; 10 | strcpy(password2, "........"); 11 | size_t keylen = strlen(doorid); 12 | char input[64] = "abbhdwsy"; 13 | char digest[MD5_DIGEST_LENGTH]; 14 | 15 | char buf[64]; 16 | char *dst = input + keylen; 17 | int i = 1; 18 | for (;; i++) { 19 | sprintf(dst, "%d", i); 20 | MD5((unsigned char *)input, keylen + strlen(dst), (unsigned char *)digest); 21 | 22 | unsigned char skip = 23 | (unsigned char)(digest[0] | digest[1] | (digest[2] & 0xf0)); 24 | if (0 == skip) { 25 | sprintf(buf, "%02x%02x", digest[2], digest[3]); 26 | 27 | if (p1 < 8) { 28 | password[p1++] = buf[1]; 29 | } 30 | 31 | pos = buf[1] - '0'; 32 | if (pos >= 0 && pos < 8 && password2[pos] == '.') { 33 | password2[pos] = buf[2]; 34 | p2++; 35 | } 36 | 37 | if (p1 >= 8 && p2 >= 8) { 38 | break; 39 | } 40 | } 41 | } 42 | 43 | // EVP_MD_CTX_free(mdctx); 44 | } 45 | 46 | int main() { 47 | clock_t start_t, end_t; 48 | start_t = clock(); 49 | 50 | char pt1buf[16] = ""; 51 | char pt2buf[16] = ""; 52 | solve("abbhdwsy", pt1buf, pt2buf); 53 | 54 | printf("--- Day 5: How About a Nice Game of Chess? ---\n"); 55 | printf("Part 1: %s\n", pt1buf); 56 | printf("Part 2: %s\n", pt2buf); 57 | 58 | end_t = clock(); 59 | double total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC * 1000; 60 | printf("Time: %.2fms\n", total_t); 61 | } 62 | -------------------------------------------------------------------------------- /2016-c/06-signals-and-noise/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 6: Signals and Noise --- 2 | 3 | Something is jamming your communications with Santa. Fortunately, your signal is only partially jammed, and protocol in situations like this is to switch to a simple repetition code to get the message through. 4 | 5 | https://adventofcode.com/2016/day/6 6 | -------------------------------------------------------------------------------- /2016-c/06-signals-and-noise/input_test.txt: -------------------------------------------------------------------------------- 1 | eedadn 2 | drvtee 3 | eandsr 4 | raavrd 5 | atevrs 6 | tsrnev 7 | sdttsa 8 | rasrtv 9 | nssdts 10 | ntnada 11 | svetve 12 | tesnvt 13 | vntsnd 14 | vrdear 15 | dvrsen 16 | enarar 17 | 18 | -------------------------------------------------------------------------------- /2016-c/06-signals-and-noise/main.c: -------------------------------------------------------------------------------- 1 | #include "../adventofcode.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define PUZZLE_NAME "Day 6: Signals and Noise" 8 | 9 | void pt1(char *pt1, char *pt2, char *input) { 10 | int counts[8][26]; 11 | memset(counts, 0, 26 * 8 * sizeof(int)); 12 | char *s = input; 13 | int col = 0; 14 | while (*s != 0x0) { 15 | counts[col++][*s++ - 'a']++; 16 | 17 | if (*s == '\n') { 18 | s++; 19 | col = 0; 20 | } 21 | } 22 | 23 | for (col = 0; col < 8; col++) { 24 | char max = 'a'; 25 | char least = 'a'; 26 | for (char ch = 'b'; ch <= 'z'; ch++) { 27 | if (counts[col][ch - 'a'] > counts[col][max - 'a']) { 28 | max = ch; 29 | } 30 | 31 | if (counts[col][ch - 'a'] < counts[col][least - 'a']) { 32 | least = ch; 33 | } 34 | } 35 | pt1[col] = max; 36 | pt2[col] = least; 37 | } 38 | pt1[9] = 0x0; 39 | pt2[9] = 0x0; 40 | } 41 | 42 | int main() { 43 | clock_t t = clock_time(); 44 | char input[1024 * 64] = ""; 45 | read_input_file(input, 1024 * 64, "input.txt"); 46 | 47 | char a1[32]; 48 | char a2[32]; 49 | pt1(a1, a2, input); 50 | 51 | printf("--- %s ---\n", PUZZLE_NAME); 52 | printf("Part 1: %s\n", a1); 53 | printf("Part 2: %s\n", a2); 54 | printf("Time: %.2fms\n", clock_time_since(t)); 55 | return EXIT_SUCCESS; 56 | } 57 | -------------------------------------------------------------------------------- /2016-c/07-internet-protocol-version-7/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 7: Internet Protocol Version 7 --- 2 | 3 | While snooping around the local network of EBHQ, you compile a list of IP addresses (they're IPv7, of course; IPv6 is much too limited). You'd like to figure out which IPs support TLS (transport-layer snooping). 4 | 5 | https://adventofcode.com/2016/day/7 6 | -------------------------------------------------------------------------------- /2016-c/07-internet-protocol-version-7/input_test.txt: -------------------------------------------------------------------------------- 1 | aba[bab]xyz 2 | xyx[xyx]xyx 3 | aaa[kek]eke 4 | zazbz[bzb]cdb 5 | 6 | -------------------------------------------------------------------------------- /2016-c/08-two-factor-authentication/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 8: Two-Factor Authentication --- 2 | 3 | You come across a door implementing what you can only assume is an implementation of two-factor authentication after a long game of requirements telephone. 4 | 5 | https://adventofcode.com/2016/day/8 6 | -------------------------------------------------------------------------------- /2016-c/08-two-factor-authentication/input_test.txt: -------------------------------------------------------------------------------- 1 | rect 3x2 2 | rotate column x=1 by 1 3 | rotate row y=0 by 4 4 | rotate row y=0 by 4 5 | 6 | -------------------------------------------------------------------------------- /2016-c/09-explosives-in-cyberspace/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 9: Explosives in Cyberspace --- 2 | 3 | Wandering around a secure area, you come across a datalink port to a new part of the network. After briefly scanning it for interesting files, you find one file in particular that catches your attention. It's compressed with an experimental format, but fortunately, the documentation for the format is nearby. 4 | 5 | https://adventofcode.com/2016/day/9 6 | -------------------------------------------------------------------------------- /2016-c/09-explosives-in-cyberspace/input_test.txt: -------------------------------------------------------------------------------- 1 | (25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN 2 | -------------------------------------------------------------------------------- /2016-c/new.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | mkdir $1 6 | touch $1/main.c 7 | touch $1/input.txt 8 | touch $1/input_test.txt 9 | 10 | cat template.c > $1/main.c 11 | touch $1/README.txt 12 | 13 | cd "$1" 14 | -------------------------------------------------------------------------------- /2016-c/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | CC="${CC:=gcc}" 4 | CFLAGS="$CFLAGS 5 | -O2 -flto -march=native -mtune=native" 6 | 7 | TIME_LINES="" 8 | for d in */; do 9 | cd "$d" 10 | $CC $CFLAGS main.c -lcrypto 11 | OUT=$(./a.out) 12 | TIME_LINES+=$(echo -e "$OUT" | grep "Time: ") 13 | TIME_LINES+="\n" 14 | echo -e "$OUT\n" 15 | cd .. 16 | done 17 | 18 | # Print sum of runtimes 19 | echo -e "$TIME_LINES" | awk 'BEGIN {sum=0.0} { gsub(/(Time: | μs)/,"", $2); sum += $2; } END { printf "Total time: %.3f ms\n", sum }' 20 | -------------------------------------------------------------------------------- /2016-c/template.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "../adventofcode.h" 6 | 7 | #define PUZZLE_NAME "Day 10: Knot Hash" 8 | 9 | 10 | int main() { 11 | clock_t t = timer_start(); 12 | char input[1024 * 64]; 13 | read_input_file(input, "input.txt"); 14 | 15 | printf("--- %s ---\n", PUZZLE_NAME); 16 | printf("Part 1: %d\n", 0); 17 | printf("Part 2: %d\n", 0); 18 | printf("Time: %.2fms\n", timer_stop(t)); 19 | return EXIT_SUCCESS; 20 | } 21 | -------------------------------------------------------------------------------- /2016-cpp/.clangd: -------------------------------------------------------------------------------- 1 | CompileFlags: 2 | Add: [-std=c++20, -Wall, -Wextra, -Wpedantic ] 3 | -------------------------------------------------------------------------------- /2016-cpp/03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | bool is_valid_triangle(int a, int b, int c) { 6 | return a + b > c && a + c > b && b + c > a; 7 | } 8 | 9 | int main() { 10 | auto tstart = std::chrono::high_resolution_clock::now(); 11 | int pt1 = 0; 12 | int pt2 = 0; 13 | 14 | int a, b, c; 15 | std::string input; 16 | int window[3][3]; 17 | int wi = 0; 18 | 19 | while (std::getline(std::cin, input)) { 20 | a = std::stoi(&input[0]); 21 | b = std::stoi(&input[5]); 22 | c = std::stoi(&input[10]); 23 | 24 | if (is_valid_triangle(a, b, c)) { 25 | pt1++; 26 | } 27 | 28 | window[0][wi] = a; 29 | window[1][wi] = b; 30 | window[2][wi] = c; 31 | 32 | // every 3 rows 33 | if (wi == 2) { 34 | for (int i = 0; i < 3; i++) { 35 | if (is_valid_triangle(window[i][0], window[i][1], window[i][2])) { 36 | pt2++; 37 | } 38 | } 39 | wi = 0; 40 | } else { 41 | wi++; 42 | } 43 | } 44 | 45 | std::cout << "--- Day 3: Squares With Three Sides ---\n"; 46 | std::cout << "Part 1: " << pt1 << "\n"; 47 | std::cout << "Part 2: " << pt2 << "\n"; 48 | 49 | auto tstop = std::chrono::high_resolution_clock::now(); 50 | auto duration = 51 | std::chrono::duration_cast(tstop - tstart); 52 | std::cout << "Time: " << duration.count() << " μs" 53 | << "\n"; 54 | return EXIT_SUCCESS; 55 | } 56 | -------------------------------------------------------------------------------- /2016-cpp/06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::string; 6 | 7 | int main() { 8 | auto tstart = std::chrono::high_resolution_clock::now(); 9 | 10 | std::unordered_map maps[8]; 11 | for (unsigned int col = 0; col < 8; col++) { 12 | for (char ch = 'a'; ch <= 'z'; ch++) { 13 | maps[col][ch] = 0; 14 | } 15 | } 16 | string input; 17 | while (std::getline(std::cin, input)) { 18 | for (unsigned int col = 0; col < 8; col++) { 19 | maps[col][input[col]]++; 20 | } 21 | } 22 | 23 | string pt1(8, 'a'); 24 | string pt2(8, 'a'); 25 | 26 | for (unsigned int col = 0; col < 8; col++) { 27 | for (char ch = 'b'; ch <= 'z'; ch++) { 28 | if (maps[col][ch] > maps[col][pt1[col]]) { 29 | pt1[col] = ch; 30 | } 31 | 32 | if (maps[col][ch] < maps[col][pt2[col]]) { 33 | pt2[col] = ch; 34 | } 35 | } 36 | } 37 | 38 | std::cout << "--- Day 6: Signals and Noise ---\n"; 39 | std::cout << "Part 1: " << pt1 << "\n"; 40 | std::cout << "Part 2: " << pt2 << "\n"; 41 | 42 | auto tstop = std::chrono::high_resolution_clock::now(); 43 | auto duration = 44 | std::chrono::duration_cast(tstop - tstart); 45 | std::cout << "Time: " << duration.count() << " μs" 46 | << "\n"; 47 | return EXIT_SUCCESS; 48 | } 49 | -------------------------------------------------------------------------------- /2016-cpp/19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Josephus Problem 5 | // https://www.youtube.com/watch?v=uCsD3ZGzMgE 6 | // a = largest power of 2 < n 7 | // l = n - a 8 | // Winner(n) = (2*l)+1 9 | unsigned int pt1(unsigned int n) { 10 | unsigned int a = 1; 11 | while (a < n) { 12 | a <<= 1; 13 | } 14 | a >>= 1; 15 | return (2 * (n - a)) + 1; 16 | } 17 | 18 | unsigned int pt2(unsigned int n) { 19 | unsigned int w = 1; 20 | 21 | for (unsigned int i = 1; i < n; i++) { 22 | w = w % i + 1; 23 | if (w > (i + 1) / 2) { 24 | w++; 25 | } 26 | } 27 | return w; 28 | } 29 | 30 | int main() { 31 | auto tstart = std::chrono::high_resolution_clock::now(); 32 | 33 | std::string input; 34 | std::getline(std::cin, input); 35 | 36 | unsigned int number_of_elves = static_cast(std::stoi(input)); 37 | 38 | unsigned a1 = pt1(number_of_elves); 39 | unsigned a2 = pt2(number_of_elves); 40 | 41 | std::cout << "--- Day 19: An Elephant Named Joseph ---\n"; 42 | std::cout << "Part 1: " << a1 << "\n"; 43 | std::cout << "Part 2: " << a2 << "\n"; 44 | 45 | auto tstop = std::chrono::high_resolution_clock::now(); 46 | auto duration = 47 | std::chrono::duration_cast(tstop - tstart); 48 | std::cout << "Time: " << duration.count() << " μs" 49 | << "\n"; 50 | return EXIT_SUCCESS; 51 | } 52 | -------------------------------------------------------------------------------- /2016-cpp/23.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::string; 5 | 6 | int factorial(int n) { 7 | int p = n--; 8 | 9 | while (n > 0) { 10 | p *= n--; 11 | } 12 | return p; 13 | } 14 | 15 | int main() { 16 | auto tstart = std::chrono::high_resolution_clock::now(); 17 | int pt1 = 0; 18 | int pt2 = 0; 19 | 20 | string input; 21 | int c = 1; 22 | 23 | // parse input to find our constant 24 | // taken from the JNZ or CPY instruction 25 | // with first parameter larger than 10 26 | while (getline(std::cin, input)) { 27 | if (input[0] != 'c' && input[0] != 'j') { 28 | continue; 29 | } 30 | 31 | if (!std::isdigit(input[4])) { 32 | continue; 33 | } 34 | 35 | int v = std::stoi(&input[4]); 36 | if (v > 10) { 37 | c *= v; 38 | } 39 | } 40 | 41 | pt1 = factorial(7) + c; 42 | pt2 = factorial(12) + c; 43 | 44 | std::cout << "--- Day 23: Safe Cracking ---\n"; 45 | std::cout << "Part 1: " << pt1 << "\n"; 46 | std::cout << "Part 2: " << pt2 << "\n"; 47 | 48 | auto tstop = std::chrono::high_resolution_clock::now(); 49 | auto duration = 50 | std::chrono::duration_cast(tstop - tstart); 51 | std::cout << "Time: " << duration.count() << " μs" 52 | << "\n"; 53 | return EXIT_SUCCESS; 54 | } 55 | -------------------------------------------------------------------------------- /2016-cpp/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS+= -std=c++20 -Wall -Wextra -Wpedantic \ 2 | -Wundef -Winline -Wimplicit-fallthrough -Wformat=2 \ 3 | -Wconversion 4 | PROGRAMS=01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 5 | 6 | ifndef RELEASE 7 | CXXFLAGS+= -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS \ 8 | -fsanitize=address -fsanitize=undefined -g \ 9 | -fstack-protector-strong -D_GLIBCXX_DEBUG 10 | else 11 | CXXFLAGS+= -O2 -march=native -mtune=native 12 | endif 13 | 14 | .PHONY: all clean 15 | 16 | all: $(PROGRAMS) 17 | 18 | # These days need to be linked against -lcrypto 19 | 05 14 17: LDLIBS += -lcrypto 20 | 21 | $(PROGRAMS): 22 | 23 | clean: 24 | rm -f $(PROGRAMS) 25 | -------------------------------------------------------------------------------- /2016-cpp/new.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [[ "$AOCSESSION" == "" ]]; then 4 | echo "\$AOCSESSION environment variable not set." 5 | exit 1 6 | fi; 7 | 8 | DAY=$1 9 | DAYNAME=$(printf "%02d\n" $DAY) 10 | cp template.cpp $DAYNAME.cpp 11 | wget --no-cookies --header="Cookie: session=$AOCSESSION" https://adventofcode.com/2016/day/$DAY/input -O $DAYNAME.txt 12 | -------------------------------------------------------------------------------- /2016-cpp/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | make clean 4 | RELEASE=1 make all 5 | 6 | TIME_LINES="" 7 | for DAY in {01..25}; do 8 | OUT=$(cat $DAY.txt | ./$DAY) 9 | TIME_LINES+=$(echo -e "$OUT" | grep "Time: ") 10 | TIME_LINES+="\n" 11 | echo -e "$OUT\n" 12 | done 13 | 14 | # Print sum of runtimes 15 | echo -e "$TIME_LINES" | awk 'BEGIN {sum=0.0} { gsub(/(Time: | μs)/,"", $2); sum += $2; } END { printf "Total time: %.2f μs\n", sum }' 16 | -------------------------------------------------------------------------------- /2016-cpp/template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | auto tstart = std::chrono::high_resolution_clock::now(); 6 | int pt1 = 0; 7 | int pt2 = 0; 8 | 9 | std::string input; 10 | std::getline(std::cin, input); 11 | 12 | std::cout << "--- Day 1: No Time for a Taxicab ---\n"; 13 | std::cout << "Part 1: " << pt1 << "\n"; 14 | std::cout << "Part 2: " << pt2 << "\n"; 15 | 16 | auto tstop = std::chrono::high_resolution_clock::now(); 17 | auto duration = 18 | std::chrono::duration_cast(tstop - tstart); 19 | std::cout << "Time: " << duration.count() << " μs" 20 | << "\n"; 21 | return EXIT_SUCCESS; 22 | } 23 | -------------------------------------------------------------------------------- /2017-c/.clangd: -------------------------------------------------------------------------------- 1 | CompileFlags: 2 | Add: [-std=c17, -Wall, -Wextra, -Wpedantic ] 3 | -------------------------------------------------------------------------------- /2017-c/01-inverse-captcha/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 1: Inverse Captcha --- 2 | 3 | The night before Christmas, one of Santa's Elves calls you in a panic. "The printer's broken! We can't print the Naughty or Nice List!" By the time you make it to sub-basement 17, there are only a few minutes until midnight. "We have a big problem," she says; "there must be almost fifty bugs in this system, but nothing else can print The List. Stand in this square, quick! There's no time to explain; if you can convince them to pay you in stars, you'll be able to--" She pulls a lever and the world goes blurry. 4 | 5 | https://adventofcode.com/2017/day/1 6 | -------------------------------------------------------------------------------- /2017-c/01-inverse-captcha/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int solve(char *input, int size, int offset) { 6 | int sum = 0; 7 | for (int i = 0; i < size; i++) { 8 | int next = (i + offset) % (size-1); 9 | if (input[i] == input[next]) { 10 | sum += input[i] - '0'; 11 | } 12 | } 13 | 14 | return sum; 15 | } 16 | 17 | int main() { 18 | clock_t start_t, end_t; 19 | start_t = clock(); 20 | 21 | FILE *fp = fopen("input.txt", "r"); 22 | if (fp == NULL) { 23 | fprintf(stderr, "error reading input.txt"); 24 | exit(EXIT_FAILURE); 25 | } 26 | char input[32*1024]; 27 | int nread = fread(input, 1, 32*1024, fp); 28 | input[nread] = '\0'; 29 | fclose(fp); 30 | 31 | printf("--- Day 1: Inverse Captcha ---\n"); 32 | printf("Part 1: %d\n", solve(input, nread, 1)); 33 | printf("Part 2: %d\n", solve(input, nread, (nread-1)/2)); 34 | 35 | end_t = clock(); 36 | double total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC * 1000; 37 | printf("Time: %.2fms\n", total_t); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /2017-c/02-corruption-checksum/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 2: Corruption Checksum --- 2 | 3 | As you walk through the door, a glowing humanoid shape yells in your direction. "You there! Your state appears to be idle. Come help us repair the corruption in this spreadsheet - if we take another millisecond, we'll have to display an hourglass cursor!" 4 | 5 | https://adventofcode.com/2017/day/2 6 | -------------------------------------------------------------------------------- /2017-c/03-spiral-memory/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 4: High-Entropy Passphrases --- 2 | 3 | A new system policy has been put in place that requires all accounts to use a passphrase instead of simply a password. A passphrase consists of a series of words (lowercase letters) separated by spaces. 4 | 5 | https://adventofcode.com/2017/day/4 6 | -------------------------------------------------------------------------------- /2017-c/04-high-entropy-passphrases/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 3: Spiral Memory --- 2 | 3 | You come across an experimental new kind of memory stored on an infinite two-dimensional grid. 4 | 5 | https://adventofcode.com/2017/day/3 6 | -------------------------------------------------------------------------------- /2017-c/05-a-maze-of-twisty-trampolines-all-alike/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 5: A Maze of Twisty Trampolines, All Alike --- 2 | 3 | An urgent interrupt arrives from the CPU: it's trapped in a maze of jump instructions, and it would like assistance from any programs with spare cycles to help find the exit. 4 | 5 | https://adventofcode.com/2017/day/5 -------------------------------------------------------------------------------- /2017-c/05-a-maze-of-twisty-trampolines-all-alike/input_test.txt: -------------------------------------------------------------------------------- 1 | 0 2 | 3 3 | 0 4 | 1 5 | -3 6 | -------------------------------------------------------------------------------- /2017-c/06-memory-reallocation/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 6: Memory Reallocation --- 2 | 3 | A debugger program here is having an issue: it is trying to repair a memory reallocation routine, but it keeps getting stuck in an infinite loop. 4 | 5 | https://adventofcode.com/2017/day/6 -------------------------------------------------------------------------------- /2017-c/06-memory-reallocation/input_test.txt: -------------------------------------------------------------------------------- 1 | 0 2 7 0 2 | -------------------------------------------------------------------------------- /2017-c/07-recursive-circus/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 7: Recursive Circus --- 2 | 3 | Wandering further through the circuits of the computer, you come upon a tower of programs that have gotten themselves into a bit of trouble. A recursive algorithm has gotten out of hand, and now they're balanced precariously in a large tower. 4 | 5 | https://adventofcode.com/2017/day/7 -------------------------------------------------------------------------------- /2017-c/07-recursive-circus/input_test.txt: -------------------------------------------------------------------------------- 1 | pbga (66) 2 | xhth (57) 3 | ebii (61) 4 | havc (66) 5 | ktlj (57) 6 | fwft (72) -> ktlj, cntj, xhth 7 | qoyq (66) 8 | padx (45) -> pbga, havc, qoyq 9 | tknk (41) -> ugml, padx, fwft 10 | jptl (61) 11 | ugml (68) -> gyxo, ebii, jptl 12 | gyxo (61) 13 | cntj (57) 14 | -------------------------------------------------------------------------------- /2017-c/08-i-heard-you-like-registers/README.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dannyvankooten/advent-of-code/c40ff779c5ea239d606091e4fdc638fb4502816f/2017-c/08-i-heard-you-like-registers/README.txt -------------------------------------------------------------------------------- /2017-c/08-i-heard-you-like-registers/input_test.txt: -------------------------------------------------------------------------------- 1 | b inc 5 if a > 1 2 | a inc 1 if b < 5 3 | c dec -10 if a >= 1 4 | c inc -20 if c == 10 5 | -------------------------------------------------------------------------------- /2017-c/09-stream-processing/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 9: Stream Processing --- 2 | 3 | A large stream blocks your path. According to the locals, it's not safe to cross the stream at the moment because it's full of garbage. You look down at the stream; rather than water, you discover that it's a stream of characters. 4 | 5 | https://adventofcode.com/2017/day/9 -------------------------------------------------------------------------------- /2017-c/09-stream-processing/input_test.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /2017-c/10-knot-hash/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 10: Knot Hash --- 2 | 3 | You come across some programs that are trying to implement a software emulation of a hash based on knot-tying. The hash these programs are implementing isn't very strong, but you decide to help them anyway. You make a mental note to remind the Elves later not to invent their own cryptographic functions. 4 | 5 | https://adventofcode.com/2017/day/10 -------------------------------------------------------------------------------- /2017-c/10-knot-hash/input_test.txt: -------------------------------------------------------------------------------- 1 | 1,2,3 2 | -------------------------------------------------------------------------------- /2017-c/11-hex-ed/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 11: Hex Ed --- 2 | 3 | Crossing the bridge, you've barely reached the other side of the stream when a program comes up to you, clearly in distress. "It's my child process," she says, "he's gotten lost in an infinite grid!" 4 | 5 | https://adventofcode.com/2017/day/11 -------------------------------------------------------------------------------- /2017-c/11-hex-ed/input_test.txt: -------------------------------------------------------------------------------- 1 | ne,ne,s,s -------------------------------------------------------------------------------- /2017-c/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | CC="${CC:=gcc}" 4 | CFLAGS="$CFLAGS 5 | -O2 -flto -march=native -mtune=native 6 | -Wall -Wextra -Wpedantic -std=c17" 7 | 8 | TIME_LINES="" 9 | for d in */; do 10 | cd "$d" 11 | $CC $CFLAGS main.c -lcrypto 12 | OUT=$(./a.out) 13 | TIME_LINES+=$(echo -e "$OUT" | grep "Time: ") 14 | TIME_LINES+="\n" 15 | echo -e "$OUT\n" 16 | cd .. 17 | done 18 | 19 | # Print sum of runtimes 20 | echo -e "$TIME_LINES" | awk 'BEGIN {sum=0.0} { gsub(/(Time: | μs)/,"", $2); sum += $2; } END { printf "Total time: %.3f ms\n", sum }' 21 | -------------------------------------------------------------------------------- /2017-cpp/.clangd: -------------------------------------------------------------------------------- 1 | CompileFlags: 2 | Add: [-std=c++20, -Wall, -Wextra, -Wpedantic ] 3 | -------------------------------------------------------------------------------- /2017-cpp/01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | auto tstart = std::chrono::high_resolution_clock::now(); 6 | int pt1 = 0; 7 | int pt2 = 0; 8 | 9 | std::string input; 10 | std::getline(std::cin, input); 11 | 12 | for (size_t i = 0; i < input.length(); i++) { 13 | size_t j = i == input.length() - 1 ? 0 : i + 1; 14 | if (input[i] == input[j]) { 15 | pt1 += input[i] - '0'; 16 | } 17 | 18 | size_t k = (i + (input.length() / 2)) % input.length(); 19 | if (input[i] == input[k]) { 20 | pt2 += input[i] - '0'; 21 | } 22 | } 23 | 24 | std::cout << "--- Day 1: Inverse Captcha ---\n"; 25 | std::cout << "Part 1: " << pt1 << "\n"; 26 | std::cout << "Part 2: " << pt2 << "\n"; 27 | 28 | auto tstop = std::chrono::high_resolution_clock::now(); 29 | auto duration = 30 | std::chrono::duration_cast(tstop - tstart); 31 | std::cout << "Time: " << (static_cast(duration.count()) / 1000.0) 32 | << " ms" 33 | << "\n"; 34 | return EXIT_SUCCESS; 35 | } 36 | -------------------------------------------------------------------------------- /2017-cpp/05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::vector; 6 | 7 | int solve_pt1(vector jumps) { 8 | int steps = 0; 9 | unsigned int ji = 0; 10 | 11 | while (ji < jumps.size()) { 12 | int j = jumps[ji]; 13 | jumps[ji] += 1; 14 | ji = ji + static_cast(j); 15 | steps += 1; 16 | } 17 | 18 | return steps; 19 | } 20 | 21 | int solve_pt2(vector jumps) { 22 | int steps = 0; 23 | unsigned int ji = 0; 24 | while (ji < jumps.size()) { 25 | int j = jumps[ji]; 26 | jumps[ji] = j >= 3 ? j - 1 : j + 1; 27 | ji = ji + static_cast(j); 28 | steps += 1; 29 | } 30 | return steps; 31 | } 32 | 33 | int main() { 34 | auto tstart = std::chrono::high_resolution_clock::now(); 35 | int pt1 = 0; 36 | int pt2 = 0; 37 | 38 | std::string input; 39 | std::vector jumps; 40 | while (std::getline(std::cin, input)) { 41 | jumps.push_back(std::stoi(input)); 42 | } 43 | 44 | pt1 = solve_pt1(jumps); 45 | pt2 = solve_pt2(jumps); 46 | 47 | std::cout << "--- Day 5: A Maze of Twisty Trampolines, All Alike ---\n"; 48 | std::cout << "Part 1: " << pt1 << "\n"; 49 | std::cout << "Part 2: " << pt2 << "\n"; 50 | 51 | auto tstop = std::chrono::high_resolution_clock::now(); 52 | auto duration = 53 | std::chrono::duration_cast(tstop - tstart); 54 | std::cout << "Time: " << (static_cast(duration.count()) / 1000.0) 55 | << " ms" 56 | << "\n"; 57 | return EXIT_SUCCESS; 58 | } 59 | -------------------------------------------------------------------------------- /2017-cpp/09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | auto tstart = std::chrono::high_resolution_clock::now(); 6 | int pt1 = 0; 7 | int pt2 = 0; 8 | 9 | std::string input; 10 | std::getline(std::cin, input); 11 | 12 | auto it = input.begin(); 13 | auto end = input.end(); 14 | int group_depth = 1; 15 | bool in_garbage = false; 16 | 17 | while (it != end) { 18 | if (in_garbage) { 19 | switch (*it) { 20 | case '!': 21 | it++; 22 | break; 23 | 24 | case '>': 25 | in_garbage = false; 26 | break; 27 | 28 | default: 29 | pt2 += 1; 30 | break; 31 | } 32 | } else { 33 | switch (*it) { 34 | case '{': 35 | group_depth += 1; 36 | break; 37 | 38 | case '}': 39 | group_depth -= 1; 40 | pt1 += group_depth; 41 | break; 42 | 43 | case '<': 44 | in_garbage = true; 45 | break; 46 | } 47 | } 48 | 49 | it++; 50 | } 51 | 52 | std::cout << "--- Day 9: Stream Processing ---\n"; 53 | std::cout << "Part 1: " << pt1 << "\n"; 54 | std::cout << "Part 2: " << pt2 << "\n"; 55 | 56 | auto tstop = std::chrono::high_resolution_clock::now(); 57 | auto duration = 58 | std::chrono::duration_cast(tstop - tstart); 59 | std::cout << "Time: " << (static_cast(duration.count()) / 1000.0) 60 | << " ms" 61 | << "\n"; 62 | return EXIT_SUCCESS; 63 | } 64 | -------------------------------------------------------------------------------- /2017-cpp/17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | auto tstart = std::chrono::high_resolution_clock::now(); 7 | int pt1 = 0; 8 | int pt2 = 0; 9 | 10 | unsigned int steps; 11 | std::cin >> steps; 12 | 13 | std::list ring = {0}; 14 | auto it = ring.begin(); 15 | 16 | for (int i = 0; i < 2017; i++) { 17 | // step forward steps times 18 | for (unsigned int s = 0; s < steps % ring.size(); s++) { 19 | if (++it == ring.end()) { 20 | it = ring.begin(); 21 | } 22 | } 23 | 24 | ring.insert(it, i + 1); 25 | } 26 | pt1 = *it; 27 | 28 | int pos = 0; 29 | int limit = 50000000; 30 | int n = 0; 31 | while (n < limit) { 32 | if (pos == 1) { 33 | pt2 = n; 34 | } 35 | 36 | int fits = (n - pos) / steps; 37 | n += fits + 1; 38 | pos = (pos + (fits + 1) * (steps + 1) - 1) % n + 1; 39 | } 40 | 41 | std::cout << "--- Day 17: Spinlock ---\n"; 42 | std::cout << "Part 1: " << pt1 << "\n"; 43 | std::cout << "Part 2: " << pt2 << "\n"; 44 | 45 | auto tstop = std::chrono::high_resolution_clock::now(); 46 | auto duration = 47 | std::chrono::duration_cast(tstop - tstart); 48 | std::cout << "Time: " << (static_cast(duration.count()) / 1000.0) 49 | << " ms" 50 | << "\n"; 51 | return EXIT_SUCCESS; 52 | } 53 | -------------------------------------------------------------------------------- /2017-cpp/Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS+= -std=c++20 -Wall -Wextra -Werror -Wpedantic \ 2 | -Wundef -Winline -Wimplicit-fallthrough -Wformat=2 \ 3 | -Wconversion 4 | PROGRAMS= 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 5 | 6 | ifndef RELEASE 7 | CXXFLAGS+= -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS \ 8 | -fsanitize=address -fsanitize=undefined -g \ 9 | -fstack-protector-strong 10 | else 11 | CXXFLAGS+= -O2 -march=native -mtune=native 12 | endif 13 | 14 | .PHONY: all clean 15 | 16 | all: $(PROGRAMS) 17 | 18 | $(PROGRAMS): 19 | 20 | clean: 21 | rm -f $(PROGRAMS) 22 | 23 | -------------------------------------------------------------------------------- /2017-cpp/new.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [[ "$AOCSESSION" == "" ]]; then 4 | echo "\$AOCSESSION environment variable not set." 5 | exit 1 6 | fi; 7 | 8 | DAY=$1 9 | DAYNAME=$(printf "%02d\n" $DAY) 10 | cp template.cpp $DAYNAME.cpp 11 | wget --no-cookies --header="Cookie: session=$AOCSESSION" https://adventofcode.com/2017/day/$DAY/input -O $DAYNAME.txt 12 | -------------------------------------------------------------------------------- /2017-cpp/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | make clean 4 | RELEASE=1 make all 5 | 6 | TIME_LINES="" 7 | for DAY in {01..25}; do 8 | 9 | if [[ -f ./$DAY ]]; then 10 | OUT=$(cat $DAY.txt | ./$DAY) 11 | TIME_LINES+=$(echo -e "$OUT" | grep "Time: ") 12 | TIME_LINES+="\n" 13 | echo -e "$OUT\n" 14 | sleep 0.5; 15 | fi 16 | done 17 | 18 | # Print sum of runtimes 19 | echo -e "$TIME_LINES" | awk 'BEGIN {sum=0.0} { gsub(/(Time: | μs)/,"", $2); sum += $2; } END { printf "Total time: %.3f ms\n", sum }' 20 | -------------------------------------------------------------------------------- /2017-cpp/template.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | auto tstart = std::chrono::high_resolution_clock::now(); 6 | int pt1 = 0; 7 | int pt2 = 0; 8 | 9 | std::string input; 10 | std::getline(std::cin, input); 11 | 12 | std::cout << "--- Day 1: No Time for a Taxicab ---\n"; 13 | std::cout << "Part 1: " << pt1 << "\n"; 14 | std::cout << "Part 2: " << pt2 << "\n"; 15 | 16 | auto tstop = std::chrono::high_resolution_clock::now(); 17 | auto duration = 18 | std::chrono::duration_cast(tstop - tstart); 19 | std::cout << "Time: " << (static_cast(duration.count()) / 1000.0) 20 | << " ms" 21 | << "\n"; 22 | return EXIT_SUCCESS; 23 | } 24 | -------------------------------------------------------------------------------- /2018-c/.clangd: -------------------------------------------------------------------------------- 1 | CompileFlags: 2 | Add: [-std=c17, -Wall, -Wextra, -Wpedantic, -Wundef, -Winline, -Wimplicit-fallthrough, -Wformat=2, -Wvla, -Wconversion, -Wstringop-overflow=3 ] 3 | -------------------------------------------------------------------------------- /2018-c/.gitignore: -------------------------------------------------------------------------------- 1 | *.txt 2 | build/ 3 | -------------------------------------------------------------------------------- /2018-c/01.c: -------------------------------------------------------------------------------- 1 | #include "adventofcode.h" 2 | #include 3 | #include 4 | #include 5 | 6 | #define PUZZLE_NAME "Day 1: Chronal Calibration" 7 | 8 | int pt1(const char *s) { 9 | int freq = 0; 10 | while (*s != '\0') { 11 | int n; 12 | s = parse_int(&n, s); 13 | freq += n; 14 | 15 | if (*s == '\n') 16 | s++; 17 | } 18 | return freq; 19 | } 20 | 21 | int pt2(char *input) { 22 | int freq = 0; 23 | uint8_t seen[256 * 1024] = {0}; 24 | 25 | for (;;) { 26 | const char *s = input; 27 | int hash; 28 | int n; 29 | 30 | while (*s != '\0') { 31 | s = parse_int(&n, s); 32 | freq += n; 33 | hash = freq + 128 * 1024; 34 | if (seen[hash] == 1) { 35 | return freq; 36 | } 37 | seen[hash] = 1; 38 | 39 | if (*s == '\n') 40 | s++; 41 | } 42 | } 43 | } 44 | 45 | int main(void) { 46 | clock_t t = clock_time(); 47 | char input[1024 * 64]; 48 | read_input_file(input, 1024 * 64, "01.txt"); 49 | 50 | printf("--- %s ---\n", PUZZLE_NAME); 51 | printf("Part 1: %d\n", pt1(input)); 52 | printf("Part 2: %d\n", pt2(input)); 53 | printf("Time: %.2fms\n", clock_time_since(t)); 54 | return EXIT_SUCCESS; 55 | } 56 | -------------------------------------------------------------------------------- /2018-c/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS+= -std=c17 -Wall -Wextra -Wpedantic \ 2 | -Wundef -Winline -Wimplicit-fallthrough -Wformat=2 \ 3 | -Wconversion -Wvla -Wstringop-overflow=3 4 | 5 | ifndef RELEASE 6 | CFLAGS+= -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS \ 7 | -fsanitize=address -fsanitize=undefined -g \ 8 | -fstack-protector-strong -fstrict-aliasing 9 | else 10 | CFLAGS+= -O2 -march=native 11 | endif 12 | 13 | PROGRAMS= 01 02 03 04 05 06 07 08 09 10 11 12 13 14 16 17 18 19 14 | 15 | .PHONY: all clean 16 | 17 | all: $(PROGRAMS) 18 | 19 | $(PROGRAMS): 20 | mkdir -p build 21 | $(CC) $(CFLAGS) $@.c -o build/$@ 22 | 23 | clean: 24 | rm -rf $(BINDIR) 25 | -------------------------------------------------------------------------------- /2018-c/new.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | YEAR=2018 6 | 7 | if [[ "$AOCSESSION" == "" ]]; then 8 | echo "\$AOCSESSION environment variable not set." 9 | exit 1 10 | fi; 11 | 12 | DAY=$1 13 | DAYNAME=$(printf "%02d\n" $DAY) 14 | cp template.c $DAYNAME.c 15 | wget --no-cookies --header="Cookie: session=$AOCSESSION" https://adventofcode.com/$YEAR/day/$DAY/input -O $DAYNAME.txt 16 | -------------------------------------------------------------------------------- /2018-c/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | RELEASE=1 make all 5 | 6 | TIME_LINES="" 7 | for PROGRAM in build/*; do 8 | DAY=$(basename $PROGRAM) 9 | OUT=$(./$PROGRAM < $DAY.txt) 10 | TIME_LINES+=$(echo -e "$OUT" | grep "Time: ") 11 | TIME_LINES+="\n" 12 | echo -e "$OUT\n" 13 | done 14 | 15 | # Print sum of runtimes 16 | echo -e "$TIME_LINES" | awk 'BEGIN {sum=0.0} { gsub(/(Time: | μs)/,"", $2); sum += $2; } END { printf "Total time: %.3f ms\n", sum }' 17 | -------------------------------------------------------------------------------- /2018-c/template.c: -------------------------------------------------------------------------------- 1 | #include "adventofcode.h" 2 | #include 3 | #include 4 | #include 5 | 6 | #define PUZZLE_NAME "Day 10: Knot Hash" 7 | 8 | int main(void) { 9 | clock_t t = clock_time(); 10 | char input[1024 * 64]; 11 | read_input_file(input, 64 * 1024, "input.txt"); 12 | 13 | printf("--- %s ---\n", PUZZLE_NAME); 14 | printf("Part 1: %d\n", 0); 15 | printf("Part 2: %d\n", 0); 16 | printf("Time: %.2fms\n", clock_time_since(t)); 17 | return EXIT_SUCCESS; 18 | } 19 | -------------------------------------------------------------------------------- /2019-rust/.gitignore: -------------------------------------------------------------------------------- 1 | */target/* 2 | -------------------------------------------------------------------------------- /2019-rust/README.md: -------------------------------------------------------------------------------- 1 | Advent of Code 2019, in Rust 2 | ============================= 3 | 4 | 2019 was my first year of participating in [Advent of Code](https://adventofcode.com/2019). 5 | I used it in an attempt to simultaneously learn some Rust. 6 | 7 | 8 | -------------------------------------------------------------------------------- /2019-rust/day-01/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | input.txt 3 | -------------------------------------------------------------------------------- /2019-rust/day-01/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "day-01" 7 | version = "0.1.0" 8 | -------------------------------------------------------------------------------- /2019-rust/day-01/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day-01" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /2019-rust/day-01/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | use std::time::Instant; 3 | 4 | fn fuel_required(mass: i32) -> i32 { 5 | return (mass / 3) - 2; 6 | } 7 | 8 | fn main() { 9 | let t = Instant::now(); 10 | let lines = io::stdin().lines(); 11 | let mut pt1 : i32 = 0; 12 | let mut pt2 : i32 = 0; 13 | 14 | for line in lines { 15 | let mass : i32 = match line.unwrap().parse() { 16 | Ok(n) => n, 17 | Err(_e) => break, 18 | }; 19 | 20 | let mut fuel = fuel_required(mass); 21 | 22 | pt1 += fuel; 23 | while fuel > 0 { 24 | pt2 += fuel; 25 | fuel = fuel_required(fuel); 26 | } 27 | } 28 | println!("Part 1: {}", pt1); 29 | println!("Part 2: {}", pt2); 30 | println!("Time: {:.3?} ms", t.elapsed().as_millis()); 31 | } 32 | -------------------------------------------------------------------------------- /2019-rust/day-02/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "day-02" 7 | version = "0.1.0" 8 | -------------------------------------------------------------------------------- /2019-rust/day-02/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day-02" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /2019-rust/day-02/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | use std::time::Instant; 3 | 4 | const OP_ADD: i64 = 1; 5 | const OP_MUL: i64 = 2; 6 | const OP_HALT: i64 = 99; 7 | 8 | fn run(mut ins: Vec, noun: i64, verb: i64) -> i64 { 9 | ins[1] = noun; 10 | ins[2] = verb; 11 | 12 | let mut ip: usize = 0; 13 | while ip < ins.len() && ins[ip] != OP_HALT { 14 | let a = ins[ip + 1] as usize; 15 | let b = ins[ip + 2] as usize; 16 | let t = ins[ip + 3] as usize; 17 | ins[t] = match ins[ip] { 18 | OP_ADD => ins[a] + ins[b], 19 | OP_MUL => ins[a] * ins[b], 20 | _ => { 21 | panic!("Invalid operand"); 22 | } 23 | }; 24 | ip += 4; 25 | } 26 | 27 | return ins[0]; 28 | } 29 | 30 | fn main() { 31 | let t = Instant::now(); 32 | let ins: Vec = io::read_to_string(io::stdin()) 33 | .unwrap() 34 | .trim() 35 | .split(',') 36 | .map(|v| v.parse().unwrap()) 37 | .collect(); 38 | 39 | let pt1 = run(ins.clone(), 12, 2); 40 | let mut pt2 = 0; 41 | 'outer: for noun in 0..100 { 42 | for verb in 0..100 { 43 | if run(ins.clone(), noun, verb) == 19690720 { 44 | pt2 = 100 * noun + verb; 45 | break 'outer; 46 | } 47 | } 48 | } 49 | 50 | println!("Part 1: {}", pt1); 51 | println!("Part 2: {}", pt2); 52 | println!("Time: {:.3?} ms", t.elapsed().as_millis()); 53 | } 54 | -------------------------------------------------------------------------------- /2019-rust/day-03/.gitignore: -------------------------------------------------------------------------------- 1 | input.txt 2 | target/ 3 | -------------------------------------------------------------------------------- /2019-rust/day-03/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "day-03" 7 | version = "0.1.0" 8 | -------------------------------------------------------------------------------- /2019-rust/day-03/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day-03" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /2019-rust/day-04/.gitignore: -------------------------------------------------------------------------------- 1 | input.txt 2 | target/ 3 | -------------------------------------------------------------------------------- /2019-rust/day-04/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "day-04" 7 | version = "0.1.0" 8 | -------------------------------------------------------------------------------- /2019-rust/day-04/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day-04" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /2019-rust/day-04/src/main.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | use std::time::Instant; 3 | 4 | fn main() { 5 | let now = Instant::now(); 6 | let input: Vec = std::io::read_to_string(io::stdin()) 7 | .unwrap() 8 | .trim() 9 | .split('-') 10 | .map(|v| v.parse::().unwrap()) 11 | .collect(); 12 | 13 | let mut pt1 = 0; 14 | let mut pt2 = 0; 15 | for mut n in input[0]..=input[1] { 16 | let mut digits : [u8; 6] = [0; 6]; 17 | for i in 0..6 { 18 | digits[6 - 1 - i] = (n % 10) as u8; 19 | n /= 10; 20 | } 21 | 22 | let mut has_double = false; 23 | let mut has_double_pt2 = false; 24 | let mut decreases = false; 25 | for i in 0..6 - 1 { 26 | if digits[i] == digits[i + 1] { 27 | has_double = true; 28 | 29 | if (i == 0 || digits[i] != digits[i - 1]) && (i >= 4 || digits[i] != digits[i + 2]) 30 | { 31 | has_double_pt2 = true; 32 | } 33 | } 34 | 35 | if digits[i] > digits[i + 1] { 36 | decreases = true; 37 | break; 38 | } 39 | } 40 | 41 | if has_double && !decreases { 42 | pt1 += 1; 43 | 44 | if has_double_pt2 { 45 | pt2 += 1; 46 | } 47 | } 48 | } 49 | 50 | println!("Part 1: {}", pt1); 51 | println!("Part 2: {}", pt2); 52 | println!("Time: {:.3?} ms", now.elapsed().as_millis()); 53 | } 54 | -------------------------------------------------------------------------------- /2019-rust/day-05/.gitignore: -------------------------------------------------------------------------------- 1 | input.txt 2 | target/ 3 | -------------------------------------------------------------------------------- /2019-rust/day-05/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "day-05" 7 | version = "0.1.0" 8 | -------------------------------------------------------------------------------- /2019-rust/day-05/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day-05" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /2019-rust/day-12/.gitignore: -------------------------------------------------------------------------------- 1 | input.txt 2 | target/ 3 | -------------------------------------------------------------------------------- /2019-rust/day-12/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "day-12" 7 | version = "0.1.0" 8 | -------------------------------------------------------------------------------- /2019-rust/day-12/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "day-12" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 7 | 8 | [dependencies] 9 | -------------------------------------------------------------------------------- /2019-rust/day-12/input_test.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /2019-rust/day06/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "aoc06" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /2019-rust/day06/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "aoc06" 3 | version = "0.1.0" 4 | authors = ["Danny van Kooten "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /2019-rust/day06/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 6: Universal Orbit Map --- 2 | 3 | You've landed at the Universal Orbit Map facility on Mercury. Because navigation in space often involves transferring between orbits, the orbit maps here are useful for finding efficient routes between, for example, you and Santa. You download a map of the local orbits (your puzzle input). 4 | 5 | https://adventofcode.com/2019/day/6 6 | -------------------------------------------------------------------------------- /2019-rust/day07/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "aoc07" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /2019-rust/day07/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "aoc07" 3 | version = "0.1.0" 4 | authors = ["Danny van Kooten "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /2019-rust/day07/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 7: Amplification Circuit --- 2 | 3 | Based on the navigational maps, you're going to need to send more power to your ship's thrusters to reach Santa in time. To do this, you'll need to configure a series of amplifiers already installed on the ship. 4 | 5 | https://adventofcode.com/2019/day/7 6 | -------------------------------------------------------------------------------- /2019-rust/day08/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "aoc08" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /2019-rust/day08/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "aoc08" 3 | version = "0.1.0" 4 | authors = ["Danny van Kooten "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /2019-rust/day08/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 8: Space Image Format --- 2 | 3 | The Elves' spirits are lifted when they realize you have an opportunity to reboot one of their Mars rovers, and so they are curious if you would spend a brief sojourn on Mars. You land your ship near the rover. 4 | 5 | https://adventofcode.com/2019/day/8 6 | -------------------------------------------------------------------------------- /2019-rust/day09/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "aoc09" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /2019-rust/day09/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "aoc09" 3 | version = "0.1.0" 4 | authors = ["Danny van Kooten "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /2019-rust/day09/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 9: Sensor Boost --- 2 | 3 | You've just said goodbye to the rebooted rover and left Mars when you receive a faint distress signal coming from the asteroid belt. It must be the Ceres monitoring station! 4 | 5 | https://adventofcode.com/2019/day/9 6 | -------------------------------------------------------------------------------- /2019-rust/day10/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "aoc10" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /2019-rust/day10/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "aoc10" 3 | version = "0.1.0" 4 | authors = ["Danny van Kooten "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /2019-rust/day10/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 10: Monitoring Station --- 2 | 3 | You fly into the asteroid belt and reach the Ceres monitoring station. The Elves here have an emergency: they're having trouble tracking all of the asteroids and can't be sure they're safe. 4 | 5 | https://adventofcode.com/2019/day/10 6 | -------------------------------------------------------------------------------- /2019-rust/new.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | YEAR=2019 6 | 7 | if [[ "$AOCSESSION" == "" ]]; then 8 | echo "\$AOCSESSION environment variable not set." 9 | exit 1 10 | fi; 11 | 12 | DAY=$1 13 | DAYNAME=$(printf "%02d\n" $DAY) 14 | DIR="day-$DAYNAME" 15 | cargo new "$DIR" 16 | cp template.rs "$DIR/src/main.rs" 17 | wget \ 18 | --no-cookies \ 19 | --header="Accept: text/plain" \ 20 | --header="Cookie: session=$AOCSESSION" \ 21 | --header="User-Agent: github.com/dannyvankooten/advent-of-code by hi@dvk.co" \ 22 | https://adventofcode.com/$YEAR/day/$DAY/input -O "$DIR/input.txt" 23 | echo "input.txt" >> "$DIR/.gitignore" 24 | echo "target/" >> "$DIR/.gitignore" 25 | -------------------------------------------------------------------------------- /2019-rust/run-all.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | TIME_LINES="" 5 | for DIR in ./day-*; do 6 | cd $DIR 7 | OUT=$(cargo run --release --quiet < input.txt &2> /dev/null) 8 | TIME_LINES+=$(echo -e "$OUT" | grep "Time: ") 9 | TIME_LINES+="\n" 10 | 11 | echo -e "$DIR" 12 | echo -e "$OUT\n" 13 | cd .. 14 | done 15 | 16 | # Print sum of runtimes 17 | echo -e "$TIME_LINES" | awk 'BEGIN {sum=0.0} { gsub(/(Time: | μs)/,"", $2); sum += $2; } END { printf "Total time: %.3f ms\n", sum }' 18 | -------------------------------------------------------------------------------- /2019-rust/template.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | use std::time::Instant; 3 | 4 | fn main() { 5 | let t = Instant::now(); 6 | let lines = io::stdin().lines(); 7 | let pt1 = 0; 8 | let pt2 = 0; 9 | println!("Part 1: {}", pt1); 10 | println!("Part 2: {}", pt2); 11 | println!("Time: {:.3?} ms", t.elapsed().as_millis()); 12 | } 13 | -------------------------------------------------------------------------------- /2020-c/.clangd: -------------------------------------------------------------------------------- 1 | CompileFlags: 2 | Add: [-std=c11, -Wall, -Wextra, -Wpedantic, -Wvla, -Wconversion ] 3 | -------------------------------------------------------------------------------- /2020-c/01.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "inputs/01.h" 6 | 7 | static int 8 | intcmp(const void *p1, const void *p2) { 9 | int a = *(int *) p1; 10 | int b = *(int *) p2; 11 | if (a == b) { 12 | return 0; 13 | } 14 | 15 | return a < b ? -1 : 1; 16 | } 17 | 18 | int day1(void) { 19 | int numbers[200] = {0}; 20 | size_t n = 0; 21 | const unsigned char *s = input; 22 | while (*s != '\0') { 23 | while (*s >= '0' && *s <= '9') { 24 | numbers[n] = (numbers[n] * 10) + (*s - '0'); 25 | s++; 26 | } 27 | 28 | if (*s == '\n') s++; 29 | n++; 30 | } 31 | 32 | // Since the 3 numbers need to sum to 2020 and most input numbers are over half that 33 | // We're likely to find out answer sooner on a sorted list (small to large). 34 | qsort(numbers, n, sizeof(int), intcmp); 35 | 36 | int answer = 0; 37 | for (size_t i = 0; i < n; i++) { 38 | for (size_t j = i; j < n; j++) { 39 | for (size_t k = j; k < n; k++) { 40 | if (numbers[i] + numbers[j] + numbers[k] == 2020) { 41 | answer = numbers[i] * numbers[j] * numbers[k]; 42 | goto PRINT_RESULT; 43 | } 44 | } 45 | } 46 | } 47 | 48 | PRINT_RESULT: ; 49 | printf("%d\n", answer); 50 | assert(answer == 157667328); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /2020-c/02.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "inputs/02.h" 5 | 6 | int day2(void) { 7 | int count_valid = 0; 8 | const unsigned char *s = input; 9 | while (*s != '\0') { 10 | // read lower limit 11 | int lower_limit = 0; 12 | while (*s >= '0' && *s <= '9') { 13 | lower_limit = (lower_limit * 10) + (*s - '0'); 14 | s++; 15 | } 16 | 17 | // '-' 18 | s++; 19 | 20 | // read higher limit 21 | int higher_limit = 0; 22 | while (*s >= '0' && *s <= '9') { 23 | higher_limit = (higher_limit * 10) + (*s - '0'); 24 | s++; 25 | } 26 | 27 | // ' ' 28 | s++; 29 | 30 | // read char requirement 31 | char c = *s++; 32 | 33 | // ": " 34 | s += 2; 35 | 36 | // read until "\n", check if c is between low and higher limit 37 | if ((s[lower_limit - 1] == c && s[higher_limit - 1] != c) || 38 | (s[higher_limit - 1] == c && s[lower_limit - 1] != c)) { 39 | count_valid++; 40 | } 41 | 42 | while (*s != '\n' && *s != '\0') s++; 43 | if (*s == '\n') s++; 44 | } 45 | 46 | printf("%d\n", count_valid); 47 | assert(count_valid == 342); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /2020-c/09.c: -------------------------------------------------------------------------------- 1 | #include "inputs/09.h" 2 | #include 3 | #include 4 | #include 5 | 6 | int day9(void) { 7 | const unsigned char *s = input; 8 | unsigned int numbers[1000]; 9 | unsigned int numbers_n = 0; 10 | unsigned int invalid_n = 104054607; 11 | while (*s != '\0') { 12 | unsigned int n = 0; 13 | while (*s >= '0' && *s <= '9') { 14 | n = n * 10ul + (*s - '0'); 15 | s++; 16 | } 17 | numbers[numbers_n++] = n; 18 | 19 | if (*s == '\n') 20 | s++; 21 | } 22 | 23 | // loop through numbers to find contiguous set 24 | // that sums to invalid_n (127) 25 | unsigned int range_start = 0; 26 | unsigned int range_end = 0; 27 | for (unsigned int i = 0; i < numbers_n - 1; i++) { 28 | unsigned int sum = numbers[i]; 29 | unsigned j = i; 30 | for (; j < numbers_n - 1 && sum < invalid_n;) { 31 | sum += numbers[++j]; 32 | } 33 | 34 | if (sum == invalid_n && j > i) { 35 | range_start = i; 36 | range_end = j; 37 | break; 38 | } 39 | } 40 | 41 | unsigned int smallest = numbers[range_start]; 42 | unsigned int largest = numbers[range_start]; 43 | for (unsigned int i = range_start; i <= range_end; i++) { 44 | if (numbers[i] < smallest) { 45 | smallest = numbers[i]; 46 | } else if (numbers[i] > largest) { 47 | largest = numbers[i]; 48 | } 49 | } 50 | 51 | unsigned int answer = largest + smallest; 52 | printf("%d\n", answer); 53 | assert(answer == 13935797); 54 | 55 | return EXIT_SUCCESS; 56 | } 57 | -------------------------------------------------------------------------------- /2020-c/25.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "intmap.h" 6 | 7 | static int64_t 8 | modpow(int64_t x, int exp, int mod) 9 | { 10 | int64_t res = 1; 11 | x = x % mod; 12 | 13 | while (exp > 0) 14 | { 15 | // If exp is odd, multiply x with result 16 | if (exp & 1) { 17 | res = (res * x) % mod; 18 | } 19 | 20 | // exp must be even now 21 | exp = exp >> 1; 22 | x = (x * x) % mod; 23 | } 24 | 25 | return res; 26 | } 27 | 28 | static int 29 | discrete_log(int subject, int value, int mod) { 30 | int e = 1; 31 | int modw = (int) sqrt((double) mod); 32 | intmap_t *hm = intmap_new(modw); 33 | 34 | for (int i=0; i < modw; i++) { 35 | intmap_set(hm, e, i); 36 | e = (e * subject) % mod; 37 | } 38 | int64_t f = modpow(subject, mod-modw-1, mod); 39 | 40 | // baby step giant step 41 | int r; 42 | for(int i=0; i < modw; i++) { 43 | r = intmap_get(hm, value); 44 | if (r != 0) { 45 | intmap_free(hm); 46 | return i * modw + r; 47 | } 48 | 49 | value = (value * f) % mod; 50 | } 51 | 52 | exit(1); 53 | } 54 | 55 | int day25(void) { 56 | int card_pubkey = 1965712; 57 | int door_pubkey = 19072108; 58 | int loop_size = discrete_log(7, card_pubkey, 20201227); 59 | assert(loop_size == 7779516); 60 | 61 | // find encryption key using other public key 62 | int64_t value = modpow(door_pubkey, loop_size, 20201227); 63 | assert(value == 16881444); 64 | printf("%ld\n", value); 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /2020-c/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS+= -std=c11 -Wall -Wextra -Wpedantic \ 2 | -Wundef -Winline -Wimplicit-fallthrough -Wformat=2 -Wvla 3 | BIN=aoc2020 4 | 5 | all: release 6 | 7 | debug: CFLAGS+=-DDEBUG -g -D_FORTIFY_SOURCE=2 \ 8 | -D_GLIBCXX_ASSERTIONS -fsanitize=address \ 9 | -fsanitize=undefined -fstack-protector-strong 10 | debug: BIN=aoc2020debug 11 | debug: $(BIN) 12 | 13 | profile: CFLAGS+= -pg 14 | profile: BIN=aoc2020debug 15 | profile: $(BIN) 16 | 17 | release: CFLAGS+=-O2 -march=native -mtune=native -flto 18 | release: $(BIN) 19 | 20 | $(BIN): *.c 21 | $(CC) $(CFLAGS) $^ -lm -o $(BIN) 22 | 23 | .PHONY: clean 24 | clean: 25 | rm -f aoc2020debug 26 | rm -f aoc2020 27 | -------------------------------------------------------------------------------- /2020-c/ec2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | AWS="54.186.28.90" 4 | rsync -az -e "ssh -i ~/Downloads/pair1.pem" --exclude=".git/*" . ec2-user@$AWS:~/advent-of-code 5 | ssh -i ~/Downloads/pair1.pem ec2-user@$AWS 'cd ~/advent-of-code && ./pgo.sh' 6 | ssh -i ~/Downloads/pair1.pem ec2-user@$AWS 'cd ~/advent-of-code && ./aoc2020 --bench' 7 | -------------------------------------------------------------------------------- /2020-c/hugemem.c: -------------------------------------------------------------------------------- 1 | #include "hugemem.h" 2 | #include 3 | 4 | const size_t pagesize = 1 << 21; // 2MB 5 | 6 | void* hugemem(size_t _sz){ 7 | size_t sz = (_sz + pagesize - 1) & ~(pagesize - 1); 8 | void *ptr = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS|MAP_HUGETLB|MAP_HUGE_2MB, 0, 0); 9 | if (ptr == MAP_FAILED) { 10 | ptr = NULL; 11 | } 12 | 13 | return ptr; 14 | } 15 | 16 | void hugemem_free(void* ptr, size_t _sz) { 17 | size_t sz = (_sz + pagesize - 1) & ~(pagesize - 1); 18 | assert(munmap(ptr, sz) == 0); 19 | } 20 | -------------------------------------------------------------------------------- /2020-c/hugemem.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void* hugemem(size_t _sz); 7 | void hugemem_free(void* ptr, size_t _sz); -------------------------------------------------------------------------------- /2020-c/inputs/01.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "1313\n1968\n1334\n1566\n820\n1435\n1369\n1230\n1383\n1816\n1396\n1974\n1911\n1989\n1824\n1430\n1709\n" 3 | "1204\n1792\n1800\n1703\n2009\n1467\n1400\n1315\n1985\n1598\n1215\n1574\n1770\n1870\n1352\n1544\n1339" 4 | "\n188\n1347\n1986\n2003\n1538\n1839\n1688\n1350\n1191\n1961\n1578\n1946\n1548\n1975\n1745\n1631\n139" 5 | "0\n1811\n1586\n1409\n247\n1600\n1565\n1929\n1854\n1602\n1773\n1815\n1887\n1689\n1266\n1573\n1534\n19" 6 | "39\n1909\n1273\n1386\n1713\n1268\n1611\n1348\n1478\n1857\n1916\n1113\n936\n1603\n1716\n1875\n1855\n1" 7 | "834\n1701\n1279\n1346\n1503\n1797\n1287\n1447\n1475\n1950\n1614\n1261\n1442\n1299\n1465\n896\n1481\n" 8 | "1804\n1931\n1849\n1675\n1726\n355\n1485\n1343\n1697\n1735\n1858\n1205\n1345\n1281\n253\n1808\n1557\n" 9 | "1964\n1771\n1891\n1583\n1896\n1398\n1930\n1258\n1338\n1208\n1328\n1493\n1963\n1374\n1212\n1223\n1501" 10 | "\n2004\n1591\n1954\n115\n1972\n1814\n1643\n1270\n1349\n1297\n1399\n1969\n1237\n1228\n1379\n1779\n176" 11 | "5\n1427\n1464\n1247\n1967\n1577\n1719\n1559\n1274\n1879\n1504\n1732\n1277\n1758\n1721\n1936\n1605\n1" 12 | "358\n1505\n1411\n1823\n1576\n1682\n1439\n1901\n1940\n1760\n1414\n1193\n1900\n1990\n1781\n1801\n1239\n" 13 | "1729\n1360\n1780\n1848\n1468\n1484\n1280\n1278\n1851\n1903\n1894\n1731\n1451\n549\n1570"; 14 | static const unsigned int input_len = 990; 15 | -------------------------------------------------------------------------------- /2020-c/inputs/08_test.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "nop +0\nacc +1\njmp +4\nacc +3\njmp -3\nacc -99\nacc +1\njmp -4\nacc +6"; 3 | static const unsigned int input_len = 63; 4 | -------------------------------------------------------------------------------- /2020-c/inputs/08_test.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 -------------------------------------------------------------------------------- /2020-c/inputs/09_test.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "35\n20\n15\n25\n47\n40\n62\n55\n65\n95\n102\n117\n150\n182\n127\n219\n299\n277\n309\n576"; 3 | static const unsigned int input_len = 69; 4 | -------------------------------------------------------------------------------- /2020-c/inputs/09_test.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 -------------------------------------------------------------------------------- /2020-c/inputs/10.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "145\n3\n157\n75\n84\n141\n40\n20\n60\n48\n15\n4\n2\n21\n129\n113\n54\n28\n69\n42\n34\n1\n155\n63\n15" 3 | "1\n8\n139\n135\n33\n81\n70\n132\n150\n112\n102\n59\n154\n53\n144\n149\n116\n13\n41\n156\n85\n22\n165" 4 | "\n51\n14\n125\n52\n64\n16\n134\n110\n71\n107\n124\n164\n160\n10\n25\n66\n74\n161\n111\n122\n166\n140" 5 | "\n87\n126\n123\n146\n35\n91\n106\n133\n26\n77\n19\n86\n105\n39\n99\n76\n58\n31\n96\n78\n88\n168\n119" 6 | "\n27\n45\n9\n92\n138\n38\n97\n32\n7\n98\n167\n95\n55\n65"; 7 | static const unsigned int input_len = 351; 8 | -------------------------------------------------------------------------------- /2020-c/inputs/10.txt: -------------------------------------------------------------------------------- 1 | 145 2 | 3 3 | 157 4 | 75 5 | 84 6 | 141 7 | 40 8 | 20 9 | 60 10 | 48 11 | 15 12 | 4 13 | 2 14 | 21 15 | 129 16 | 113 17 | 54 18 | 28 19 | 69 20 | 42 21 | 34 22 | 1 23 | 155 24 | 63 25 | 151 26 | 8 27 | 139 28 | 135 29 | 33 30 | 81 31 | 70 32 | 132 33 | 150 34 | 112 35 | 102 36 | 59 37 | 154 38 | 53 39 | 144 40 | 149 41 | 116 42 | 13 43 | 41 44 | 156 45 | 85 46 | 22 47 | 165 48 | 51 49 | 14 50 | 125 51 | 52 52 | 64 53 | 16 54 | 134 55 | 110 56 | 71 57 | 107 58 | 124 59 | 164 60 | 160 61 | 10 62 | 25 63 | 66 64 | 74 65 | 161 66 | 111 67 | 122 68 | 166 69 | 140 70 | 87 71 | 126 72 | 123 73 | 146 74 | 35 75 | 91 76 | 106 77 | 133 78 | 26 79 | 77 80 | 19 81 | 86 82 | 105 83 | 39 84 | 99 85 | 76 86 | 58 87 | 31 88 | 96 89 | 78 90 | 88 91 | 168 92 | 119 93 | 27 94 | 45 95 | 9 96 | 92 97 | 138 98 | 38 99 | 97 100 | 32 101 | 7 102 | 98 103 | 167 104 | 95 105 | 55 106 | 65 -------------------------------------------------------------------------------- /2020-c/inputs/10_test.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "16\n10\n15\n5\n1\n11\n7\n19\n6\n12\n4"; 3 | static const unsigned int input_len = 27; 4 | -------------------------------------------------------------------------------- /2020-c/inputs/10_test.txt: -------------------------------------------------------------------------------- 1 | 16 2 | 10 3 | 15 4 | 5 5 | 1 6 | 11 7 | 7 8 | 19 9 | 6 10 | 12 11 | 4 -------------------------------------------------------------------------------- /2020-c/inputs/11_test.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "L.LL.LL.LL\nLLLLLLL.LL\nL.L.L..L..\nLLLL.LL.LL\nL.LL.LL.LL\nL.LLLLL.LL\n..L.L.....\nLLLLLLLLLL\nL.LL" 3 | "LLLL.L\nL.LLLLL.LL"; 4 | static const unsigned int input_len = 109; 5 | -------------------------------------------------------------------------------- /2020-c/inputs/11_test.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 -------------------------------------------------------------------------------- /2020-c/inputs/12_test.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "F10\nN3\nF7\nR90\nF11"; 3 | static const unsigned int input_len = 17; 4 | -------------------------------------------------------------------------------- /2020-c/inputs/12_test.txt: -------------------------------------------------------------------------------- 1 | F10 2 | N3 3 | F7 4 | R90 5 | F11 -------------------------------------------------------------------------------- /2020-c/inputs/14_test.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X\nmem[8] = 11\nmem[7] = 101\nmem[8] = 0"; 3 | static const unsigned int input_len = 79; 4 | -------------------------------------------------------------------------------- /2020-c/inputs/14_test.txt: -------------------------------------------------------------------------------- 1 | mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X 2 | mem[8] = 11 3 | mem[7] = 101 4 | mem[8] = 0 -------------------------------------------------------------------------------- /2020-c/inputs/16_test.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "class\072 0-1 or 4-19\nrow\072 0-5 or 8-19\nseat\072 0-13 or 16-19\n\nyour ticket\072\n11,12,13\n\nn" 3 | "earby tickets\072\n3,9,18\n15,1,5\n5,14,9"; 4 | static const unsigned int input_len = 116; 5 | -------------------------------------------------------------------------------- /2020-c/inputs/16_test.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 -------------------------------------------------------------------------------- /2020-c/inputs/17.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "...#..#.\n.....##.\n##..##.#\n#.#.##..\n#..#.###\n...##.#.\n#..##..#\n.#.#..#."; 3 | static const unsigned int input_len = 71; 4 | -------------------------------------------------------------------------------- /2020-c/inputs/17.txt: -------------------------------------------------------------------------------- 1 | ...#..#. 2 | .....##. 3 | ##..##.# 4 | #.#.##.. 5 | #..#.### 6 | ...##.#. 7 | #..##..# 8 | .#.#..#. -------------------------------------------------------------------------------- /2020-c/inputs/17_test.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | ".#.\n..#\n###"; 3 | static const unsigned int input_len = 11; 4 | -------------------------------------------------------------------------------- /2020-c/inputs/17_test.txt: -------------------------------------------------------------------------------- 1 | .#. 2 | ..# 3 | ### -------------------------------------------------------------------------------- /2020-c/inputs/19_test.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "9\072 14 27 | 1 26\n10\072 23 14 | 28 1\n1\072 \"a\"\n11\072 42 31\n5\072 1 14 | 15 1\n19\072 14 1 |" 3 | " 14 14\n12\072 24 14 | 19 1\n16\072 15 1 | 14 14\n6\072 14 14 | 1 14\n2\072 1 24 | 14 4\n0\072 8 11\n" 4 | "13\072 14 3 | 1 12\n15\072 1 | 14\n17\072 14 2 | 1 7\n23\072 25 1 | 22 14\n28\072 16 1\n4\072 1 1\n2" 5 | "0\072 14 14 | 1 15\n3\072 5 14 | 16 1\n27\072 1 6 | 14 18\n14\072 \"b\"\n21\072 14 1 | 1 14\n25\072 " 6 | "1 1 | 1 14\n22\072 14 14\n8\072 42\n26\072 14 22 | 1 20\n18\072 15 15\n7\072 14 5 | 1 21\n24\072 14 " 7 | "1\n31\072 14 17 | 1 13\n42\072 9 14 | 10 1\n8\072 42 | 42 8\n11\072 42 31 | 42 11 31\n\nabbbbbabbbaa" 8 | "aababbaabbbbabababbbabbbbbbabaaaa\nbbabbbbaabaabba\nbabbbbaabbbbbabbbbbbaabaaabaaa\naaabbbbbbaaaabaa" 9 | "babaabababbabaaabbababababaaa\nbbbbbbbaaaabbbbaaabbabaaa\nbbbababbbbaaaaaaaabbababaaababaabab\nababa" 10 | "aaaaabaaab\nababaaaaabbbaba\nbaabbaaaabbaaaababbaababb\nabbbbabbbbaaaababbbbbbaaaababb\naaaaabbaabaa" 11 | "aaababaa\naaaabbaaaabbaaa\naaaabbaabbaaaaaaabbbabbbaaabbaabaaa\nbabaaabbbaaabaababbaabababaaab\naabb" 12 | "bbbaabbbaaaaaabbbbbababaaaaabbaaabba"; 13 | static const unsigned int input_len = 886; 14 | -------------------------------------------------------------------------------- /2020-c/inputs/19_test.txt: -------------------------------------------------------------------------------- 1 | 9: 14 27 | 1 26 2 | 10: 23 14 | 28 1 3 | 1: "a" 4 | 11: 42 31 5 | 5: 1 14 | 15 1 6 | 19: 14 1 | 14 14 7 | 12: 24 14 | 19 1 8 | 16: 15 1 | 14 14 9 | 6: 14 14 | 1 14 10 | 2: 1 24 | 14 4 11 | 0: 8 11 12 | 13: 14 3 | 1 12 13 | 15: 1 | 14 14 | 17: 14 2 | 1 7 15 | 23: 25 1 | 22 14 16 | 28: 16 1 17 | 4: 1 1 18 | 20: 14 14 | 1 15 19 | 3: 5 14 | 16 1 20 | 27: 1 6 | 14 18 21 | 14: "b" 22 | 21: 14 1 | 1 14 23 | 25: 1 1 | 1 14 24 | 22: 14 14 25 | 8: 42 26 | 26: 14 22 | 1 20 27 | 18: 15 15 28 | 7: 14 5 | 1 21 29 | 24: 14 1 30 | 31: 14 17 | 1 13 31 | 42: 9 14 | 10 1 32 | 8: 42 | 42 8 33 | 11: 42 31 | 42 11 31 34 | 35 | abbbbbabbbaaaababbaabbbbabababbbabbbbbbabaaaa 36 | bbabbbbaabaabba 37 | babbbbaabbbbbabbbbbbaabaaabaaa 38 | aaabbbbbbaaaabaababaabababbabaaabbababababaaa 39 | bbbbbbbaaaabbbbaaabbabaaa 40 | bbbababbbbaaaaaaaabbababaaababaabab 41 | ababaaaaaabaaab 42 | ababaaaaabbbaba 43 | baabbaaaabbaaaababbaababb 44 | abbbbabbbbaaaababbbbbbaaaababb 45 | aaaaabbaabaaaaababaa 46 | aaaabbaaaabbaaa 47 | aaaabbaabbaaaaaaabbbabbbaaabbaabaaa 48 | babaaabbbaaabaababbaabababaaab 49 | aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba -------------------------------------------------------------------------------- /2020-c/inputs/20_test.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "Tile 2311\072\n..##.#..#.\n##..#.....\n#...##..#.\n####.#...#\n##.##.###.\n##...#.###\n.#.#.#..##\n." 3 | ".#....#..\n###...#.#.\n..###..###\n\nTile 1951\072\n#.##...##.\n#.####...#\n.....#..##\n#...######\n" 4 | ".##.#....#\n.###.#####\n###.##.##.\n.###....#.\n..#.#..#.#\n#...##.#..\n\nTile 1171\072\n####...##.\n" 5 | "#..##.#..#\n##.#..#.#.\n.###.####.\n..###.####\n.##....##.\n.#...####.\n#.##.####.\n####..#...\n...." 6 | ".##...\n\nTile 1427\072\n###.##.#..\n.#..#.##..\n.#.##.#..#\n#.#.#.##.#\n....#...##\n...##..##.\n..." 7 | "#.#####\n.#.####.#.\n..#..###.#\n..##.#..#.\n\nTile 1489\072\n##.#.#....\n..##...#..\n.##..##...\n.." 8 | "#...#...\n#####...#.\n#..#.#.#.#\n...#.#.#..\n##.#...##.\n..##.##.##\n###.##.#..\n\nTile 2473\072\n#" 9 | "....####.\n#..#.##...\n#.##..#...\n######.#.#\n.#...#.#.#\n.#########\n.###.#..#.\n########.#\n##..." 10 | "##.#.\n..###.#.#.\n\nTile 2971\072\n..#.#....#\n#...###...\n#.#.###...\n##.##..#..\n.#####..##\n.#.." 11 | "####.#\n#..#.#..#.\n..####.###\n..#.#.###.\n...#.#.#.#\n\nTile 2729\072\n...#.#.#.#\n####.#....\n..#" 12 | ".#.....\n....#..#.#\n.##..##.#.\n.#.####...\n####.#.#..\n##.####...\n##..#.##..\n#.##...##.\n\nTile " 13 | "3079\072\n#.#.#####.\n.#..######\n..#.......\n######....\n####.#..#.\n.#...#.##.\n#.#####.##\n..#.##" 14 | "#...\n..#.......\n..#.###..."; 15 | static const unsigned int input_len = 1096; 16 | -------------------------------------------------------------------------------- /2020-c/inputs/21_test.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "mxmxvkd kfcds sqjhc nhms (contains dairy, fish)\ntrh fvjkl sbzzf mxmxvkd (contains dairy)\nsqjhc fvj" 3 | "kl (contains soy)\nsqjhc mxmxvkd sbzzf (contains fish)"; 4 | static const unsigned int input_len = 151; 5 | -------------------------------------------------------------------------------- /2020-c/inputs/21_test.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) -------------------------------------------------------------------------------- /2020-c/inputs/22.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "Player 1\072\n10\n21\n37\n2\n47\n13\n6\n29\n9\n3\n4\n48\n46\n25\n44\n41\n23\n20\n24\n12\n45\n43\n5\n" 3 | "27\n50\n\nPlayer 2\072\n39\n42\n31\n36\n7\n1\n49\n19\n40\n35\n8\n11\n18\n30\n14\n17\n15\n34\n26\n33\n" 4 | "32\n38\n28\n16\n22\n"; 5 | static const unsigned int input_len = 162; 6 | -------------------------------------------------------------------------------- /2020-c/inputs/22.txt: -------------------------------------------------------------------------------- 1 | Player 1: 2 | 10 3 | 21 4 | 37 5 | 2 6 | 47 7 | 13 8 | 6 9 | 29 10 | 9 11 | 3 12 | 4 13 | 48 14 | 46 15 | 25 16 | 44 17 | 41 18 | 23 19 | 20 20 | 24 21 | 12 22 | 45 23 | 43 24 | 5 25 | 27 26 | 50 27 | 28 | Player 2: 29 | 39 30 | 42 31 | 31 32 | 36 33 | 7 34 | 1 35 | 49 36 | 19 37 | 40 38 | 35 39 | 8 40 | 11 41 | 18 42 | 30 43 | 14 44 | 17 45 | 15 46 | 34 47 | 26 48 | 33 49 | 32 50 | 38 51 | 28 52 | 16 53 | 22 54 | -------------------------------------------------------------------------------- /2020-c/inputs/22_test.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "Player 1\072\n9\n2\n6\n3\n1\n\nPlayer 2\072\n5\n8\n4\n7\n10"; 3 | static const unsigned int input_len = 41; 4 | -------------------------------------------------------------------------------- /2020-c/inputs/22_test.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 -------------------------------------------------------------------------------- /2020-c/inputs/24_test.h: -------------------------------------------------------------------------------- 1 | static const unsigned char input[] = 2 | "sesenwnenenewseeswwswswwnenewsewsw\nneeenesenwnwwswnenewnwwsewnenwseswesw\nseswneswswsenwwnwse\nnwnw" 3 | "neseeswswnenewneswwnewseswneseene\nswweswneswnenwsewnwneneseenw\neesenwseswswnenwswnwnwsewwnwsene\ns" 4 | "ewnenenenesenwsewnenwwwse\nwenwwweseeeweswwwnwwe\nwsweesenenewnwwnwsenewsenwwsesesenwne\nneeswseenww" 5 | "swnwswswnw\nnenwswwsewswnenenewsenwsenwnesesenew\nenewnwewneswsewnwswenweswnenwsenwsw\nsweneswneswne" 6 | "neenwnewenewwneswswnese\nswwesenesewenwneswnwwneseswwne\nenesenwswwswneneswsenwnewswseenwsese\nwnwne" 7 | "senesenenwwnenwsewesewsesesew\nnenewswnwewswnenesenwnesewesw\neneswnwswnwsenenwnwnwwseeswneewsenese\n" 8 | "neswnwewnwnwseenwseesewsenwsweewe\nwseweeenwnesenwwwswnew\n"; 9 | static const unsigned int input_len = 640; 10 | -------------------------------------------------------------------------------- /2020-c/inputs/24_test.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 | -------------------------------------------------------------------------------- /2020-c/inputs/dump.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | for FILE in ./*.txt 3 | do 4 | NAME=${FILE#"./"} 5 | NAME=${NAME%".txt"} 6 | echo $NAME 7 | strliteral --ident input $FILE $NAME.h 8 | done -------------------------------------------------------------------------------- /2020-c/intmap.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | typedef struct { 6 | size_t key; 7 | int64_t value; 8 | } intmap_entry_t; 9 | 10 | typedef struct { 11 | // uint32_t *keys; 12 | // uint32_t *values; 13 | intmap_entry_t *entries; 14 | size_t cap; 15 | } intmap_t; 16 | 17 | intmap_t * 18 | intmap_new(size_t size); 19 | 20 | int64_t 21 | intmap_get(intmap_t *hm, const size_t key); 22 | 23 | int64_t 24 | intmap_set(intmap_t *hm, const size_t key, int64_t value); 25 | 26 | void 27 | intmap_free(intmap_t *m); -------------------------------------------------------------------------------- /2020-c/new.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ $# != 1 ]; then 4 | echo "Usage: $(basename "$0") " >&2 5 | exit 1 6 | fi 7 | 8 | FILE="$1.c" 9 | if [ -f "$FILE" ]; then 10 | read -p "File exists. Overwrite with default template? (N/y)" ANSWER 11 | if [ "$ANSWER" != "Y" ]; then 12 | exit 13 | fi 14 | fi; 15 | 16 | touch "$1.txt" 17 | touch $FILE 18 | cat > $FILE <<- EOM 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | int main() { 25 | FILE *f = fopen("$1.txt", "r"); 26 | if (!f) err(EXIT_FAILURE, "error reading input file"); 27 | char linebuf[BUFSIZ] = {0}; 28 | while (fgets(linebuf, BUFSIZ, f) != NULL) { 29 | 30 | } 31 | fclose(f); 32 | } 33 | EOM -------------------------------------------------------------------------------- /2020-c/pgo.sh: -------------------------------------------------------------------------------- 1 | #/bin/bash 2 | 3 | rm aoc2020 4 | CFLAGS="-fprofile-generate -g" make release 5 | ./aoc2020 --bench &> /dev/null 6 | rm aoc2020 7 | CFLAGS=-fprofile-use make release -------------------------------------------------------------------------------- /2021-py/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | venv 3 | __pycache__ -------------------------------------------------------------------------------- /2021-py/01-sonar-sweep/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 1: Sonar Sweep --- 2 | 3 | You're minding your own business on a ship at sea when the overboard alarm goes off! You rush to see if you can help. Apparently, one of the Elves tripped and accidentally sent the sleigh keys flying into the ocean! 4 | 5 | https://adventofcode.com/2021/day/1 6 | -------------------------------------------------------------------------------- /2021-py/01-sonar-sweep/main.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | input = Path("input.txt").read_text() 4 | numbers = [int(n) for n in input.split("\n")] 5 | 6 | prev = 1 << 30 7 | pt1 = 0 8 | for n in numbers: 9 | if n > prev: 10 | pt1 += 1 11 | prev = n 12 | 13 | 14 | a, b, c = numbers[0:3] 15 | prev = 0 16 | pt2 = 0 17 | for n in numbers[3:]: 18 | cur = a + b + c 19 | 20 | if cur > prev: 21 | pt2 += 1 22 | 23 | prev = cur 24 | a = b 25 | b = c 26 | c = n 27 | 28 | 29 | print("--- Day 1: Sonar Sweep ---") 30 | print("Part 1: ", pt1, pt1 == 1400) 31 | print("Part 2: ", pt2, pt2 == 1429) -------------------------------------------------------------------------------- /2021-py/02-dive/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 2: Dive! --- 2 | 3 | Now, you need to figure out how to pilot this thing. 4 | 5 | https://adventofcode.com/2021/day/2 6 | -------------------------------------------------------------------------------- /2021-py/02-dive/main.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | def part_1(lines): 4 | pos, depth = 0, 0 5 | for line in lines: 6 | ins, n = line.strip().split(" ") 7 | 8 | match ins: 9 | case "up": 10 | depth -= int(n) 11 | 12 | case "down": 13 | depth += int(n) 14 | 15 | case "forward": 16 | pos = pos + int(n) 17 | 18 | return pos*depth 19 | 20 | 21 | def part_2(lines): 22 | pos, depth, aim = 0, 0, 0 23 | for line in lines: 24 | ins, n = line.strip().split(" ") 25 | 26 | match ins: 27 | case "up": 28 | aim -= int(n) 29 | 30 | case "down": 31 | aim += int(n) 32 | 33 | case "forward": 34 | pos = pos + int(n) 35 | depth += aim * int(n) 36 | 37 | return pos*depth 38 | 39 | if __name__ == '__main__': 40 | lines = Path("input.txt").read_text().split("\n") 41 | print("--- Day 2: Dive! ---") 42 | print("Part 1: ", part_1(lines)) 43 | print("Part 2: ", part_2(lines)) -------------------------------------------------------------------------------- /2021-py/03-binary-diagnostic/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 3: Binary Diagnostic --- 2 | 3 | The submarine has been making some odd creaking noises, so you ask it to produce a diagnostic report just in case. 4 | 5 | https://adventofcode.com/2021/day/3 6 | -------------------------------------------------------------------------------- /2021-py/03-binary-diagnostic/main.py: -------------------------------------------------------------------------------- 1 | 2 | from pathlib import Path 3 | 4 | def part_one(): 5 | lines = Path("input.txt").read_text().strip().split("\n") 6 | length = len(lines[0]) 7 | pos = 0 8 | gamma = 0 9 | epsilon = 0 10 | 11 | for pos in range(length): 12 | bits = [s[pos] for s in lines] 13 | bit_pos = length - pos - 1 14 | 15 | if bits.count('1') >= bits.count('0'): 16 | gamma |= 1 << bit_pos 17 | epsilon |= 0 << bit_pos 18 | else: 19 | gamma |= 0 << bit_pos 20 | epsilon |= 1 << bit_pos 21 | 22 | return gamma * epsilon 23 | 24 | 25 | def search(lines, most_common=True): 26 | length = len(lines[0]) 27 | 28 | for pos in range(length): 29 | bits = [s[pos] for s in lines] 30 | 31 | # read top bitcount 32 | bit_criteria = 1 if bits.count('1') >= bits.count('0') else 0 33 | if not most_common: 34 | bit_criteria = 1 - bit_criteria 35 | 36 | # remove lines not matching bit criteria 37 | lines = [line for line in lines if int(line[pos]) == bit_criteria] 38 | if len(lines) == 1: 39 | return int(lines[0], 2) 40 | 41 | return None 42 | 43 | def part_two(): 44 | lines = Path("input.txt").read_text().strip().split("\n") 45 | oxygen_gen_rating = search(lines) 46 | co2_rating = search(lines, False) 47 | return oxygen_gen_rating * co2_rating 48 | 49 | 50 | pt1 = part_one() 51 | pt2 = part_two() 52 | 53 | print("--- Day 3: Binary Diagnostic ---") 54 | print(pt1, pt1 == 4160394) 55 | print(pt2, pt2 == 4125600) -------------------------------------------------------------------------------- /2021-py/03-binary-diagnostic/test_input.txt: -------------------------------------------------------------------------------- 1 | 00100 2 | 11110 3 | 10110 4 | 10111 5 | 10101 6 | 01111 7 | 00111 8 | 11100 9 | 10000 10 | 11001 11 | 00010 12 | 01010 -------------------------------------------------------------------------------- /2021-py/04-giant-squid/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 4: Giant Squid --- 2 | 3 | You're already almost 1.5km (almost a mile) below the surface of the ocean, already so deep that you can't see any sunlight. What you can see, however, is a giant squid that has attached itself to the outside of your submarine. 4 | 5 | https://adventofcode.com/2021/day/4 6 | -------------------------------------------------------------------------------- /2021-py/05-hydrothermal-venture/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 5: Hydrothermal Venture --- 2 | 3 | You come across a field of hydrothermal vents on the ocean floor! These vents constantly produce large, opaque clouds, so it would be best to avoid them if possible. 4 | 5 | https://adventofcode.com/2021/day/5 6 | -------------------------------------------------------------------------------- /2021-py/05-hydrothermal-venture/main.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | def solve(lines, diagonals=False): 4 | WIDTH = 999 5 | grid = [0] * WIDTH * WIDTH 6 | for line in lines: 7 | start, end = line.split(" -> ") 8 | start_x, start_y = [int(p) for p in start.split(',')] 9 | end_x, end_y = [int(p) for p in end.split(',')] 10 | rcx = 0 if end_x == start_x else 1 if end_x > start_x else -1 11 | rcy = 0 if end_y == start_y else 1 if end_y > start_y else -1 12 | 13 | if diagonals == False and rcx != 0 and rcy != 0: 14 | continue 15 | 16 | grid[start_y*WIDTH + start_x] += 1 17 | while start_x != end_x or start_y != end_y: 18 | start_x += rcx 19 | start_y += rcy 20 | grid[start_y*WIDTH + start_x] += 1 21 | 22 | return sum(n >= 2 for n in grid) 23 | 24 | 25 | if __name__ == '__main__': 26 | input = Path("input.txt").read_text() 27 | lines = input.split("\n") 28 | 29 | print("--- Day 5: Hydrothermal Venture ---") 30 | 31 | pt1 = solve(lines, diagonals=False) 32 | print("Part 1: ", pt1, pt1 == 7473) 33 | 34 | pt2 = solve(lines, diagonals=True) 35 | print("Part 2: ", pt2, pt2 == 24164) -------------------------------------------------------------------------------- /2021-py/06-lanternfish/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 6: Lanternfish --- 2 | 3 | The sea floor is getting steeper. Maybe the sleigh keys got carried this way? 4 | 5 | https://adventofcode.com/2021/day/6 6 | -------------------------------------------------------------------------------- /2021-py/06-lanternfish/main.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | fish = [int(n) for n in Path("input.txt").read_text().split(",")] 4 | 5 | # create list of pregnancies so we know how many fish are bown on each cycle day 6 | pregnancies = [0] * 7 7 | for f in fish: 8 | pregnancies[f] += 1 9 | 10 | # babies are fish that have yet to mature 11 | babies = [0] * 7 12 | 13 | num_fish = len(fish) 14 | for d in range(0, 256): 15 | day_in_cycle = d % 7 16 | 17 | # a new fish is born 18 | num_fish += pregnancies[day_in_cycle] 19 | 20 | # this fish will start producing babies after 9 days 21 | babies[(d+2) % 7] += pregnancies[day_in_cycle] 22 | 23 | pregnancies[day_in_cycle] += babies[day_in_cycle] 24 | babies[day_in_cycle] = 0 25 | 26 | 27 | print("--- Day 6: Lanternfish ---") 28 | print(num_fish, num_fish == 1595779846729) -------------------------------------------------------------------------------- /2021-py/07-treachery-of-whales/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 7: The Treachery of Whales --- 2 | 3 | A giant whale has decided your submarine is its next meal, and it's much faster than you are. There's nowhere to run! 4 | 5 | https://adventofcode.com/2021/day/7 6 | -------------------------------------------------------------------------------- /2021-py/07-treachery-of-whales/main.py: -------------------------------------------------------------------------------- 1 | crabs = [int(n) for n in open("input.txt").read().split(',')] 2 | 3 | # cheapest position to align on is probably average position 4 | avg = int(sum(crabs) / len(crabs)) 5 | 6 | cheapest = 1 << 64 7 | for pos in range(avg-1, avg+2): 8 | sum = 0 9 | for c in crabs: 10 | diff = abs(c - pos) 11 | sum += int((diff ** 2 + diff) / 2) 12 | 13 | cheapest = min(sum, cheapest) 14 | 15 | print("--- Day 7: The Treachery of Whales ---") 16 | print(cheapest) 17 | -------------------------------------------------------------------------------- /2021-py/08-seven-segment-search/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 8: Seven Segment Search --- 2 | 3 | You barely reach the safety of the cave when the whale smashes into the cave mouth, collapsing it. Sensors indicate another exit to this cave at a much greater depth, so you have no choice but to press on. 4 | 5 | https://adventofcode.com/2021/day/8 6 | -------------------------------------------------------------------------------- /2021-py/09-smoke-basin/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 9: Smoke Basin --- 2 | 3 | These caves seem to be lava tubes. Parts are even still volcanically active; small hydrothermal vents release smoke into the caves that slowly settles like rain. 4 | 5 | https://adventofcode.com/2021/day/9 6 | -------------------------------------------------------------------------------- /2021-py/10-syntax-scoring/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 10: Syntax Scoring --- 2 | 3 | You ask the submarine to determine the best route out of the deep-sea cave, but it only replies: 4 | 5 | https://adventofcode.com/2021/day/10 6 | -------------------------------------------------------------------------------- /2021-py/11-dumbo-octopus/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 11: Dumbo Octopus --- 2 | 3 | You enter a large cavern full of rare bioluminescent dumbo octopuses! They seem to not like the Christmas lights on your submarine, so you turn them off for now. 4 | 5 | https://adventofcode.com/2021/day/11 6 | -------------------------------------------------------------------------------- /2021-py/12-passage-pathing/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 12: Passage Pathing --- 2 | 3 | With your submarine's subterranean subsystems subsisting suboptimally, the only way you're getting out of this cave anytime soon is by finding a path yourself. Not just a path - the only way to know if you've found the best path is to find all of them. 4 | 5 | https://adventofcode.com/2021/day/12 6 | -------------------------------------------------------------------------------- /2021-py/12-passage-pathing/example1.txt: -------------------------------------------------------------------------------- 1 | start-A 2 | start-b 3 | A-c 4 | A-b 5 | b-d 6 | A-end 7 | b-end -------------------------------------------------------------------------------- /2021-py/12-passage-pathing/example2.txt: -------------------------------------------------------------------------------- 1 | fs-end 2 | he-DX 3 | fs-he 4 | start-DX 5 | pj-DX 6 | end-zg 7 | zg-sl 8 | zg-pj 9 | pj-he 10 | RW-he 11 | fs-DX 12 | pj-RW 13 | zg-RW 14 | start-pj 15 | he-WI 16 | zg-he 17 | pj-fs 18 | start-RW -------------------------------------------------------------------------------- /2021-py/12-passage-pathing/example3.txt: -------------------------------------------------------------------------------- 1 | dc-end 2 | HN-start 3 | start-kj 4 | dc-start 5 | dc-HN 6 | LN-dc 7 | HN-end 8 | kj-sa 9 | kj-HN 10 | kj-dc -------------------------------------------------------------------------------- /2021-py/12-passage-pathing/main.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | from pathlib import Path 3 | 4 | def parse(input): 5 | neighbors = defaultdict(list) 6 | for line in input.split("\n"): 7 | a, b = line.strip().split('-') 8 | neighbors[a] += [b] 9 | neighbors[b] += [a] 10 | 11 | return neighbors 12 | 13 | def solve(neighbors, part, seen : dict, cave : str): 14 | if cave == 'end': 15 | return 1 16 | 17 | if cave in seen: 18 | if cave == 'start': 19 | return 0 20 | if cave.islower(): 21 | if part == 1: 22 | return 0 23 | else: 24 | part = 1 25 | 26 | seen[cave] = True 27 | 28 | return sum(solve(neighbors, part, seen.copy(), n) for n in neighbors[cave]) 29 | 30 | 31 | if __name__ == '__main__': 32 | input = Path("input.txt").read_text() 33 | neighbors = parse(input) 34 | print("--- Day 12: Passage Pathing ---") 35 | print("Part 1: ", solve(neighbors, part=1, seen=dict(), cave='start')) 36 | print("Part 2: ", solve(neighbors, part=2, seen=dict(), cave='start')) -------------------------------------------------------------------------------- /2021-py/13-transparent-origami/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 13: Transparent Origami --- 2 | 3 | You reach another volcanically active part of the cave. It would be nice if you could do some kind of thermal imaging so you could tell ahead of time which caves are too hot to safely enter. 4 | 5 | https://adventofcode.com/2021/day/13 6 | -------------------------------------------------------------------------------- /2021-py/13-transparent-origami/input_test.txt: -------------------------------------------------------------------------------- 1 | 6,10 2 | 0,14 3 | 9,10 4 | 0,3 5 | 10,4 6 | 4,11 7 | 6,0 8 | 6,12 9 | 4,1 10 | 0,13 11 | 10,12 12 | 3,4 13 | 3,0 14 | 8,4 15 | 1,10 16 | 2,14 17 | 8,10 18 | 9,0 19 | 20 | fold along y=7 21 | fold along x=5 -------------------------------------------------------------------------------- /2021-py/14-extend-polymerization/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 14: Extended Polymerization --- 2 | 3 | The incredible pressures at this depth are starting to put a strain on your submarine. The submarine has polymerization equipment that would produce suitable materials to reinforce the submarine, and the nearby volcanically-active caves should even have the necessary input elements in sufficient quantities. 4 | 5 | https://adventofcode.com/2021/day/14 6 | -------------------------------------------------------------------------------- /2021-py/15-chiton/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 15: Chiton --- 2 | 3 | You've almost reached the exit of the cave, but the walls are getting closer together. Your submarine can barely still fit, though; the main problem is that the walls of the cave are covered in chitons, and it would be best not to bump any of them. 4 | 5 | https://adventofcode.com/2021/day/15 6 | -------------------------------------------------------------------------------- /2021-py/15-chiton/point.py: -------------------------------------------------------------------------------- 1 | 2 | from dataclasses import dataclass 3 | 4 | INT_MAX = int(pow(2, 64)) 5 | 6 | @dataclass 7 | class Point(): 8 | __slots__ = ['x', 'y', 'risk_factor', 'tent_distance', 'visited'] 9 | x: int 10 | y: int 11 | risk_factor: int 12 | tent_distance: int 13 | visited: bool 14 | 15 | def __eq__(self, other): 16 | return self.x == other.x and self.y == other.y 17 | 18 | def __hash__(self): 19 | return hash((self.x, self.y)) 20 | 21 | def __lt__(self, other): 22 | return self.tent_distance < other.tent_distance -------------------------------------------------------------------------------- /2021-py/16-packet-decoder/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 16: Packet Decoder --- 2 | 3 | As you leave the cave and reach open waters, you receive a transmission from the Elves back on the ship. 4 | 5 | https://adventofcode.com/2021/day/16 6 | -------------------------------------------------------------------------------- /2021-py/16-packet-decoder/input_test.txt: -------------------------------------------------------------------------------- 1 | D2FE28 -------------------------------------------------------------------------------- /2021-py/17-trick-shot/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 17: Trick Shot --- 2 | 3 | You finally decode the Elves' message. HI, the message says. You continue searching for the sleigh keys. 4 | 5 | https://adventofcode.com/2021/day/17 6 | -------------------------------------------------------------------------------- /2021-py/17-trick-shot/input_test.txt: -------------------------------------------------------------------------------- 1 | target area: x=20..30, y=-10..-5 -------------------------------------------------------------------------------- /2021-py/18-snailfish/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 18: Snailfish --- 2 | 3 | You descend into the ocean trench and encounter some snailfish. They say they saw the sleigh keys! They'll even tell you which direction the keys went if you help one of the smaller snailfish with his math homework. 4 | 5 | https://adventofcode.com/2021/day/18 6 | -------------------------------------------------------------------------------- /2021-py/18-snailfish/example1.txt: -------------------------------------------------------------------------------- 1 | [[[[[9,8],1],2],3],4] -------------------------------------------------------------------------------- /2021-py/18-snailfish/example2.txt: -------------------------------------------------------------------------------- 1 | [[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]] 2 | [[[5,[2,8]],4],[5,[[9,9],0]]] 3 | [6,[[[6,2],[5,6]],[[7,6],[4,7]]]] 4 | [[[6,[0,7]],[0,9]],[4,[9,[9,0]]]] 5 | [[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]] 6 | [[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]] 7 | [[[[5,4],[7,7]],8],[[8,3],8]] 8 | [[9,3],[[9,9],[6,[4,9]]]] 9 | [[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]] 10 | [[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]] -------------------------------------------------------------------------------- /2021-py/19-beacon-scanner/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 19: Beacon Scanner --- 2 | 3 | As your probe drifted down through this area, it released an assortment of beacons and scanners into the water. It's difficult to navigate in the pitch black open waters of the ocean trench, but if you can build a map of the trench using data from the scanners, you should be able to safely reach the bottom. 4 | 5 | https://adventofcode.com/2021/day/19 6 | -------------------------------------------------------------------------------- /2021-py/19-beacon-scanner/test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from main import * 4 | 5 | class PuzzleTestCase(unittest.TestCase): 6 | 7 | def test_matrix_rotations(self): 8 | values = [(1, 2, 3), (4, 5, 6)] 9 | new_values = matrix_rotations(values) 10 | 11 | print(f"{new_values} (length={len(new_values)})") 12 | self.assertEqual(len(new_values), 24) 13 | 14 | 15 | 16 | if __name__ == '__main__': 17 | unittest.main() -------------------------------------------------------------------------------- /2021-py/20-trench-map/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 20: Trench Map --- 2 | 3 | With the scanners fully deployed, you turn their attention to mapping the floor of the ocean trench. 4 | 5 | https://adventofcode.com/2021/day/20 6 | -------------------------------------------------------------------------------- /2021-py/20-trench-map/input_test.txt: -------------------------------------------------------------------------------- 1 | ..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..# 2 | 3 | #..#. 4 | #.... 5 | ##..# 6 | ..#.. 7 | ..### -------------------------------------------------------------------------------- /2021-py/20-trench-map/test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from main import * 4 | 5 | class PuzzleTestCase(unittest.TestCase): 6 | 7 | def test_pad(self): 8 | image = [[ 1 ]] # 1x1 9 | image = pad(image, 1) 10 | 11 | self.assertEqual(len(image), 3) 12 | self.assertEqual(len(image[0]), 3) 13 | self.assertEqual(image[0], [ 0, 0, 0]) 14 | self.assertEqual(image[1], [ 0, 1, 0]) 15 | self.assertEqual(image[2], [ 0, 0, 0]) 16 | 17 | 18 | if __name__ == '__main__': 19 | unittest.main() -------------------------------------------------------------------------------- /2021-py/21-dirac-dice/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 21: Dirac Dice --- 2 | 3 | There's not much to do as you slowly descend to the bottom of the ocean. The submarine computer challenges you to a nice game of Dirac Dice. 4 | 5 | https://adventofcode.com/2021/day/21 6 | -------------------------------------------------------------------------------- /2021-py/21-dirac-dice/input_test.txt: -------------------------------------------------------------------------------- 1 | Player 1 starting position: 4 2 | Player 2 starting position: 8 -------------------------------------------------------------------------------- /2021-py/21-dirac-dice/test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from main import * 4 | 5 | class PuzzleTestCase(unittest.TestCase): 6 | 7 | def test_runs(self): 8 | self.assertTrue(True) 9 | 10 | 11 | 12 | if __name__ == '__main__': 13 | unittest.main() -------------------------------------------------------------------------------- /2021-py/22-reactor-reboot/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 22: Reactor Reboot --- 2 | 3 | Operating at these extreme ocean depths has overloaded the submarine's reactor; it needs to be rebooted. 4 | 5 | https://adventofcode.com/2021/day/22 6 | -------------------------------------------------------------------------------- /2021-py/22-reactor-reboot/input_test.txt: -------------------------------------------------------------------------------- 1 | on x=10..12,y=10..12,z=10..12 2 | on x=11..13,y=11..13,z=11..13 3 | off x=9..11,y=9..11,z=9..11 4 | on x=10..10,y=10..10,z=10..10 -------------------------------------------------------------------------------- /2021-py/22-reactor-reboot/input_test_2.txt: -------------------------------------------------------------------------------- 1 | on x=-20..26,y=-36..17,z=-47..7 2 | on x=-20..33,y=-21..23,z=-26..28 3 | on x=-22..28,y=-29..23,z=-38..16 4 | on x=-46..7,y=-6..46,z=-50..-1 5 | on x=-49..1,y=-3..46,z=-24..28 6 | on x=2..47,y=-22..22,z=-23..27 7 | on x=-27..23,y=-28..26,z=-21..29 8 | on x=-39..5,y=-6..47,z=-3..44 9 | on x=-30..21,y=-8..43,z=-13..34 10 | on x=-22..26,y=-27..20,z=-29..19 11 | off x=-48..-32,y=26..41,z=-47..-37 12 | on x=-12..35,y=6..50,z=-50..-2 13 | off x=-48..-32,y=-32..-16,z=-15..-5 14 | on x=-18..26,y=-33..15,z=-7..46 15 | off x=-40..-22,y=-38..-28,z=23..41 16 | on x=-16..35,y=-41..10,z=-47..6 17 | off x=-32..-23,y=11..30,z=-14..3 18 | on x=-49..-5,y=-3..45,z=-29..18 19 | off x=18..30,y=-20..-8,z=-3..13 20 | on x=-41..9,y=-7..43,z=-33..15 21 | on x=-54112..-39298,y=-85059..-49293,z=-27449..7877 22 | on x=967..23432,y=45373..81175,z=27513..53682 -------------------------------------------------------------------------------- /2021-py/22-reactor-reboot/test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from main import * 4 | 5 | class PuzzleTestCase(unittest.TestCase): 6 | 7 | def test_overlap(self): 8 | a = Cube(10, 12, 10, 12, 10, 12, 1) 9 | b = Cube(11, 13, 11, 13, 11, 13, 1) 10 | c = Cube(9, 11, 9, 11, 9, 11, 0) 11 | z = Cube(100, 105, 100, 105, 100, 105, 1) 12 | 13 | self.assertEqual(overlap(a, b), 8) 14 | self.assertEqual(overlap(b, a), 8) 15 | self.assertEqual(overlap(a, c), 8) 16 | self.assertEqual(overlap(a, z), 0) 17 | self.assertEqual(overlap(z, a), 0) 18 | 19 | 20 | 21 | 22 | if __name__ == '__main__': 23 | unittest.main() -------------------------------------------------------------------------------- /2021-py/23-amphipod/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 23: Amphipod --- 2 | 3 | A group of amphipods notice your fancy submarine and flag you down. "With such an impressive shell," one amphipod says, "surely you can help us with a question that has stumped our best scientists." 4 | 5 | https://adventofcode.com/2021/day/23 6 | -------------------------------------------------------------------------------- /2021-py/23-amphipod/input_test.txt: -------------------------------------------------------------------------------- 1 | ############# 2 | #...........# 3 | ###B#C#B#D### 4 | #A#D#C#A# 5 | ######### -------------------------------------------------------------------------------- /2021-py/23-amphipod/part1.txt: -------------------------------------------------------------------------------- 1 | ############# 2 | #A..........# 3 | ###B#.#A#D### 4 | #B#C#D#C# 5 | ######### 6 | 5 7 | 8 | ############# 9 | #AA.........# 10 | ###B#.#.#D### 11 | #B#C#D#C# 12 | ######### 13 | 5+6 14 | 15 | ############# 16 | #AA.......D.# 17 | ###B#.#.#.### 18 | #B#C#D#C# 19 | ######### 20 | 5+6+2000 21 | 22 | 23 | ############# 24 | #AA...C...D.# 25 | ###B#.#.#.### 26 | #B#C#D#.# 27 | ######### 28 | 5+6+2000+500 29 | 30 | ############# 31 | #AA.........# 32 | ###.#B#C#D### 33 | #.#B#C#D# 34 | ######### 35 | 5+6+2000+500+6000+2000+300+500+100 36 | 37 | ############# 38 | #AA.........# 39 | ###.#B#C#D### 40 | #.#B#C#D# 41 | ######### 42 | 5+6+2000+500+6000+2000+300+500+100+6 -------------------------------------------------------------------------------- /2021-py/23-amphipod/part2.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dannyvankooten/advent-of-code/c40ff779c5ea239d606091e4fdc638fb4502816f/2021-py/23-amphipod/part2.txt -------------------------------------------------------------------------------- /2021-py/24-arithmetic-logic-unit/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 24: Arithmetic Logic Unit --- 2 | 3 | Magic smoke starts leaking from the submarine's arithmetic logic unit (ALU). Without the ability to perform basic arithmetic and logic functions, the submarine can't produce cool patterns with its Christmas lights! 4 | 5 | https://adventofcode.com/2021/day/24 6 | -------------------------------------------------------------------------------- /2021-py/24-arithmetic-logic-unit/input_test.txt: -------------------------------------------------------------------------------- 1 | inp w 2 | add z w 3 | mod z 2 4 | div w 2 5 | add y w 6 | mod y 2 7 | div w 2 8 | add x w 9 | mod x 2 10 | div w 2 11 | mod w 2 -------------------------------------------------------------------------------- /2021-py/24-arithmetic-logic-unit/monad.ods: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dannyvankooten/advent-of-code/c40ff779c5ea239d606091e4fdc638fb4502816f/2021-py/24-arithmetic-logic-unit/monad.ods -------------------------------------------------------------------------------- /2021-py/25-sea-cucumber/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 25: Sea Cucumber --- 2 | 3 | This is it: the bottom of the ocean trench, the last place the sleigh keys could be. Your submarine's experimental antenna still isn't boosted enough to detect the keys, but they must be here. All you need to do is reach the seafloor and find them. 4 | 5 | https://adventofcode.com/2021/day/25 6 | -------------------------------------------------------------------------------- /2021-py/25-sea-cucumber/input_test.txt: -------------------------------------------------------------------------------- 1 | v...>>.vv> 2 | .vv>>.vv.. 3 | >>.>v>...v 4 | >>v>>.>.v. 5 | v>v.vv.v.. 6 | >.>>..v... 7 | .vv..>.>v. 8 | v.v..>>v.v 9 | ....v..v.> -------------------------------------------------------------------------------- /2021-py/25-sea-cucumber/main.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from re import X 3 | 4 | def print_grid(grid): 5 | for row in grid: 6 | print("".join(row)) 7 | print() 8 | 9 | def solve(input) -> int: 10 | grid = [[state for state in line] for line in input.split("\n") ] 11 | 12 | for step in range(1, 2**16): 13 | moved = 0 14 | 15 | # move '>' cucumbers 16 | new_grid = [list(row) for row in grid] 17 | for y, row in enumerate(grid): 18 | for x, col in enumerate(row): 19 | if col != '>': 20 | continue 21 | 22 | next = (x + 1) % len(row) 23 | if row[next] == '.': 24 | new_grid[y][x] = '.' 25 | new_grid[y][next] = '>' 26 | moved += 1 27 | 28 | 29 | # move down cucumbers 30 | grid = [list(row) for row in new_grid] 31 | for y, row in enumerate(grid): 32 | for x, col in enumerate(row): 33 | if col != 'v': 34 | continue 35 | 36 | next = (y + 1) % len(grid) 37 | if grid[next][x] == '.': 38 | new_grid[y][x], new_grid[next][x] = new_grid[next][x], new_grid[y][x] 39 | moved += 1 40 | 41 | if moved == 0: 42 | return step 43 | 44 | grid = new_grid 45 | 46 | return 0 47 | 48 | if __name__ == '__main__': 49 | print("--- Day 25: Sea Cucumber ---") 50 | input = Path("input.txt").read_text() 51 | 52 | pt1 = solve(input) 53 | print(pt1) -------------------------------------------------------------------------------- /2021-py/25-sea-cucumber/test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from main import * 4 | 5 | class PuzzleTestCase(unittest.TestCase): 6 | 7 | def test_runs(self): 8 | self.assertTrue(True) 9 | 10 | 11 | 12 | if __name__ == '__main__': 13 | unittest.main() -------------------------------------------------------------------------------- /2021-py/README.md: -------------------------------------------------------------------------------- 1 | # Advent of Code 2021 in Python 2 | 3 | My solutions for [Advent of Code 2021](https://adventofcode.com/2021), in Python 3 using only the standard library. 4 | 5 | Puzzle inputs are read from a `input.txt` file in the respective directory for each day's solution. 6 | 7 | Previous editions: 8 | 9 | - [2020: C](https://github.com/dannyvankooten/advent-of-code/tree/main/2020) 10 | - [2019: Rust](https://github.com/dannyvankooten/advent-of-code/tree/main/2019) 11 | - [2015: C](https://github.com/dannyvankooten/advent-of-code/tree/main/2015) 12 | 13 | ## License 14 | 15 | Solution implementations are released into the public domain under the [UNLICENSE](UNLICENSE) license. 16 | 17 | Puzzle text and inputs are all the sole property of [Advent of Code](https://adventofcode.com/) and may not be replicated or used by any other person or entity without express written consent of Advent of Code. -------------------------------------------------------------------------------- /2021-py/new: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | mkdir $1 4 | touch $1/main.py 5 | touch $1/input.txt 6 | touch $1/input_test.txt 7 | 8 | cat template_main.py > $1/main.py 9 | cat template_test.py > $1/test.py 10 | touch $1/README.txt 11 | 12 | cd "$1" 13 | -------------------------------------------------------------------------------- /2021-py/requirements.txt: -------------------------------------------------------------------------------- 1 | parse==1.19.0 -------------------------------------------------------------------------------- /2021-py/run-all.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | TIME="0.0" 6 | for d in */; do 7 | cd "$d" 8 | python3 main.py 9 | cd .. 10 | done -------------------------------------------------------------------------------- /2021-py/template_main.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | def solve(input) -> int: 4 | # TODO: Write puzzle code here 5 | return 0 6 | 7 | if __name__ == '__main__': 8 | input = Path("input.txt").read_text() 9 | pt1 = solve(input) 10 | -------------------------------------------------------------------------------- /2021-py/template_test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from main import * 4 | 5 | class PuzzleTestCase(unittest.TestCase): 6 | 7 | def test_runs(self): 8 | self.assertTrue(True) 9 | 10 | 11 | 12 | if __name__ == '__main__': 13 | unittest.main() -------------------------------------------------------------------------------- /2022-py/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | venv 3 | __pycache__ 4 | 5 | # ignore rust binaries 6 | */main -------------------------------------------------------------------------------- /2022-py/01-calorie-counting/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 1: Calorie Counting --- 2 | 3 | Santa's reindeer typically eat regular reindeer food, but they need a lot of magical energy to deliver presents on Christmas. For that, their favorite snack is a special type of star fruit that only grows deep in the jungle. The Elves have brought you on their annual expedition to the grove where the fruit grows. 4 | 5 | https://adventofcode.com/2022/day/1 6 | -------------------------------------------------------------------------------- /2022-py/01-calorie-counting/main.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | def solve(input): 4 | elves = [sum([int(x) for x in elf.split('\n')]) for elf in input.split('\n\n')] 5 | elves.sort() 6 | return elves 7 | 8 | if __name__ == '__main__': 9 | input = Path("input.txt").read_text().strip() 10 | elves = solve(input) 11 | print("part 1", elves[-1]) 12 | print("part 2", sum(elves[-3:])) 13 | -------------------------------------------------------------------------------- /2022-py/02-rock-paper-scissors/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 2: Rock Paper Scissors --- 2 | 3 | The Elves begin to set up camp on the beach. To decide whose tent gets to be closest to the snack storage, a giant Rock Paper Scissors tournament is already in progress. 4 | 5 | https://adventofcode.com/2022/day/2 6 | -------------------------------------------------------------------------------- /2022-py/02-rock-paper-scissors/input_test.txt: -------------------------------------------------------------------------------- 1 | A Y 2 | B X 3 | C Z -------------------------------------------------------------------------------- /2022-py/02-rock-paper-scissors/main.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | def normalize(b): 4 | match b: 5 | case 'X' | 'A': return 0 6 | case 'Y' | 'B': return 1 7 | case 'Z' | 'C': return 2 8 | 9 | def outcome(opponent, you): 10 | if you == opponent: 11 | return 3 12 | diff = you - opponent 13 | if diff == 1 or diff == -2: 14 | return 6 15 | return 0 16 | 17 | def score(opponent, you): 18 | scores = [1, 2, 3] 19 | return scores[you] + outcome(opponent, you) 20 | 21 | def part1(input) -> int: 22 | total_score = 0 23 | for round in input: 24 | p1, p2 = map(normalize, round.split(' ')) 25 | total_score += score(p1, p2) 26 | return total_score 27 | 28 | def part2(input) -> int: 29 | total_score = 0 30 | for round in input: 31 | p1, expected_outcome = map(normalize, round.split(' ')) 32 | match expected_outcome: 33 | case 0: p2 = p1 - 1 34 | case 1: p2 = p1 35 | case 2: p2 = p1 + 1 36 | 37 | # slightly faster than p2 % 3 38 | if p2 < 0: 39 | p2 = 2 40 | elif p2 > 2: 41 | p2 = 0 42 | 43 | total_score += score(p1, p2) 44 | return total_score 45 | 46 | if __name__ == '__main__': 47 | lines = Path("input.txt").read_text().split('\n') 48 | print("pt1: ", part1(lines)) 49 | print("pt2: ", part2(lines)) 50 | -------------------------------------------------------------------------------- /2022-py/03-rucksack-reoganization/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 3: Rucksack Reorganization --- 2 | 3 | One Elf has the important job of loading all of the rucksacks with supplies for the jungle journey. Unfortunately, that Elf didn't quite follow the packing instructions, and so a few items now need to be rearranged. 4 | 5 | https://adventofcode.com/2022/day/3 6 | -------------------------------------------------------------------------------- /2022-py/03-rucksack-reoganization/input_test.txt: -------------------------------------------------------------------------------- 1 | vJrwpWtwJgWrhcsFMMfFFhFp 2 | jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL 3 | PmmdzqPrVvPwwTWBwg 4 | wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn 5 | ttgJtRGJQctTZtZT 6 | CrZsJsPPZsGzwwsLwLmpwMDw -------------------------------------------------------------------------------- /2022-py/03-rucksack-reoganization/main.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | def priority(item_type): 4 | if item_type.isupper(): 5 | return ord(item_type) - 38 6 | else: 7 | return ord(item_type) - 96 8 | 9 | def part1(rucksacks) -> int: 10 | sum = 0 11 | for rucksack in rucksacks: 12 | mid = len(rucksack) // 2 13 | left, right = set(rucksack[:mid]), set(rucksack[mid:]) 14 | for shared_item in left & right: 15 | sum += priority(shared_item) 16 | 17 | return sum 18 | 19 | def part2(rucksacks) -> int: 20 | sum = 0 21 | groups = [rucksacks[n:n+3] for n in range(0, len(rucksacks) - 1, 3)] 22 | for group in groups: 23 | [a, b, c] = group 24 | shared_items = set(a) & set(b) & set(c) 25 | sum += priority(shared_items.pop()) 26 | 27 | return sum 28 | 29 | if __name__ == '__main__': 30 | input = Path("input.txt").read_text() 31 | rucksacks= input.split('\n') 32 | 33 | pt1 = part1(rucksacks) 34 | print("pt1:", pt1) 35 | 36 | pt2 = part2(rucksacks) 37 | print("pt2:", pt2) 38 | -------------------------------------------------------------------------------- /2022-py/04-camp-cleanup/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 4: Camp Cleanup --- 2 | 3 | Space needs to be cleared before the last supplies can be unloaded from the ships, and so several Elves have been assigned the job of cleaning up sections of the camp. Every section has a unique ID number, and each Elf is assigned a range of section IDs. 4 | 5 | https://adventofcode.com/2022/day/4 6 | -------------------------------------------------------------------------------- /2022-py/04-camp-cleanup/input_test.txt: -------------------------------------------------------------------------------- 1 | 2-4,6-8 2 | 2-3,4-5 3 | 5-7,7-9 4 | 2-8,3-7 5 | 6-6,4-6 6 | 2-6,4-8 -------------------------------------------------------------------------------- /2022-py/04-camp-cleanup/main.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | def parse_section(desc): 4 | return tuple(map(int, desc.split('-'))) 5 | 6 | def contains(a, b): 7 | # a fully contains b 8 | if a[0] <= b[0] and a[1] >= b[1]: 9 | return True 10 | 11 | # b fully contains 12 | if b[0] <= a[0] and b[1] >= a[1]: 13 | return True 14 | 15 | return False 16 | 17 | def overlap(a, b): 18 | # if a starts within b 19 | if a[0] >= b[0] and a[0] <= b[1]: 20 | return True 21 | 22 | # if b starts within a 23 | if b[0] >= a[0] and b[0] <= a[1]: 24 | return True 25 | 26 | return False 27 | 28 | def solve(input) -> tuple[int, int]: 29 | count_contained = 0 30 | count_overlapped = 0 31 | 32 | for pair in input.split('\n'): 33 | (a, b) = map(parse_section, pair.split(',')) 34 | count_contained += contains(a,b) 35 | count_overlapped += overlap(a,b) 36 | 37 | return (count_contained, count_overlapped) 38 | 39 | if __name__ == '__main__': 40 | input = Path("input.txt").read_text() 41 | (pt1, pt2) = solve(input) 42 | print("pt1: ", pt1) 43 | print("pt2: ", pt2) 44 | 45 | -------------------------------------------------------------------------------- /2022-py/05-supply-stacks/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 5: Supply Stacks --- 2 | 3 | The expedition can depart as soon as the final supplies have been unloaded from the ships. Supplies are stored in stacks of marked crates, but because the needed supplies are buried under many other crates, the crates need to be rearranged. 4 | 5 | https://adventofcode.com/2022/day/5 6 | -------------------------------------------------------------------------------- /2022-py/05-supply-stacks/input_test.txt: -------------------------------------------------------------------------------- 1 | [D] 2 | [N] [C] 3 | [Z] [M] [P] 4 | 1 2 3 5 | 6 | move 1 from 2 to 1 7 | move 3 from 1 to 3 8 | move 2 from 2 to 1 9 | move 1 from 1 to 2 -------------------------------------------------------------------------------- /2022-py/05-supply-stacks/main.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from collections import defaultdict 3 | 4 | def solve(input, retain_order=False) -> str: 5 | stacks_config, instructions = input.split('\n\n') 6 | stacks = defaultdict(lambda: []) 7 | for line in reversed(stacks_config.split('\n')[:-1]): 8 | # change line into format like: X]A]O]]C 9 | #line = line.replace(' ', ' ]').replace('[', '') 10 | for stack_idx, crate in enumerate(line[1::4]): 11 | crate = crate.strip() 12 | if crate: 13 | stacks[stack_idx + 1].append(crate) 14 | 15 | # parse instructions 16 | for line in instructions.split('\n'): 17 | _, n, _, src, _, dest = line.split() 18 | n, src, dest = int(n), int(src), int(dest) 19 | crates = stacks[src][-n:] 20 | if not retain_order: 21 | crates = reversed(crates) 22 | del stacks[src][-n:] 23 | stacks[dest].extend(crates) 24 | 25 | # get top crate from each stack 26 | return "".join([stacks[s][-1] for s in stacks]) 27 | 28 | if __name__ == '__main__': 29 | input = Path("input.txt").read_text() 30 | print("pt1: ", solve(input)) 31 | print("pt2: ", solve(input, retain_order=True)) 32 | 33 | -------------------------------------------------------------------------------- /2022-py/06-tuning-trouble/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 6: Tuning Trouble --- 2 | 3 | The preparations are finally complete; you and the Elves leave camp on foot and begin to make your way toward the star fruit grove. 4 | 5 | https://adventofcode.com/2022/day/6 6 | -------------------------------------------------------------------------------- /2022-py/06-tuning-trouble/input_test.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dannyvankooten/advent-of-code/c40ff779c5ea239d606091e4fdc638fb4502816f/2022-py/06-tuning-trouble/input_test.txt -------------------------------------------------------------------------------- /2022-py/06-tuning-trouble/main.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | def solve(input, n) -> int: 4 | for i in range(0, len(input) - n): 5 | if len(set(input[i:i+n])) == n: 6 | return i + n 7 | 8 | raise Exception("No start packet") 9 | 10 | if __name__ == '__main__': 11 | input = Path("input.txt").read_text() 12 | assert(solve("bvwbjplbgvbhsrlpgdmjqwftvncz", 4) == 5) 13 | assert(solve("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", 4) == 11) 14 | 15 | print("pt1: ", solve(input, 4)) 16 | print("pt2: ", solve(input, 14)) 17 | -------------------------------------------------------------------------------- /2022-py/06-tuning-trouble/main2.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | def solve(input, n): 4 | for i in range(0, len(input) -n): 5 | if len(set(input[i:i+n])) == n: 6 | return i+n 7 | return None 8 | 9 | 10 | if __name__ == '__main__': 11 | input = Path("input.txt").read_text() 12 | print("pt1: ", solve(input, 4)) 13 | -------------------------------------------------------------------------------- /2022-py/06-tuning-trouble/test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from main import * 4 | 5 | class PuzzleTestCase(unittest.TestCase): 6 | 7 | def test_runs(self): 8 | self.assertTrue(True) 9 | 10 | 11 | 12 | if __name__ == '__main__': 13 | unittest.main() -------------------------------------------------------------------------------- /2022-py/07-no-space-left-on-device/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 7: No Space Left On Device --- 2 | 3 | You can hear birds chirping and raindrops hitting leaves as the expedition proceeds. Occasionally, you can even hear much louder sounds in the distance; how big do the animals get out here, anyway? 4 | 5 | Find the smallest directory that, if deleted, would free up enough space on the filesystem to run the update. What is the total size of that directory? 6 | 7 | https://adventofcode.com/2022/day/7 8 | -------------------------------------------------------------------------------- /2022-py/07-no-space-left-on-device/input_test.txt: -------------------------------------------------------------------------------- 1 | $ cd / 2 | $ ls 3 | dir a 4 | 14848514 b.txt 5 | 8504156 c.dat 6 | dir d 7 | $ cd a 8 | $ ls 9 | dir e 10 | 29116 f 11 | 2557 g 12 | 62596 h.lst 13 | $ cd e 14 | $ ls 15 | 584 i 16 | $ cd .. 17 | $ cd .. 18 | $ cd d 19 | $ ls 20 | 4060174 j 21 | 8033020 d.log 22 | 5626152 d.ext 23 | 7214296 k 24 | -------------------------------------------------------------------------------- /2022-py/08-treetop-tree-house/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 8: Treetop Tree House --- 2 | 3 | The expedition comes across a peculiar patch of tall trees all planted carefully in a grid. The Elves explain that a previous expedition planted these trees as a reforestation effort. Now, they're curious if this would be a good location for a tree house. 4 | 5 | https://adventofcode.com/2022/day/8 6 | -------------------------------------------------------------------------------- /2022-py/08-treetop-tree-house/input_test.txt: -------------------------------------------------------------------------------- 1 | 30373 2 | 25512 3 | 65332 4 | 33549 5 | 35390 6 | -------------------------------------------------------------------------------- /2022-py/09-rope-bridge/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 9: Rope Bridge --- 2 | 3 | This rope bridge creaks as you walk along it. You aren't sure how old it is, or whether it can even support your weight. 4 | 5 | https://adventofcode.com/2022/day/9 6 | -------------------------------------------------------------------------------- /2022-py/09-rope-bridge/input_test.txt: -------------------------------------------------------------------------------- 1 | R 4 2 | U 4 3 | L 3 4 | D 1 5 | R 4 6 | D 1 7 | L 5 8 | R 2 9 | -------------------------------------------------------------------------------- /2022-py/10-cathode-ray-tube/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 10: Cathode-Ray Tube --- 2 | 3 | You avoid the ropes, plunge into the river, and swim to shore. 4 | 5 | https://adventofcode.com/2022/day/10 6 | -------------------------------------------------------------------------------- /2022-py/10-cathode-ray-tube/main.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | def draw(cycle: int, x: int): 4 | """draw CRT pixel""" 5 | n = cycle % 40 6 | if n == 0: 7 | print() 8 | 9 | if abs(x - n) <= 1: 10 | print('#', end='') 11 | else: 12 | print('.', end='') 13 | 14 | 15 | def signal_strength(cycle: int, x: int) -> int: 16 | """Returns signal strength of current cycle + register combo""" 17 | if cycle == 20 or (cycle - 20) % 40 == 0 and cycle <= 220: 18 | return x * cycle 19 | 20 | return 0 21 | 22 | 23 | def solve(input: list[str]) -> int: 24 | x = 1 25 | cycles = 0 26 | signal_sum = 0 27 | 28 | for line in map(lambda l: l.split(), input): 29 | n = 2 if line[0] == 'addx' else 1 30 | for _ in range(0, n): 31 | draw(cycles, x) 32 | cycles += 1 33 | signal_sum += signal_strength(cycles, x) 34 | if n == 2: 35 | x += int(line[1]) 36 | 37 | return signal_sum 38 | 39 | if __name__ == '__main__': 40 | input = Path("input.txt").read_text().strip().split('\n') 41 | print("\npt1: ", solve(input)) 42 | 43 | -------------------------------------------------------------------------------- /2022-py/11-monkey-in-the-middle/.gitignore: -------------------------------------------------------------------------------- 1 | main 2 | -------------------------------------------------------------------------------- /2022-py/11-monkey-in-the-middle/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 11: Monkey in the Middle --- 2 | 3 | As you finally start making your way upriver, you realize your pack is much lighter than you remember. Just then, one of the items from your pack goes flying overhead. Monkeys are playing Keep Away with your missing things! 4 | 5 | https://adventofcode.com/2022/day/11 6 | -------------------------------------------------------------------------------- /2022-py/11-monkey-in-the-middle/input_test.txt: -------------------------------------------------------------------------------- 1 | Monkey 0: 2 | Starting items: 79, 98 3 | Operation: new = old * 19 4 | Test: divisible by 23 5 | If true: throw to monkey 2 6 | If false: throw to monkey 3 7 | 8 | Monkey 1: 9 | Starting items: 54, 65, 75, 74 10 | Operation: new = old + 6 11 | Test: divisible by 19 12 | If true: throw to monkey 2 13 | If false: throw to monkey 0 14 | 15 | Monkey 2: 16 | Starting items: 79, 60, 97 17 | Operation: new = old * old 18 | Test: divisible by 13 19 | If true: throw to monkey 1 20 | If false: throw to monkey 3 21 | 22 | Monkey 3: 23 | Starting items: 74 24 | Operation: new = old + 3 25 | Test: divisible by 17 26 | If true: throw to monkey 0 27 | If false: throw to monkey 1 28 | -------------------------------------------------------------------------------- /2022-py/12-hill-climbing-algorithm/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 12: Hill Climbing Algorithm --- 2 | 3 | You try contacting the Elves using your handheld device, but the river you're following must be too low to get a decent signal. 4 | 5 | https://adventofcode.com/2022/day/12 6 | -------------------------------------------------------------------------------- /2022-py/12-hill-climbing-algorithm/input_test.txt: -------------------------------------------------------------------------------- 1 | Sabqponm 2 | abcryxxl 3 | accszExk 4 | acctuvwj 5 | abdefghi 6 | -------------------------------------------------------------------------------- /2022-py/13-distress-signal/README.md: -------------------------------------------------------------------------------- 1 | --- Day 13: Distress Signal --- 2 | 3 | You climb the hill and again try contacting the Elves. However, you instead receive a signal you weren't expecting: a distress signal. 4 | 5 | https://adventofcode.com/2022/day/13 6 | -------------------------------------------------------------------------------- /2022-py/13-distress-signal/input_test.txt: -------------------------------------------------------------------------------- 1 | [1,1,3,1,1] 2 | [1,1,5,1,1] 3 | 4 | [[1],[2,3,4]] 5 | [[1],4] 6 | 7 | [9] 8 | [[8,7,6]] 9 | 10 | [[4,4],4,4] 11 | [[4,4],4,4,4] 12 | 13 | [7,7,7,7] 14 | [7,7,7] 15 | 16 | [] 17 | [3] 18 | 19 | [[[]]] 20 | [[]] 21 | 22 | [1,[2,[3,[4,[5,6,7]]]],8,9] 23 | [1,[2,[3,[4,[5,6,0]]]],8,9] 24 | -------------------------------------------------------------------------------- /2022-py/14-regolith-reservoir/README.md: -------------------------------------------------------------------------------- 1 | --- Day 14: Regolith Reservoir --- 2 | 3 | The distress signal leads you to a giant waterfall! Actually, hang on - the signal seems like it's coming from the waterfall itself, and that doesn't make any sense. However, you do notice a little path that leads behind the waterfall. 4 | 5 | https://adventofcode.com/2022/day/14 6 | -------------------------------------------------------------------------------- /2022-py/14-regolith-reservoir/input_test.txt: -------------------------------------------------------------------------------- 1 | 498,4 -> 498,6 -> 496,6 2 | 503,4 -> 502,4 -> 502,9 -> 494,9 3 | -------------------------------------------------------------------------------- /2022-py/15-beacon-exclusion-zone/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 15: Beacon Exclusion Zone --- 2 | 3 | You feel the ground rumble again as the distress signal leads you to a large network of subterranean tunnels. You don't have time to search them all, but you don't need to: your pack contains a set of deployable sensors that you imagine were originally built to locate lost Elves. 4 | 5 | https://adventofcode.com/2022/day/15 6 | -------------------------------------------------------------------------------- /2022-py/15-beacon-exclusion-zone/input_test.txt: -------------------------------------------------------------------------------- 1 | Sensor at x=2, y=18: closest beacon is at x=-2, y=15 2 | Sensor at x=9, y=16: closest beacon is at x=10, y=16 3 | Sensor at x=13, y=2: closest beacon is at x=15, y=3 4 | Sensor at x=12, y=14: closest beacon is at x=10, y=16 5 | Sensor at x=10, y=20: closest beacon is at x=10, y=16 6 | Sensor at x=14, y=17: closest beacon is at x=10, y=16 7 | Sensor at x=8, y=7: closest beacon is at x=2, y=10 8 | Sensor at x=2, y=0: closest beacon is at x=2, y=10 9 | Sensor at x=0, y=11: closest beacon is at x=2, y=10 10 | Sensor at x=20, y=14: closest beacon is at x=25, y=17 11 | Sensor at x=17, y=20: closest beacon is at x=21, y=22 12 | Sensor at x=16, y=7: closest beacon is at x=15, y=3 13 | Sensor at x=14, y=3: closest beacon is at x=15, y=3 14 | Sensor at x=20, y=1: closest beacon is at x=15, y=3 15 | -------------------------------------------------------------------------------- /2022-py/16-proboscidea-volcanium/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 16: Proboscidea Volcanium --- 2 | 3 | The sensors have led you to the origin of the distress signal: yet another handheld device, just like the one the Elves gave you. However, you don't see any Elves around; instead, the device is surrounded by elephants! They must have gotten lost in these tunnels, and one of the elephants apparently figured out how to turn on the distress signal. 4 | 5 | https://adventofcode.com/2022/day/16 6 | -------------------------------------------------------------------------------- /2022-py/16-proboscidea-volcanium/input_test.txt: -------------------------------------------------------------------------------- 1 | Valve AA has flow rate=0; tunnels lead to valves DD, II, BB 2 | Valve BB has flow rate=13; tunnels lead to valves CC, AA 3 | Valve CC has flow rate=2; tunnels lead to valves DD, BB 4 | Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE 5 | Valve EE has flow rate=3; tunnels lead to valves FF, DD 6 | Valve FF has flow rate=0; tunnels lead to valves EE, GG 7 | Valve GG has flow rate=0; tunnels lead to valves FF, HH 8 | Valve HH has flow rate=22; tunnel leads to valve GG 9 | Valve II has flow rate=0; tunnels lead to valves AA, JJ 10 | Valve JJ has flow rate=21; tunnel leads to valve II 11 | -------------------------------------------------------------------------------- /2022-py/16-proboscidea-volcanium/main2.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | import functools 3 | 4 | def parse(input): 5 | rates = {} 6 | on = {} 7 | valve_exits = {} 8 | for line in input.split('\n'): 9 | desc, exits = line.split(';' ) 10 | _, name, _, _, rate = desc.split() 11 | exits = exits.replace(',', '').split(" ")[5:] 12 | rate = int(rate.split('=')[1]) 13 | rates[name] = rate 14 | on[name] = False 15 | valve_exits[name] = exits 16 | 17 | return rates, on, valve_exits 18 | 19 | def solve(rates, exits, on, cur: str, total_pressure: int, time_left: int): 20 | if time_left == 0: 21 | return total_pressure 22 | 23 | # add pressure for each open valve 24 | for name in on: 25 | if on[name]: 26 | total_pressure += rates[name] 27 | 28 | # if already on, do not recurse into exits? 29 | #if on[cur]: 30 | # return total_pressure 31 | 32 | results = [] 33 | if not on[cur] and rates[cur] > 0: 34 | on_copy = on.copy() 35 | on_copy[cur] = True 36 | results.append(solve(rates, exits, on_copy, cur, total_pressure, time_left=time_left - 1)) 37 | 38 | for e in exits[cur]: 39 | results.append(solve(rates, exits, on, e, total_pressure, time_left=time_left - 1)) 40 | 41 | return max(results) 42 | 43 | 44 | if __name__ == '__main__': 45 | input = Path("input_test.txt").read_text().strip() 46 | rates, on, exits = parse(input) 47 | print("rates: ", rates) 48 | print("statuses: ", on) 49 | print("exits: ", exits) 50 | print("pt1: ", solve(rates, exits, on, 'AA', 0, 30)) 51 | 52 | -------------------------------------------------------------------------------- /2022-py/16-proboscidea-volcanium/test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from main import * 4 | 5 | class PuzzleTestCase(unittest.TestCase): 6 | 7 | def test_runs(self): 8 | self.assertTrue(True) 9 | 10 | 11 | 12 | if __name__ == '__main__': 13 | unittest.main() -------------------------------------------------------------------------------- /2022-py/README.md: -------------------------------------------------------------------------------- 1 | My solutions for [Advent of Code 2022](https://adventofcode.com/2022), in Python. 2 | 3 | Previous editions: 4 | 5 | - [2021: Python](https://github.com/dannyvankooten/advent-of-code/tree/main/2021) 6 | - [2020: C](https://github.com/dannyvankooten/advent-of-code/tree/main/2020) 7 | - [2019: Rust](https://github.com/dannyvankooten/advent-of-code/tree/main/2019) -------------------------------------------------------------------------------- /2022-py/new: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | mkdir $1 4 | touch $1/main.py 5 | touch $1/input.txt 6 | touch $1/input_test.txt 7 | 8 | cat template_main.py > $1/main.py 9 | cat template_test.py > $1/test.py 10 | touch $1/README.txt 11 | 12 | cd "$1" 13 | -------------------------------------------------------------------------------- /2022-py/run_all.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | TOTAL=0 4 | BENCHMARK_FILE="/tmp/benchmark.json" 5 | 6 | # run each day 7 | for DIR in */; do 8 | cd $DIR 9 | 10 | if [[ -e "main.rs" ]]; then 11 | rustc -O main.rs 12 | hyperfine './main' --warmup 5 --runs 5 --export-json $BENCHMARK_FILE --style none 2> /dev/null 13 | elif [[ -x "main.py" ]]; then 14 | hyperfine './main.py' --warmup 5 --runs 5 --export-json $BENCHMARK_FILE --style none 2> /dev/null 15 | else 16 | hyperfine 'python main.py' --warmup 5 --runs 5 --export-json $BENCHMARK_FILE --style none 2> /dev/null 17 | fi 18 | ELAPSED_TIME=$(cat $BENCHMARK_FILE | jq '.results[0].min') 19 | TOTAL=$(echo "$TOTAL + $ELAPSED_TIME" | bc) 20 | printf "%-30s %.4fs\n" ${DIR::-1} $ELAPSED_TIME 21 | cd .. 22 | done 23 | 24 | printf "%-30s %.4fs\n" "Total runtime:" $TOTAL 25 | -------------------------------------------------------------------------------- /2022-py/template_main.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | 3 | 4 | def solve(input): 5 | return 0 6 | 7 | 8 | if __name__ == '__main__': 9 | input = Path("input.txt").read_text() 10 | print("pt1: ", solve(input)) 11 | -------------------------------------------------------------------------------- /2022-py/template_test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from main import * 4 | 5 | class PuzzleTestCase(unittest.TestCase): 6 | 7 | def test_runs(self): 8 | self.assertTrue(True) 9 | 10 | 11 | 12 | if __name__ == '__main__': 13 | unittest.main() -------------------------------------------------------------------------------- /2023-go/01-trebuchet/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 1: Trebuchet?! --- 2 | 3 | Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems. 4 | 5 | https://adventofcode.com/2023/day/1 6 | -------------------------------------------------------------------------------- /2023-go/01-trebuchet/input_test.txt: -------------------------------------------------------------------------------- 1 | 1abc2 2 | pqr3stu8vwx 3 | a1b2c3d4e5f 4 | treb7uchet 5 | -------------------------------------------------------------------------------- /2023-go/01-trebuchet/input_test2.txt: -------------------------------------------------------------------------------- 1 | two1nine 2 | eightwothree 3 | abcone2threexyz 4 | xtwone3four 5 | 4nineeightseven2 6 | zoneight234 7 | 7pqrstsixteen 8 | -------------------------------------------------------------------------------- /2023-go/01-trebuchet/main_test.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dannyvankooten/advent-of-code/c40ff779c5ea239d606091e4fdc638fb4502816f/2023-go/01-trebuchet/main_test.go -------------------------------------------------------------------------------- /2023-go/02-cube-conundrum/02-cube-conundrum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dannyvankooten/advent-of-code/c40ff779c5ea239d606091e4fdc638fb4502816f/2023-go/02-cube-conundrum/02-cube-conundrum -------------------------------------------------------------------------------- /2023-go/02-cube-conundrum/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 2: Cube Conundrum --- 2 | 3 | You're launched high into the atmosphere! The apex of your trajectory just barely reaches the surface of a large island floating in the sky. You gently land in a fluffy pile of leaves. It's quite cold, but you don't see much snow. An Elf runs over to greet you. 4 | 5 | https://adventofcode.com/2023/day/2 6 | -------------------------------------------------------------------------------- /2023-go/02-cube-conundrum/input_test.txt: -------------------------------------------------------------------------------- 1 | Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green 2 | Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue 3 | Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red 4 | Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red 5 | Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green 6 | -------------------------------------------------------------------------------- /2023-go/02-cube-conundrum/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "testing" 4 | 5 | func TestFoo(t *testing.T) { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /2023-go/03-gear-ratios/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 3: Gear Ratios --- 2 | 3 | You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source, but this is as far as he can bring you. You go inside. 4 | 5 | https://adventofcode.com/2023/day/3 6 | -------------------------------------------------------------------------------- /2023-go/03-gear-ratios/input_test.txt: -------------------------------------------------------------------------------- 1 | 467..114.. 2 | ...*...... 3 | ..35..633. 4 | ......#... 5 | 617*...... 6 | .....+.58. 7 | ..592..... 8 | ......755. 9 | ...$.*.... 10 | .664.598.. 11 | -------------------------------------------------------------------------------- /2023-go/04-scratchcards/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 4: Scratchcards --- 2 | 3 | The gondola takes you up. Strangely, though, the ground doesn't seem to be coming with you; you're not climbing a mountain. As the circle of Snow Island recedes below you, an entire new landmass suddenly appears above you! The gondola carries you to the surface of the new island and lurches into the station. 4 | 5 | https://adventofcode.com/2023/day/4 6 | -------------------------------------------------------------------------------- /2023-go/04-scratchcards/input_test.txt: -------------------------------------------------------------------------------- 1 | Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 2 | Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 3 | Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 4 | Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 5 | Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 6 | Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 7 | -------------------------------------------------------------------------------- /2023-go/05-if-you-give-a-seed-a-fertilizer/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 5: If You Give A Seed A Fertilizer --- 2 | 3 | You take the boat and find the gardener right where you were told he would be: managing a giant "garden" that looks more to you like a farm. 4 | 5 | https://adventofcode.com/2023/day/5 6 | -------------------------------------------------------------------------------- /2023-go/05-if-you-give-a-seed-a-fertilizer/input_test.txt: -------------------------------------------------------------------------------- 1 | seeds: 79 14 55 13 2 | 3 | seed-to-soil map: 4 | 50 98 2 5 | 52 50 48 6 | 7 | soil-to-fertilizer map: 8 | 0 15 37 9 | 37 52 2 10 | 39 0 15 11 | 12 | fertilizer-to-water map: 13 | 49 53 8 14 | 0 11 42 15 | 42 0 7 16 | 57 7 4 17 | 18 | water-to-light map: 19 | 88 18 7 20 | 18 25 70 21 | 22 | light-to-temperature map: 23 | 45 77 23 24 | 81 45 19 25 | 68 64 13 26 | 27 | temperature-to-humidity map: 28 | 0 69 1 29 | 1 0 69 30 | 31 | humidity-to-location map: 32 | 60 56 37 33 | 56 93 4 34 | -------------------------------------------------------------------------------- /2023-go/06-wait-for-it/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 6: Wait For It --- 2 | 3 | The ferry quickly brings you across Island Island. After asking around, you discover that there is indeed normally a large pile of sand somewhere near here, but you don't see anything besides lots of water and the small island where the ferry has docked. 4 | 5 | https://adventofcode.com/2023/day/6 6 | -------------------------------------------------------------------------------- /2023-go/06-wait-for-it/input_test.txt: -------------------------------------------------------------------------------- 1 | seeds: 79 14 55 13 2 | 3 | seed-to-soil map: 4 | 50 98 2 5 | 52 50 48 6 | 7 | soil-to-fertilizer map: 8 | 0 15 37 9 | 37 52 2 10 | 39 0 15 11 | 12 | fertilizer-to-water map: 13 | 49 53 8 14 | 0 11 42 15 | 42 0 7 16 | 57 7 4 17 | 18 | water-to-light map: 19 | 88 18 7 20 | 18 25 70 21 | 22 | light-to-temperature map: 23 | 45 77 23 24 | 81 45 19 25 | 68 64 13 26 | 27 | temperature-to-humidity map: 28 | 0 69 1 29 | 1 0 69 30 | 31 | humidity-to-location map: 32 | 60 56 37 33 | 56 93 4 34 | -------------------------------------------------------------------------------- /2023-go/06-wait-for-it/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | "time" 7 | ) 8 | 9 | func solve(times []int, distances []int) int { 10 | // -x^2 + tx - d > 0 11 | // x = (-t + sqrt(t^2 - 4 * -1 * -d ) / - 2 12 | // x = (-t - sqrt(t^2 - 4 * -1 * -d ) / - 2 13 | prod := 1 14 | for i := 0; i < len(times); i++ { 15 | t := float64(times[i]) 16 | d := float64(distances[i]) 17 | dis := math.Sqrt(t*t - 4*d) 18 | x1 := (t - dis) / 2 19 | x2 := (t + dis) / 2 20 | prod *= int(x2) - int(x1) 21 | } 22 | 23 | return prod 24 | } 25 | 26 | func main() { 27 | timeStart := time.Now() 28 | 29 | times := []int{61, 70, 90, 66} 30 | distances := []int{643, 1184, 1362, 1041} 31 | 32 | fmt.Print("---- Day 6: Wait For It ---\n") 33 | 34 | a1 := solve(times, distances) 35 | fmt.Printf("Part 1: %d\n", a1) 36 | 37 | a2 := solve([]int{61709066}, []int{643118413621040}) 38 | fmt.Printf("Part 2: %d\n", a2) 39 | 40 | fmt.Printf("Time: %.2fms\n", float64(time.Since(timeStart).Microseconds())/1000) 41 | } 42 | -------------------------------------------------------------------------------- /2023-go/07-camel-cards/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 7: Camel Cards --- 2 | 3 | Your all-expenses-paid trip turns out to be a one-way, five-minute ride in an airship. (At least it's a cool airship!) It drops you off at the edge of a vast desert and descends back to Island Island. 4 | 5 | https://adventofcode.com/2023/day/7 6 | -------------------------------------------------------------------------------- /2023-go/07-camel-cards/input_test.txt: -------------------------------------------------------------------------------- 1 | 32T3K 765 2 | T55J5 684 3 | KK677 28 4 | KTJJT 220 5 | QQQJA 483 6 | -------------------------------------------------------------------------------- /2023-go/08-haunted-wasteland/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 8: Haunted Wasteland --- 2 | 3 | You're still riding a camel across Desert Island when you spot a sandstorm quickly approaching. When you turn to warn the Elf, she disappears before your eyes! To be fair, she had just finished warning you about ghosts a few minutes ago. 4 | 5 | https://adventofcode.com/2023/day/8 6 | -------------------------------------------------------------------------------- /2023-go/08-haunted-wasteland/input_test.txt: -------------------------------------------------------------------------------- 1 | LR 2 | 3 | 11A = (11B, XXX) 4 | 11B = (XXX, 11Z) 5 | 11Z = (11B, XXX) 6 | 22A = (22B, XXX) 7 | 22B = (22C, 22C) 8 | 22C = (22Z, 22Z) 9 | 22Z = (22B, 22B) 10 | XXX = (XXX, XXX) 11 | -------------------------------------------------------------------------------- /2023-go/08-haunted-wasteland/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "testing" 4 | 5 | func Test_primeFactors(t *testing.T) { 6 | tests := []struct { 7 | input int 8 | expects []int 9 | }{ 10 | {2, []int{2}}, 11 | {5, []int{5}}, 12 | {5 * 5 * 3, []int{3, 5, 5}}, 13 | {2 * 4 * 7, []int{2, 2, 2, 7}}, 14 | } 15 | 16 | for _, tc := range tests { 17 | got := primeFactors(tc.input) 18 | 19 | if len(got) != len(tc.expects) { 20 | t.Errorf("expected %#v, got %#v\n", tc.expects, got) 21 | } 22 | for i := 0; i < len(tc.expects); i++ { 23 | if got[i] != tc.expects[i] { 24 | t.Errorf("expected %#v, got %#v\n", tc.expects, got) 25 | } 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /2023-go/09-mirage-maintenance/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 9: Mirage Maintenance --- 2 | 3 | You ride the camel through the sandstorm and stop where the ghost's maps told you to stop. The sandstorm subsequently subsides, somehow seeing you standing at an oasis! 4 | 5 | https://adventofcode.com/2023/day/9 6 | -------------------------------------------------------------------------------- /2023-go/09-mirage-maintenance/input_test.txt: -------------------------------------------------------------------------------- 1 | 0 3 6 9 12 15 2 | 1 3 6 10 15 21 3 | 10 13 16 21 30 45 4 | 5 | -------------------------------------------------------------------------------- /2023-go/09-mirage-maintenance/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "testing" 4 | 5 | func Test_primeFactors(t *testing.T) { 6 | tests := []struct { 7 | input int 8 | expects []int 9 | }{ 10 | {2, []int{2}}, 11 | {5, []int{5}}, 12 | {5 * 5 * 3, []int{3, 5, 5}}, 13 | {2 * 4 * 7, []int{2, 2, 2, 7}}, 14 | } 15 | 16 | for _, tc := range tests { 17 | got := primeFactors(tc.input) 18 | 19 | if len(got) != len(tc.expects) { 20 | t.Errorf("expected %#v, got %#v\n", tc.expects, got) 21 | } 22 | for i := 0; i < len(tc.expects); i++ { 23 | if got[i] != tc.expects[i] { 24 | t.Errorf("expected %#v, got %#v\n", tc.expects, got) 25 | } 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /2023-go/10-pipe-maze/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 10: Pipe Maze --- 2 | 3 | You use the hang glider to ride the hot air from Desert Island all the way up to the floating metal island. This island is surprisingly cold and there definitely aren't any thermals to glide on, so you leave your hang glider behind. 4 | 5 | https://adventofcode.com/2023/day/10 6 | -------------------------------------------------------------------------------- /2023-go/10-pipe-maze/input_test.txt: -------------------------------------------------------------------------------- 1 | FF7FSF7F7F7F7F7F---7 2 | L|LJ||||||||||||F--J 3 | FL-7LJLJ||||||LJL-77 4 | F--JF--7||LJLJ7F7FJ- 5 | L---JF-JLJ.||-FJLJJ7 6 | |F|F-JF---7F7-L7L|7| 7 | |FFJF7L7F-JF7|JL---7 8 | 7-L-JL7||F7|L7F-7F7| 9 | L.L7LFJ|||||FJL7||LJ 10 | L7JLJL-JLJLJL--JLJ.L 11 | -------------------------------------------------------------------------------- /2023-go/11-cosmic-expansion/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 11: Cosmic Expansion --- 2 | 3 | You continue following signs for "Hot Springs" and eventually come across an observatory. The Elf within turns out to be a researcher studying cosmic expansion using the giant telescope here. 4 | 5 | https://adventofcode.com/2023/day/11 6 | -------------------------------------------------------------------------------- /2023-go/11-cosmic-expansion/input_test.txt: -------------------------------------------------------------------------------- 1 | ...#...... 2 | .......#.. 3 | #......... 4 | .......... 5 | ......#... 6 | .#........ 7 | .........# 8 | .......... 9 | .......#.. 10 | #...#..... 11 | -------------------------------------------------------------------------------- /2023-go/12-hot-springs/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 12: Hot Springs --- 2 | 3 | You finally reach the hot springs! You can see steam rising from secluded areas attached to the primary, ornate building. 4 | 5 | https://adventofcode.com/2023/day/12 6 | -------------------------------------------------------------------------------- /2023-go/12-hot-springs/input_2.txt: -------------------------------------------------------------------------------- 1 | ?..#?.??#?...??##??? 1,2,2,1,5 2 | ?????????#? 1,4,1 3 | -------------------------------------------------------------------------------- /2023-go/12-hot-springs/input_test.txt: -------------------------------------------------------------------------------- 1 | ???.### 1,1,3 2 | .??..??...?##. 1,1,3 3 | ?#?#?#?#?#?#?#? 1,3,1,6 4 | ????.#...#... 4,1,1 5 | ????.######..#####. 1,6,5 6 | ?###???????? 3,2,1 7 | -------------------------------------------------------------------------------- /2023-go/12-hot-springs/solution.py: -------------------------------------------------------------------------------- 1 | cache = {} 2 | 3 | def count(cfg, nums): 4 | if cfg == "": 5 | return 1 if nums == () else 0 6 | 7 | if nums == (): 8 | return 0 if "#" in cfg else 1 9 | 10 | key = (cfg, nums) 11 | if key in cache: 12 | return cache[key] 13 | 14 | result = 0 15 | 16 | if cfg[0] in ".?": 17 | result += count(cfg[1:], nums) 18 | 19 | if cfg[0] in "#?": 20 | if nums[0] <= len(cfg) and "." not in cfg[:nums[0]] and (nums[0] == len(cfg) or cfg[nums[0]] != "#"): 21 | result += count(cfg[nums[0] + 1:], nums[1:]) 22 | 23 | cache[key] = result 24 | return result 25 | 26 | total = 0 27 | 28 | for line in open(0): 29 | cfg, nums = line.split() 30 | nums = tuple(map(int, nums.split(","))) 31 | 32 | cfg = "?".join([cfg] * 5) 33 | nums *= 5 34 | 35 | total += count(cfg, nums) 36 | 37 | print(total) 38 | -------------------------------------------------------------------------------- /2023-go/13-point-of-incidence/README.txt: -------------------------------------------------------------------------------- 1 | i--- Day 13: Point of Incidence --- 2 | 3 | With your help, the hot springs team locates an appropriate spring which launches you neatly and precisely up to the edge of Lava Island. 4 | 5 | https://adventofcode.com/2023/day/13 6 | -------------------------------------------------------------------------------- /2023-go/13-point-of-incidence/input_test.txt: -------------------------------------------------------------------------------- 1 | #.##..##. 2 | ..#.##.#. 3 | ##......# 4 | ##......# 5 | ..#.##.#. 6 | ..##..##. 7 | #.#.##.#. 8 | 9 | #...##..# 10 | #....#..# 11 | ..##..### 12 | #####.##. 13 | #####.##. 14 | ..##..### 15 | #....#..# 16 | 17 | -------------------------------------------------------------------------------- /2023-go/13-point-of-incidence/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "testing" 4 | 5 | func TestFoo(t *testing.T) { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /2023-go/14-parabolic-reflector-dish/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 14: Parabolic Reflector Dish --- 2 | 3 | You reach the place where all of the mirrors were pointing: a massive parabolic reflector dish attached to the side of another large mountain. 4 | 5 | https://adventofcode.com/2023/day/14 -------------------------------------------------------------------------------- /2023-go/14-parabolic-reflector-dish/input_test.txt: -------------------------------------------------------------------------------- 1 | O....#.... 2 | O.OO#....# 3 | .....##... 4 | OO.#O....O 5 | .O.....O#. 6 | O.#..O.#.# 7 | ..O..#O..O 8 | .......O.. 9 | #....###.. 10 | #OO..#.... -------------------------------------------------------------------------------- /2023-go/14-parabolic-reflector-dish/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "testing" 4 | 5 | func TestFoo(t *testing.T) { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /2023-go/15-lens-library/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 15: Lens Library --- 2 | 3 | The newly-focused parabolic reflector dish is sending all of the collected light to a point on the side of yet another mountain - the largest mountain on Lava Island. As you approach the mountain, you find that the light is being collected by the wall of a large facility embedded in the mountainside. 4 | 5 | https://adventofcode.com/2023/day/15 -------------------------------------------------------------------------------- /2023-go/15-lens-library/input_test.txt: -------------------------------------------------------------------------------- 1 | rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7 -------------------------------------------------------------------------------- /2023-go/16-the-floor-will-be-lava/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 16: The Floor Will Be Lava --- 2 | 3 | With the beam of light completely focused somewhere, the reindeer leads you deeper still into the Lava Production Facility. At some point, you realize that the steel facility walls have been replaced with cave, and the doorways are just cave, and the floor is cave, and you're pretty sure this is actually just a giant cave. 4 | 5 | https://adventofcode.com/2023/day/16 -------------------------------------------------------------------------------- /2023-go/16-the-floor-will-be-lava/input_test.txt: -------------------------------------------------------------------------------- 1 | .|...\.... 2 | |.-.\..... 3 | .....|-... 4 | ........|. 5 | .......... 6 | .........\ 7 | ..../.\\.. 8 | .-.-/..|.. 9 | .|....-|.\ 10 | ..//.|.... -------------------------------------------------------------------------------- /2023-go/17-clumsy-crucible/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 17: Clumsy Crucible --- 2 | 3 | The lava starts flowing rapidly once the Lava Production Facility is operational. As you leave, the reindeer offers you a parachute, allowing you to quickly reach Gear Island. 4 | 5 | https://adventofcode.com/2023/day/17 -------------------------------------------------------------------------------- /2023-go/17-clumsy-crucible/input_test.txt: -------------------------------------------------------------------------------- 1 | 111111111111 2 | 999999999991 3 | 999999999991 4 | 999999999991 5 | 999999999991 -------------------------------------------------------------------------------- /2023-go/18-lavaduct-lagoon/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 18: Lavaduct Lagoon --- 2 | 3 | Thanks to your efforts, the machine parts factory is one of the first factories up and running since the lavafall came back. However, to catch up with the large backlog of parts requests, the factory will also need a large supply of lava for a while; the Elves have already started creating a large lagoon nearby for this purpose. 4 | 5 | https://adventofcode.com/2023/day/18 -------------------------------------------------------------------------------- /2023-go/18-lavaduct-lagoon/input_test.txt: -------------------------------------------------------------------------------- 1 | R 6 (#70c710) 2 | D 5 (#0dc571) 3 | L 2 (#5713f0) 4 | D 2 (#d2c081) 5 | R 2 (#59c680) 6 | D 2 (#411b91) 7 | L 5 (#8ceee2) 8 | U 2 (#caa173) 9 | L 1 (#1b58a2) 10 | U 2 (#caa171) 11 | R 2 (#7807d2) 12 | U 3 (#a77fa3) 13 | L 2 (#015232) 14 | U 2 (#7a21e3) -------------------------------------------------------------------------------- /2023-go/19-aplenty/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 19: Aplenty --- 2 | 3 | The Elves of Gear Island are thankful for your help and send you on your way. They even have a hang glider that someone stole from Desert Island; since you're already going that direction, it would help them a lot if you would use it to get down there and return it to them. 4 | 5 | https://adventofcode.com/2023/day/19 -------------------------------------------------------------------------------- /2023-go/19-aplenty/input_test.txt: -------------------------------------------------------------------------------- 1 | px{a<2006:qkq,m>2090:A,rfg} 2 | pv{a>1716:R,A} 3 | lnx{m>1548:A,A} 4 | rfg{s<537:gd,x>2440:R,A} 5 | qs{s>3448:A,lnx} 6 | qkq{x<1416:A,crn} 7 | crn{x>2662:A,R} 8 | in{s<1351:px,qqz} 9 | qqz{s>2770:qs,m<1801:hdj,R} 10 | gd{a>3333:R,R} 11 | hdj{m>838:A,pv} 12 | 13 | {x=787,m=2655,a=1222,s=2876} 14 | {x=1679,m=44,a=2067,s=496} 15 | {x=2036,m=264,a=79,s=2244} 16 | {x=2461,m=1339,a=466,s=291} 17 | {x=2127,m=1623,a=2188,s=1013} -------------------------------------------------------------------------------- /2023-go/20-pulse-propagation/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 20: Pulse Propagation --- 2 | 3 | With your help, the Elves manage to find the right parts and fix all of the machines. Now, they just need to send the command to boot up the machines and get the sand flowing again. 4 | 5 | https://adventofcode.com/2023/day/20#part2 6 | -------------------------------------------------------------------------------- /2023-go/20-pulse-propagation/input_test.txt: -------------------------------------------------------------------------------- 1 | broadcaster -> a, b, c 2 | %a -> b 3 | %b -> c 4 | %c -> inv 5 | &inv -> a 6 | -------------------------------------------------------------------------------- /2023-go/20-pulse-propagation/input_test_2.txt: -------------------------------------------------------------------------------- 1 | broadcaster -> a 2 | %a -> inv, con 3 | &inv -> b 4 | %b -> con 5 | &con -> output 6 | -------------------------------------------------------------------------------- /2023-go/21-step-counter/README.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dannyvankooten/advent-of-code/c40ff779c5ea239d606091e4fdc638fb4502816f/2023-go/21-step-counter/README.txt -------------------------------------------------------------------------------- /2023-go/21-step-counter/input_test.txt: -------------------------------------------------------------------------------- 1 | ........... 2 | .....###.#. 3 | .###.##..#. 4 | ..#.#...#.. 5 | ....#.#.... 6 | .##..S####. 7 | .##..#...#. 8 | .......##.. 9 | .##.#.####. 10 | .##..##.##. 11 | ........... 12 | -------------------------------------------------------------------------------- /2023-go/21-step-counter/main.py: -------------------------------------------------------------------------------- 1 | with open("input.txt") as f: 2 | data = f.read().strip().splitlines() 3 | 4 | n = len(data) 5 | sparse = {(i,j) for i in range(n) for j in range(n) if data[i][j] in '.S'} 6 | S = next((i,j) for i in range(n) for j in range(n) if data[i][j] == 'S') 7 | dirs = [(1,0),(-1,0),(0,1),(0,-1)] 8 | 9 | def tadd(a,b): return ((a[0]+b[0]),(a[1]+b[1])) 10 | def modp(a): return(a[0]%n, a[1]%n) 11 | 12 | visited, new, cache = {S}, {S}, {0:1} 13 | k, r = 26501365//n, 26501365%n 14 | 15 | for c in range(1,r+2*n+1): 16 | visited, new = new, { np for p in new for di in dirs for np in [tadd(p,di)] 17 | if np not in visited and modp(np) in sparse} 18 | cache[c] = len(new) + (cache[c-2] if c>1 else 0) 19 | 20 | d2 = cache[r+2*n]+cache[r]-2*cache[r+n] 21 | d1 = cache[r+2*n]-cache[r+n] 22 | print("Part1: %d Part2: %d" % (cache[64],cache[r+2*n]+(k-2)*(2*d1+(k-1)*d2)//2)) 23 | -------------------------------------------------------------------------------- /2023-go/22-sand-slabs/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 22: Sand Slabs --- 2 | 3 | Enough sand has fallen; it can finally filter water for Snow Island. 4 | 5 | https://adventofcode.com/2023/day/22 6 | -------------------------------------------------------------------------------- /2023-go/22-sand-slabs/input_test.txt: -------------------------------------------------------------------------------- 1 | 1,0,1~1,2,1 2 | 0,0,2~2,0,2 3 | 0,2,3~2,2,3 4 | 0,0,4~0,2,4 5 | 2,0,5~2,2,5 6 | 0,1,6~2,1,6 7 | 1,1,8~1,1,9 8 | -------------------------------------------------------------------------------- /2023-go/23-a-long-walk/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 23: A Long Walk --- 2 | 3 | The Elves resume water filtering operations! Clean water starts flowing over the edge of Island Island. 4 | 5 | https://adventofcode.com/2023/day/23 6 | -------------------------------------------------------------------------------- /2023-go/23-a-long-walk/input_test.txt: -------------------------------------------------------------------------------- 1 | #.##################### 2 | #.......#########...### 3 | #######.#########.#.### 4 | ###.....#.>.>.###.#.### 5 | ###v#####.#v#.###.#.### 6 | ###.>...#.#.#.....#...# 7 | ###v###.#.#.#########.# 8 | ###...#.#.#.......#...# 9 | #####.#.#.#######.#.### 10 | #.....#.#.#.......#...# 11 | #.#####.#.#.#########v# 12 | #.#...#...#...###...>.# 13 | #.#.#v#######v###.###v# 14 | #...#.>.#...>.>.#.###.# 15 | #####v#.#.###v#.#.###.# 16 | #.....#...#...#.#.#...# 17 | #.#########.###.#.#.### 18 | #...###...#...#...#.### 19 | ###.###.#.###v#####v### 20 | #...#...#.#.>.>.#.>.### 21 | #.###.###.#.###.#.#v### 22 | #.....###...###...#...# 23 | #####################.# 24 | -------------------------------------------------------------------------------- /2023-go/23-a-long-walk/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | ) 7 | 8 | var a int 9 | 10 | func BenchmarkS(b *testing.B) { 11 | input, _ := os.ReadFile("input.txt") 12 | grid := parse(input) 13 | 14 | for n := 0; n < b.N; n++ { 15 | a = solve(grid, false) 16 | } 17 | 18 | b.ReportAllocs() 19 | } 20 | -------------------------------------------------------------------------------- /2023-go/24-never-tell-me-the-odds/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 24: Never Tell Me The Odds --- 2 | 3 | It seems like something is going wrong with the snow-making process. Instead of forming snow, the water that's been absorbed into the air seems to be forming hail! 4 | 5 | https://adventofcode.com/2023/day/24 6 | -------------------------------------------------------------------------------- /2023-go/24-never-tell-me-the-odds/input_test.txt: -------------------------------------------------------------------------------- 1 | 19, 13, 30 @ -2, 1, -2 2 | 18, 19, 22 @ -1, -1, -2 3 | 20, 25, 34 @ -2, -2, -4 4 | 12, 31, 28 @ -1, -2, -1 5 | 20, 19, 15 @ 1, -5, -3 6 | -------------------------------------------------------------------------------- /2023-go/24-never-tell-me-the-odds/main.py: -------------------------------------------------------------------------------- 1 | 2 | import z3 3 | 4 | with open(0) as f: 5 | input = f.read().strip().replace(' @' , ', ').splitlines() 6 | data = [line.split(',') for line in input] 7 | data = [[int(n.strip()) for n in line] for line in data] 8 | 9 | px, py, pz, vx, vy, vz = z3.Ints("px py pz vx vy vz") 10 | times = [z3.Int("t" + str(i)) for i in range(len(data))] 11 | 12 | s = z3.Solver() 13 | for i, (px2, py2, pz2, vx2, vy2, vz2) in enumerate(data): 14 | s.add(px + vx * times[i] == px2 + vx2 * times[i]) 15 | s.add(py + vy * times[i] == py2 + vy2 * times[i]) 16 | s.add(pz + vz * times[i] == pz2 + vz2 * times[i]) 17 | s.check() 18 | ans = s.model().evaluate(px + py + pz) 19 | 20 | print(ans.as_long()) 21 | -------------------------------------------------------------------------------- /2023-go/25-snowverload/README.txt: -------------------------------------------------------------------------------- 1 | --- Day 25: Snowverload --- 2 | 3 | Still somehow without snow, you go to the last place you haven't checked: the center of Snow Island, directly below the waterfall. 4 | 5 | https://adventofcode.com/2023/day/25 6 | -------------------------------------------------------------------------------- /2023-go/25-snowverload/input_test.txt: -------------------------------------------------------------------------------- 1 | jqt: rhn xhk nvd 2 | rsh: frs pzl lsr 3 | xhk: hfx 4 | cmg: qnr nvd lhk bvb 5 | rhn: xhk bvb hfx 6 | bvb: xhk hfx 7 | pzl: lsr hfx nvd 8 | qnr: nvd 9 | ntq: jqt hfx bvb xhk 10 | nvd: lhk 11 | lsr: lhk 12 | rzs: qnr cmg lsr rsh 13 | frs: qnr lhk lsr 14 | -------------------------------------------------------------------------------- /2023-go/25-snowverload/input_test_2.txt: -------------------------------------------------------------------------------- 1 | jqt: rhn xhk 2 | rsh: frs pzl lsr 3 | xhk: hfx 4 | cmg: qnr nvd lhk 5 | rhn: xhk bvb hfx 6 | bvb: xhk hfx 7 | pzl: lsr nvd 8 | qnr: nvd 9 | ntq: jqt hfx bvb xhk 10 | nvd: lhk 11 | lsr: lhk 12 | rzs: qnr cmg lsr rsh 13 | frs: qnr lhk lsr 14 | -------------------------------------------------------------------------------- /2023-go/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/dannyvankooten/advent-of-code 2 | 3 | go 1.21.4 4 | 5 | require ( 6 | github.com/gammazero/deque v0.2.1 7 | github.com/mitchellh/go-z3 v0.0.0-20191228203228-4cbedeba863f 8 | ) 9 | 10 | require ( 11 | github.com/edwingeng/deque v1.0.3 // indirect 12 | github.com/edwingeng/deque/v2 v2.1.1 // indirect 13 | ) 14 | -------------------------------------------------------------------------------- /2023-go/go.sum: -------------------------------------------------------------------------------- 1 | github.com/edwingeng/deque v1.0.3 h1:gx+5OnQK8qMcYNUxcD/M76BT/LujVNVAZEP/MGF8WNw= 2 | github.com/edwingeng/deque v1.0.3/go.mod h1:3Ys1pJhyVaB6iWigv4o2r6Ug1GZmfDWqvqmO6bjojg0= 3 | github.com/edwingeng/deque/v2 v2.1.1 h1:+xjC3TnaeMPLZMi7QQf9jN2K00MZmTwruApqplbL9IY= 4 | github.com/edwingeng/deque/v2 v2.1.1/go.mod h1:HukI8CQe9KDmZCcURPZRYVYjH79Zy2tIjTF9sN3Bgb0= 5 | github.com/gammazero/deque v0.2.1 h1:qSdsbG6pgp6nL7A0+K/B7s12mcCY/5l5SIUpMOl+dC0= 6 | github.com/gammazero/deque v0.2.1/go.mod h1:LFroj8x4cMYCukHJDbxFCkT+r9AndaJnFMuZDV34tuU= 7 | github.com/mitchellh/go-z3 v0.0.0-20191228203228-4cbedeba863f h1:I2Rx8N5cDzVL4amj76w9F+fRS5zvZdmMyphvPMA+HJQ= 8 | github.com/mitchellh/go-z3 v0.0.0-20191228203228-4cbedeba863f/go.mod h1:SCzzTuqNJ1cVcftwEustt+uZcgDQhp0lJnWQPU3a3Lw= 9 | -------------------------------------------------------------------------------- /2023-go/new.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | mkdir $1 4 | touch $1/main.go 5 | touch $1/input.txt 6 | touch $1/input_test.txt 7 | 8 | cat template_main.go > $1/main.go 9 | cat template_main_test.go > $1/main_test.go 10 | touch $1/README.txt 11 | 12 | cd "$1" 13 | -------------------------------------------------------------------------------- /2023-go/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ALLOUT="" 4 | for d in */; do 5 | cd "$d" 6 | OUT=$(go run main.go) 7 | ALLOUT+="$OUT\n" 8 | echo -e "$OUT\n" 9 | cd .. 10 | done 11 | 12 | echo -e "$ALLOUT" | awk 'BEGIN {sum=0.0} NR%4==0 { gsub(/ms$/,"", $2); sum += $2; } END { printf "Total time: %.2fms\n", sum }' -------------------------------------------------------------------------------- /2023-go/template_main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "os" 7 | "time" 8 | ) 9 | 10 | func main() { 11 | timeStart := time.Now() 12 | 13 | input, err := os.ReadFile("input.txt") 14 | if err != nil { 15 | log.Fatal(err) 16 | } 17 | log.Printf("Input: %s\n", input) 18 | 19 | pt1 := 0 20 | pt2 := 0 21 | fmt.Printf("--- He Forgot To Set The Puzzle Name Today ---\n") 22 | fmt.Printf("Part 1: %d\n", pt1) 23 | fmt.Printf("Part 2: %d\n", pt2) 24 | fmt.Printf("Time: %.2fms\n", float64(time.Since(timeStart).Microseconds())/1000) 25 | } -------------------------------------------------------------------------------- /2023-go/template_main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "testing" 4 | 5 | func TestFoo(t *testing.T) { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /2024-php/.gitignore: -------------------------------------------------------------------------------- 1 | *.txt 2 | -------------------------------------------------------------------------------- /2024-php/01.php: -------------------------------------------------------------------------------- 1 | 3) { 12 | return false; 13 | } 14 | 15 | // sign of each diff must match preceding diff 16 | if ($i >= 2 && $diff > 0 != $diff_prev > 0) { 17 | return false; 18 | } 19 | 20 | $diff_prev = $diff; 21 | } 22 | 23 | return true; 24 | } 25 | 26 | $input = trim(file_get_contents('02.txt')); 27 | $pt1 = 0; 28 | $pt2 = 0; 29 | foreach (explode("\n", $input) as $line) { 30 | $numbers = array_map(function($v) { return (int) $v; }, explode(' ', $line)); 31 | 32 | if (is_safe($numbers)) { 33 | $pt1++; 34 | $pt2++; 35 | continue; 36 | } 37 | 38 | // for part 2, brute-force remove each number and see if safe 39 | for ($i = 0; $i < count($numbers); $i++) { 40 | $copy = $numbers; 41 | unset($copy[$i]); 42 | $copy = array_values($copy); 43 | if (is_safe($copy)) { 44 | $pt2++; 45 | break; 46 | } 47 | } 48 | } 49 | 50 | 51 | echo "--- Day 2: Red-Nosed Reports ---", PHP_EOL; 52 | echo "Part 1: ", $pt1, PHP_EOL; 53 | echo "Part 2: ", $pt2, PHP_EOL; 54 | echo "Took ", (microtime(true) - $time_start) * 1000, " ms", PHP_EOL; 55 | echo PHP_EOL; 56 | -------------------------------------------------------------------------------- /2024-php/03.php: -------------------------------------------------------------------------------- 1 | $expected_result || count($numbers) === 0) { 8 | return (int) ($left === $expected_result); 9 | } 10 | 11 | $right = array_pop($numbers); 12 | return solve($expected_result, $left * $right, $numbers, $concat) || solve($expected_result, $left + $right, $numbers, $concat) || ($concat && solve($expected_result, intval("$left$right"), $numbers, $concat)); 13 | } 14 | 15 | $input = array_map(function($line) { 16 | [$expected, $numbers] = explode(": ", $line); 17 | $expected = (int) $expected; 18 | $numbers = array_reverse(array_map('intval', explode(" ", $numbers))); 19 | return [$expected, $numbers]; 20 | }, explode("\n", $input)); 21 | 22 | $pt1 = 0; 23 | $pt2 = 0; 24 | foreach ($input as [$expected, $numbers]) { 25 | $left = array_pop($numbers); 26 | $pt1 += solve($expected, $left, $numbers, false) ? $expected : 0; 27 | $pt2 += solve($expected, $left, $numbers, true) ? $expected : 0; 28 | } 29 | 30 | echo "--- Day 7: Bridge Repair ---", PHP_EOL; 31 | echo "Part 1: ", $pt1, PHP_EOL; 32 | echo "Part 2: ", $pt2, PHP_EOL; 33 | echo "Took ", (microtime(true) - $time_start) * 1000, " ms", PHP_EOL; 34 | echo PHP_EOL; 35 | 36 | assert($pt1 === 303766880536); 37 | assert($pt2 === 337041851384440); 38 | -------------------------------------------------------------------------------- /2024-php/10.php: -------------------------------------------------------------------------------- 1 | = count($map) || $c2 >= count($map[$r2])) { 34 | continue; 35 | } 36 | 37 | if ($map[$r2][$c2] - $map[$r][$c] !== 1) { 38 | continue; 39 | } 40 | 41 | $score += hike($map, $r + $dir[0], $c + $dir[1], $visited, $unique); 42 | } 43 | 44 | return $score; 45 | } 46 | 47 | 48 | $pt1 = 0; 49 | $pt2 = 0; 50 | for ($r = 0; $r < count($map); $r++) { 51 | for ($c = 0; $c < count($map[$r]); $c++) { 52 | if ($map[$r][$c] === 0) { 53 | $visited = []; 54 | $pt1 += hike($map, $r, $c, $visited, true); 55 | 56 | $visited = []; 57 | $pt2 += hike($map, $r, $c, $visited, false); 58 | } 59 | } 60 | } 61 | 62 | echo "--- Day 10 ---", PHP_EOL; 63 | echo "Part 1: ", $pt1, PHP_EOL; 64 | echo "Part 2: ", $pt2, PHP_EOL; 65 | echo "Took ", (microtime(true) - $time_start) * 1000, " ms", PHP_EOL; 66 | echo PHP_EOL; 67 | 68 | assert($pt1 === 746); 69 | assert($pt2 === 1541); 70 | -------------------------------------------------------------------------------- /2024-php/11.php: -------------------------------------------------------------------------------- 1 | ", $line); 19 | [$a, $op, $b] = explode(" ", $expr); 20 | $gates[] = [$a, $op, $b, $out]; 21 | } 22 | 23 | while (!empty($gates)) { 24 | foreach ($gates as $i => [$a, $op, $b, $out]) { 25 | if (!isset($wires[$a], $wires[$b])) continue; 26 | 27 | switch ($op) { 28 | case "AND": 29 | $wires[$out] = (int) ($wires[$a] && $wires[$b]); 30 | break; 31 | 32 | case "OR": 33 | $wires[$out] = (int) ($wires[$a] || $wires[$b]); 34 | break; 35 | 36 | case "XOR": 37 | $wires[$out] = (int) ($wires[$a] != $wires[$b]); 38 | break; 39 | } 40 | 41 | unset($gates[$i]); 42 | } 43 | } 44 | 45 | ksort($wires); 46 | 47 | $pt1 = 0; 48 | foreach (array_reverse($wires) as $name => $value) { 49 | if ($name[0] !== 'z') continue; 50 | 51 | $pt1 = ($pt1 << 1) | $value; 52 | } 53 | 54 | $pt2 = 0; 55 | 56 | echo "--- Day 24 ---", PHP_EOL; 57 | echo "Part 1: ", $pt1, PHP_EOL; 58 | echo "Part 2: ", $pt2, PHP_EOL; 59 | echo "Took ", (microtime(true) - $time_start) * 1000, " ms", PHP_EOL; 60 | echo PHP_EOL; 61 | 62 | assert($pt1 === 58367545758258); 63 | -------------------------------------------------------------------------------- /2024-php/25.php: -------------------------------------------------------------------------------- 1 | $space) { 39 | continue 2; 40 | } 41 | } 42 | 43 | // key fits in lock 44 | $pt1++; 45 | } 46 | } 47 | 48 | $pt2 = "🎄"; 49 | 50 | echo "--- Day 25 ---", PHP_EOL; 51 | echo "Part 1: ", $pt1, PHP_EOL; 52 | echo "Part 2: ", $pt2, PHP_EOL; 53 | echo "Took ", (microtime(true) - $time_start) * 1000, " ms", PHP_EOL; 54 | echo PHP_EOL; 55 | 56 | -------------------------------------------------------------------------------- /2024-php/new.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/bash 2 | 3 | 4 | if [[ "$AOCSESSION" == "" ]]; then 5 | echo "\$AOCSESSION environment variable not set." 6 | exit 1 7 | fi; 8 | 9 | DAY=$1 10 | DAYNAME=$(printf "%02d\n" $DAY) 11 | 12 | cp template.php "$DAYNAME.php" 13 | sed -i "s/%%DAYNAME%%/$DAYNAME/" "$DAYNAME.php" 14 | wget --no-cookies --header="Cookie: session=$AOCSESSION" https://adventofcode.com/2024/day/$DAY/input -O $DAYNAME.txt 15 | -------------------------------------------------------------------------------- /2024-php/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ALLOUT="" 4 | for d in *.php; do 5 | OUT=$(php "$d") 6 | ALLOUT+="$OUT\n" 7 | echo -e "$OUT\n" 8 | done 9 | 10 | echo -e "$ALLOUT" | awk 'BEGIN {sum=0.0} NR%4==0 { gsub(/ms$/,"", $2); sum += $2; } END { printf "Total time: %.2fms\n", sum }' 11 | -------------------------------------------------------------------------------- /2024-php/template.php: -------------------------------------------------------------------------------- 1 | --------------------------------------------------------------------------------