Skip to content

Commit 95d02dc

Browse files
committed
feat: completed day 10
1 parent e5f166a commit 95d02dc

File tree

7 files changed

+302
-1
lines changed

7 files changed

+302
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- [Day 7](https://github.com/ankjevel/adventofcode/tree/2020/day_07) 🌟 🌟
1313
- [Day 8](https://github.com/ankjevel/adventofcode/tree/2020/day_08) 🌟 🌟
1414
- [Day 9](https://github.com/ankjevel/adventofcode/tree/2020/day_09) 🌟 🌟
15-
- [Day 10](#)
15+
- [Day 10](https://github.com/ankjevel/adventofcode/tree/2020/day_10) 🌟 🌟
1616
- [Day 11](#)
1717
- [Day 12](#)
1818
- [Day 13](#)

day_10/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "day_10"
3+
version = "0.1.0"
4+
edition = "2018"
5+
authors = ["Dennis Pettersson <mail@dennispettersson.se>"]
6+
7+
[lib]
8+
doctest = false
9+
10+
[[bin]]
11+
name = "day_10"
12+
13+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
14+
15+
[dependencies]

day_10/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pub mod part_01;
2+
pub mod part_02;
3+
4+
pub type Input = Vec<u32>;
5+
6+
pub fn parse_input(input: &str) -> Input {
7+
input
8+
.lines()
9+
.map(str::trim)
10+
.filter(|string| !string.is_empty())
11+
.map(|string| string.parse::<_>().unwrap_or(0))
12+
.collect()
13+
}
14+
15+
#[cfg(test)]
16+
mod tests {
17+
use super::*;
18+
19+
const EXAMPLE_DATA: &'static str = "
20+
16
21+
10
22+
15
23+
";
24+
25+
#[test]
26+
fn it_parses_example() {
27+
assert_eq!(parse_input(&EXAMPLE_DATA), vec![16, 10, 15]);
28+
}
29+
}

day_10/src/main.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std::io::Result;
2+
3+
use day_10::{parse_input, part_01::main as part_01, part_02::main as part_02};
4+
5+
fn main() -> Result<()> {
6+
let input = parse_input(include_str!("../../input/day_10"));
7+
8+
println!("part_01: {:?}", part_01(&input)?);
9+
println!("part_02: {:?}", part_02(&input)?);
10+
11+
Ok(())
12+
}
13+
14+

day_10/src/part_01.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use std::io::Result;
2+
3+
use crate::Input;
4+
5+
pub fn main(input: &Input) -> Result<u32> {
6+
let mut input = input.clone().to_owned();
7+
input.sort();
8+
input.push(*input.iter().max().unwrap() + 3);
9+
10+
let mut iter = input.iter_mut().peekable();
11+
let mut jolt = 0;
12+
let mut diff = (0, 0);
13+
while iter.len() > 0 {
14+
if let Some(next) = iter.peek() {
15+
if !(jolt + 1..=jolt + 3).contains(*next) {
16+
break;
17+
}
18+
}
19+
let adapter = iter.next().unwrap();
20+
match *adapter - jolt {
21+
1 => diff.0 += 1,
22+
3 => diff.1 += 1,
23+
_ => (),
24+
}
25+
jolt = *adapter;
26+
}
27+
let (one_jolt, three_jolt) = diff;
28+
Ok(one_jolt * three_jolt)
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use crate::parse_input;
34+
35+
use super::*;
36+
37+
const EXAMPLE_DATA: &'static str = "
38+
16
39+
10
40+
15
41+
5
42+
1
43+
11
44+
7
45+
19
46+
6
47+
12
48+
4
49+
";
50+
51+
#[test]
52+
fn it_gets_the_example_correct() -> Result<()> {
53+
assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 35);
54+
Ok(())
55+
}
56+
}

day_10/src/part_02.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use std::io::Result;
2+
3+
use crate::Input;
4+
5+
fn naive_recursion(jolt: u32, index: usize, adapters: &Vec<u32>) -> u64 {
6+
let len = adapters.len();
7+
if len == index {
8+
return 1;
9+
}
10+
(index..=index + 3)
11+
.filter(|i| i < &len && *adapters.get(*i).unwrap() <= jolt + 3)
12+
.map(|i| naive_recursion(*adapters.get(i).unwrap(), i + 1, adapters))
13+
.sum()
14+
}
15+
16+
pub fn main(input: &Input) -> Result<u64> {
17+
let mut adapters = input.clone().to_owned();
18+
adapters.sort();
19+
adapters.push(*adapters.iter().max().unwrap() + 3);
20+
21+
Ok(naive_recursion(0, 0, &adapters))
22+
}
23+
24+
#[cfg(test)]
25+
mod tests {
26+
use crate::parse_input;
27+
28+
use super::*;
29+
30+
const SHORT_EXAMPLE_DATA: &'static str = "
31+
16
32+
10
33+
15
34+
5
35+
1
36+
11
37+
7
38+
19
39+
6
40+
12
41+
4
42+
";
43+
44+
#[test]
45+
fn it_gets_the_first_example_correct() -> Result<()> {
46+
assert_eq!(main(&parse_input(&SHORT_EXAMPLE_DATA))?, 8);
47+
Ok(())
48+
}
49+
50+
const LONGER_EXAMPLE_DATA: &'static str = "
51+
28
52+
33
53+
18
54+
42
55+
31
56+
14
57+
46
58+
20
59+
48
60+
47
61+
24
62+
23
63+
49
64+
45
65+
19
66+
38
67+
39
68+
11
69+
1
70+
32
71+
25
72+
35
73+
8
74+
17
75+
7
76+
9
77+
4
78+
2
79+
34
80+
10
81+
3
82+
";
83+
84+
#[test]
85+
fn it_gets_the_longer_example_correct() -> Result<()> {
86+
assert_eq!(main(&parse_input(&LONGER_EXAMPLE_DATA))?, 19208);
87+
Ok(())
88+
}
89+
}

input/day_10

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
126
2+
38
3+
162
4+
123
5+
137
6+
97
7+
92
8+
67
9+
136
10+
37
11+
146
12+
2
13+
139
14+
74
15+
101
16+
163
17+
128
18+
127
19+
13
20+
111
21+
30
22+
117
23+
3
24+
93
25+
29
26+
152
27+
80
28+
21
29+
7
30+
54
31+
69
32+
40
33+
48
34+
104
35+
110
36+
142
37+
57
38+
116
39+
31
40+
70
41+
28
42+
151
43+
108
44+
20
45+
157
46+
121
47+
47
48+
75
49+
94
50+
39
51+
73
52+
77
53+
129
54+
41
55+
24
56+
44
57+
132
58+
87
59+
114
60+
58
61+
64
62+
4
63+
10
64+
19
65+
138
66+
45
67+
76
68+
147
69+
59
70+
155
71+
156
72+
83
73+
118
74+
109
75+
107
76+
160
77+
61
78+
91
79+
102
80+
115
81+
68
82+
150
83+
34
84+
16
85+
27
86+
135
87+
161
88+
46
89+
122
90+
90
91+
1
92+
164
93+
100
94+
103
95+
84
96+
145
97+
51
98+
60

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy