This commit is contained in:
Bizcochito 2022-12-14 13:00:09 +01:00
parent dba886ece0
commit c44c94c229
6 changed files with 231 additions and 25 deletions

4
Cargo.lock generated
View File

@ -63,6 +63,10 @@ dependencies = [
name = "day13"
version = "0.1.0"
[[package]]
name = "day14"
version = "0.1.0"
[[package]]
name = "either"
version = "1.8.0"

View File

@ -15,7 +15,7 @@ members = [
"day11",
"day12",
"day13",
# "day14",
"day14",
# "day15",
# "day16",
# "day17",

View File

@ -3,12 +3,11 @@ use std::str::FromStr;
fn main() {
const FILE_PATH: &str = "input";
println!("Hi this is the twELFth day of AOC2022, first we will read the file {}", FILE_PATH);
println!("Hi this is the thirteenth day of AOC2022, first we will read the file {}", FILE_PATH);
let contents = fs::read_to_string(FILE_PATH)
.expect("Should have been able to read the file");
let mut mes = parse(&contents);
println!("The sum of indexes of ordered packets is: {}", mes.chunks(2).map(|l| l[0] < l[1]).enumerate().filter(|(_, b)| *b).map(|(i,_)| i+1).sum::<usize>());
mes.push(parse_line("[[2]]"));
mes.push(parse_line("[[6]]"));
@ -50,28 +49,7 @@ impl PartialOrd for Item{
}
impl Ord for Item {
fn cmp(&self, other: &Self) -> Ordering {
match self {
Item::Num(s) => {
match other {
Item::Num(o) => {
s.cmp(o)
},
Item::List(_) => {
(Item::List(vec![self.clone()])).cmp(other)
},
}
},
Item::List(s) => {
match other {
Item::List(o) => {
s.cmp(o)
},
Item::Num(_) => {
self.cmp(&Item::List(vec![other.clone()]))
},
}
},
}
self.partial_cmp(other).expect("Order must exist")
}
}

8
day14/Cargo.toml Normal file
View File

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

130
day14/input Normal file
View File

@ -0,0 +1,130 @@
522,57 -> 522,59 -> 516,59 -> 516,67 -> 531,67 -> 531,59 -> 526,59 -> 526,57
479,105 -> 479,107 -> 472,107 -> 472,113 -> 483,113 -> 483,107 -> 482,107 -> 482,105
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
510,43 -> 515,43
477,141 -> 477,144 -> 469,144 -> 469,148 -> 491,148 -> 491,144 -> 483,144 -> 483,141
525,54 -> 537,54 -> 537,53
504,47 -> 509,47
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
489,136 -> 494,136
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
479,105 -> 479,107 -> 472,107 -> 472,113 -> 483,113 -> 483,107 -> 482,107 -> 482,105
460,93 -> 464,93
499,32 -> 499,35 -> 498,35 -> 498,40 -> 512,40 -> 512,35 -> 505,35 -> 505,32
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
477,141 -> 477,144 -> 469,144 -> 469,148 -> 491,148 -> 491,144 -> 483,144 -> 483,141
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
469,91 -> 473,91
496,15 -> 496,16 -> 510,16
500,138 -> 505,138
499,32 -> 499,35 -> 498,35 -> 498,40 -> 512,40 -> 512,35 -> 505,35 -> 505,32
479,105 -> 479,107 -> 472,107 -> 472,113 -> 483,113 -> 483,107 -> 482,107 -> 482,105
469,87 -> 473,87
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
522,49 -> 527,49
463,91 -> 467,91
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
496,136 -> 501,136
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
472,93 -> 476,93
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
499,32 -> 499,35 -> 498,35 -> 498,40 -> 512,40 -> 512,35 -> 505,35 -> 505,32
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
477,141 -> 477,144 -> 469,144 -> 469,148 -> 491,148 -> 491,144 -> 483,144 -> 483,141
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
486,138 -> 491,138
499,32 -> 499,35 -> 498,35 -> 498,40 -> 512,40 -> 512,35 -> 505,35 -> 505,32
481,130 -> 481,131 -> 494,131
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
514,45 -> 519,45
518,47 -> 523,47
501,49 -> 506,49
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
484,100 -> 488,100
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
481,102 -> 485,102
475,91 -> 479,91
499,32 -> 499,35 -> 498,35 -> 498,40 -> 512,40 -> 512,35 -> 505,35 -> 505,32
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
522,57 -> 522,59 -> 516,59 -> 516,67 -> 531,67 -> 531,59 -> 526,59 -> 526,57
478,93 -> 482,93
492,134 -> 497,134
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
477,141 -> 477,144 -> 469,144 -> 469,148 -> 491,148 -> 491,144 -> 483,144 -> 483,141
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
472,89 -> 476,89
479,105 -> 479,107 -> 472,107 -> 472,113 -> 483,113 -> 483,107 -> 482,107 -> 482,105
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
507,45 -> 512,45
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
522,57 -> 522,59 -> 516,59 -> 516,67 -> 531,67 -> 531,59 -> 526,59 -> 526,57
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
472,84 -> 482,84
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
479,105 -> 479,107 -> 472,107 -> 472,113 -> 483,113 -> 483,107 -> 482,107 -> 482,105
477,141 -> 477,144 -> 469,144 -> 469,148 -> 491,148 -> 491,144 -> 483,144 -> 483,141
479,105 -> 479,107 -> 472,107 -> 472,113 -> 483,113 -> 483,107 -> 482,107 -> 482,105
479,105 -> 479,107 -> 472,107 -> 472,113 -> 483,113 -> 483,107 -> 482,107 -> 482,105
508,49 -> 513,49
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
466,93 -> 470,93
522,57 -> 522,59 -> 516,59 -> 516,67 -> 531,67 -> 531,59 -> 526,59 -> 526,57
499,32 -> 499,35 -> 498,35 -> 498,40 -> 512,40 -> 512,35 -> 505,35 -> 505,32
522,57 -> 522,59 -> 516,59 -> 516,67 -> 531,67 -> 531,59 -> 526,59 -> 526,57
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
511,47 -> 516,47
493,138 -> 498,138
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
522,57 -> 522,59 -> 516,59 -> 516,67 -> 531,67 -> 531,59 -> 526,59 -> 526,57
499,102 -> 503,102
477,141 -> 477,144 -> 469,144 -> 469,148 -> 491,148 -> 491,144 -> 483,144 -> 483,141
525,54 -> 537,54 -> 537,53
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
496,15 -> 496,16 -> 510,16
487,98 -> 491,98
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
477,141 -> 477,144 -> 469,144 -> 469,148 -> 491,148 -> 491,144 -> 483,144 -> 483,141
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
490,96 -> 494,96
496,100 -> 500,100
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
493,98 -> 497,98
466,89 -> 470,89
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
481,130 -> 481,131 -> 494,131
481,126 -> 481,121 -> 481,126 -> 483,126 -> 483,122 -> 483,126 -> 485,126 -> 485,125 -> 485,126
499,32 -> 499,35 -> 498,35 -> 498,40 -> 512,40 -> 512,35 -> 505,35 -> 505,32
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
487,102 -> 491,102
465,161 -> 465,155 -> 465,161 -> 467,161 -> 467,151 -> 467,161 -> 469,161 -> 469,151 -> 469,161
522,57 -> 522,59 -> 516,59 -> 516,67 -> 531,67 -> 531,59 -> 526,59 -> 526,57
493,102 -> 497,102
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
479,80 -> 479,78 -> 479,80 -> 481,80 -> 481,77 -> 481,80 -> 483,80 -> 483,77 -> 483,80 -> 485,80 -> 485,71 -> 485,80 -> 487,80 -> 487,74 -> 487,80 -> 489,80 -> 489,71 -> 489,80 -> 491,80 -> 491,75 -> 491,80
487,29 -> 487,21 -> 487,29 -> 489,29 -> 489,22 -> 489,29 -> 491,29 -> 491,22 -> 491,29 -> 493,29 -> 493,19 -> 493,29 -> 495,29 -> 495,23 -> 495,29 -> 497,29 -> 497,27 -> 497,29 -> 499,29 -> 499,23 -> 499,29 -> 501,29 -> 501,26 -> 501,29
515,49 -> 520,49
490,100 -> 494,100

86
day14/src/main.rs Normal file
View File

@ -0,0 +1,86 @@
use std::fs;
use std::str::FromStr;
use std::collections::HashMap;
fn main() {
const FILE_PATH: &str = "input";
println!("Hi this is the fourteenth day of AOC2022, first we will read the file {}", FILE_PATH);
let contents = fs::read_to_string(FILE_PATH)
.expect("Should have been able to read the file");
println!("The ammount of grains of sand with void are: {}", throw_sand_untill_timeout(parse(&contents), (500, 0)).iter().filter(|h| *h.1).count());
println!("The ammount of grains of sand with a floor are: {}", throw_sand_untill_timeout(add_floor(parse(&contents)), (500, 0)).iter().filter(|h| *h.1).count());
}
use std::cmp::{max,min};
fn parse(s: &str) -> HashMap<(usize, usize), bool> {
let mut cave = HashMap::new();
for line in s.lines(){
let coords: Vec<_> = line.split(" -> ").map(|t| (usize::from_str(t.split(",").next().expect("must exist")).expect("NaN"),usize::from_str(t.split(",").skip(1).next().expect("must exist")).expect("NaN"))).collect();
for range in coords.windows(2){
let (ix, iy) = range[0];
let (fx, fy) = range[1];
for i in min(ix, fx)..=max(ix, fx){
for j in min(iy, fy)..=max(iy, fy){
cave.insert((i,j), false);
}
}
}
}
cave
}
fn throw_sand(mut cave: HashMap<(usize, usize), bool>, hole: (usize, usize)) -> (HashMap<(usize, usize), bool>, bool){
let mut timeout = 0;
let (mut sx, mut sy) = hole;
while timeout < 200{
if !cave.contains_key(&(sx, sy + 1)){
sy +=1;
}else if !cave.contains_key(&(sx-1, sy + 1)){
sy +=1;
sx -=1;
}else if !cave.contains_key(&(sx+1, sy + 1)){
sy +=1;
sx +=1;
}else{
if let Some(_) = cave.insert((sx, sy), true){
return (cave, true);
}else{
return (cave, false);
}
}
timeout +=1;
}
(cave, true)
}
fn throw_sand_untill_timeout(mut cave: HashMap<(usize, usize), bool>, hole: (usize, usize)) -> HashMap<(usize, usize), bool>{
loop {
let (binding, timeout) = throw_sand(cave, hole);
cave = binding;
if timeout {break cave;}
}
}
fn add_floor(mut cave: HashMap<(usize, usize), bool>) -> HashMap<(usize, usize), bool>{
let floor = *cave.iter().map(|((_, y), _)| y).max().expect("must have a lowest point")+2;
for i in 0..=1000{
cave.insert((i, floor), false);
}
cave
}
#[cfg(test)]
mod test {
const INPUT: &str = r#"498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9"#;
use super::*;
#[test]
fn sand1() {
assert_eq!(throw_sand_untill_timeout(parse(INPUT), (500, 0)).iter().filter(|h| *h.1).count(), 24);
}
#[test]
fn sand2() {
assert_eq!(throw_sand_untill_timeout(add_floor(parse(INPUT)), (500, 0)).iter().filter(|h| *h.1).count(), 93);
}
}