Completed day 10. Not wildly happy with my runtime
This commit is contained in:
12
day_09/cheating.py
Normal file
12
day_09/cheating.py
Normal file
@@ -0,0 +1,12 @@
|
||||
l = [[int(i) for i in s.split()] for s in open('input.txt').read().split('\n') if s.strip()]
|
||||
|
||||
def n(l):
|
||||
if sum(i != 0 for i in l) == 0:
|
||||
return 0
|
||||
m = []
|
||||
for i in range(len(l)-1):
|
||||
m.append(l[i+1]-l[i])
|
||||
return l[-1] + n(m)
|
||||
|
||||
print([n(i) for i in l])
|
||||
print(sum(n(i[::-1]) for i in l))
|
||||
@@ -24,7 +24,7 @@ fn expand(sequence: Vec<i64>) -> Array2<i64> {
|
||||
}
|
||||
|
||||
fn extrapolate(mut reduced: Array2<i64>) -> i64 {
|
||||
let first_i = reduced.outer_iter().position(|row| {row.sum() == 0}).unwrap() - 1;
|
||||
let first_i = reduced.outer_iter().position(|row| {row.iter().all(|x| { x == &0 })}).unwrap() - 1;
|
||||
let mut j = reduced.slice(s!(first_i,..)).iter().position(|value| {value == &0}).unwrap();
|
||||
for i in (0..=first_i).rev() {
|
||||
reduced[[i,j]] = reduced[[i, j-1]] + reduced[[i+1, j-1]];
|
||||
@@ -37,15 +37,20 @@ pub fn part1(input: &str) -> String {
|
||||
input.lines().map(|line| {
|
||||
parse_line(line).unwrap().1
|
||||
}).map(|x| {
|
||||
dbg!(&x);
|
||||
let reduced = expand(x);
|
||||
dbg!(extrapolate(reduced.clone()));
|
||||
extrapolate(reduced)
|
||||
}).sum::<i64>().to_string()
|
||||
}
|
||||
|
||||
pub fn part2(input: &str) -> String {
|
||||
todo!()
|
||||
input.lines().map(|line| {
|
||||
let mut nums = parse_line(line).unwrap().1;
|
||||
nums.reverse();
|
||||
nums
|
||||
}).map(|x| {
|
||||
let reduced = expand(x);
|
||||
extrapolate(reduced)
|
||||
}).sum::<i64>().to_string()
|
||||
}
|
||||
|
||||
pub mod prelude {
|
||||
|
||||
@@ -16,13 +16,13 @@ mod tests {
|
||||
1 3 6 10 15 21
|
||||
10 13 16 21 30 45";
|
||||
assert_eq!(part1(test_input), "114".to_string());
|
||||
panic!();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part2() {
|
||||
let test_input = "";
|
||||
assert_eq!(part2(test_input), "".to_string());
|
||||
assert_eq!(part2("0 3 6 9 12 15"), "-3".to_string());
|
||||
assert_eq!(part2("1 3 6 10 15 21"), "0".to_string());
|
||||
assert_eq!(part2("10 13 16 21 30 45"), "5".to_string());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user