This commit is contained in:
Andrew Glaze 2023-12-28 13:36:06 -05:00
parent b44361e14d
commit 0407edf928
2 changed files with 110 additions and 0 deletions

109
src/day10.rs Normal file
View File

@ -0,0 +1,109 @@
use std::collections::HashMap;
use aoc_runner_derive::{aoc, aoc_generator};
use itertools::Itertools;
#[aoc_generator(day10)]
fn parse(input: &str) -> Vec<u32> {
input.lines().map(str::parse).map(Result::unwrap).sorted().collect_vec()
}
#[aoc(day10, part1)]
fn part1(input: &Vec<u32>) -> u32 {
let mut count1 = 1;
let mut count3 = 1;
for (lhs, rhs) in input.iter().tuple_windows() {
match rhs - lhs {
1 => count1 += 1,
3 => count3 += 1,
_ => unreachable!()
}
}
count1 * count3
}
#[aoc(day10, part2)]
fn part2(input: &Vec<u32>) -> i64 {
let mut ans = HashMap::new();
ans.insert(0, 1);
for adapter in input.iter().cloned().map(|x| x as i32) {
let to_insert = ans.get(&(adapter as i32 - 1)).unwrap_or(&0)
+ ans.get(&(adapter as i32 - 2)).unwrap_or(&0)
+ ans.get(&(adapter as i32 - 3)).unwrap_or(&0);
ans.insert(adapter, to_insert.clone());
}
ans[&(*input.last().unwrap() as i32)]
}
#[cfg(test)]
mod tests {
use super::*;
const EX: &str = r"16
10
15
5
1
11
7
19
6
12
4";
const EX_2: &str = r"28
33
18
42
31
14
46
20
48
47
24
23
49
45
19
38
39
11
1
32
25
35
8
17
7
9
4
2
34
10
3";
#[test]
fn part1_example() {
assert_eq!(part1(&parse(EX)), 35);
}
#[test]
fn part1_example2() {
assert_eq!(part1(&parse(EX_2)), 220);
}
#[test]
fn part2_example() {
assert_eq!(part2(&parse(EX)), 8);
}
#[test]
fn part2_example2() {
assert_eq!(part2(&parse(EX_2)), 19208);
}
}

View File

@ -1,3 +1,4 @@
mod day10;
mod day9; mod day9;
mod day8; mod day8;
mod day7; mod day7;