diff options
author | Prefetch | 2023-02-25 11:41:27 +0100 |
---|---|---|
committer | Prefetch | 2023-02-25 11:41:27 +0100 |
commit | 3b877bf4cc667eb8bcc787d145203600a4dba2d2 (patch) | |
tree | c1d247def29fcb58ae28e4ae4e4d73d1b4e1b27f /d08 |
Initial commit
Diffstat (limited to 'd08')
-rw-r--r-- | d08/Cargo.toml | 6 | ||||
-rw-r--r-- | d08/input.txt | 162 | ||||
-rw-r--r-- | d08/src/main.rs | 78 |
3 files changed, 246 insertions, 0 deletions
diff --git a/d08/Cargo.toml b/d08/Cargo.toml new file mode 100644 index 0000000..fbd4f83 --- /dev/null +++ b/d08/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "d08" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/d08/input.txt b/d08/input.txt new file mode 100644 index 0000000..6085312 --- /dev/null +++ b/d08/input.txt @@ -0,0 +1,162 @@ +rect 1x1 +rotate row y=0 by 6 +rect 1x1 +rotate row y=0 by 3 +rect 1x1 +rotate row y=0 by 5 +rect 1x1 +rotate row y=0 by 4 +rect 2x1 +rotate row y=0 by 5 +rect 2x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 5 +rect 4x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 3 +rect 1x1 +rotate row y=0 by 3 +rect 1x1 +rotate row y=0 by 2 +rect 1x1 +rotate row y=0 by 6 +rect 4x1 +rotate row y=0 by 4 +rotate column x=0 by 1 +rect 3x1 +rotate row y=0 by 6 +rotate column x=0 by 1 +rect 4x1 +rotate column x=10 by 1 +rotate row y=2 by 16 +rotate row y=0 by 8 +rotate column x=5 by 1 +rotate column x=0 by 1 +rect 7x1 +rotate column x=37 by 1 +rotate column x=21 by 2 +rotate column x=15 by 1 +rotate column x=11 by 2 +rotate row y=2 by 39 +rotate row y=0 by 36 +rotate column x=33 by 2 +rotate column x=32 by 1 +rotate column x=28 by 2 +rotate column x=27 by 1 +rotate column x=25 by 1 +rotate column x=22 by 1 +rotate column x=21 by 2 +rotate column x=20 by 3 +rotate column x=18 by 1 +rotate column x=15 by 2 +rotate column x=12 by 1 +rotate column x=10 by 1 +rotate column x=6 by 2 +rotate column x=5 by 1 +rotate column x=2 by 1 +rotate column x=0 by 1 +rect 35x1 +rotate column x=45 by 1 +rotate row y=1 by 28 +rotate column x=38 by 2 +rotate column x=33 by 1 +rotate column x=28 by 1 +rotate column x=23 by 1 +rotate column x=18 by 1 +rotate column x=13 by 2 +rotate column x=8 by 1 +rotate column x=3 by 1 +rotate row y=3 by 2 +rotate row y=2 by 2 +rotate row y=1 by 5 +rotate row y=0 by 1 +rect 1x5 +rotate column x=43 by 1 +rotate column x=31 by 1 +rotate row y=4 by 35 +rotate row y=3 by 20 +rotate row y=1 by 27 +rotate row y=0 by 20 +rotate column x=17 by 1 +rotate column x=15 by 1 +rotate column x=12 by 1 +rotate column x=11 by 2 +rotate column x=10 by 1 +rotate column x=8 by 1 +rotate column x=7 by 1 +rotate column x=5 by 1 +rotate column x=3 by 2 +rotate column x=2 by 1 +rotate column x=0 by 1 +rect 19x1 +rotate column x=20 by 3 +rotate column x=14 by 1 +rotate column x=9 by 1 +rotate row y=4 by 15 +rotate row y=3 by 13 +rotate row y=2 by 15 +rotate row y=1 by 18 +rotate row y=0 by 15 +rotate column x=13 by 1 +rotate column x=12 by 1 +rotate column x=11 by 3 +rotate column x=10 by 1 +rotate column x=8 by 1 +rotate column x=7 by 1 +rotate column x=6 by 1 +rotate column x=5 by 1 +rotate column x=3 by 2 +rotate column x=2 by 1 +rotate column x=1 by 1 +rotate column x=0 by 1 +rect 14x1 +rotate row y=3 by 47 +rotate column x=19 by 3 +rotate column x=9 by 3 +rotate column x=4 by 3 +rotate row y=5 by 5 +rotate row y=4 by 5 +rotate row y=3 by 8 +rotate row y=1 by 5 +rotate column x=3 by 2 +rotate column x=2 by 3 +rotate column x=1 by 2 +rotate column x=0 by 2 +rect 4x2 +rotate column x=35 by 5 +rotate column x=20 by 3 +rotate column x=10 by 5 +rotate column x=3 by 2 +rotate row y=5 by 20 +rotate row y=3 by 30 +rotate row y=2 by 45 +rotate row y=1 by 30 +rotate column x=48 by 5 +rotate column x=47 by 5 +rotate column x=46 by 3 +rotate column x=45 by 4 +rotate column x=43 by 5 +rotate column x=42 by 5 +rotate column x=41 by 5 +rotate column x=38 by 1 +rotate column x=37 by 5 +rotate column x=36 by 5 +rotate column x=35 by 1 +rotate column x=33 by 1 +rotate column x=32 by 5 +rotate column x=31 by 5 +rotate column x=28 by 5 +rotate column x=27 by 5 +rotate column x=26 by 5 +rotate column x=17 by 5 +rotate column x=16 by 5 +rotate column x=15 by 4 +rotate column x=13 by 1 +rotate column x=12 by 5 +rotate column x=11 by 5 +rotate column x=10 by 1 +rotate column x=8 by 1 +rotate column x=2 by 5 +rotate column x=1 by 5 diff --git a/d08/src/main.rs b/d08/src/main.rs new file mode 100644 index 0000000..cbdd1fd --- /dev/null +++ b/d08/src/main.rs @@ -0,0 +1,78 @@ +use std::fs; + +fn solve_puzzle(lines: &Vec<&str>) -> [[bool; 6]; 50] { + let mut pixels = [[false; 6]; 50]; + + for line in lines { + let words: Vec<&str> = line.split_ascii_whitespace().collect(); + if words[0] == "rect" { + // Read operation parameters + let wxh: Vec<&str> = words[1].split('x').collect(); + let w: usize = wxh[0].parse().unwrap(); + let h: usize = wxh[1].parse().unwrap(); + + // Apply operation to state + for x in 0..w { + for y in 0..h { + pixels[x][y] = true; + } + } + } else if words[0] == "rotate" && words[1] == "row" { + // Read operation parameters + let which: Vec<&str> = words[2].split('=').collect(); + let y: usize = which[1].parse().unwrap(); + let n: usize = words[4].parse().unwrap(); + + // Apply operation to state + let mut old = Vec::new(); + for x in 0..50 { + old.push(pixels[x][y]); + } + for x in 0..50 { + pixels[x][y] = old[(x + 50 - n) % 50]; + } + } else if words[0] == "rotate" && words[1] == "column" { + // Read operation parameters + let which: Vec<&str> = words[2].split('=').collect(); + let x: usize = which[1].parse().unwrap(); + let n: usize = words[4].parse().unwrap(); + + // Apply operation to state + let old = pixels[x]; + for y in 0..6 { + pixels[x][y] = old[(y + 6 - n) % 6]; + } + } + } + + pixels +} + +fn main() { + let input = fs::read_to_string("input.txt").unwrap(); + let lines = input.lines().collect(); + + let pixels = solve_puzzle(&lines); + + // Part 2 must be read by eye, gives "UPOJFLBCEZ" for me + println!("Part 2 solution:"); + let mut total = 0; + for y in 0..6 { + print!("\t"); + for x in 0..50 { + print!( + "{}", + if pixels[x][y] { + total += 1; + '#' + } else { + ' ' + } + ); + } + print!("\n"); + } + + // Part 1 gives 116 for me + println!("Part 1 solution: {}", total); +} |