Added day 9
This commit is contained in:
parent
cf6fda2f0c
commit
bd149415db
File diff suppressed because it is too large
Load Diff
|
@ -1,26 +1,26 @@
|
|||
pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980
|
||||
hcl:#623a2f
|
||||
|
||||
eyr:2029 ecl:blu cid:129 byr:1989
|
||||
iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm
|
||||
|
||||
hcl:#888785
|
||||
hgt:164cm byr:2001 iyr:2015 cid:88
|
||||
pid:545766238 ecl:hzl
|
||||
eyr:2022
|
||||
|
||||
iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719
|
||||
|
||||
eyr:1972 cid:100
|
||||
hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926
|
||||
|
||||
iyr:2019
|
||||
hcl:#602927 eyr:1967 hgt:170cm
|
||||
ecl:grn pid:012533040 byr:1946
|
||||
|
||||
hcl:dab227 iyr:2012
|
||||
ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277
|
||||
|
||||
hgt:59cm ecl:zzz
|
||||
eyr:2038 hcl:74454a iyr:2023
|
||||
pid:3556412378 byr:2007
|
||||
133
|
||||
281
|
||||
137
|
||||
134
|
||||
154
|
||||
148
|
||||
150
|
||||
161
|
||||
179
|
||||
211
|
||||
181
|
||||
182
|
||||
215
|
||||
203
|
||||
217
|
||||
232
|
||||
233
|
||||
237
|
||||
234
|
||||
241
|
||||
267
|
||||
254
|
||||
256
|
||||
257
|
||||
270
|
||||
403
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
use itertools::Itertools;
|
||||
use std::error::Error;
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
use std::vec::Vec;
|
||||
|
||||
const WIN_LEN: usize = 25;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let file = File::open("inputs/day9.txt")?;
|
||||
let lines = io::BufReader::new(file).lines().map(|l| l.unwrap());
|
||||
|
||||
let numbers: Vec<u64> = lines.map(|l| l.parse().unwrap()).collect();
|
||||
|
||||
let mut target = 0;
|
||||
|
||||
for win in numbers.windows(WIN_LEN + 1) {
|
||||
target = win[WIN_LEN];
|
||||
let canidates = &win[0..WIN_LEN];
|
||||
let mut valid = false;
|
||||
|
||||
for pair in canidates.iter().combinations(2) {
|
||||
//println!("{} + {} = {}", pair[0], pair[1], pair[0] + pair[1]);
|
||||
if pair[0] + pair[1] == target {
|
||||
valid = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if !valid {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
println!("Invalid Number: {}", target);
|
||||
let invalid_pos = numbers.iter().position(|n| *n == target).unwrap();
|
||||
|
||||
println!("Target is at: {}", invalid_pos);
|
||||
'sum_outer: for len in 2..invalid_pos {
|
||||
for win in numbers.windows(len) {
|
||||
let sum = win.iter().fold(0, |x, acc| x + acc);
|
||||
if sum == target {
|
||||
println!("Set: {:?}", win);
|
||||
let min = win
|
||||
.iter()
|
||||
.fold(*win.first().unwrap(), |x, acc| u64::min(x, *acc));
|
||||
|
||||
let max = win
|
||||
.iter()
|
||||
.fold(*win.first().unwrap(), |x, acc| u64::max(x, *acc));
|
||||
|
||||
println!("Answer: {}", min + max);
|
||||
|
||||
break 'sum_outer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue