Skip to content

Instantly share code, notes, and snippets.

@hellow554
Last active December 2, 2019 11:54
Show Gist options
  • Save hellow554/ac971bec08e5770ae76782f8d7819989 to your computer and use it in GitHub Desktop.
Save hellow554/ac971bec08e5770ae76782f8d7819989 to your computer and use it in GitHub Desktop.
2019
const INPUT: &str = include_str!("../input");
fn parse(s: &str) -> Vec<u32> {
s.lines().map(|x| x.parse().unwrap()).collect()
}
fn required_fuel(st: u32) -> Option<u32> {
(st / 3).checked_sub(2)
}
fn solve_a(v: &[u32]) -> u32 {
v.iter().map(|x| required_fuel(*x)).sum::<Option<_>>().unwrap()
}
fn solve_b(v: &[u32]) -> u32 {
fn calc(st: u32) -> impl Iterator<Item = u32> {
std::iter::successors(required_fuel(st), |v| required_fuel(*v))
}
v.iter().flat_map(|v| calc(*v)).sum()
}
fn main() {
let input = parse(INPUT);
println!("A: {}", solve_a(&input));
println!("B: {}", solve_b(&input));
}
#[test]
fn test_a() {
assert_eq!(2, solve_a(&[12]));
assert_eq!(2, solve_a(&[14]));
assert_eq!(654, solve_a(&[1969]));
assert_eq!(33583, solve_a(&[100756]));
}
#[test]
fn test_b() {
assert_eq!(2, solve_b(&[12]));
assert_eq!(966, solve_b(&[1969]));
assert_eq!(50346, solve_b(&[100756]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment