Skip to content

Commit

Permalink
Year 2019 Day 8
Browse files Browse the repository at this point in the history
  • Loading branch information
maneatingape committed Sep 1, 2023
1 parent e424dc0 commit 79ce632
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ pie
| 5 | [Sunny with a Chance of Asteroids](https://adventofcode.com/2019/day/5) | [Source](src/year2019/day05.rs) | 34 |
| 6 | [Universal Orbit Map](https://adventofcode.com/2019/day/6) | [Source](src/year2019/day06.rs) | 28 |
| 7 | [Amplification Circuit](https://adventofcode.com/2019/day/7) | [Source](src/year2019/day07.rs) | 30000 |
| 8 | [Space Image Format](https://adventofcode.com/2019/day/8) | [Source](src/year2019/day08.rs) | 5 |

## 2015

Expand Down
1 change: 1 addition & 0 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ mod year2019 {
benchmark!(year2019, day05);
benchmark!(year2019, day06);
benchmark!(year2019, day07);
benchmark!(year2019, day08);
}

mod year2015 {
Expand Down
1 change: 1 addition & 0 deletions input/year2019/day08.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
222222220212022222222222020022222222222220202102222001222022222222222012222222222002022222202022222222022122222202220222222122222222220222222222222222122222220222222222222222020022222222222222212112222211222022222122222202222222222002122222222222222222022022222212220222222122222222222222222222222222122222221202222222222222020122222222222221202002202001222222222222222022222222222220022222202122222222222022222222220222222122222222221222222222222222122222221202022222222222221122222222222221222202222110222122222122222022222212222002222222212122222222222222222212221222222222222222222022222222222222222222221202122222222222112222222222222222202000222100222022222122222222222212222121122222212022222222222022222212221222222122222222220022222222222222122222221212222222222222010122222222222220202111222110222222222222222212222102222010022222222022222222222122222222220222222222222222222122222222222222122222222212222222222222100022222222222222212000222111222022222122222022222012222112122222202022222222122022222202221222222122222222222022222222222222222222222212222222022222022122222222222220212201222000222022222022222202222012222221122222202022222222222122222222222222222022222222221022222222222222022222220222222222122222200022222222222222212021212112222022222222222112222222222212022222202022222222022022222202220222222022222222221022222222222222122222222212222222122222122222222222222220212222202122222222222022222012222112222110222222222222222222122122222201221222222222222222220222222222222222022222222202122222122222212222222222222221202111212000222022222222222202222122222010022222212022222222222022222200221222222122222222220122222222222222222222222202022222022222000022222222222222022222212121222222222222222112222012222100022222222122222222222122122220222222222122222222222022222222222222222222221222022222022222010022222222222222102101212122222002202022222122222012222001222212222222222222020222222202222222222122222222221022222222222222222222220202222222122222102222222222222221212000202222222122212222222122222222222212222202212222222222122122222222222222222022222222122022222222222222222222221202022222122222002022222222222221212211202200222022202022222002222012222022222222222122222222222122122211221222222222222222022222222222222222222222221212122222022222202122222222222221102211222221222122212222222002222002222002222202222222222222120122122220221222222022222222221222222222222222022222222022022222122222112122222222222222122200202020222002212022222102222112222022222222212122222222120122122221221222222122222222122122222222222222022222221022122222222222210022222222222222012202212202222002212122222022222112222122122222222022222222122022122210222222222022222222220222222222222222222222122112222222022222021022222222222220012211222000222212202122222122222102222222122202212222222222020222222221220222222122222222121022222222222222022222121202122222222222101122222222222222212002212221222222222122222212222102222210022202212122222222121122122210222222222122222222121122222222222222122222220022022222122222002222222222222220222201222111222222222022222202222222222202222222222022222222020102022210220222202222222222102122222222222222022222121000022222022222012022222222222222102120222111222002212122222212222012222100022222202022222222021022222222221222212222222222022122222222220222022222022201122222122222202222222222222220022211202122222212212122202012222002222212022222222222222222220102222212221222212022222222120022222222221212222222020111222222022222022222222222222222122122212122222222212022212012222012222021022212222122222222122202022202221222222122222222121022222222222202122222222002222222122222102122222222222221212221222021222202212222212002222102222101122202212022222222121002022200220222222122222222110222222222222202222222120010222222022222021122222222222222002201212021222102202222222002222002222120122202202222222222120022022211220222212022222222210222222222221212222222020120022222022222212122222222222222020201202021222212212022202122222202222100022202212222222222222012222221221222202222222222202222222222222202222222022200022222122222022222222222222222222112212100222102212022222112222112222202222222212222222222122202022221221222202122222222001122222222222202221222222120122222222222112122222222222221200010222012222212212122202202222222222210222212212222222222020002222211222222222022222222202022221222221222120222020211122222122222021022222222222220212021212122222202202022212222222012222210122202202022222222121002222212221222222022222222001022220222221212121222120221122222022222011022222221222221100022212000222202222122212012222112222210122212222122222222121202202210221222222022222222010222221222220212122222022200222222222222222222222221222221021200202200222212222222222202222112222011112212212122222222120112022202222220202022222222021122222222221202220222220212122222022222212222222221222222100122212201222212222122202002220002222202212202222222222222121222022200220222222122222222210022220222220212020222020000022222122222012222222222222221212012222101222212202222222112220201222001222202202222222222020122112220222221202222222222010022221222220212021222021210122222122222212222222222222222002010222021222122212122212012222000222001102212202122222222222222002202221220212222222222201222222222222202222222022012022222022222000222222221222220001101212012222022220122202112221100222211122222212122222222120202012202220220212122222222100122221222220222022222020011122222122222022222222221222221002222212012222212220122222102221122222221022212212022222222122102012210222222222222222222101122221222221200222222121101122222022222200022222222222222120211212120222012201022212112221011222012012212222122222222222102122221222222222222222222100222221222220221022222220221122222222222112222222221222222211020222112220012210222202102221011222200012222222122222222220212212202221220202122222222202222222222222202121222122022022222222222221122222220222221020210202202222212200022202112222112222210222202212122222222121012122210021220222022222222110222222222222210221222121022022222022222002222222221222221022002222011222202222222212112220011222101012222212022222222221222112202121221202022222222122022221212220220022222022210122222022222111222222221222222200012212200220002200122212112221211222000022202212122222222121212212210220220202022222222101022221222220201122222020212222222122222110022222122222220101101211011021022222222202222222221222201012222202122222222020122222210021221222022222222201022222212220222220220020012022222222222200122222021222222220120210022222002212122222202220002222221002212212022222222021002102221220220222122222222200122220202222212021220020120212222222220122222222020222221101011202111121202210122212102222121222212202222212222222220022002012211220020202222222222000022220222222222221222121110120222022222020022120120222221222012201110220122221122202102221120222200222202212222222222122212022221122222212122222202100222221212222202122221121222112222022222100022220222222222022220211102222212200022212102221211222101012202222222222221120022112212120122222122222222111122220212220202120222022220212222222221000122121022222222121212202202120102201022202202222100222121202202212022222220022222012211021022202222222222022222222202220221121222222210200222222222122222120121222222222022200222021012200022222112222220222212202202212022222221120012012221020022202022222222120222221202222221120221222000111222122222101222122121222222011012200021122202211022212022221202222101122202222122222220221202222210020220212122222222211122220202222202222220022222221222022222202022120201222221211020212110121212221122212020222011222220002222202002222220022012002211120221202122222222020222220212220211021220220021001222022221002122022210222220101210212111121202211122222201220102222210212222222112222222222102202200220221212022022222111122221222222222122221122002221222122221012122222010222222111100212100222222202222222001222112222110212202202122222222221022222211020020212222022222001122220202220222020221121212211222022220221222022211222221020111200020020112211222222022220201222010202212202112222220222020022200022022212022102212010022222212220212220220122010022222122221002222221100222220211212200222210012201022212101220001222000122002202002222222222101222212120020222222002202221022220212220201122220120212222222222220112122221102222220020020222020010222221222212202020121222212222122222002222220121012102212222221222222222222111122222212222220121221020112020222022222002122122012222220112122202222221002212222222021021200202101222202202122222221120102022212222221202122122202200222222202221202122221020012202222022221010022221220222221011021220200012112211122212011020001212021222212222002222222020202112202020022202122022222021222220222222222022221121210210222222221221122022121222221110202212100102002210022222021021221212210112222202002122220020111202201020122222222102202102222221212222221222220020010111222022222200222120120222222100111212212201112220222022102022101202222102002212222022221222112212200221020212022002222001122222222221211122222122021011222022220200022220212222220121001222102120112211022112000121212212012112122202122022222222202122221020022222122212202102122222202220210222222220120102222022220011222121200222220000110211102000202202022102210021011212001102022202122022220220201012211022021212022112212220122222222220221122222022100122222122220110022020222222220120201200121020122202122000100121010212101222212212022222221220022102200120221212022102221122222221202222202221222022201201222022222221122022020022221110220202111011112200022122020021001212001202022202022121222121201222210021200212022002222221122220202221200220220221200000222222222022220121022222222202001211102120112220022122101221201202201212222222202022220222212112200221022202222202201120222221202222001021221222112100222022221200022021011122220120102200022111002210222020112020210202100002002202202020220020001022210120121202222212212012222220202221202221221122111022222022220111020021021022220020221210012200202222122110011020110212122022202222102022222020002022202220201222221112221212022222212221120022221022212221222222222111021020202222220202212212211020002210122102220021120212120212002222202120222122201122212020121212220202210201122220212222212120220020111001222222221100222122001222222222002201212212202210222222102022111202220222022222112222221222100212221012222222022022211212222221202222011020222021022212222222221002122121022022210002212211200200202212022121122022101202202102102222022121221122010112201221002202022122221122122220212221222222221022021020222222222002220121011022201002202212002001122202122011010021020212102022202202022121222122010112211001122202001212201000122222222221012022220220200021222122220212021122112022211001110220112120222202222000101022200202211122002202002121220220000222212211120222112012211002122222222222011122020022000022222202222210020120121022221202201211101202012211122102012221221212101122022222112021222120012222220010120222220222220002022221202220010020021022012022222202222211021120002022200102200222111222102220122121202222112222200102222212222221221222200022021100012202112112200221222222222221202121122220000201222202222002121100212122212000101202201001002202022100000121122222211212112202212020220022121212210122211222012002211000022220202220201220222220012020222222221201222101022022222220000210100002122202022220120120111212112202222212002122220022220022020122220202010112201220222021212221112222221020000222222222222112022002022122022010111202201221022210222221022222011202212002212222012220222022012002000221021222100020010011122222222220000121020220002121222022221121120002202122001121120202122221222201022221220011020212211002022202012220222122020212102100100202011220200101222021222221200120121221220221222012221002022101102022022020111201220210012222122222021222220202210012112222102122222020022022101021011212100222122100222122202220020022221021121110222222222201222210210022101001202200020100012212222201021101120222212112122212222021221122100202221222021212000221120001222220202220101022022222011022222002222122221002001122001001020220122010002210122221222102111201102022002212212020220122202002222000011212002211002012022121222221201121221220021201222002221011021021210122100112011202020112012201222001102221220202002002222222102022221122022112201001201222212200012200122221222221102210122222102222222122222102220211000022210101102221001102212221222011001200001222010212112222222221221020211122022122112200101222021122122222222222121120020022022110222202220112020212222122011001200210221121012202222112210012121210011022112212102222221221200222021210200221212210111012222222202220210011221220211202222222222021021021221222012201222222101110112221122002012120110220201002212212122122220220201022211222011102211021002212022021222222000212222220102022222122221122122122211122020102101202021110022210022021001122102222211002112212202121222020110112201010021010121121202121122222212221111000001022110200222212220021110102010222100012220202001122202221222020121121202201012012212202202020220121220222201110022022201101020120122122202221120201220222000002222002222012201010112102010020002221202012222210022112011001220200222212102222102021220122001212001112102212122220222201222021202220001001211020212221222012220111220012021002022211012222022222212221022022210000121220202202122212212221222020020202211100201020020121112021222122222221010121101221212022222222222220120220100022010221010201100010212212122210222222120222021212212202122222222020102022202000012010002202211200122221202221200021122121222001222002222201212211112202011200002222021011002210222002010111100220112022212202222122221222111112120012112110201100202111022021222221101221121122122121222202222120121220200222200021222211021201202211022011211212001201110202012222202121221022221002122021022021220211102221222220222220211002120122111102222202222021112020100012000002002202111000212221222220001020211221220022122202202122220222210112110221101000212002112020022020222221111211020022122120222002220000212022110112001220110222221002112212022012100002020221211212012202121120221020221102111122211102011012002222222222222222111120102221211010222102221102120201212202211111020211012221002202122222120010001220010002112112120020220022010012001002011101220200211100022121202220001022010122112011222112222011000012220022112102120202211111222202022110021102020212122000222012220022220222110002200201221000202110210100122020202221122222000120102101222012221020122021111112020002002211101021100221022002001102102221000100012102000220220221202212211102122122211020221102122021202222101022002220002220222002220221002221120022020001120220102112121210022022121000000210112012212012100222220022001222122211210202011100010211122221212222200122001022200220222012222022021120020122202210011210101101212221121210102200121220021110122012202222220022002122022120102112101221010201022220222212100202022211112210100200111012202010022201000111210110011210222110102211221200021000222121120000201111010100120121120020010102021202121221111000121021221
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub mod year2019 {
pub mod day05;
pub mod day06;
pub mod day07;
pub mod day08;
}

/// # Help Santa by solving puzzles to fix the weather machine's snow function.
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ fn all_solutions() -> Vec<Solution> {
solution!(year2019, day05),
solution!(year2019, day06),
solution!(year2019, day07),
solution!(year2019, day08),
// 2015
solution!(year2015, day01),
solution!(year2015, day02),
Expand Down
71 changes: 71 additions & 0 deletions src/year2019/day08.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! # Space Image Format

pub fn parse(input: &str) -> &str {
input
}

/// Each layer is 25 * 6 = 150 bytes and there are 100 layers total.
/// It's faster to count pixels 8 at a time by parsing the bytes as `u64` then using bitwise logic
/// and the [`count_ones`] intrinsic. The only minor wrinkle is that 8 does not divide 150 evenly
/// so we must handle the last 6 bytes specially.
pub fn part1(input: &str) -> u32 {
let bytes = input.as_bytes();
let mut index = 0;
let mut ones = 0;
let mut twos = 0;
let mut most = 0;
let mut result = 0;

for _ in 0..100 {
// First 144 of 150 bytes.
for _ in 0..18 {
let slice = &bytes[index..(index + 8)];
let n = u64::from_be_bytes(slice.try_into().unwrap());
ones += (n & 0x0101010101010101).count_ones();
twos += (n & 0x0202020202020202).count_ones();
index += 8;
}

// Handle remaining 6 bytes.
// The masks exclude the most significant 2 bytes to prevent double counting.
let slice = &bytes[(index - 2)..(index + 6)];
let n = u64::from_be_bytes(slice.try_into().unwrap());
ones += (n & 0x0000010101010101).count_ones();
twos += (n & 0x0000020202020202).count_ones();
index += 6;

if ones + twos > most {
most = ones + twos;
result = ones * twos;
}

ones = 0;
twos = 0;
}

result
}

/// Since a black or white pixel covers those in lower layers, it's faster to check each pixel
/// stopping as soon as we hit a non-transparent value.
pub fn part2(input: &str) -> String {
let bytes = input.as_bytes();
let mut image = ['.'; 150];

for (i, pixel) in image.iter_mut().enumerate() {
let mut j = i;

while bytes[j] == b'2' {
j += 150;
}

if bytes[j] == b'1' {
*pixel = '#';
}
}

let mut result =
image.chunks_exact(25).map(|row| row.iter().collect()).collect::<Vec<String>>().join("\n");
result.insert(0, '\n');
result
}
1 change: 1 addition & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ mod year2019 {
mod day05_test;
mod day06_test;
mod day07_test;
mod day08_test;
}

mod year2015 {
Expand Down
9 changes: 9 additions & 0 deletions tests/year2019/day08_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[test]
fn part1_test() {
// No example data
}

#[test]
fn part2_test() {
// No example data
}

0 comments on commit 79ce632

Please sign in to comment.