diff options
| author | Aleksa Vuckovic <aleksa@vuckovic.cc> | 2023-12-15 03:57:07 +0100 |
|---|---|---|
| committer | Aleksa Vuckovic <aleksa@vuckovic.cc> | 2023-12-15 04:31:46 +0100 |
| commit | a06ae0bf475fda36bb1cbc5b0b7bbf355c499bed (patch) | |
| tree | 8b0b04ee2097fbfe2f1d426081258ae0033200f4 /src | |
| parent | b987ec6481ea40e257dc81a8b3ebfc5f0483afe5 (diff) | |
day13 & day14pt1
Diffstat (limited to 'src')
| -rw-r--r-- | src/day13pt1.rs | 70 | ||||
| -rw-r--r-- | src/day13pt2.rs | 60 | ||||
| -rw-r--r-- | src/day14pt1.rs | 23 | ||||
| -rw-r--r-- | src/day14pt2.rs | 23 | ||||
| -rw-r--r-- | src/main.rs | 8 |
5 files changed, 180 insertions, 4 deletions
diff --git a/src/day13pt1.rs b/src/day13pt1.rs new file mode 100644 index 0000000..bc71c44 --- /dev/null +++ b/src/day13pt1.rs @@ -0,0 +1,70 @@ +pub fn f(input: Vec<Vec<char>>) -> u64 { + for x in 1..input[0].len() { + let mut ind: bool = true; + + for i in 0..input.len() { + if !ind { + break; + } + + for j in 0..x { + if 2 * x - 1 - j >= input[0].len() { + continue; + } + if input[i][j] != input[i][2 * x - 1 - j] { + ind = false; + break; + } + } + } + + if ind { + return x as u64; + } + } + + for y in 1..input.len() { + let mut ind: bool = true; + + for i in 0..y { + if !ind { + break; + } + + for j in 0..input[i].len() { + if 2 * y - 1 - i >= input.len() { + continue; + } + if input[i][j] != input[2 * y - 1 - i][j] { + ind = false; + break; + } + } + } + + if ind { + return (y * 100) as u64; + } + } + + panic!(); +} + +pub fn main() { + let txt = std::fs::read_to_string("./input/day13.txt").unwrap(); + let input = txt + .split("\n\n") + .map(|s| { + s.lines() + .map(|s| s.chars().collect::<Vec<char>>()) + .collect::<Vec<_>>() + }) + .collect::<Vec<_>>(); + + let mut sum: u64 = 0; + for i in input { + sum += f(i); + } + + println!("{}", sum); +} diff --git a/src/day13pt2.rs b/src/day13pt2.rs new file mode 100644 index 0000000..5af732b --- /dev/null +++ b/src/day13pt2.rs @@ -0,0 +1,60 @@ +pub fn f(input: Vec<Vec<char>>) -> u64 { + for x in 1..input[0].len() { + let mut cnt: u32 = 0; + + for i in 0..input.len() { + for j in 0..x { + if 2 * x - 1 - j >= input[0].len() { + continue; + } + if input[i][j] != input[i][2 * x - 1 - j] { + cnt += 1; + } + } + } + + if cnt == 1 { + return x as u64; + } + } + + for y in 1..input.len() { + let mut cnt: u32 = 0; + + for i in 0..y { + for j in 0..input[i].len() { + if 2 * y - 1 - i >= input.len() { + continue; + } + if input[i][j] != input[2 * y - 1 - i][j] { + cnt += 1; + } + } + } + + if cnt == 1 { + return (y * 100) as u64; + } + } + + panic!(); +} + +pub fn main() { + let txt = std::fs::read_to_string("./input/day13.txt").unwrap(); + let input = txt + .split("\n\n") + .map(|s| { + s.lines() + .map(|s| s.chars().collect::<Vec<char>>()) + .collect::<Vec<_>>() + }) + .collect::<Vec<_>>(); + + let mut sum: u64 = 0; + for i in input { + sum += f(i); + } + + println!("{}", sum); +} diff --git a/src/day14pt1.rs b/src/day14pt1.rs new file mode 100644 index 0000000..9b68f62 --- /dev/null +++ b/src/day14pt1.rs @@ -0,0 +1,23 @@ +pub fn main() { + let txt = std::fs::read_to_string("./input/day14.txt").unwrap(); + let input = txt + .lines() + .map(|s| s.chars().collect::<Vec<char>>()) + .collect::<Vec<Vec<char>>>(); + + let mut sum: u64 = 0; + + for j in 0..input[0].len() { + let mut off: u64 = 0; + for i in 0..input.len() { + if input[i][j] == 'O' { + sum += input.len() as u64 - off; + off += 1; + } else if input[i][j] == '#' { + off = (i + 1) as u64; + } + } + } + + println!("{}", sum); +} diff --git a/src/day14pt2.rs b/src/day14pt2.rs new file mode 100644 index 0000000..9b68f62 --- /dev/null +++ b/src/day14pt2.rs @@ -0,0 +1,23 @@ +pub fn main() { + let txt = std::fs::read_to_string("./input/day14.txt").unwrap(); + let input = txt + .lines() + .map(|s| s.chars().collect::<Vec<char>>()) + .collect::<Vec<Vec<char>>>(); + + let mut sum: u64 = 0; + + for j in 0..input[0].len() { + let mut off: u64 = 0; + for i in 0..input.len() { + if input[i][j] == 'O' { + sum += input.len() as u64 - off; + off += 1; + } else if input[i][j] == '#' { + off = (i + 1) as u64; + } + } + } + + println!("{}", sum); +} diff --git a/src/main.rs b/src/main.rs index 11d3ec1..ed80bc3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ -mod day11pt1; -mod day11pt2; +mod day14pt1; +mod day14pt2; fn main() { - day11pt1::main(); - day11pt2::main(); + day14pt1::main(); + day14pt2::main(); } |
