Compare commits
11 Commits
43ec9c7d2f
...
master
Author | SHA1 | Date | |
---|---|---|---|
976cb3651c | |||
5d522dc0f6 | |||
1affc44c28 | |||
40aae04d70 | |||
0d60232b6f | |||
a194cb2d6c | |||
ccc4301d62 | |||
c3437af12b | |||
0b0e9bc0f4 | |||
c7a6c47f0e | |||
e39887f0c2 |
@@ -1,4 +1,5 @@
|
||||
use std::env;
|
||||
use std::cmp::Ordering;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, BufRead};
|
||||
|
||||
@@ -17,40 +18,43 @@ fn parse_num(s: &[u8]) -> (u64, usize) {
|
||||
}
|
||||
|
||||
// Assuming no malformed input
|
||||
fn is_in_order(left: &[u8], right: &[u8]) -> bool {
|
||||
//println!(">> {:?}, {:?} -- {:?}", str::from_utf8(left), str::from_utf8(right));
|
||||
fn compare(left: &[u8], right: &[u8]) -> Ordering {
|
||||
if left.len() == 0 {
|
||||
return true;
|
||||
return Ordering::Less;
|
||||
}
|
||||
if right.len() == 0 {
|
||||
return false;
|
||||
return Ordering::Greater;
|
||||
}
|
||||
let (l, r) = (left[0], right[0]);
|
||||
if l == b',' && r == b',' {
|
||||
is_in_order(&left[1..], &right[1..])
|
||||
compare(&left[1..], &right[1..])
|
||||
} else if l == b'[' && r == b'[' {
|
||||
is_in_order(&left[1..], &right[1..])
|
||||
compare(&left[1..], &right[1..])
|
||||
} else if l == b']' && r == b']' {
|
||||
is_in_order(&left[1..], &right[1..])
|
||||
compare(&left[1..], &right[1..])
|
||||
} else if l == b']' {
|
||||
true
|
||||
Ordering::Less
|
||||
} else if r == b']' {
|
||||
false
|
||||
Ordering::Greater
|
||||
} else if l == b'[' {
|
||||
let (num, len) = parse_num(right);
|
||||
is_in_order(&left, &[b"[", num.to_string().as_bytes(), b"]", &right[len..]].concat())
|
||||
compare(
|
||||
&left,
|
||||
&[b"[", num.to_string().as_bytes(), b"]", &right[len..]].concat())
|
||||
} else if r == b'[' {
|
||||
let (num, len) = parse_num(left);
|
||||
is_in_order(&[b"[", num.to_string().as_bytes(), b"]", &left[len..]].concat(), &right)
|
||||
compare(
|
||||
&[b"[", num.to_string().as_bytes(), b"]", &left[len..]].concat(),
|
||||
&right)
|
||||
} else {
|
||||
let (lnum, llen) = parse_num(left);
|
||||
let (rnum, rlen) = parse_num(right);
|
||||
if lnum < rnum {
|
||||
true
|
||||
Ordering::Less
|
||||
} else if lnum > rnum {
|
||||
false
|
||||
Ordering::Greater
|
||||
} else {
|
||||
is_in_order(&left[llen..], &right[rlen..])
|
||||
compare(&left[llen..], &right[rlen..])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,17 +62,20 @@ fn is_in_order(left: &[u8], right: &[u8]) -> bool {
|
||||
|
||||
fn main() {
|
||||
let file = File::open(&env::args().nth(1).expect("input")).expect("io err");
|
||||
let lines: Vec<String> = BufReader::new(file).lines().flatten().collect();
|
||||
// TODO: array_chunks() for lazy iter?
|
||||
let blocks: Vec<_> = lines.chunks(3).collect();
|
||||
let mut lines: Vec<_> = BufReader::new(file).lines().flatten()
|
||||
.filter(|line| line != "").collect();
|
||||
lines.push(String::from("[[2]]"));
|
||||
lines.push(String::from("[[6]]"));
|
||||
lines.sort_unstable_by(|s1, s2| compare(s1.as_bytes(), s2.as_bytes()));
|
||||
|
||||
let mut sum = 0;
|
||||
for (i, block) in blocks.iter().enumerate() {
|
||||
// TODO: return error here instead of assert?
|
||||
assert!((block.len() == 3 && block[2] == "") || block.len() == 2);
|
||||
if is_in_order(block[0].as_bytes(), block[1].as_bytes()) {
|
||||
sum += i + 1;
|
||||
let (mut six, mut two): (usize, usize) = (0, 0);
|
||||
for (i, line) in lines.iter().enumerate() {
|
||||
if line == "[[6]]" {
|
||||
six = i + 1;
|
||||
}
|
||||
if line == "[[2]]" {
|
||||
two = i + 1;
|
||||
}
|
||||
}
|
||||
println!("Answer: {:?}", sum);
|
||||
println!("{:?}", six*two);
|
||||
}
|
||||
|
74
day13/src/main1.rs
Normal file
74
day13/src/main1.rs
Normal file
@@ -0,0 +1,74 @@
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, BufRead};
|
||||
|
||||
fn parse_num(s: &[u8]) -> (u64, usize) {
|
||||
let mut num: u64 = 0;
|
||||
let mut len = 0;
|
||||
for i in 0..s.len() {
|
||||
if s[i].is_ascii_digit() {
|
||||
num = num*10 + (s[i] as u64 - '0' as u64);
|
||||
len += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
(num, len)
|
||||
}
|
||||
|
||||
// Assuming no malformed input
|
||||
fn is_in_order(left: &[u8], right: &[u8]) -> bool {
|
||||
//println!(">> {:?}, {:?} -- {:?}", str::from_utf8(left), str::from_utf8(right));
|
||||
if left.len() == 0 {
|
||||
return true;
|
||||
}
|
||||
if right.len() == 0 {
|
||||
return false;
|
||||
}
|
||||
let (l, r) = (left[0], right[0]);
|
||||
if l == b',' && r == b',' {
|
||||
is_in_order(&left[1..], &right[1..])
|
||||
} else if l == b'[' && r == b'[' {
|
||||
is_in_order(&left[1..], &right[1..])
|
||||
} else if l == b']' && r == b']' {
|
||||
is_in_order(&left[1..], &right[1..])
|
||||
} else if l == b']' {
|
||||
true
|
||||
} else if r == b']' {
|
||||
false
|
||||
} else if l == b'[' {
|
||||
let (num, len) = parse_num(right);
|
||||
is_in_order(&left, &[b"[", num.to_string().as_bytes(), b"]", &right[len..]].concat())
|
||||
} else if r == b'[' {
|
||||
let (num, len) = parse_num(left);
|
||||
is_in_order(&[b"[", num.to_string().as_bytes(), b"]", &left[len..]].concat(), &right)
|
||||
} else {
|
||||
let (lnum, llen) = parse_num(left);
|
||||
let (rnum, rlen) = parse_num(right);
|
||||
if lnum < rnum {
|
||||
true
|
||||
} else if lnum > rnum {
|
||||
false
|
||||
} else {
|
||||
is_in_order(&left[llen..], &right[rlen..])
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let file = File::open(&env::args().nth(1).expect("input")).expect("io err");
|
||||
let lines: Vec<String> = BufReader::new(file).lines().flatten().collect();
|
||||
// TODO: array_chunks() for lazy iter?
|
||||
let blocks: Vec<_> = lines.chunks(3).collect();
|
||||
|
||||
let mut sum = 0;
|
||||
for (i, block) in blocks.iter().enumerate() {
|
||||
// TODO: return error here instead of assert?
|
||||
assert!((block.len() == 3 && block[2] == "") || block.len() == 2);
|
||||
if is_in_order(block[0].as_bytes(), block[1].as_bytes()) {
|
||||
sum += i + 1;
|
||||
}
|
||||
}
|
||||
println!("Answer: {:?}", sum);
|
||||
}
|
7
day14/Cargo.lock
generated
Normal file
7
day14/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day14"
|
||||
version = "0.1.0"
|
8
day14/Cargo.toml
Normal file
8
day14/Cargo.toml
Normal 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]
|
151
day14/input.txt
Normal file
151
day14/input.txt
Normal file
@@ -0,0 +1,151 @@
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
481,92 -> 481,96 -> 476,96 -> 476,99 -> 487,99 -> 487,96 -> 485,96 -> 485,92
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
470,76 -> 470,80 -> 466,80 -> 466,84 -> 481,84 -> 481,80 -> 474,80 -> 474,76
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
495,155 -> 499,155
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
500,138 -> 505,138
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
481,92 -> 481,96 -> 476,96 -> 476,99 -> 487,99 -> 487,96 -> 485,96 -> 485,92
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
489,115 -> 489,119 -> 481,119 -> 481,126 -> 494,126 -> 494,119 -> 493,119 -> 493,115
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
|
||||
481,92 -> 481,96 -> 476,96 -> 476,99 -> 487,99 -> 487,96 -> 485,96 -> 485,92
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
470,49 -> 470,53 -> 468,53 -> 468,60 -> 477,60 -> 477,53 -> 476,53 -> 476,49
|
||||
483,160 -> 487,160
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
491,131 -> 502,131 -> 502,130
|
||||
475,88 -> 475,89 -> 483,89 -> 483,88
|
||||
483,164 -> 487,164
|
||||
498,147 -> 503,147
|
||||
505,147 -> 510,147
|
||||
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
|
||||
516,150 -> 521,150
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
496,13 -> 501,13
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
512,147 -> 517,147
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
497,141 -> 502,141
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
480,162 -> 484,162
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
|
||||
489,155 -> 493,155
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
498,157 -> 502,157
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
491,147 -> 496,147
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
489,115 -> 489,119 -> 481,119 -> 481,126 -> 494,126 -> 494,119 -> 493,119 -> 493,115
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
470,76 -> 470,80 -> 466,80 -> 466,84 -> 481,84 -> 481,80 -> 474,80 -> 474,76
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
475,88 -> 475,89 -> 483,89 -> 483,88
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
486,162 -> 490,162
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
508,144 -> 513,144
|
||||
481,92 -> 481,96 -> 476,96 -> 476,99 -> 487,99 -> 487,96 -> 485,96 -> 485,92
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
470,49 -> 470,53 -> 468,53 -> 468,60 -> 477,60 -> 477,53 -> 476,53 -> 476,49
|
||||
477,164 -> 481,164
|
||||
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
|
||||
504,141 -> 509,141
|
||||
489,164 -> 493,164
|
||||
480,42 -> 485,42
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
470,49 -> 470,53 -> 468,53 -> 468,60 -> 477,60 -> 477,53 -> 476,53 -> 476,49
|
||||
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
489,115 -> 489,119 -> 481,119 -> 481,126 -> 494,126 -> 494,119 -> 493,119 -> 493,115
|
||||
486,157 -> 490,157
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
470,76 -> 470,80 -> 466,80 -> 466,84 -> 481,84 -> 481,80 -> 474,80 -> 474,76
|
||||
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
|
||||
501,144 -> 506,144
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
|
||||
470,76 -> 470,80 -> 466,80 -> 466,84 -> 481,84 -> 481,80 -> 474,80 -> 474,76
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
492,166 -> 496,166
|
||||
509,150 -> 514,150
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
480,134 -> 480,135 -> 500,135 -> 500,134
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
486,166 -> 490,166
|
||||
494,144 -> 499,144
|
||||
481,92 -> 481,96 -> 476,96 -> 476,99 -> 487,99 -> 487,96 -> 485,96 -> 485,92
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
480,134 -> 480,135 -> 500,135 -> 500,134
|
||||
481,46 -> 486,46
|
||||
477,44 -> 482,44
|
||||
470,49 -> 470,53 -> 468,53 -> 468,60 -> 477,60 -> 477,53 -> 476,53 -> 476,49
|
||||
491,131 -> 502,131 -> 502,130
|
||||
492,157 -> 496,157
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
488,150 -> 493,150
|
||||
481,92 -> 481,96 -> 476,96 -> 476,99 -> 487,99 -> 487,96 -> 485,96 -> 485,92
|
||||
474,46 -> 479,46
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
489,115 -> 489,119 -> 481,119 -> 481,126 -> 494,126 -> 494,119 -> 493,119 -> 493,115
|
||||
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
|
||||
489,115 -> 489,119 -> 481,119 -> 481,126 -> 494,126 -> 494,119 -> 493,119 -> 493,115
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
470,76 -> 470,80 -> 466,80 -> 466,84 -> 481,84 -> 481,80 -> 474,80 -> 474,76
|
||||
488,46 -> 493,46
|
||||
495,150 -> 500,150
|
||||
474,166 -> 478,166
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
484,44 -> 489,44
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
|
||||
489,115 -> 489,119 -> 481,119 -> 481,126 -> 494,126 -> 494,119 -> 493,119 -> 493,115
|
||||
481,92 -> 481,96 -> 476,96 -> 476,99 -> 487,99 -> 487,96 -> 485,96 -> 485,92
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
489,115 -> 489,119 -> 481,119 -> 481,126 -> 494,126 -> 494,119 -> 493,119 -> 493,115
|
||||
492,153 -> 496,153
|
||||
502,150 -> 507,150
|
||||
470,49 -> 470,53 -> 468,53 -> 468,60 -> 477,60 -> 477,53 -> 476,53 -> 476,49
|
||||
475,88 -> 475,89 -> 483,89 -> 483,88
|
||||
480,134 -> 480,135 -> 500,135 -> 500,134
|
||||
470,76 -> 470,80 -> 466,80 -> 466,84 -> 481,84 -> 481,80 -> 474,80 -> 474,76
|
||||
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
|
||||
480,166 -> 484,166
|
||||
470,49 -> 470,53 -> 468,53 -> 468,60 -> 477,60 -> 477,53 -> 476,53 -> 476,49
|
||||
470,76 -> 470,80 -> 466,80 -> 466,84 -> 481,84 -> 481,80 -> 474,80 -> 474,76
|
||||
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
|
||||
470,49 -> 470,53 -> 468,53 -> 468,60 -> 477,60 -> 477,53 -> 476,53 -> 476,49
|
105
day14/src/main.rs
Normal file
105
day14/src/main.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, BufRead};
|
||||
use std::cmp::{min, max};
|
||||
|
||||
fn parse_num(s: &[u8]) -> (usize, usize) {
|
||||
let mut num: usize = 0;
|
||||
let mut len = 0;
|
||||
for i in 0..s.len() {
|
||||
if s[i].is_ascii_digit() {
|
||||
num = num*10 + (s[i] as usize - '0' as usize);
|
||||
len += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
(num, len)
|
||||
}
|
||||
|
||||
fn print_map(map: &Vec<Vec<char>>) {
|
||||
for row in map {
|
||||
let s: String = row.iter().collect();
|
||||
println!("{}", s);
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let file = File::open(&env::args().nth(1).expect("input")).expect("io err");
|
||||
let lines = BufReader::new(file).lines();
|
||||
let (mut minx, mut miny, mut maxx, mut maxy) =
|
||||
(usize::MAX, usize::MAX, 0, 0);
|
||||
let mut lists: Vec<Vec<(usize, usize)>> = Vec::new();
|
||||
|
||||
for line in lines.flatten() {
|
||||
let l_bytes = line.as_bytes();
|
||||
let mut cursor = 0;
|
||||
let mut list: Vec<(usize, usize)> = Vec::new();
|
||||
while cursor < l_bytes.len() {
|
||||
let (x, lenx) = parse_num(&l_bytes[cursor..]);
|
||||
let (y, leny) = parse_num(&l_bytes[cursor + lenx + 1..]);
|
||||
list.push((x, y));
|
||||
minx = if x < minx {x} else {minx};
|
||||
miny = if y < miny {y} else {miny};
|
||||
maxx = if x > maxx {x} else {maxx};
|
||||
maxy = if y > maxy {y} else {maxy};
|
||||
cursor = cursor + lenx + 1 + leny + 4;
|
||||
}
|
||||
lists.push(list);
|
||||
}
|
||||
|
||||
// part2 hack here
|
||||
minx = 0;
|
||||
maxx = 1000;
|
||||
maxy = maxy + 2;
|
||||
lists.push(vec![(minx, maxy), (maxx, maxy)]);
|
||||
|
||||
println!("{:?} {:?} {:?} {:?}", minx, miny, maxx, maxy);
|
||||
let mut map: Vec<Vec<char>> =
|
||||
vec![vec!['.'; maxx - minx + 3]; maxy + 1];
|
||||
for list in lists {
|
||||
let (mut xprev, mut yprev) = &list[0];
|
||||
for (x, y) in &list[1..] {
|
||||
if *x == xprev {
|
||||
for ty in *min(y, &yprev)..=*max(y, &yprev) {
|
||||
map[ty][*x - minx + 1] = '#';
|
||||
}
|
||||
} else if *y == yprev {
|
||||
for tx in *min(x, &xprev)..=*max(x, &xprev) {
|
||||
map[*y][tx - minx + 1] = '#';
|
||||
}
|
||||
}
|
||||
xprev = *x;
|
||||
yprev = *y;
|
||||
}
|
||||
}
|
||||
print_map(&map);
|
||||
|
||||
let mut total = 0;
|
||||
'map: loop {
|
||||
let (mut rockx, mut rocky) = (500, 0);
|
||||
'rock: loop {
|
||||
//println!("{:?}, {:?}", rockx, rocky);
|
||||
if map[rocky + 1][rockx + 1 - minx] == '.' {
|
||||
rocky = rocky + 1;
|
||||
} else if map[rocky + 1][rockx + 1 - minx - 1] == '.' {
|
||||
rocky = rocky + 1;
|
||||
rockx = rockx - 1;
|
||||
} else if map[rocky + 1][rockx + 1 - minx + 1] == '.' {
|
||||
rocky = rocky + 1;
|
||||
rockx = rockx + 1;
|
||||
} else {
|
||||
map[rocky][rockx - minx + 1] = 'o';
|
||||
total += 1;
|
||||
if rockx == 500 && rocky == 0 {
|
||||
break 'map;
|
||||
}
|
||||
//print_map(&map);
|
||||
break 'rock;
|
||||
}
|
||||
}
|
||||
}
|
||||
print_map(&map);
|
||||
println!("{:?}", total);
|
||||
}
|
97
day14/src/main1.rs
Normal file
97
day14/src/main1.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, BufRead};
|
||||
use std::cmp::{min, max};
|
||||
|
||||
fn parse_num(s: &[u8]) -> (usize, usize) {
|
||||
let mut num: usize = 0;
|
||||
let mut len = 0;
|
||||
for i in 0..s.len() {
|
||||
if s[i].is_ascii_digit() {
|
||||
num = num*10 + (s[i] as usize - '0' as usize);
|
||||
len += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
(num, len)
|
||||
}
|
||||
|
||||
fn print_map(map: &Vec<Vec<char>>) {
|
||||
for row in map {
|
||||
let s: String = row.iter().collect();
|
||||
println!("{}", s);
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let file = File::open(&env::args().nth(1).expect("input")).expect("io err");
|
||||
let lines = BufReader::new(file).lines();
|
||||
let (mut minx, mut miny, mut maxx, mut maxy) =
|
||||
(usize::MAX, usize::MAX, 0, 0);
|
||||
let mut lists: Vec<Vec<(usize, usize)>> = Vec::new();
|
||||
|
||||
for line in lines.flatten() {
|
||||
let l_bytes = line.as_bytes();
|
||||
let mut cursor = 0;
|
||||
let mut list: Vec<(usize, usize)> = Vec::new();
|
||||
while cursor < l_bytes.len() {
|
||||
let (x, lenx) = parse_num(&l_bytes[cursor..]);
|
||||
let (y, leny) = parse_num(&l_bytes[cursor + lenx + 1..]);
|
||||
list.push((x, y));
|
||||
minx = if x < minx {x} else {minx};
|
||||
miny = if y < miny {y} else {miny};
|
||||
maxx = if x > maxx {x} else {maxx};
|
||||
maxy = if y > maxy {y} else {maxy};
|
||||
cursor = cursor + lenx + 1 + leny + 4;
|
||||
}
|
||||
lists.push(list);
|
||||
}
|
||||
println!("{:?} {:?} {:?} {:?}", minx, miny, maxx, maxy);
|
||||
let mut map: Vec<Vec<char>> =
|
||||
vec![vec!['.'; maxx - minx + 3]; maxy + 1];
|
||||
for list in lists {
|
||||
let (mut xprev, mut yprev) = &list[0];
|
||||
for (x, y) in &list[1..] {
|
||||
if *x == xprev {
|
||||
for ty in *min(y, &yprev)..=*max(y, &yprev) {
|
||||
map[ty][*x - minx + 1] = '#';
|
||||
}
|
||||
} else if *y == yprev {
|
||||
for tx in *min(x, &xprev)..=*max(x, &xprev) {
|
||||
map[*y][tx - minx + 1] = '#';
|
||||
}
|
||||
}
|
||||
xprev = *x;
|
||||
yprev = *y;
|
||||
}
|
||||
}
|
||||
print_map(&map);
|
||||
|
||||
let mut total = 0;
|
||||
'map: loop {
|
||||
let (mut rockx, mut rocky) = (500, 0);
|
||||
'rock: loop {
|
||||
//println!("{:?}, {:?}", rockx, rocky);
|
||||
if rockx < minx || rockx > maxx || rocky == maxy {
|
||||
break 'map;
|
||||
} else if map[rocky + 1][rockx + 1 - minx] == '.' {
|
||||
rocky = rocky + 1;
|
||||
} else if map[rocky + 1][rockx + 1 - minx - 1] == '.' {
|
||||
rocky = rocky + 1;
|
||||
rockx = rockx - 1;
|
||||
} else if map[rocky + 1][rockx + 1 - minx + 1] == '.' {
|
||||
rocky = rocky + 1;
|
||||
rockx = rockx + 1;
|
||||
} else {
|
||||
map[rocky][rockx - minx + 1] = 'o';
|
||||
total += 1;
|
||||
//print_map(&map);
|
||||
break 'rock;
|
||||
}
|
||||
}
|
||||
}
|
||||
print_map(&map);
|
||||
println!("{:?}", total);
|
||||
}
|
2
day14/test.txt
Normal file
2
day14/test.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
498,4 -> 498,6 -> 496,6
|
||||
503,4 -> 502,4 -> 502,9 -> 494,9
|
7
day15/Cargo.lock
generated
Normal file
7
day15/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day15"
|
||||
version = "0.1.0"
|
8
day15/Cargo.toml
Normal file
8
day15/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day15"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
33
day15/input.txt
Normal file
33
day15/input.txt
Normal file
@@ -0,0 +1,33 @@
|
||||
Sensor at x=2899860, y=3122031: closest beacon is at x=2701269, y=3542780
|
||||
Sensor at x=1836719, y=1116779: closest beacon is at x=2037055, y=2000000
|
||||
Sensor at x=3995802, y=2706630: closest beacon is at x=3944538, y=3053285
|
||||
Sensor at x=2591204, y=2008272: closest beacon is at x=2597837, y=2509170
|
||||
Sensor at x=2546593, y=1538222: closest beacon is at x=2037055, y=2000000
|
||||
Sensor at x=252214, y=61954: closest beacon is at x=1087565, y=-690123
|
||||
Sensor at x=950, y=1106672: closest beacon is at x=-893678, y=1276864
|
||||
Sensor at x=1349445, y=1752783: closest beacon is at x=2037055, y=2000000
|
||||
Sensor at x=3195828, y=3483667: closest beacon is at x=3334657, y=3531523
|
||||
Sensor at x=2057761, y=2154359: closest beacon is at x=2037055, y=2000000
|
||||
Sensor at x=2315350, y=3364640: closest beacon is at x=2701269, y=3542780
|
||||
Sensor at x=327139, y=2426600: closest beacon is at x=-88420, y=3646947
|
||||
Sensor at x=3943522, y=2854345: closest beacon is at x=3944538, y=3053285
|
||||
Sensor at x=3358620, y=516881: closest beacon is at x=3260516, y=2246079
|
||||
Sensor at x=1788376, y=8679: closest beacon is at x=1087565, y=-690123
|
||||
Sensor at x=3344883, y=3537985: closest beacon is at x=3334657, y=3531523
|
||||
Sensor at x=2961064, y=2697125: closest beacon is at x=2597837, y=2509170
|
||||
Sensor at x=3780090, y=2093546: closest beacon is at x=3260516, y=2246079
|
||||
Sensor at x=3291917, y=3398703: closest beacon is at x=3334657, y=3531523
|
||||
Sensor at x=3999864, y=2998005: closest beacon is at x=3944538, y=3053285
|
||||
Sensor at x=2919272, y=3732950: closest beacon is at x=2701269, y=3542780
|
||||
Sensor at x=2057404, y=2947435: closest beacon is at x=2037055, y=2000000
|
||||
Sensor at x=1072126, y=645784: closest beacon is at x=1087565, y=-690123
|
||||
Sensor at x=3549465, y=2554712: closest beacon is at x=3260516, y=2246079
|
||||
Sensor at x=3550313, y=3121694: closest beacon is at x=3944538, y=3053285
|
||||
Sensor at x=3405149, y=3483630: closest beacon is at x=3334657, y=3531523
|
||||
Sensor at x=2600212, y=3961193: closest beacon is at x=2701269, y=3542780
|
||||
Sensor at x=1102632, y=3932527: closest beacon is at x=-88420, y=3646947
|
||||
Sensor at x=67001, y=3506079: closest beacon is at x=-88420, y=3646947
|
||||
Sensor at x=3994250, y=3975025: closest beacon is at x=3944538, y=3053285
|
||||
Sensor at x=3019750, y=2125144: closest beacon is at x=3260516, y=2246079
|
||||
Sensor at x=3282319, y=3656404: closest beacon is at x=3334657, y=3531523
|
||||
Sensor at x=2797371, y=3645126: closest beacon is at x=2701269, y=3542780
|
83
day15/src/main.rs
Normal file
83
day15/src/main.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, BufRead};
|
||||
use std::cmp::{min, max};
|
||||
use std::collections::HashSet;
|
||||
|
||||
fn parse_num(s: &[u8]) -> (i64, usize) {
|
||||
let mut num: i64 = 0;
|
||||
let negative = s[0] == '-' as u8;
|
||||
let start = if negative {1} else {0};
|
||||
let mut len = if negative {1} else {0};
|
||||
|
||||
for i in start..s.len() {
|
||||
if s[i].is_ascii_digit() {
|
||||
num = num*10 + (s[i] as i64 - '0' as i64);
|
||||
len += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
(if negative {-num} else {num}, len)
|
||||
}
|
||||
|
||||
fn combine(ranges: &mut Vec<(i64, i64)>, l: i64, r: i64) {
|
||||
for (l0, r0) in ranges.iter_mut() {
|
||||
if *l0 <= r && l <= *r0 {
|
||||
*l0 = min(*l0, l);
|
||||
*r0 = max(*r0, r);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if ranges.len() > 0 {
|
||||
let (_, last_r) = ranges.last_mut().unwrap();
|
||||
if l == *last_r + 1 {
|
||||
*last_r = r;
|
||||
return;
|
||||
}
|
||||
}
|
||||
ranges.push((l, r));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let file = File::open(&env::args().nth(1).expect("input")).expect("io err");
|
||||
let lines = BufReader::new(file).lines();
|
||||
let mut sensors: Vec<(i64, i64, u64)> = Vec::new();
|
||||
let mut beacons: HashSet<(i64, i64)> = HashSet::new();
|
||||
for line in lines.flatten() {
|
||||
let l_bytes = line.as_bytes();
|
||||
let (sx, e1) = parse_num(&l_bytes[12..]);
|
||||
let (sy, e2) = parse_num(&l_bytes[12 + e1 + 4..]);
|
||||
let (bx, e3) = parse_num(&l_bytes[12 + e1 + 4 + e2 + 25..]);
|
||||
let (by, _) = parse_num(&l_bytes[12 + e1 + 4 + e2 + 25 + e3 + 4..]);
|
||||
sensors.push((sx, sy, ((bx - sx).abs() + (by - sy).abs()) as u64));
|
||||
beacons.insert((bx, by));
|
||||
}
|
||||
sensors.sort_by_key(|(x, _, _)| *x);
|
||||
println!("{:?}", sensors);
|
||||
println!("{:?}", beacons);
|
||||
|
||||
let max_y = &env::args().nth(2).expect("max y").parse::<i64>().unwrap();
|
||||
for target_y in 0..=*max_y {
|
||||
let mut line_coverages: Vec<(i64, i64)> = Vec::new();
|
||||
for (sx, sy, range) in &sensors {
|
||||
let clip = (*range as i64) - (sy - target_y).abs();
|
||||
if clip < 0 {
|
||||
continue;
|
||||
}
|
||||
let startx = sx - clip;
|
||||
let endx = sx + clip;
|
||||
line_coverages.push((startx, endx));
|
||||
}
|
||||
line_coverages.sort_by_key(|(x, _)| *x);
|
||||
let mut coverage: Vec<(i64, i64)> = Vec::new();
|
||||
for (startx, endx) in line_coverages {
|
||||
combine(&mut coverage, startx, endx);
|
||||
}
|
||||
if coverage.len() > 1 {
|
||||
let score = 4000000 * (coverage[0].1 + 1) + target_y;
|
||||
println!("{:?} --> {:?}", coverage, score);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
78
day15/src/main1.rs
Normal file
78
day15/src/main1.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, BufRead};
|
||||
use std::cmp::{min, max};
|
||||
use std::collections::HashSet;
|
||||
|
||||
fn parse_num(s: &[u8]) -> (i64, usize) {
|
||||
let mut num: i64 = 0;
|
||||
let negative = s[0] == '-' as u8;
|
||||
let start = if negative {1} else {0};
|
||||
let mut len = if negative {1} else {0};
|
||||
|
||||
for i in start..s.len() {
|
||||
if s[i].is_ascii_digit() {
|
||||
num = num*10 + (s[i] as i64 - '0' as i64);
|
||||
len += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
(if negative {-num} else {num}, len)
|
||||
}
|
||||
|
||||
fn combine(ranges: &mut Vec<(i64, i64)>, l: i64, r: i64) {
|
||||
for (l0, r0) in ranges.iter_mut() {
|
||||
if *l0 <= r && l <= *r0 {
|
||||
*l0 = min(*l0, l);
|
||||
*r0 = max(*r0, r);
|
||||
return;
|
||||
}
|
||||
}
|
||||
ranges.push((l, r));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let file = File::open(&env::args().nth(1).expect("input")).expect("io err");
|
||||
let lines = BufReader::new(file).lines();
|
||||
let mut sensors: Vec<(i64, i64, u64)> = Vec::new();
|
||||
let mut beacons: HashSet<(i64, i64)> = HashSet::new();
|
||||
for line in lines.flatten() {
|
||||
let l_bytes = line.as_bytes();
|
||||
let (sx, e1) = parse_num(&l_bytes[12..]);
|
||||
let (sy, e2) = parse_num(&l_bytes[12 + e1 + 4..]);
|
||||
let (bx, e3) = parse_num(&l_bytes[12 + e1 + 4 + e2 + 25..]);
|
||||
let (by, _) = parse_num(&l_bytes[12 + e1 + 4 + e2 + 25 + e3 + 4..]);
|
||||
sensors.push((sx, sy, ((bx - sx).abs() + (by - sy).abs()) as u64));
|
||||
beacons.insert((bx, by));
|
||||
}
|
||||
sensors.sort_by_key(|(x, _, _)| *x);
|
||||
println!("{:?}", sensors);
|
||||
println!("{:?}", beacons);
|
||||
|
||||
let target_y = &env::args().nth(2).expect("line").parse::<i64>().unwrap();
|
||||
let mut coverage: Vec<(i64, i64)> = Vec::new();
|
||||
for (sx, sy, range) in sensors {
|
||||
let clip = (range as i64) - (sy - target_y).abs();
|
||||
if clip < 0 {
|
||||
continue;
|
||||
}
|
||||
let startx = sx - clip;
|
||||
let endx = sx + clip;
|
||||
combine(&mut coverage, startx, endx);
|
||||
|
||||
}
|
||||
println!("{:?}", coverage);
|
||||
|
||||
let mut sum = 0;
|
||||
for (l, r) in coverage {
|
||||
let mut overlap = 0;
|
||||
for (bx, by) in &beacons {
|
||||
if *by == *target_y && l <= *bx && r >= *bx {
|
||||
overlap += 1;
|
||||
}
|
||||
}
|
||||
sum += r - l + 1 - overlap;
|
||||
}
|
||||
println!("answer: {:?}", sum);
|
||||
}
|
14
day15/test.txt
Normal file
14
day15/test.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
Sensor at x=2, y=18: closest beacon is at x=-2, y=15
|
||||
Sensor at x=9, y=16: closest beacon is at x=10, y=16
|
||||
Sensor at x=13, y=2: closest beacon is at x=15, y=3
|
||||
Sensor at x=12, y=14: closest beacon is at x=10, y=16
|
||||
Sensor at x=10, y=20: closest beacon is at x=10, y=16
|
||||
Sensor at x=14, y=17: closest beacon is at x=10, y=16
|
||||
Sensor at x=8, y=7: closest beacon is at x=2, y=10
|
||||
Sensor at x=2, y=0: closest beacon is at x=2, y=10
|
||||
Sensor at x=0, y=11: closest beacon is at x=2, y=10
|
||||
Sensor at x=20, y=14: closest beacon is at x=25, y=17
|
||||
Sensor at x=17, y=20: closest beacon is at x=21, y=22
|
||||
Sensor at x=16, y=7: closest beacon is at x=15, y=3
|
||||
Sensor at x=14, y=3: closest beacon is at x=15, y=3
|
||||
Sensor at x=20, y=1: closest beacon is at x=15, y=3
|
7
day16/Cargo.lock
generated
Normal file
7
day16/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day16"
|
||||
version = "0.1.0"
|
8
day16/Cargo.toml
Normal file
8
day16/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day16"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
52
day16/input.txt
Normal file
52
day16/input.txt
Normal file
@@ -0,0 +1,52 @@
|
||||
Valve AP has flow rate=0; tunnels lead to valves AA, ON
|
||||
Valve QN has flow rate=21; tunnels lead to valves RI, CG
|
||||
Valve LK has flow rate=0; tunnels lead to valves XM, AA
|
||||
Valve HA has flow rate=0; tunnels lead to valves WH, KF
|
||||
Valve DS has flow rate=16; tunnel leads to valve II
|
||||
Valve KD has flow rate=0; tunnels lead to valves KG, QB
|
||||
Valve JW has flow rate=0; tunnels lead to valves AD, KF
|
||||
Valve HU has flow rate=0; tunnels lead to valves UK, CO
|
||||
Valve AE has flow rate=10; tunnels lead to valves IR, PT, UV
|
||||
Valve XA has flow rate=0; tunnels lead to valves CG, EU
|
||||
Valve SE has flow rate=17; tunnels lead to valves YR, AD
|
||||
Valve TR has flow rate=0; tunnels lead to valves AL, CS
|
||||
Valve BS has flow rate=0; tunnels lead to valves YH, XM
|
||||
Valve IJ has flow rate=24; tunnels lead to valves XN, WE
|
||||
Valve AA has flow rate=0; tunnels lead to valves LK, AP, IZ, PC, QD
|
||||
Valve KG has flow rate=0; tunnels lead to valves KD, CS
|
||||
Valve QV has flow rate=0; tunnels lead to valves XM, II
|
||||
Valve PC has flow rate=0; tunnels lead to valves AA, YF
|
||||
Valve GJ has flow rate=20; tunnel leads to valve RI
|
||||
Valve UV has flow rate=0; tunnels lead to valves UK, AE
|
||||
Valve IR has flow rate=0; tunnels lead to valves EU, AE
|
||||
Valve EU has flow rate=13; tunnels lead to valves IR, DT, XA, ON
|
||||
Valve ED has flow rate=0; tunnels lead to valves XN, CO
|
||||
Valve DT has flow rate=0; tunnels lead to valves EU, UK
|
||||
Valve YE has flow rate=0; tunnels lead to valves XM, WS
|
||||
Valve AD has flow rate=0; tunnels lead to valves JW, SE
|
||||
Valve WE has flow rate=0; tunnels lead to valves IJ, NA
|
||||
Valve UK has flow rate=5; tunnels lead to valves UV, DT, QD, HU
|
||||
Valve YR has flow rate=0; tunnels lead to valves OS, SE
|
||||
Valve II has flow rate=0; tunnels lead to valves QV, DS
|
||||
Valve GT has flow rate=0; tunnels lead to valves CS, MN
|
||||
Valve YH has flow rate=0; tunnels lead to valves BS, QB
|
||||
Valve BQ has flow rate=0; tunnels lead to valves XM, KF
|
||||
Valve OS has flow rate=0; tunnels lead to valves YR, NA
|
||||
Valve WH has flow rate=0; tunnels lead to valves QB, HA
|
||||
Valve QB has flow rate=4; tunnels lead to valves WH, KD, YH, IZ
|
||||
Valve ON has flow rate=0; tunnels lead to valves AP, EU
|
||||
Valve IZ has flow rate=0; tunnels lead to valves AA, QB
|
||||
Valve MN has flow rate=25; tunnel leads to valve GT
|
||||
Valve CG has flow rate=0; tunnels lead to valves XA, QN
|
||||
Valve QD has flow rate=0; tunnels lead to valves UK, AA
|
||||
Valve AL has flow rate=0; tunnels lead to valves KF, TR
|
||||
Valve XN has flow rate=0; tunnels lead to valves ED, IJ
|
||||
Valve WS has flow rate=0; tunnels lead to valves YE, CS
|
||||
Valve CO has flow rate=18; tunnels lead to valves ED, PT, HU
|
||||
Valve PT has flow rate=0; tunnels lead to valves CO, AE
|
||||
Valve RI has flow rate=0; tunnels lead to valves QN, GJ
|
||||
Valve CS has flow rate=9; tunnels lead to valves YF, GT, WS, TR, KG
|
||||
Valve YF has flow rate=0; tunnels lead to valves PC, CS
|
||||
Valve NA has flow rate=23; tunnels lead to valves OS, WE
|
||||
Valve KF has flow rate=12; tunnels lead to valves HA, AL, JW, BQ
|
||||
Valve XM has flow rate=3; tunnels lead to valves LK, QV, YE, BS, BQ
|
157
day16/src/main.rs
Normal file
157
day16/src/main.rs
Normal file
@@ -0,0 +1,157 @@
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, BufRead};
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
use std::cmp::max;
|
||||
use std::str::FromStr;
|
||||
use std::fmt::Debug;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
const TIME: u64 = 26;
|
||||
|
||||
fn parse_str<T: FromStr>(str: &str) -> Result<(T, usize), <T as FromStr>::Err> {
|
||||
let mut iter = str.chars().peekable();
|
||||
let mut prefix = String::new();
|
||||
|
||||
while let Some(c) = iter.peek() {
|
||||
if !c.to_string().parse::<T>().is_ok() {
|
||||
break;
|
||||
}
|
||||
prefix.push(iter.next().unwrap());
|
||||
}
|
||||
Ok((prefix.parse::<T>()?, prefix.len()))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Valve {
|
||||
rate: u64,
|
||||
links: Vec<(String, u64)>,
|
||||
}
|
||||
|
||||
fn read_input(fname: &String) -> Result<HashMap<String, Valve>, <u64 as FromStr>::Err> {
|
||||
let file = File::open(fname).expect("cannot open input file");
|
||||
let lines = BufReader::new(file).lines();
|
||||
let mut valves = HashMap::new();
|
||||
for line in lines.flatten() {
|
||||
let name = String::from(&line[6..=7]);
|
||||
let (rate, rate_len) = parse_str::<u64>(&line[23..])?;
|
||||
let links_str = &line[23 + rate_len + 24..];
|
||||
let mut link = String::new();
|
||||
let mut links = Vec::new();
|
||||
let mut iter = links_str.chars();
|
||||
loop {
|
||||
match iter.next() {
|
||||
Some(' ') | Some(',') => continue,
|
||||
None => break,
|
||||
Some(c) => {
|
||||
link.push(c);
|
||||
link.push(iter.next().unwrap());
|
||||
links.push((link.clone(), 1));
|
||||
link = String::new();
|
||||
},
|
||||
}
|
||||
}
|
||||
valves.insert(name, Valve { rate, links });
|
||||
}
|
||||
Ok(valves)
|
||||
}
|
||||
|
||||
fn routes_from(valve: &String, valves: &HashMap<String, Valve>) -> Valve {
|
||||
let mut visited = HashSet::from([valve.clone()]);
|
||||
let mut queue = VecDeque::from([(valve.clone(), 0)]);
|
||||
let mut links = Vec::new();
|
||||
while !queue.is_empty() {
|
||||
let (current_valve, current_cost) = queue.pop_front().unwrap();
|
||||
if current_valve != *valve && valves[¤t_valve].rate > 0 {
|
||||
// +1 because it takes 1min to open a valve
|
||||
links.push((current_valve.clone(), current_cost + 1));
|
||||
}
|
||||
for (next_valve, _) in &valves[¤t_valve].links {
|
||||
if !visited.contains(next_valve) {
|
||||
queue.push_back((next_valve.clone(), current_cost + 1));
|
||||
visited.insert(next_valve.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
Valve { rate: valves[valve].rate, links: links }
|
||||
}
|
||||
|
||||
fn print_map<T1: Debug, T2: Debug>(map: &HashMap<T1, T2>) {
|
||||
for (k, v) in map {
|
||||
println!("{:?}: {:?}", k, v);
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
struct Visited {
|
||||
set: HashSet<String>,
|
||||
}
|
||||
|
||||
impl Hash for Visited {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
let l = &mut self.set.iter()
|
||||
.filter(|x| *x != "AA")
|
||||
.cloned().collect::<Vec<String>>();
|
||||
l.sort();
|
||||
l.join("");
|
||||
l.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
fn get_pressure_map(valves: &HashMap<String, Valve>) -> HashMap<Visited, u64> {
|
||||
let mut max_pressure: HashMap<Visited, u64> = HashMap::new();
|
||||
let mut queue: VecDeque<(String, u64, u64, Visited)> =
|
||||
VecDeque::from([(
|
||||
String::from("AA"), 0, 0,
|
||||
Visited { set: HashSet::new() },
|
||||
)]);
|
||||
while !queue.is_empty() {
|
||||
let (valve, cost, pressure, visited) = queue.pop_front().unwrap();
|
||||
// println!("{}: {}, {}, {:?}", valve, cost, pressure, visited);
|
||||
if let Some(last_max) = max_pressure.get_mut(&visited) {
|
||||
*last_max = max(*last_max, pressure);
|
||||
} else {
|
||||
max_pressure.insert(visited.clone(), pressure);
|
||||
}
|
||||
|
||||
for (hop, hop_cost) in &valves[&valve].links {
|
||||
let new_cost = cost + hop_cost;
|
||||
if new_cost >= TIME || visited.set.contains(hop) {
|
||||
continue;
|
||||
}
|
||||
let new_pressure = pressure + (TIME - new_cost)*valves[hop].rate;
|
||||
let mut new_visited = visited.clone();
|
||||
new_visited.set.insert(hop.clone());
|
||||
queue.push_back((hop.clone(), new_cost, new_pressure, new_visited));
|
||||
}
|
||||
}
|
||||
max_pressure
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input_fname = env::args().nth(1).expect("need input file");
|
||||
let valves = read_input(&input_fname).expect("parse error");
|
||||
let mut good_valves: HashMap<String, Valve> = HashMap::new();
|
||||
for (name, valve) in &valves {
|
||||
if valve.rate > 0 || name == "AA" {
|
||||
good_valves.insert(name.clone(), routes_from(name, &valves));
|
||||
}
|
||||
}
|
||||
print_map(&good_valves);
|
||||
|
||||
let pressure_map = get_pressure_map(&good_valves);
|
||||
// print_map(&pressure_map);
|
||||
println!("pressure_map.len(): {}", pressure_map.len());
|
||||
|
||||
let mut max_pressure = 0;
|
||||
for (visited1, pressure1) in &pressure_map {
|
||||
for (visited2, pressure2) in &pressure_map {
|
||||
if visited1.set.is_disjoint(&visited2.set) {
|
||||
max_pressure = max(max_pressure, pressure1 + pressure2);
|
||||
}
|
||||
}
|
||||
}
|
||||
println!(">> {} <<", max_pressure);
|
||||
}
|
||||
|
103
day16/src/main1.rs
Normal file
103
day16/src/main1.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, BufRead};
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
use std::cmp::max;
|
||||
use std::str::FromStr;
|
||||
|
||||
fn parse_str<T: FromStr>(str: &str) -> Result<(T, usize), <T as FromStr>::Err> {
|
||||
let mut iter = str.chars().peekable();
|
||||
let mut prefix = String::new();
|
||||
|
||||
while let Some(c) = iter.peek() {
|
||||
if !c.to_string().parse::<T>().is_ok() {
|
||||
break;
|
||||
}
|
||||
prefix.push(iter.next().unwrap());
|
||||
}
|
||||
Ok((prefix.parse::<T>()?, prefix.len()))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Valve {
|
||||
rate: u64,
|
||||
links: Vec<(String, u64)>,
|
||||
}
|
||||
|
||||
fn read_input(fname: &String) -> Result<HashMap<String, Valve>, <u64 as FromStr>::Err> {
|
||||
let file = File::open(fname).expect("cannot open input file");
|
||||
let lines = BufReader::new(file).lines();
|
||||
let mut valves = HashMap::new();
|
||||
for line in lines.flatten() {
|
||||
let name = String::from(&line[6..=7]);
|
||||
let (rate, rate_len) = parse_str::<u64>(&line[23..])?;
|
||||
let links_str = &line[23 + rate_len + 24..];
|
||||
let mut link = String::new();
|
||||
let mut links = Vec::new();
|
||||
let mut iter = links_str.chars();
|
||||
loop {
|
||||
match iter.next() {
|
||||
Some(' ') | Some(',') => continue,
|
||||
None => break,
|
||||
Some(c) => {
|
||||
link.push(c);
|
||||
link.push(iter.next().unwrap());
|
||||
links.push((link.clone(), 1));
|
||||
link = String::new();
|
||||
},
|
||||
}
|
||||
}
|
||||
valves.insert(name, Valve { rate, links });
|
||||
}
|
||||
Ok(valves)
|
||||
}
|
||||
|
||||
fn routes_from(valve: &String, valves: &HashMap<String, Valve>) -> Valve {
|
||||
let mut visited = HashSet::from([valve.clone()]);
|
||||
let mut queue = VecDeque::from([(valve.clone(), 0)]);
|
||||
let mut links = Vec::new();
|
||||
while !queue.is_empty() {
|
||||
let (current_valve, current_cost) = queue.pop_front().unwrap();
|
||||
if current_valve != *valve && valves[¤t_valve].rate > 0 {
|
||||
// +1 because it takes 1min to open a valve
|
||||
links.push((current_valve.clone(), current_cost + 1));
|
||||
}
|
||||
for (next_valve, _) in &valves[¤t_valve].links {
|
||||
if !visited.contains(next_valve) {
|
||||
queue.push_back((next_valve.clone(), current_cost + 1));
|
||||
visited.insert(next_valve.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
Valve { rate: valves[valve].rate, links: links }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input_fname = env::args().nth(1).expect("need input file");
|
||||
let valves = read_input(&input_fname).expect("parse error");
|
||||
let mut good_valves = HashMap::new();
|
||||
for (valve_name, valve) in &valves {
|
||||
if valve.rate > 0 || valve_name == "AA" {
|
||||
good_valves.insert(valve_name, routes_from(valve_name, &valves));
|
||||
}
|
||||
}
|
||||
|
||||
let mut max_pressure = 0;
|
||||
let mut queue: VecDeque<(String, u64, u64, HashSet<_>)> = VecDeque::from([(
|
||||
String::from("AA"), 0, 0, HashSet::from([String::from("AA")]))]);
|
||||
while !queue.is_empty() {
|
||||
let (valve, cost, pressure, visited) = queue.pop_front().unwrap();
|
||||
max_pressure = max(max_pressure, pressure);
|
||||
for (hop, hop_cost) in &good_valves[&valve].links {
|
||||
if cost + hop_cost >= 30 || visited.contains(hop) {
|
||||
continue;
|
||||
}
|
||||
let new_cost = cost + hop_cost;
|
||||
let new_pressure = pressure + (30 - new_cost)*valves[hop].rate;
|
||||
let mut new_visited = visited.clone();
|
||||
new_visited.insert(hop.clone());
|
||||
queue.push_back((hop.clone(), new_cost, new_pressure, new_visited));
|
||||
}
|
||||
}
|
||||
println!("Max pressure: {}", max_pressure);
|
||||
}
|
10
day16/test.txt
Normal file
10
day16/test.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
|
||||
Valve BB has flow rate=13; tunnels lead to valves CC, AA
|
||||
Valve CC has flow rate=2; tunnels lead to valves DD, BB
|
||||
Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE
|
||||
Valve EE has flow rate=3; tunnels lead to valves FF, DD
|
||||
Valve FF has flow rate=0; tunnels lead to valves EE, GG
|
||||
Valve GG has flow rate=0; tunnels lead to valves FF, HH
|
||||
Valve HH has flow rate=22; tunnel leads to valve GG
|
||||
Valve II has flow rate=0; tunnels lead to valves AA, JJ
|
||||
Valve JJ has flow rate=21; tunnel leads to valve II
|
7
day17/Cargo.lock
generated
Normal file
7
day17/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day17"
|
||||
version = "0.1.0"
|
6
day17/Cargo.toml
Normal file
6
day17/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day17"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
1
day17/input.txt
Normal file
1
day17/input.txt
Normal file
File diff suppressed because one or more lines are too long
210
day17/src/main.rs
Normal file
210
day17/src/main.rs
Normal file
@@ -0,0 +1,210 @@
|
||||
use std::io;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::io::BufRead;
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Shape {
|
||||
bot: [i8; 4], // distance from datum. -1 for unused.
|
||||
top: [i8; 4],
|
||||
left: [i8; 4],
|
||||
right: [i8; 4],
|
||||
width: usize,
|
||||
height: usize,
|
||||
}
|
||||
|
||||
// a Shape's datum is its bottom left corner
|
||||
#[derive(Debug)]
|
||||
struct Pos {y: usize, x: usize}
|
||||
|
||||
//static SHAPE_NAMES: [char; 5] = ['-', '+', 'L', '|', '.'];
|
||||
static SHAPES: [Shape; 5] = [
|
||||
Shape { // -
|
||||
bot: [0, 0, 0, 0], top: [0, 0, 0, 0],
|
||||
left: [0, -1, -1, -1], right: [3, -1, -1, -1],
|
||||
width: 4, height: 1,
|
||||
},
|
||||
Shape { // +
|
||||
bot: [1, 0, 1, -1], top: [1, 2, 1, -1],
|
||||
left: [1, 0, 1, -1], right: [1, 2, 1, -1],
|
||||
width: 3, height: 3,
|
||||
},
|
||||
Shape { // backward L
|
||||
bot: [0, 0, 0, -1], top: [0, 0, 2, -1],
|
||||
left: [0, 2, 2, -1], right: [2, 2, 2, -1],
|
||||
width: 3, height: 3,
|
||||
},
|
||||
Shape { // |
|
||||
bot: [0, -1, -1, -1], top: [0, -1, -1, -1],
|
||||
left: [0, 0, 0, 0], right: [0, 0, 0, 0],
|
||||
width: 1, height: 4,
|
||||
},
|
||||
Shape { // square
|
||||
bot: [0, 0, -1, -1], top: [1, 1, -1, -1],
|
||||
left: [0, 0, -1, -1], right: [1, 1, -1, -1],
|
||||
width: 2, height: 2,
|
||||
},
|
||||
];
|
||||
|
||||
fn iterate(
|
||||
map: &mut Vec<[u8; 7]>, shape: &Shape, pos: &mut Pos, wind: u8
|
||||
) -> bool {
|
||||
//println!(" __ {}, start {:?}", wind as char, pos);
|
||||
// Wind move
|
||||
match wind {
|
||||
b'>' => 'rightmove: {
|
||||
if pos.x + shape.width == 7 { break 'rightmove; }
|
||||
let mut i = 0;
|
||||
while i < 4 && shape.right[i] >= 0 {
|
||||
let right_point = pos.x + shape.right[i] as usize;
|
||||
if right_point + 1 == 7 { break 'rightmove; }
|
||||
if map[pos.y + i][right_point + 1] != 0 { break 'rightmove; }
|
||||
i += 1;
|
||||
}
|
||||
pos.x += 1;
|
||||
//println!(" >> {:?}", pos);
|
||||
},
|
||||
b'<' => 'leftmove: {
|
||||
if pos.x == 0 { break 'leftmove; }
|
||||
let mut i = 0;
|
||||
while i < 4 && shape.left[i] >= 0 {
|
||||
let left_point = pos.x + shape.left[i] as usize;
|
||||
if left_point == 0 { break 'leftmove; }
|
||||
if map[pos.y + i][left_point - 1] != 0 { break 'leftmove; }
|
||||
i += 1;
|
||||
}
|
||||
pos.x -= 1;
|
||||
//println!(" << {:?}", pos);
|
||||
},
|
||||
_ => panic!(),
|
||||
}
|
||||
|
||||
// Gravity move
|
||||
'downmove: {
|
||||
let mut i = 0;
|
||||
while i < 4 && shape.bot[i] >= 0 {
|
||||
let bot_point = pos.y + shape.bot[i] as usize;
|
||||
if bot_point == 0 { break 'downmove; }
|
||||
if map[bot_point - 1][pos.x + i] != 0 { break 'downmove; }
|
||||
i += 1;
|
||||
}
|
||||
pos.y -= 1;
|
||||
//println!(" vv {:?}", pos);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Settling
|
||||
let mut i = 0;
|
||||
while i < 4 && shape.left[i] >= 0 && shape.right[i] >= 0 {
|
||||
map[pos.y + i][pos.x + shape.left[i] as usize] = 1;
|
||||
map[pos.y + i][pos.x + shape.right[i] as usize] = 1;
|
||||
i += 1;
|
||||
}
|
||||
i = 0;
|
||||
while i < 4 && shape.top[i] >= 0 && shape.bot[i] >= 0 {
|
||||
map[pos.y + shape.top[i] as usize][pos.x + i] = 1;
|
||||
map[pos.y + shape.bot[i] as usize][pos.x + i] = 1;
|
||||
i += 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let input_fname = env::args().nth(1).expect("need input file");
|
||||
let limit = env::args().nth(2).expect("need limit").parse::<usize>().unwrap();
|
||||
let mut winds: Vec<u8> = Vec::new();
|
||||
let _ = BufReader::new(File::open(input_fname)?).read_until(b'\n', &mut winds);
|
||||
if winds.last() == Some(&b'\n') { winds.pop(); }
|
||||
println!("winds = {}, shapes = {}", winds.len(), SHAPES.len());
|
||||
|
||||
let mut shape_id = 0;
|
||||
let mut shape = &SHAPES[shape_id];
|
||||
let mut current_top = 0;
|
||||
let mut shape_pos = Pos{x: 2, y: 3};
|
||||
let mut map: Vec<[u8; 7]> = vec![
|
||||
[0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0],
|
||||
];
|
||||
|
||||
let mut answers: Vec<(usize, usize)> = Vec::new();
|
||||
|
||||
let mut i = 0;
|
||||
'mainloop: while answers.len() < limit {
|
||||
let c = winds[i % winds.len()];
|
||||
if iterate(&mut map, shape, &mut shape_pos, c) {
|
||||
let last_shape_top = shape_pos.y + shape.height - 1;
|
||||
if last_shape_top > current_top {
|
||||
current_top = last_shape_top;
|
||||
}
|
||||
shape_id = (shape_id + 1) % SHAPES.len(); // next shape
|
||||
shape = &SHAPES[shape_id];
|
||||
for _ in map.len()..(current_top + 4 + shape.height) {
|
||||
map.push([0, 0, 0, 0, 0, 0, 0]);
|
||||
}
|
||||
shape_pos = Pos{x:2, y: current_top + 4};
|
||||
answers.push((i, current_top));
|
||||
let last_rock = answers.len() - 1;
|
||||
print!(
|
||||
"rock {} - (i {}, top {})",
|
||||
last_rock, i, current_top
|
||||
);
|
||||
|
||||
if last_rock >= 2*SHAPES.len() {
|
||||
let mut j = last_rock - SHAPES.len();
|
||||
'lookback: while j > (last_rock / 2) {
|
||||
let (hist_i, hist_top) = answers[j];
|
||||
let next_j = 2*j - last_rock;
|
||||
let (hist2_i, hist2_top) = answers[next_j];
|
||||
if (i - hist_i) % winds.len() == 0
|
||||
&& (i - hist_i) == (hist_i - hist2_i)
|
||||
&& (current_top - hist_top) == (hist_top - hist2_top) {
|
||||
print!(
|
||||
" {}-{}-{}-({}-{}-{})",
|
||||
j, hist_i, hist_top,
|
||||
next_j, hist2_i, hist2_top,
|
||||
);
|
||||
print!(
|
||||
" rock cycle {}, off {} | \
|
||||
top cycle {} off {} ",
|
||||
last_rock - j, next_j,
|
||||
current_top - hist_top, hist2_top,
|
||||
);
|
||||
if (limit - 1 - next_j) % (last_rock - j) == 0 {
|
||||
let n_cycles = (limit - 1 - next_j) / (last_rock - j);
|
||||
println!(
|
||||
"\nheight = {}",
|
||||
hist2_top + n_cycles*(current_top - hist_top) + 1
|
||||
);
|
||||
break 'mainloop;
|
||||
} else {
|
||||
break 'lookback;
|
||||
}
|
||||
}
|
||||
j -= SHAPES.len();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
println!();
|
||||
//prettyprint(&map, &shape_pos, SHAPE_NAMES[shape_id]);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn prettyprint(map: &Vec<[u8; 7]>, pos: &Pos, shape_name: char) {
|
||||
let mut i = map.len();
|
||||
for row in map.into_iter().rev() {
|
||||
i -= 1;
|
||||
print!("|");
|
||||
for j in 0..7 {
|
||||
print!("{}", if row[j] == 0 { "." } else { "#" });
|
||||
}
|
||||
println!("| {} {}", i, if i == pos.y {shape_name} else {' '});
|
||||
}
|
||||
}
|
168
day17/src/main1.rs
Normal file
168
day17/src/main1.rs
Normal file
@@ -0,0 +1,168 @@
|
||||
use std::io;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::io::BufRead;
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Shape {
|
||||
bot: [i8; 4], // distance from datum. -1 for unused.
|
||||
top: [i8; 4],
|
||||
left: [i8; 4],
|
||||
right: [i8; 4],
|
||||
width: usize,
|
||||
height: usize,
|
||||
}
|
||||
|
||||
// a Shape's datum is its bottom left corner
|
||||
#[derive(Debug)]
|
||||
struct Pos {y: usize, x: usize}
|
||||
|
||||
static SHAPE_NAMES: [char; 5] = ['-', '+', 'L', '|', '.'];
|
||||
static SHAPES: [Shape; 5] = [
|
||||
Shape { // -
|
||||
bot: [0, 0, 0, 0], top: [0, 0, 0, 0],
|
||||
left: [0, -1, -1, -1], right: [3, -1, -1, -1],
|
||||
width: 4, height: 1,
|
||||
},
|
||||
Shape { // +
|
||||
bot: [1, 0, 1, -1], top: [1, 2, 1, -1],
|
||||
left: [1, 0, 1, -1], right: [1, 2, 1, -1],
|
||||
width: 3, height: 3,
|
||||
},
|
||||
Shape { // backward L
|
||||
bot: [0, 0, 0, -1], top: [0, 0, 2, -1],
|
||||
left: [0, 2, 2, -1], right: [2, 2, 2, -1],
|
||||
width: 3, height: 3,
|
||||
},
|
||||
Shape { // |
|
||||
bot: [0, -1, -1, -1], top: [0, -1, -1, -1],
|
||||
left: [0, 0, 0, 0], right: [0, 0, 0, 0],
|
||||
width: 1, height: 4,
|
||||
},
|
||||
Shape { // square
|
||||
bot: [0, 0, -1, -1], top: [1, 1, -1, -1],
|
||||
left: [0, 0, -1, -1], right: [1, 1, -1, -1],
|
||||
width: 2, height: 2,
|
||||
},
|
||||
];
|
||||
|
||||
fn iterate(
|
||||
map: &mut Vec<[u8; 7]>, shape: &Shape, pos: &mut Pos, wind: u8
|
||||
) -> bool {
|
||||
//println!(" __ {}, start {:?}", wind as char, pos);
|
||||
// Wind move
|
||||
match wind {
|
||||
b'>' => 'rightmove: {
|
||||
if pos.x + shape.width == 7 { break 'rightmove; }
|
||||
let mut i = 0;
|
||||
while i < 4 && shape.right[i] >= 0 {
|
||||
let right_point = pos.x + shape.right[i] as usize;
|
||||
if right_point + 1 == 7 { break 'rightmove; }
|
||||
if map[pos.y + i][right_point + 1] != 0 { break 'rightmove; }
|
||||
i += 1;
|
||||
}
|
||||
pos.x += 1;
|
||||
//println!(" >> {:?}", pos);
|
||||
},
|
||||
b'<' => 'leftmove: {
|
||||
if pos.x == 0 { break 'leftmove; }
|
||||
let mut i = 0;
|
||||
while i < 4 && shape.left[i] >= 0 {
|
||||
let left_point = pos.x + shape.left[i] as usize;
|
||||
if left_point == 0 { break 'leftmove; }
|
||||
if map[pos.y + i][left_point - 1] != 0 { break 'leftmove; }
|
||||
i += 1;
|
||||
}
|
||||
pos.x -= 1;
|
||||
//println!(" << {:?}", pos);
|
||||
},
|
||||
_ => panic!(),
|
||||
}
|
||||
|
||||
// Gravity move
|
||||
'downmove: {
|
||||
let mut i = 0;
|
||||
while i < 4 && shape.bot[i] >= 0 {
|
||||
let bot_point = pos.y + shape.bot[i] as usize;
|
||||
if bot_point == 0 { break 'downmove; }
|
||||
if map[bot_point - 1][pos.x + i] != 0 { break 'downmove; }
|
||||
i += 1;
|
||||
}
|
||||
pos.y -= 1;
|
||||
//println!(" vv {:?}", pos);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Settling
|
||||
let mut i = 0;
|
||||
while i < 4 && shape.left[i] >= 0 && shape.right[i] >= 0 {
|
||||
map[pos.y + i][pos.x + shape.left[i] as usize] = 1;
|
||||
map[pos.y + i][pos.x + shape.right[i] as usize] = 1;
|
||||
i += 1;
|
||||
}
|
||||
i = 0;
|
||||
while i < 4 && shape.top[i] >= 0 && shape.bot[i] >= 0 {
|
||||
map[pos.y + shape.top[i] as usize][pos.x + i] = 1;
|
||||
map[pos.y + shape.bot[i] as usize][pos.x + i] = 1;
|
||||
i += 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let input_fname = env::args().nth(1).expect("need input file");
|
||||
let mut winds: Vec<u8> = Vec::new();
|
||||
let _ = BufReader::new(File::open(input_fname)?).read_until(b'\n', &mut winds);
|
||||
if winds.last() == Some(&b'\n') { winds.pop(); }
|
||||
|
||||
let mut shape_id = 0;
|
||||
let mut shape = &SHAPES[shape_id];
|
||||
let mut current_top = 0;
|
||||
let mut shape_pos = Pos{x: 2, y: 3};
|
||||
let mut map: Vec<[u8; 7]> = vec![
|
||||
[0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0],
|
||||
];
|
||||
prettyprint(&map, &shape_pos, SHAPE_NAMES[shape_id]);
|
||||
|
||||
let mut nrocks = 0;
|
||||
let mut i = 0;
|
||||
while nrocks < 2022 { //debug
|
||||
let c = winds[i % winds.len()];
|
||||
i += 1;
|
||||
if iterate(&mut map, shape, &mut shape_pos, c) {
|
||||
let last_shape_top = shape_pos.y + shape.height - 1;
|
||||
if last_shape_top > current_top {
|
||||
current_top = last_shape_top;
|
||||
}
|
||||
shape_id = (shape_id + 1) % SHAPES.len(); // next shape
|
||||
shape = &SHAPES[shape_id];
|
||||
//println!("{} -> {}", map.len(), last_shape_top + 4 + shape.height);
|
||||
for _ in map.len()..(current_top + 4 + shape.height) {
|
||||
map.push([0, 0, 0, 0, 0, 0, 0]);
|
||||
}
|
||||
nrocks += 1;
|
||||
shape_pos = Pos{x:2, y: current_top + 4};
|
||||
//println!("= {} ({})", current_top, nrocks);
|
||||
prettyprint(&map, &shape_pos, SHAPE_NAMES[shape_id]);
|
||||
}
|
||||
}
|
||||
println!("height = {}", current_top + 1);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn prettyprint(map: &Vec<[u8; 7]>, pos: &Pos, shape_name: char) {
|
||||
// let mut i = map.len();
|
||||
// for row in map.into_iter().rev() {
|
||||
// i -= 1;
|
||||
// print!("|");
|
||||
// for j in 0..7 {
|
||||
// print!("{}", if row[j] == 0 { "." } else { "#" });
|
||||
// }
|
||||
// println!("| {} {}", i, if i == pos.y {shape_name} else {' '});
|
||||
// }
|
||||
}
|
1
day17/test.txt
Normal file
1
day17/test.txt
Normal file
@@ -0,0 +1 @@
|
||||
>>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>
|
7
day18/Cargo.lock
generated
Normal file
7
day18/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day17"
|
||||
version = "0.1.0"
|
6
day18/Cargo.toml
Normal file
6
day18/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day17"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
2085
day18/input.txt
Normal file
2085
day18/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
93
day18/src/main.array3d.rs
Normal file
93
day18/src/main.array3d.rs
Normal file
@@ -0,0 +1,93 @@
|
||||
use std::io;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::io::BufRead;
|
||||
use std::collections::HashSet;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
|
||||
mod rustsux {
|
||||
use std::fmt;
|
||||
|
||||
pub struct Array3D<T: Copy> {
|
||||
backing_vec: Vec<T>,
|
||||
pub d1: usize, pub d2: usize, pub d3: usize,
|
||||
}
|
||||
|
||||
impl<T: Copy> Array3D<T> {
|
||||
pub fn new(d1: usize, d2: usize, d3: usize, init: T) -> Array3D<T> {
|
||||
Array3D {
|
||||
backing_vec: vec![init; d1 * d2 * d3],
|
||||
d1: d1, d2: d2, d3: d3,
|
||||
}
|
||||
}
|
||||
pub fn get(&self, x: usize, y: usize, z: usize) -> T {
|
||||
self.backing_vec[x*self.d2*self.d3 + y*self.d3 + z]
|
||||
}
|
||||
pub fn set(&mut self, x: usize, y: usize, z: usize, val: T) {
|
||||
self.backing_vec[x*self.d2*self.d3 + y*self.d3 + z] = val;
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Display + Copy> fmt::Debug for Array3D<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
for x in 0..self.d1 {
|
||||
write!(f, "")?;
|
||||
for y in 0..self.d2 {
|
||||
let row_start = x*self.d2*self.d3 + y*self.d3;
|
||||
let row_end = x*self.d2*self.d3 + (y + 1)*self.d3;
|
||||
for z in row_start..row_end {
|
||||
write!(f, "{}", self.backing_vec[z])?;
|
||||
}
|
||||
write!(f, " ")?;
|
||||
}
|
||||
writeln!(f, "")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn floodfill_surface<T: Copy>(map: rustsux::Array3D<T>, block_sym: T) -> usize {
|
||||
// TODO: start from arbitrary spot instead of 0, 0, 0
|
||||
let mut queue = VecDeque::new();
|
||||
queue.push_back((0, 0, 0));
|
||||
while let Some((x, y, z)) = queue.pop_front() {
|
||||
println!("{} {} {}", x, y, z);
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let input_fname = env::args().nth(1).expect("need input file");
|
||||
let lines = BufReader::new(File::open(input_fname)?).lines();
|
||||
let mut cubes: HashSet<(usize, usize, usize)> = HashSet::new();
|
||||
let (mut max_x, mut max_y, mut max_z) = (0, 0, 0);
|
||||
|
||||
for line in lines.flatten() {
|
||||
let mut split = line.split(',');
|
||||
// +1 so that we have a guard plane at 0 index
|
||||
let x = 1 + split.next().unwrap().to_string().parse::<usize>().unwrap();
|
||||
let y = 1 + split.next().unwrap().to_string().parse::<usize>().unwrap();
|
||||
let z = 1 + split.next().unwrap().to_string().parse::<usize>().unwrap();
|
||||
cubes.insert((x, y, z));
|
||||
max_x = if x > max_x {x} else {max_x};
|
||||
max_y = if y > max_y {y} else {max_y};
|
||||
max_z = if z > max_z {z} else {max_z};
|
||||
}
|
||||
|
||||
let mut map = rustsux::Array3D::new(max_x + 1, max_y + 1, max_z + 1, '.');
|
||||
for (x, y, z) in &cubes {
|
||||
map.set(*x, *y, *z, '#');
|
||||
}
|
||||
println!("{} {} {}", max_x, max_y, max_z);
|
||||
println!("{:?}", map);
|
||||
floodfill_surface(map, '#');
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
63
day18/src/main.rs
Normal file
63
day18/src/main.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
use std::io;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::io::BufRead;
|
||||
use std::collections::HashSet;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
type Map = HashSet<(i8, i8, i8)>;
|
||||
|
||||
fn floodfill(cubes: Map, max_x: i8, max_y: i8, max_z: i8) -> usize {
|
||||
let mut visited: Map = HashSet::new();
|
||||
let mut surfaces = 0;
|
||||
let mut queue = VecDeque::new();
|
||||
queue.push_back((0, 0, 0));
|
||||
println!("cubes = {:?}", cubes);
|
||||
println!("maxes = {} {} {}", max_x, max_y, max_z);
|
||||
|
||||
while let Some((x, y, z)) = queue.pop_front() {
|
||||
visited.insert((x, y, z));
|
||||
let all_neighbors = HashSet::from([
|
||||
(x + 1, y, z), (x - 1, y, z),
|
||||
(x, y + 1, z), (x, y - 1, z),
|
||||
(x, y, z + 1), (x, y, z - 1)
|
||||
]);
|
||||
surfaces += all_neighbors.intersection(&cubes).count();
|
||||
all_neighbors.difference(&cubes)
|
||||
.filter(|&(x, y, z)|
|
||||
*x >= 0 && *x <= max_x &&
|
||||
*y >= 0 && *y <= max_y &&
|
||||
*z >= 0 && *z <= max_z)
|
||||
.filter(|&cube| !visited.contains(cube))
|
||||
.for_each(|&cube|
|
||||
if !queue.contains(&cube) {queue.push_back(cube)});
|
||||
}
|
||||
surfaces
|
||||
}
|
||||
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let input_fname = env::args().nth(1).expect("need input file");
|
||||
let lines = BufReader::new(File::open(input_fname)?).lines();
|
||||
let mut cubes: Map = HashSet::new();
|
||||
let (mut max_x, mut max_y, mut max_z) = (0, 0, 0);
|
||||
|
||||
for line in lines.flatten() {
|
||||
let mut split = line.split(',');
|
||||
// +1 so we have 3 guard planes at 0
|
||||
let x = 1 + split.next().unwrap().to_string().parse::<i8>().unwrap();
|
||||
let y = 1 + split.next().unwrap().to_string().parse::<i8>().unwrap();
|
||||
let z = 1 + split.next().unwrap().to_string().parse::<i8>().unwrap();
|
||||
cubes.insert((x, y, z));
|
||||
max_x = if x > max_x {x} else {max_x};
|
||||
max_y = if y > max_y {y} else {max_y};
|
||||
max_z = if z > max_z {z} else {max_z};
|
||||
}
|
||||
|
||||
let surfaces = floodfill(cubes, max_x + 1, max_y + 1, max_z + 1);
|
||||
println!("answer = {}", surfaces);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
34
day18/src/main1.rs
Normal file
34
day18/src/main1.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use std::io;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use std::io::BufRead;
|
||||
use std::collections::HashSet;
|
||||
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let input_fname = env::args().nth(1).expect("need input file");
|
||||
let lines = BufReader::new(File::open(input_fname)?).lines();
|
||||
let mut cubes: HashSet<(i8, i8, i8)> = HashSet::new();
|
||||
|
||||
for line in lines.flatten() {
|
||||
let mut split = line.split(',');
|
||||
let x = split.next().unwrap().to_string().parse::<i8>().unwrap();
|
||||
let y = split.next().unwrap().to_string().parse::<i8>().unwrap();
|
||||
let z = split.next().unwrap().to_string().parse::<i8>().unwrap();
|
||||
cubes.insert((x, y, z));
|
||||
}
|
||||
|
||||
let mut all_neighbors = 0;
|
||||
for (x, y, z) in &cubes {
|
||||
let neighbors = HashSet::from([
|
||||
(*x + 1, *y, *z), (*x - 1, *y, *z),
|
||||
(*x, *y + 1, *z), (*x, *y - 1, *z),
|
||||
(*x, *y, *z + 1), (*x, *y, *z - 1)
|
||||
]);
|
||||
all_neighbors += cubes.intersection(&neighbors).count();
|
||||
}
|
||||
println!("{}", cubes.len() * 6 - all_neighbors);
|
||||
Ok(())
|
||||
}
|
||||
|
13
day18/test.txt
Normal file
13
day18/test.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
2,2,2
|
||||
1,2,2
|
||||
3,2,2
|
||||
2,1,2
|
||||
2,3,2
|
||||
2,2,1
|
||||
2,2,3
|
||||
2,2,4
|
||||
2,2,6
|
||||
1,2,5
|
||||
3,2,5
|
||||
2,1,5
|
||||
2,3,5
|
Reference in New Issue
Block a user