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
|
133
|
||||||
hcl:#623a2f
|
281
|
||||||
|
137
|
||||||
eyr:2029 ecl:blu cid:129 byr:1989
|
134
|
||||||
iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm
|
154
|
||||||
|
148
|
||||||
hcl:#888785
|
150
|
||||||
hgt:164cm byr:2001 iyr:2015 cid:88
|
161
|
||||||
pid:545766238 ecl:hzl
|
179
|
||||||
eyr:2022
|
211
|
||||||
|
181
|
||||||
iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719
|
182
|
||||||
|
215
|
||||||
eyr:1972 cid:100
|
203
|
||||||
hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926
|
217
|
||||||
|
232
|
||||||
iyr:2019
|
233
|
||||||
hcl:#602927 eyr:1967 hgt:170cm
|
237
|
||||||
ecl:grn pid:012533040 byr:1946
|
234
|
||||||
|
241
|
||||||
hcl:dab227 iyr:2012
|
267
|
||||||
ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277
|
254
|
||||||
|
256
|
||||||
hgt:59cm ecl:zzz
|
257
|
||||||
eyr:2038 hcl:74454a iyr:2023
|
270
|
||||||
pid:3556412378 byr:2007
|
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