day 12
This commit is contained in:
parent
e69bf8e9ff
commit
3fae7cf678
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -52,6 +52,14 @@ dependencies = [
|
|||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day_13-1"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "day_13-2"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "day_2-1"
|
||||
version = "0.1.0"
|
||||
|
|
1407
day_13/input.txt
Normal file
1407
day_13/input.txt
Normal file
File diff suppressed because it is too large
Load diff
8
day_13/part_1/Cargo.toml
Normal file
8
day_13/part_1/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day_13-1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
57
day_13/part_1/src/main.rs
Normal file
57
day_13/part_1/src/main.rs
Normal file
|
@ -0,0 +1,57 @@
|
|||
fn main() {
|
||||
let input = include_str!("../../input.txt").trim();
|
||||
let mut result = 0;
|
||||
|
||||
'balls: for section in input.split("\n\n") {
|
||||
let grid: Vec<Vec<bool>> = section
|
||||
.lines()
|
||||
.map(|line| line.chars().map(|c| c == '#').collect::<Vec<bool>>())
|
||||
.collect::<Vec<Vec<bool>>>();
|
||||
|
||||
// Vertical
|
||||
for mut i in 0..(grid.len() - 1) as i32 {
|
||||
let res_val = (i + 1) * 100;
|
||||
let mut j = (i + 1) as i32;
|
||||
|
||||
'nuts: loop {
|
||||
if i < 0 || j >= grid.len() as i32 {
|
||||
result += res_val;
|
||||
continue 'balls;
|
||||
}
|
||||
|
||||
for k in 0..grid[i as usize].len() {
|
||||
if grid[i as usize][k] != grid[j as usize][k] {
|
||||
break 'nuts;
|
||||
}
|
||||
}
|
||||
|
||||
i -= 1;
|
||||
j += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Horizontal
|
||||
for mut i in 0..(grid[0].len() - 1) as i32 {
|
||||
let res_val = i + 1;
|
||||
let mut j = (i + 1) as i32;
|
||||
|
||||
'nuts: loop {
|
||||
if i < 0 || j >= grid[0].len() as i32 {
|
||||
result += res_val;
|
||||
continue 'balls;
|
||||
}
|
||||
|
||||
for k in 0..grid.len() {
|
||||
if grid[k][i as usize] != grid[k][j as usize] {
|
||||
break 'nuts;
|
||||
}
|
||||
}
|
||||
|
||||
i -= 1;
|
||||
j += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("Result: {result}");
|
||||
}
|
8
day_13/part_2/Cargo.toml
Normal file
8
day_13/part_2/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day_13-2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
75
day_13/part_2/src/main.rs
Normal file
75
day_13/part_2/src/main.rs
Normal file
|
@ -0,0 +1,75 @@
|
|||
fn main() {
|
||||
let input = include_str!("../../input.txt").trim();
|
||||
let mut result = 0;
|
||||
|
||||
'balls: for section in input.split("\n\n") {
|
||||
let grid: Vec<Vec<bool>> = section
|
||||
.lines()
|
||||
.map(|line| line.chars().map(|c| c == '#').collect::<Vec<bool>>())
|
||||
.collect::<Vec<Vec<bool>>>();
|
||||
|
||||
// Vertical
|
||||
for mut i in 0..(grid.len() - 1) as i32 {
|
||||
let res_val = (i + 1) * 100;
|
||||
let mut j = (i + 1) as i32;
|
||||
let mut error_allowed = true;
|
||||
|
||||
'nuts: loop {
|
||||
if i < 0 || j >= grid.len() as i32 {
|
||||
if !error_allowed {
|
||||
result += res_val;
|
||||
continue 'balls;
|
||||
} else {
|
||||
break 'nuts;
|
||||
}
|
||||
}
|
||||
|
||||
for k in 0..grid[i as usize].len() {
|
||||
if grid[i as usize][k] != grid[j as usize][k] {
|
||||
if error_allowed {
|
||||
error_allowed = false;
|
||||
} else {
|
||||
break 'nuts;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
i -= 1;
|
||||
j += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Horizontal
|
||||
for mut i in 0..(grid[0].len() - 1) as i32 {
|
||||
let res_val = i + 1;
|
||||
let mut j = (i + 1) as i32;
|
||||
let mut error_allowed = true;
|
||||
|
||||
'nuts: loop {
|
||||
if i < 0 || j >= grid[0].len() as i32 {
|
||||
if !error_allowed {
|
||||
result += res_val;
|
||||
continue 'balls;
|
||||
} else {
|
||||
break 'nuts;
|
||||
}
|
||||
}
|
||||
|
||||
for k in 0..grid.len() {
|
||||
if grid[k][i as usize] != grid[k][j as usize] {
|
||||
if error_allowed {
|
||||
error_allowed = false;
|
||||
} else {
|
||||
break 'nuts;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
i -= 1;
|
||||
j += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("Result: {result}");
|
||||
}
|
Loading…
Reference in a new issue