├── .gitignore
├── day1
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day10
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day11
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day12
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day13
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day14
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day15
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day18
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day2
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day21
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day24
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day3
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day5
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day6
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day7
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day9
├── Cargo.lock
├── Cargo.toml
└── src
│ └── main.rs
├── day8
├── Cargo.toml
├── src
│ └── main.rs
└── Cargo.lock
├── day16
├── Cargo.toml
├── Cargo.lock
└── src
│ └── main.rs
├── day4
├── Cargo.toml
├── Cargo.lock
└── src
│ └── main.rs
├── day19
├── Cargo.toml
├── Cargo.lock
└── src
│ └── main.rs
└── LICENSE
/.gitignore:
--------------------------------------------------------------------------------
1 | *input.txt
2 |
--------------------------------------------------------------------------------
/day1/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 = "day1"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day10/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 = "day10"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day11/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 = "day11"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day12/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 = "day12"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day13/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 = "day13"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day14/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 = "day14"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day15/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 = "day15"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day18/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 = "day18"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day2/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 = "day2"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day21/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 = "day21"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day24/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 = "day24"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day3/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 = "day3"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day5/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 = "day5"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day6/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 = "day6"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day7/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 = "day7"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day9/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 = "day9"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/day1/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day1"
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 |
--------------------------------------------------------------------------------
/day10/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day10"
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 |
--------------------------------------------------------------------------------
/day11/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day11"
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 |
--------------------------------------------------------------------------------
/day12/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day12"
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 |
--------------------------------------------------------------------------------
/day13/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day13"
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 |
--------------------------------------------------------------------------------
/day14/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day14"
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 |
--------------------------------------------------------------------------------
/day15/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day15"
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 |
--------------------------------------------------------------------------------
/day18/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day18"
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 |
--------------------------------------------------------------------------------
/day2/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day2"
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 |
--------------------------------------------------------------------------------
/day21/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day21"
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 |
--------------------------------------------------------------------------------
/day24/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day24"
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 |
--------------------------------------------------------------------------------
/day3/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day3"
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 |
--------------------------------------------------------------------------------
/day5/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day5"
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 |
--------------------------------------------------------------------------------
/day6/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day6"
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 |
--------------------------------------------------------------------------------
/day7/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day7"
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 |
--------------------------------------------------------------------------------
/day9/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day9"
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 |
--------------------------------------------------------------------------------
/day8/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day8"
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 | num = "0.4.1"
10 |
--------------------------------------------------------------------------------
/day16/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day16"
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 | rayon = "1.8.0"
10 |
--------------------------------------------------------------------------------
/day4/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day4"
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 | lazy_static = "1.4.0"
10 | regex = "1.10.2"
11 |
--------------------------------------------------------------------------------
/day19/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "day19"
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 | lazy_static = "1.4.0"
10 | regex = "1.10.2"
11 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | This is free and unencumbered software released into the public domain.
2 |
3 | Anyone is free to copy, modify, publish, use, compile, sell, or
4 | distribute this software, either in source code form or as a compiled
5 | binary, for any purpose, commercial or non-commercial, and by any
6 | means.
7 |
8 | In jurisdictions that recognize copyright laws, the author or authors
9 | of this software dedicate any and all copyright interest in the
10 | software to the public domain. We make this dedication for the benefit
11 | of the public at large and to the detriment of our heirs and
12 | successors. We intend this dedication to be an overt act of
13 | relinquishment in perpetuity of all present and future rights to this
14 | software under copyright law.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | OTHER DEALINGS IN THE SOFTWARE.
23 |
24 | For more information, please refer to
25 |
--------------------------------------------------------------------------------
/day1/src/main.rs:
--------------------------------------------------------------------------------
1 | fn part1(input: &str) -> u32 {
2 | input
3 | .lines()
4 | .map(|line| {
5 | let iter = line.chars().filter_map(|c| c.to_digit(10));
6 | iter.clone().next().unwrap() * 10 + iter.last().unwrap()
7 | })
8 | .sum()
9 | }
10 |
11 | fn part2(input: &str) -> u32 {
12 | input
13 | .lines()
14 | .map(|line| {
15 | let line = line
16 | .replace("nineight", "98")
17 | .replace("eighthree", "83")
18 | .replace("eightwo", "82")
19 | .replace("twone", "21")
20 | .replace("oneight", "18")
21 | .replace("threeight", "38")
22 | .replace("fiveight", "58")
23 | .replace("sevenine", "79")
24 | .replace("one", "1")
25 | .replace("two", "2")
26 | .replace("three", "3")
27 | .replace("four", "4")
28 | .replace("five", "5")
29 | .replace("six", "6")
30 | .replace("seven", "7")
31 | .replace("eight", "8")
32 | .replace("nine", "9");
33 | let iter = line.chars().filter(|c| c.is_ascii_digit());
34 | iter.clone().next().unwrap().to_digit(10).unwrap() * 10
35 | + iter.last().unwrap().to_digit(10).unwrap()
36 | })
37 | .sum()
38 | }
39 |
40 | fn main() {
41 | let input = include_str!("../input.txt");
42 | println!("{}", part1(input));
43 | println!("{}", part2(input));
44 | }
45 |
--------------------------------------------------------------------------------
/day9/src/main.rs:
--------------------------------------------------------------------------------
1 | fn parse(input_str: &str) -> Vec> {
2 | input_str
3 | .lines()
4 | .map(|line| {
5 | line.split_whitespace()
6 | .map(|num| num.parse().unwrap())
7 | .collect()
8 | })
9 | .collect()
10 | }
11 |
12 | fn get_history(sequence: &[i64]) -> Vec> {
13 | let mut history: Vec> = vec![];
14 | let mut current_sequence = sequence.to_vec();
15 | while current_sequence.iter().any(|x| *x != 0) {
16 | history.push(current_sequence.clone());
17 | current_sequence = current_sequence.windows(2).map(|x| x[1] - x[0]).collect();
18 | }
19 | history
20 | }
21 |
22 | fn part1(parsed_input: &[Vec]) -> i64 {
23 | parsed_input
24 | .iter()
25 | .map(|sequence| {
26 | get_history(sequence)
27 | .iter()
28 | .map(|sequence| sequence.last().unwrap())
29 | .sum::()
30 | })
31 | .sum()
32 | }
33 |
34 | fn part2(parsed_input: &[Vec]) -> i64 {
35 | parsed_input
36 | .iter()
37 | .map(|sequence| {
38 | get_history(sequence)
39 | .iter()
40 | .enumerate()
41 | .map(|(idx, sequence)| {
42 | sequence.first().unwrap() * if idx % 2 == 0 { 1 } else { -1 }
43 | })
44 | .sum::()
45 | })
46 | .sum()
47 | }
48 |
49 | fn main() {
50 | let input = parse(include_str!("../input.txt"));
51 | println!("{}", part1(&input));
52 | println!("{}", part2(&input));
53 | }
54 |
--------------------------------------------------------------------------------
/day19/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 = "aho-corasick"
7 | version = "1.1.2"
8 | source = "registry+https://github.com/rust-lang/crates.io-index"
9 | checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
10 | dependencies = [
11 | "memchr",
12 | ]
13 |
14 | [[package]]
15 | name = "day19"
16 | version = "0.1.0"
17 | dependencies = [
18 | "lazy_static",
19 | "regex",
20 | ]
21 |
22 | [[package]]
23 | name = "lazy_static"
24 | version = "1.4.0"
25 | source = "registry+https://github.com/rust-lang/crates.io-index"
26 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
27 |
28 | [[package]]
29 | name = "memchr"
30 | version = "2.6.4"
31 | source = "registry+https://github.com/rust-lang/crates.io-index"
32 | checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
33 |
34 | [[package]]
35 | name = "regex"
36 | version = "1.10.2"
37 | source = "registry+https://github.com/rust-lang/crates.io-index"
38 | checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
39 | dependencies = [
40 | "aho-corasick",
41 | "memchr",
42 | "regex-automata",
43 | "regex-syntax",
44 | ]
45 |
46 | [[package]]
47 | name = "regex-automata"
48 | version = "0.4.3"
49 | source = "registry+https://github.com/rust-lang/crates.io-index"
50 | checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
51 | dependencies = [
52 | "aho-corasick",
53 | "memchr",
54 | "regex-syntax",
55 | ]
56 |
57 | [[package]]
58 | name = "regex-syntax"
59 | version = "0.8.2"
60 | source = "registry+https://github.com/rust-lang/crates.io-index"
61 | checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
62 |
--------------------------------------------------------------------------------
/day4/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 = "aho-corasick"
7 | version = "1.1.2"
8 | source = "registry+https://github.com/rust-lang/crates.io-index"
9 | checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
10 | dependencies = [
11 | "memchr",
12 | ]
13 |
14 | [[package]]
15 | name = "day4"
16 | version = "0.1.0"
17 | dependencies = [
18 | "lazy_static",
19 | "regex",
20 | ]
21 |
22 | [[package]]
23 | name = "lazy_static"
24 | version = "1.4.0"
25 | source = "registry+https://github.com/rust-lang/crates.io-index"
26 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
27 |
28 | [[package]]
29 | name = "memchr"
30 | version = "2.6.4"
31 | source = "registry+https://github.com/rust-lang/crates.io-index"
32 | checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
33 |
34 | [[package]]
35 | name = "regex"
36 | version = "1.10.2"
37 | source = "registry+https://github.com/rust-lang/crates.io-index"
38 | checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
39 | dependencies = [
40 | "aho-corasick",
41 | "memchr",
42 | "regex-automata",
43 | "regex-syntax",
44 | ]
45 |
46 | [[package]]
47 | name = "regex-automata"
48 | version = "0.4.3"
49 | source = "registry+https://github.com/rust-lang/crates.io-index"
50 | checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
51 | dependencies = [
52 | "aho-corasick",
53 | "memchr",
54 | "regex-syntax",
55 | ]
56 |
57 | [[package]]
58 | name = "regex-syntax"
59 | version = "0.8.2"
60 | source = "registry+https://github.com/rust-lang/crates.io-index"
61 | checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
62 |
--------------------------------------------------------------------------------
/day6/src/main.rs:
--------------------------------------------------------------------------------
1 | fn get_numbers_from_line(line: &str) -> Vec {
2 | line.split(' ')
3 | .skip(1)
4 | .map(|num| num.trim())
5 | .filter(|num| !num.is_empty())
6 | .map(|num| num.parse().unwrap())
7 | .collect()
8 | }
9 |
10 | fn get_distance_for_hold_time(hold_time: u64, total_time: u64) -> u64 {
11 | (total_time - hold_time) * hold_time
12 | }
13 |
14 | fn part1(times: &[u64], distances: &[u64]) -> u64 {
15 | (0..times.len())
16 | .map(|i| {
17 | (1..times[i])
18 | .map(|hold_time| {
19 | if get_distance_for_hold_time(hold_time, times[i]) > distances[i] {
20 | 1
21 | } else {
22 | 0
23 | }
24 | })
25 | .sum::()
26 | })
27 | .product()
28 | }
29 |
30 | fn concat_numbers(numbers: &[u64]) -> u64 {
31 | numbers
32 | .iter()
33 | .flat_map(|t| t.to_string().chars().collect::>())
34 | .collect::()
35 | .parse()
36 | .unwrap()
37 | }
38 |
39 | fn part2(times: &[u64], distances: &[u64]) -> u64 {
40 | let time: u64 = concat_numbers(times);
41 | let distance: u64 = concat_numbers(distances);
42 | (1..time)
43 | .map(|hold_time| {
44 | if get_distance_for_hold_time(hold_time, time) > distance {
45 | 1
46 | } else {
47 | 0
48 | }
49 | })
50 | .sum()
51 | }
52 |
53 | fn main() {
54 | let mut input_lines = include_str!("../input.txt").lines();
55 | let times = get_numbers_from_line(input_lines.next().unwrap());
56 | let distances = get_numbers_from_line(input_lines.next().unwrap());
57 | println!("{}", part1(×, &distances));
58 | println!("{}", part2(×, &distances));
59 | }
60 |
--------------------------------------------------------------------------------
/day13/src/main.rs:
--------------------------------------------------------------------------------
1 | fn parse(input_str: &str) -> Vec> {
2 | input_str
3 | .split("\n\n")
4 | .map(|split| split.lines().map(|l| l.to_string()).collect())
5 | .collect()
6 | }
7 |
8 | fn diff(str1: &str, str2: &str) -> usize {
9 | str1.chars()
10 | .zip(str2.chars())
11 | .filter(|(c1, c2)| c1 != c2)
12 | .count()
13 | }
14 |
15 | fn check_mirror_correctness(block: &[String], lines_above: usize) -> usize {
16 | let mut errors = 0;
17 | if lines_above <= block.len() / 2 {
18 | for i in 0..lines_above {
19 | errors += diff(&block[i], &block[2 * lines_above - i - 1]);
20 | }
21 | } else {
22 | for i in lines_above..block.len() {
23 | errors += diff(&block[i], &block[2 * lines_above - i - 1]);
24 | }
25 | }
26 | errors
27 | }
28 |
29 | fn transpose(block: &[String]) -> Vec {
30 | let mut res = vec![];
31 | for c in 0..block[0].len() {
32 | let mut row = "".to_string();
33 | for r in block {
34 | row.push(*r.as_bytes().get(c).unwrap() as char);
35 | }
36 | res.push(row);
37 | }
38 | res
39 | }
40 |
41 | fn mirror_sum(blocks: &[Vec], error_count: usize) -> usize {
42 | let mut sum = 0;
43 | 'outer: for block in blocks {
44 | for above in 1..block.len() {
45 | if check_mirror_correctness(block, above) == error_count {
46 | sum += 100 * above;
47 | continue 'outer;
48 | }
49 | }
50 | let block_t = transpose(block);
51 | for above in 1..block_t.len() {
52 | if check_mirror_correctness(&block_t, above) == error_count {
53 | sum += above;
54 | }
55 | }
56 | }
57 | sum
58 | }
59 |
60 | fn part1(blocks: &[Vec]) -> usize {
61 | mirror_sum(blocks, 0)
62 | }
63 |
64 | fn part2(blocks: &[Vec]) -> usize {
65 | mirror_sum(blocks, 1)
66 | }
67 |
68 | fn main() {
69 | let input = parse(include_str!("../input.txt"));
70 | println!("{}", part1(&input));
71 | println!("{}", part2(&input));
72 | }
73 |
--------------------------------------------------------------------------------
/day4/src/main.rs:
--------------------------------------------------------------------------------
1 | use std::collections::HashMap;
2 |
3 | use lazy_static::lazy_static;
4 | use regex::Regex;
5 |
6 | #[derive(Debug)]
7 | struct Card {
8 | id: u32,
9 | winning_numbers: Vec,
10 | have_numbers: Vec,
11 | }
12 |
13 | lazy_static! {
14 | static ref CARD_REGEX: Regex = Regex::new(r"Card *(\d*):(( ?\d* )*)\|(( ?\d* ?)*)").unwrap();
15 | }
16 |
17 | fn get_numbers_from_capture(capture_string: &str) -> Vec {
18 | capture_string
19 | .trim()
20 | .split(' ')
21 | .filter(|s| !s.is_empty())
22 | .map(|num| num.trim().parse().unwrap())
23 | .collect()
24 | }
25 |
26 | impl Card {
27 | fn from_line(line: &str) -> Self {
28 | let captures = CARD_REGEX.captures(line).unwrap();
29 | Self {
30 | id: captures.get(1).unwrap().as_str().parse().unwrap(),
31 | winning_numbers: get_numbers_from_capture(captures.get(2).unwrap().as_str()),
32 | have_numbers: get_numbers_from_capture(captures.get(4).unwrap().as_str()),
33 | }
34 | }
35 | fn points_worth(&self) -> u32 {
36 | self.have_numbers
37 | .iter()
38 | .filter(|n| self.winning_numbers.contains(n))
39 | .count() as u32
40 | }
41 | }
42 |
43 | fn part1(cards: &[Card]) -> u32 {
44 | cards
45 | .iter()
46 | .map(|card| match card.points_worth() {
47 | 0 => 0,
48 | c => (c - 1) << 1,
49 | })
50 | .sum()
51 | }
52 |
53 | fn part2(cards: &[Card]) -> u32 {
54 | let mut amounts: HashMap = HashMap::from_iter(cards.iter().map(|card| (card.id, 1)));
55 | for card in cards {
56 | let card_amount = *amounts.get(&card.id).unwrap();
57 | for i in card.id + 1..=card.id + card.points_worth() {
58 | if let Some(amount) = amounts.get_mut(&i) {
59 | *amount += card_amount;
60 | }
61 | }
62 | }
63 | amounts.values().sum()
64 | }
65 |
66 | fn main() {
67 | let input = include_str!("../input.txt");
68 | let cards: Vec = input.lines().map(Card::from_line).collect();
69 | println!("{}", part1(&cards));
70 | println!("{}", part2(&cards));
71 | }
72 |
--------------------------------------------------------------------------------
/day8/src/main.rs:
--------------------------------------------------------------------------------
1 | use std::collections::HashMap;
2 |
3 | use num::integer::lcm;
4 |
5 | type Nodes = HashMap;
6 |
7 | fn node_from_line(line: &str) -> (String, (String, String)) {
8 | (
9 | line.chars().take(3).collect(),
10 | (
11 | line.chars().skip(7).take(3).collect(),
12 | line.chars().skip(12).take(3).collect(),
13 | ),
14 | )
15 | }
16 |
17 | fn parse(input_str: &str) -> (String, Nodes) {
18 | (
19 | input_str.lines().next().unwrap().to_string(),
20 | input_str.lines().skip(2).map(node_from_line).collect(),
21 | )
22 | }
23 |
24 | fn get_next_node(instruction: &char, current_node: &str, nodes: &Nodes) -> String {
25 | let children = nodes.get(current_node).unwrap();
26 | match instruction {
27 | 'L' => children.0.clone(),
28 | 'R' => children.1.clone(),
29 | _ => panic!(),
30 | }
31 | }
32 |
33 | fn part1(instructions: &str, nodes: &Nodes) -> u32 {
34 | let mut current_node = "AAA".to_string();
35 | let mut steps = 0;
36 | for instruction in instructions.chars().cycle() {
37 | if current_node == "ZZZ" {
38 | break;
39 | }
40 | current_node = get_next_node(&instruction, ¤t_node, nodes);
41 | steps += 1;
42 | }
43 | steps
44 | }
45 |
46 | fn part2(instructions: &str, nodes: &Nodes) -> u64 {
47 | nodes
48 | .keys()
49 | .filter(|node| node.ends_with('A'))
50 | .map(|start_node| {
51 | let mut steps: u64 = 0;
52 | let mut current_node = start_node.clone();
53 | for instruction in instructions.chars().cycle() {
54 | if current_node.ends_with('Z') {
55 | break;
56 | }
57 | current_node = get_next_node(&instruction, ¤t_node, nodes);
58 | steps += 1;
59 | }
60 | steps
61 | })
62 | .fold(1, lcm)
63 | }
64 |
65 | fn main() {
66 | let input = include_str!("../input.txt");
67 | let (instructions, nodes) = parse(input);
68 | println!("{}", part1(&instructions, &nodes));
69 | println!("{}", part2(&instructions, &nodes));
70 | }
71 |
--------------------------------------------------------------------------------
/day5/src/main.rs:
--------------------------------------------------------------------------------
1 | #[derive(Debug)]
2 | struct Map {
3 | maps: Vec<(u32, u32, u32)>,
4 | }
5 |
6 | impl Map {
7 | fn from_block(block: &str) -> Self {
8 | Self {
9 | maps: block
10 | .lines()
11 | .skip(1)
12 | .map(|l| {
13 | let mut val_iter = l.split(' ').map(|n| n.parse().unwrap());
14 | (
15 | val_iter.next().unwrap(),
16 | val_iter.next().unwrap(),
17 | val_iter.next().unwrap(),
18 | )
19 | })
20 | .collect(),
21 | }
22 | }
23 | fn get_mapped_value(&self, src: u32) -> u32 {
24 | for map in &self.maps {
25 | if src >= map.1 && src <= map.1 + map.2 {
26 | return map.0 + (src - map.1);
27 | }
28 | }
29 | src
30 | }
31 | }
32 |
33 | fn parse(input: &str) -> (Vec, Vec