main
henine 4 years ago
parent 044946d52f
commit 75746ade18

@ -0,0 +1,9 @@
[package]
name = "day9"
version = "0.1.0"
authors = ["henine"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

File diff suppressed because it is too large Load Diff

@ -0,0 +1,70 @@
use std::collections::VecDeque;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::cmp::{min, max};
fn main() {
let file = match File::open("input") {
Ok(file) => file,
Err(e) => panic!(e)
};
let input_buffer = BufReader::new(&file);
let mut lines = input_buffer.lines();
let mut buffer = VecDeque::with_capacity(25);
let mut list = Vec::new();
for _i in 0..25 {
let input = lines.next().unwrap().unwrap().parse::<u32>().unwrap();
buffer.push_back(input);
list.push(input);
}
let mut code = 0;
for line in lines {
let input = line.unwrap().parse::<u32>().unwrap();
if !is_sum(&buffer, input) {
println!("Code: {}", input);
code = input;
break;
}
buffer.push_back(input);
buffer.pop_front();
list.push(input);
}
let mut start = 0;
let mut end = 1;
let mut sum = list[start] + list[end];
loop {
if sum == code {
println!("Sum: {}",
list[start..end + 1].iter().fold(0, |x,y| max(x,*y)) +
list[start..end + 1].iter().fold(list[start], |x,y|min(x,*y)));
break;
} else if sum > code {
sum -= list[start];
start += 1;
} else {
end += 1;
sum += list[end];
}
}
}
fn is_sum(buffer: &VecDeque<u32>, input: u32) -> bool {
for i in 0..buffer.len() - 1 {
for j in 1..buffer.len() {
if buffer[i] + buffer[j] == input {
return true;
}
}
}
false
}

@ -0,0 +1,20 @@
35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576
Loading…
Cancel
Save