aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksa Vuckovic <aleksa@vuckovic.cc>2023-12-16 03:00:41 +0100
committerAleksa Vuckovic <aleksa@vuckovic.cc>2023-12-16 03:00:41 +0100
commit7769eb17b7d9c0ed4fd28a26f485a76da46b5a5d (patch)
treedccb74d18c63ed0c2a563677fe3d54d005f6963c
parenta06ae0bf475fda36bb1cbc5b0b7bbf355c499bed (diff)
day14pt2
-rw-r--r--src/day14pt2.rs129
1 files changed, 117 insertions, 12 deletions
diff --git a/src/day14pt2.rs b/src/day14pt2.rs
index 9b68f62..a5d5bed 100644
--- a/src/day14pt2.rs
+++ b/src/day14pt2.rs
@@ -1,23 +1,128 @@
+fn cycle(input: &mut Vec<Vec<char>>) {
+ let mut sl: usize;
+
+ // north
+ for j in 0..input[0].len() {
+ sl = 0;
+ for i in 0..input.len() {
+ if input[i][j] == 'O' {
+ if sl != i {
+ input[sl][j] = 'O';
+ input[i][j] = '.';
+ }
+ sl += 1;
+ } else if input[i][j] == '#' {
+ sl = i + 1;
+ }
+ }
+ }
+
+ // west
+ for i in 0..input.len() {
+ sl = 0;
+ for j in 0..input[0].len() {
+ if input[i][j] == 'O' {
+ if sl != j {
+ input[i][sl] = 'O';
+ input[i][j] = '.';
+ }
+ sl += 1;
+ } else if input[i][j] == '#' {
+ sl = j + 1;
+ }
+ }
+ }
+
+ // south
+ for j in 0..input[0].len() {
+ sl = input.len() - 1;
+ for i in (0..input.len()).rev() {
+ if input[i][j] == 'O' {
+ if sl != i {
+ input[sl][j] = 'O';
+ input[i][j] = '.';
+ }
+ if i != 0 {
+ sl -= 1;
+ }
+ } else if input[i][j] == '#' {
+ if i != 0 {
+ sl = i - 1;
+ }
+ }
+ }
+ }
+
+ // east
+ for i in 0..input.len() {
+ sl = input[0].len() - 1;
+ for j in (0..input[0].len()).rev() {
+ if input[i][j] == 'O' {
+ if sl != j {
+ input[i][sl] = 'O';
+ input[i][j] = '.';
+ }
+ if j != 0 {
+ sl -= 1;
+ }
+ } else if input[i][j] == '#' {
+ if j != 0 {
+ sl = j - 1;
+ }
+ }
+ }
+ }
+}
+
+fn sum(input: &Vec<Vec<char>>) -> u64 {
+ let mut sum: u64 = 0;
+
+ for i in 0..input.len() {
+ for j in 0..input[0].len() {
+ if input[i][j] == 'O' {
+ sum += (input.len() - i) as u64;
+ }
+ }
+ }
+
+ return sum;
+}
+
+fn cycle_len(a: &Vec<u64>) -> u64 {
+ for cl in 2..(a.len() / 3) {
+ let mut ind = true;
+ for i in 0..cl {
+ if a[i] != a[cl + i] || a[cl + i] != a[cl * 2 + i] {
+ ind = false;
+ break;
+ }
+ }
+ if ind {
+ return cl as u64;
+ }
+ }
+ return 0;
+}
+
pub fn main() {
let txt = std::fs::read_to_string("./input/day14.txt").unwrap();
- let input = txt
+ let mut 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;
- }
+ let mut a = vec![];
+ for i in 0..1000 {
+ cycle(&mut input);
+ if i > 500 {
+ a.push(sum(&input));
}
}
- println!("{}", sum);
+ let cl = cycle_len(&a);
+ assert_ne!(cl, 0);
+
+ let c = 1000000000 - 501 - 1;
+ println!("{}", a[(c % cl) as usize]);
}