From c7ed5fa938c60a57d45b94bbc8644ecc0c3eb839 Mon Sep 17 00:00:00 2001 From: Ummon Date: Mon, 9 Dec 2019 22:20:31 +0100 Subject: [PATCH] Day 08 part1 --- Cargo.toml | 3 ++- data/day07.input | 1 + data/day08.input | 1 + src/day07.rs | 1 - src/day08.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 9 +++++++-- 6 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 data/day07.input create mode 100644 data/day08.input diff --git a/Cargo.toml b/Cargo.toml index c7438f7..7db3d01 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -itertools = "0.8" \ No newline at end of file +itertools = "0.8" +image = "0.22" \ No newline at end of file diff --git a/data/day07.input b/data/day07.input new file mode 100644 index 0000000..258a8a6 --- /dev/null +++ b/data/day07.input @@ -0,0 +1 @@ +3,8,1001,8,10,8,105,1,0,0,21,38,59,76,89,106,187,268,349,430,99999,3,9,1002,9,3,9,101,2,9,9,1002,9,4,9,4,9,99,3,9,1001,9,5,9,1002,9,5,9,1001,9,2,9,1002,9,3,9,4,9,99,3,9,1001,9,4,9,102,4,9,9,1001,9,3,9,4,9,99,3,9,101,4,9,9,1002,9,5,9,4,9,99,3,9,1002,9,3,9,101,5,9,9,1002,9,3,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,99 \ No newline at end of file diff --git a/data/day08.input b/data/day08.input new file mode 100644 index 0000000..b3c7dc1 --- /dev/null +++ b/data/day08.input @@ -0,0 +1 @@ +221202222022222022021222222222122202202222222122002202221222222222202022202222222222222222222222022222022202221222022222222202222212202222202202222222220212222222222222122222022222022212222222222122002212220222222222202122222222222222222222222222022222022222221222022222222222222202202222212222222222222222222022222122022222122222022212202222222222002222222222222222222222222222222222222222222222222222222202222222022222222202222222002222212212222222220202222222222022121222122222122222212222222122112202220222220222212222202222222222222222222222022222222202222222222222222202222202102222202222222222221202212222222022222222222222022212212212222022202201222222222222212222202222222222222222222222222222222212220222222222222202222222122222212202222222220202222122222222220222122222122222202212222222222202221222220222212122212222222222222222222202222222222222220222022222222222222222222222222222222222221202222122222022222222222222222222202212222122122210222222220222222122222222222222222222222222222222122222222222122222222202222222002222202202220222220202212222222122120222122222222202222202222022012221220222222222202022212222222222222222222222022222222222220222022222222212222222202221202212222222222202222122222122021222122222122222222222222222202200222222222222202022212222222222222222222202122222122212221222022222222212222202122222212222220222221212202222222222121222222222022212222202222022222220221222221222202122212222222222222222222202222222122222222222022222222222222202122222212202221222221202212122222022221222222222022202202222222022011220220222222222202022202222222222222222222222222222222222220222122222222212222212112222222202220222221212222122222022020222022222120222202212222122202221220222222222222022222222222222222222222202222222022202221222022222221202222222222221212222222222221212212022222122020222222222021212212222222222120201220222221222202122202222222202222222222222022222022202221222022222221222222212122222212202220222220222202122222122020222122222220202202212222022000210221222220202202122202222222202222222222222022222022212221222022222220202222202102222202222220222221202222122222222220222022222121202212202222222120212221222222212222122222222222212222222222202222222222222222222222222221212222212002221202222222222221212212022222022122222122222122222222222222222110222222222220212212122212222222200222222212222222222122222222222022222222212222212002222222222220222221212202122222122022222222222121212202222022122120201222222220212222221212222222201222222202202222222022222221222122222220202222212102220202212222222222202202222222122121222122222121212202222122222010200021222221222212121212222222221222222222222122222122212220222222222221212222202002222222222222222221222222222222122121222222222020222212202122022201220120222220222212220212222222210222222222212222222022202220222222222222202222212012222221222222222222212212022222212221222012222220212222212222222200212220222220222222022202222222202222222222202112222022212222222022222222202222222122221222212220222220212212222222112022222002222121202202222022022212200220222220202212222212222222212222222212212122222222202221222022222222222222212022221201212221222220212212122222202022222022222122222212222022022122202220222220222202220222222222211222222222212222222022112221222022222221222222212102220221222220222221222212122222002121222012222122212212212122122120200122222221221222222202222222012222222222222022222122002220222222222220212222202212221212222020222220202222002222022221222102222022222222222122202111201022222222210212222212222222012222222222222100222222122220222122222221212222222002221200202221222222202202002222012020222012222120202222202222112021200220222220201222021222222222222222222212222021222122012222222122022220202222202012220212212121222222212202102202002021222212222222202222202022202210222020222201222202220222222222200222222212202120222122102221222022222220212222222002221222212220222220202222222212222121222212222021222212212022102121201021222202220212122202222222000222222212212010222122102221222022022220212222222102220220222021222220202202212222222222222222222122212202212222212200200121222200220222221222222222001222222222122100222222122220222122222220212222212102220210202222222222212222102202212221222212222222212212212022022202210021222222201212021212222222102222222212122221222022002222222022122220222222202002220212212221222221202221102212212222222222222022202222212222102102221022222200211222021222222222110222222212122220222122202221222022022221212222202212221201212020222221222210002212011122022112222120212222212022202210210022222200212222221202222222200222222222002112222222202220222022222221202222202002220212202020222222221210022212122120022112222121222222202122112200201121222201211212122222222222212222222222012212222122022222222222022220222222222022222210222122222221222222202212122021022022222021222202202222002210211221222210222212221212222222121222222202122010222022222222222022022220202222222002222222222211222220212210112222010222022112220021212202212122122011202122222220211222220202222222022222222222002112222222212220222022122220202222222112222222222102222220210220002222012120022212222122202202222122212110211020222221202212020222222222002222222222102112222122202220222122022221222222212002222220222100222221200222022202211021222222222122202202222122102020222020222202222222220212222222012222222222202202222122222220222222022222222222222222220201202010222222201221012222220121022202221121222222202022222020210121222200211212220202222222222222222222012002222122212221222222022221212222222222221200202102222221221221222210110222122012222222202222202022102200222222222210220212121212222222121222222202012112222122122222222122022221202222212202222201202002222222211210112222001022222222221020222212222220102221222220222200202212120212222222102222222222202101222122002222222022222220202222222022220212212020222222212200122220001121022112220122202212222120112211220121222201022222022220222222001222222202202101222122012221222022022221202222222212222212202000222221012201102221200120022022222220202202222121002101201020222211111212020212222222110222222222102222222222202222222222222220202222202102222201202020222222212210022222001121122220221120222212202022202202220220222211022202020210222222220212222222212202222122122220222122222222202222212222222210202012222220011211122221010021021000222222222212202220112000221222222220221202222221222222212202222212112001222222112220222122222221202222222122220001222210222220012202122220021020220121222122222202202222122012210120222211002212020200222222100212222202012201222122222222222022122222222222212002222121222100222221101211012201221222020221220220202202212021202220220020222211002222222201222222211202202222012111222022112222222022022221222222212212222210222210222221101212012202211022021102220020222222202021122000200021222200010212122220222222001202202202212001222222002222222022022220212222222102221200202021222221212201122212122021021011220220212212202021002022212020222222212222122201222222121212212212022110222122002221222222022220222222212122222002222002222222202200112202220122021112221121202212222220022021211220222210122222121212222222121202212202012222222222212222222222122221202222222202220112202010222220101211222202021222022012222222222222222020022221212120222221101202021222222222211222212222112110222022122221222022222222212222222022221211222222222221101221122202220220022121222120202222202221122010210122222222001222222221122222021212212202012221222222012220222022122220202222222222221222212111222222222200202222211222021112222022202212202221022000202220222212100222022201122222210202212222102201222022122221202122222221212222212012221121212102222220212220112221000222120022220120222202222022112102201120222221222212021222122222101222202222222011222222212220202222222220212222222122222210212002212220222222202222110022222102222120212202202120012122211220222200200202022222222222210222202202112110222222212221222022122221212222222112222012212220222222222211022200122221220212222221212202222122202112212022222212201202022211122222201222222212112121222222022222202222022220201222212112222012202012222220022200002211010121022112222122222222202222022001221122222210210202021200122222222202212201202102222022202221202222022220222222212202221101202200212221020211102212022122120111221021212212222020112120200022222222221212221202022222000222212211012020222022102222222222222220212222222002220222222222222222022200222202122021120000220220222222202021022000201020122211011222021221022222012212222222112220222222122222202022222220222222212022221111212002212222110210022221100222120210221220222202202211222011222222122221010222121201022222211202212202012102222122122222222222122221221222212122221012212011222221201202022210012021220220222222221202202100122012220020222202210212222211022222110202222201022000222122012221002122222221200222202202222100202221222222021200012200111222021112221222221212212012212110211020022221010202120222022222220212222201012012222122022220002222022221202222202000221212222001202220001201112202202022121211220021211002212222012222222021122121012202021200122222110202222221212202222222012220102122122220220222202002221211212112212221120200022200102022021210221022210012202101202212222120022222012212022211222222012212222221112002222022012222002022122221220222022202221212202102222220000220112220210221022010220020202002202102222011201220022002102222021212122222001222202222202202222122122222222022022221220222112010221110212220212220101221202201000120220211222022220122222000022000200022122102012222221222222222020212212112012020222122122220222222022222200222202121220001212011222220021211222200002020121010221122212022222010111202210220122121220202120210122222120222222122012220222222022221022020022222220222022010220111222112202221120100002221220120021010222022220222212222002102200221022110221012221222022222210222202202102200222122102220212121122222201222202121221021212111212220100010012221211122020100221121201122212110020121221121120001122222021220122222110202202121112002222222012221002022022221201222112112221102212222202222112011002201000120121102222222220212222221202000210220221121010022020201222222002202212001112002222122002221122220222221202222112221221020202000202222020121112210210122120212220221202012222220020011202020220112100212020220122222012202212000022122022122102222122120022220210222002220222011202210222221211201202212111122122221220122211022202211121202200121222212010122121210022222121202202100202101222122202221112120022222200222122100220111202211212220120110102221201121022220222022220202222122200212221121020011011012021222222222220222202001112000022022022222102020022220222222122122221111202212212221102100202202110022120022222121202002202200210111222222121111121202020201122222120222222212122202022122222221112121022221221222002220220222222201212222211010002220220122220020221021222012202202220211222120021001112122020220222222100222212011012121012122222220122222122220200222212021220212012212222222200012122211112222122000220221221021222220202012201211021211211002220212222222212202212212222110022122202220112222022220211222112011221001122002202222220122012211201122121210220120211012202212011221211020121101000022122222122222012222222212212221212022202222202021122222221222002012221211222202202220120222112210101120221102220021212221222220011110201220121100010122022221022222112212212001102221202122102222102021022222202222022210220220212202212222021011212222020220022021222020201110212001120122212001121202110122221212022222221212212012011011112022202220012220222220221222212021221102102212222221101211002222211122221022220021202102202222021211212012221011020102020222122222101222212111111210212022122220112110222220221222002202220001202212212221220210122210021020122220220022200010202222112021210002120001102202022220122222122202202211002001222222112221122201022220220222102212221220202112212220012120222212021120020022221022211201212221222202202200222202000112021210022222101222012022202102012222002222102022012220220222222020221110222212212221122222012222010221122021222220220110212001111210222022120021012222022200022222000200010220100212112022022220112221202220220222112212222200112110202222022221012210111100121020220121222020022001102202201010220122200202120202022222011220001220121000022222002220202121222120220222112112222100112221212221210210022212020011220202220022221010112222210212202111122202121122222222122222220210222120210200202222212221201122022021221222100021220200102220202221012112212211201121122221211121212002212100211022221101120221002202111201222222111222222021121221002022022222211221102021220222101022220101022121212222210220022202010221220001222022211102112221012010200002022022011212021201122222122210011121220001202122122221010101102120210222210121221012012211212220102001022210102201120012212022011201112022012222220122020100221222011021122222222210001221002122012122112220122220212221202222202112221121111112202222212120012222200220121222221020022112112212210011212200002211112122200222122222122222100022202200222222012221120220222020220222121102220021022200212221112011012210212100020010220220100021112020121001222000110221012112100201122222100222122122201110012222212221221122022120220222212201221200022010222220100120012212200210121202210120001111022202010010210221012212200112102000222222112222122220111212102122122221222120222020210222100020211012000121202222202021202120001122022011202022221002122120100020212022000021001102122222022222020211012212102210112022122220011201220221200222010000211201211221212220211200012201100101120122210022100122222101200002210122200201120022122021122222210222020011210010222122122221001011202221210222111102202122100220222220100020112101200221121222200220012022212022210221211120100220022202120122022222200202001122212010222222102220210020110220201222002112222012200010212222121202202211020200221220220222002102202220000111212101002112111102101222222222020221120201101220002222112221210001221021210222102120221112201121212222220022222101011121122200222122110022022121202002210002211012110102110020222222110221001100220021102022202221110102121220200222020212211101012112202221200211212112201201021001222022012021222010000221220010222012220012020201222222011220100111020202222022202222100012102220211222100111210012020022222221000112022001201122020021201222112122122012100200220210100211222222222212022222202222112111120101002022012220112022022021201222021021201211102021222220112112112211020222200100210222120020112202100002022001212122120022012010022022122202000201111001022222102221101212122020220222212002201112201211212220011020202010210220021102202220101212202110020200001011210220010122022001022222022222111210200100222122212222212202022222220222212220220220221211022221000212202121010120112211211020201001212202211000221112100122011202012202122022112200220201212011112022212222121212101220212222121002200102200222022110212210021212111120200110002102100002120001211101220211022221012021211101111001110020002010110001220000000000010122211201020000101211002111121122220 \ No newline at end of file diff --git a/src/day07.rs b/src/day07.rs index 77d8c48..e69de29 100644 --- a/src/day07.rs +++ b/src/day07.rs @@ -1 +0,0 @@ -fn fun() { } \ No newline at end of file diff --git a/src/day08.rs b/src/day08.rs index e69de29..074a632 100644 --- a/src/day08.rs +++ b/src/day08.rs @@ -0,0 +1,52 @@ +pub fn read_from_string(raw: &str) -> Vec { + raw.chars().map(|c| c.to_digit(10).unwrap() as u8).collect::>() +} + +pub fn decode_image(digits: &[u8], width: usize, height: usize) -> Vec> { + let nb_pixel = width * height; + let nb_layers = digits.len() / nb_pixel; + let mut layers: Vec> = Vec::new(); + for i in 0 .. nb_layers { + let layer: Vec = Vec::from(&digits[i * nb_pixel .. (i+1) * nb_pixel]); + layers.push(layer); + } + layers +} + +pub fn layer_with_fewer_0(layers: &[Vec]) -> &Vec { + let mut min = std::u32::MAX; + let mut layer_min = 0; + for i in 0 .. layers.len() { + let sum = layers[i].iter().fold(0, |sum, pix| if *pix == 0u8 { sum + 1 } else { sum }); + if sum < min { + min = sum; + layer_min = i; + } + } + &layers[layer_min] +} + +pub fn one_digits_times_two_digits(layer: &[u8]) -> i32 { + let (nb_1, nb_2) = + layer + .iter() + .fold( + (0, 0), + |(nb_1, nb_2), pix| + match *pix { 1u8 => (nb_1 + 1, nb_2), 2u8 => (nb_1, nb_2 + 1), _ => (nb_1, nb_2)} + ); + nb_1 * nb_2 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1() { + let raw = read_from_string("123456789012"); + let layers = decode_image(&raw, 3, 2); + let layer = layer_with_fewer_0(&layers[..]); + assert_eq!(one_digits_times_two_digits(layer), 1); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2c96367..470327b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,11 +53,16 @@ fn day07() -> String { } fn day08() -> String { - format!("") + let img = fs::read_to_string("data/day08.input").unwrap(); + + let raw = day08::read_from_string(&img); + let layers = day08::decode_image(&raw, 25, 6); + let layer = day08::layer_with_fewer_0(&layers[..]); + format!("part1: {}, part2: {}", day08::one_digits_times_two_digits(layer), 1) } fn day09() -> String { - let code = common::read_list_of_numbers("data/day09.input", ","); + let code = common::read_list_of_numbers::<&str, i64>("data/day09.input", ","); format!("part1: {:?}, part2: {:?}", day09::execute_op_code(&code, &[1]), day09::execute_op_code(&code, &[2])) } -- 2.45.2