summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--20/Cargo.toml11
-rw-r--r--20/input102
-rw-r--r--20/small136
-rw-r--r--20/src/main.rs128
-rw-r--r--20/tests/main.rs19
-rw-r--r--Cargo.lock8
-rw-r--r--Cargo.toml1
7 files changed, 405 insertions, 0 deletions
diff --git a/20/Cargo.toml b/20/Cargo.toml
new file mode 100644
index 0000000..21adb16
--- /dev/null
+++ b/20/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "advent_20"
+version = "0.1.0"
+authors = ["Irene Knapp <ireneista@gmail.com>"]
+edition = "2018"
+
+[dependencies]
+advent_lib = { path = "../lib" }
+
+[dev-dependencies]
+assert_cmd = "0.10"
diff --git a/20/input b/20/input
new file mode 100644
index 0000000..0b3fda0
--- /dev/null
+++ b/20/input
@@ -0,0 +1,102 @@
+#.#.##..#..#..###.#.#....#.########.#.##.#..#.###..###.##.#.##.#.#.....#..##.#.#..###.###.######..#.#..#######.#..#....####..###.####.###.#.#######.#...#...#.##.###..###..##.#.#.###........##.#.....#.##.#.####...#...#.#..###.#.#...#....#...####..#.########.#...#.####.#####..#.#.###......#.##...###..##..#.#..#....#..###.#.##.....##.#####..##.####.#.###....##.###...#.##....##.#..#.#..#..#.##...#.##..#####.####.#.##...##...##...#.##.#.#.####..##...#.....#......#.#......#..###..#..##..##.###..#####..#..##.#..#.
+
+.....#..#...###..##.#.#..#..#......#.##.###.#...#.#...#####......#...#.......###.##.#.#####.##....#.
+....#.###..#...#.#..#..#..#.#.#.##.##..###......####..#.##......###...#....##....#.##.###.##..#...##
+#.#..###.#.#####..##...#..####...#####...#.....#..##...######....#......#..#.###.#....##..#.##..##..
+....######.#.#.#.##.#..#.###.#....#..#..##..#.......##...###.##..#####..##.#..#.#..##....####.....#.
+#...#..#...######.#.#....#.##...##.##....#.#..##.#.##.#.##.#.##..#..#.#....#..##.#..####.##.##.#.#.#
+..##......#.####.#..#...#.#.#..#.#...#..#.####.###.##...##...#.##.###.#.##.###...#...###.#..##...###
+#.###...#....##.#..##..#..#.###...#..#.##...#......###..#....#.##.####.....###...#.#..##..#...#.#...
+##..##.##.....###.###...#.###...#..#...#######.####.##.#.###.###...###.....##.####.##..##.#.#.###...
+.####.#..#..#..#.##...##....##..#.##..#.....##..#......#.#..##..####.#.##..###.###.##....#########..
+#.##.##.#.###.#..#.#.##.##.#..##....##..#..#.#..####..#.#.#.#...#.#....##.###.###.#####.#######...#.
+.##.#.#.#.##...##.##..##..#...#..#.#...########......##.###..##.##.##..##.##.....#.#..#..###..###..#
+##.#..##..#..#..##....##....#.##.#..##.#.###.##.##.##..#####.##..###.#.#.#.##.#########.##....#.###.
+##.#.#####..##.##.####...#.#.##....##.##.#..##.##.##.#..######......#.#.#......##.##.###.#....##.#..
+.#.#...##.###..#...#.######.###.#...#.##.#.....###.#.#..##.#.#..#...###.#.#.#.....##......####..##.#
+####..#..#.#...........###.#..###.#...##..##.##.#.#........####....##.##.....#.####..#..##.##.#.#..#
+..#.##.###..###.#.#.##.#####.....##.#.....#.####.#......##....#.###.#####...#.##.#.#####.#...###....
+####.#.#..####.#.#.#.###....###.....####.##.......#...###..##.#.#.#.##..#.#.##.#..##...##.....##..##
+.#....##.###.#.##.#.#....#.#####..##...#.##....##.#.##.##..#.#...#.##.####.#.#.####.#..##.....######
+#.#..####..#.#.###.#.##...#####..#######..###...##..###.#.###....#.###..#.#...#.#..#.##.##...#....##
+#.....##.#.#.##...###........#..##......#.....###...##...##..#.#..#.####.#.##.#.#..#####...#..#.#..#
+.##.###...#...###..#.##########.##..#...###..........#.###.####.#.#####....####.#..###.......##.#.#.
+#.#....#.####.#..####...#.#.#.###.#..#..##...#.##.##.##.#..#...#..#.#.#...#.###.##.#.#.#.####.##.##.
+.##....#..#..###.###..##....##.....####.##.##......#.######.....##..###..#.#..#..##.#.....#.####..##
+..#..##.##.######...##..##...#...#..#....#..##.....####..#..#######.#...#...#..#..#...###.#.##.#..##
+#..###.##.#.#...##.#.##.#..###..##.###.#..##..#.###....#..#..##..##.#..##..##......#.##....#.###.##.
+...##.###..##.....##...##.#....###...#..##.##.#####..#.###.##..#..#.#.##...##.#.####.##..####...#.#.
+#####...#.#.##...##.....#......###..#######...###...###...##.##...###....#.....##....####...#...#..#
+#.#.#....##.#..#...#.##.###..#.#..#.##..###.#.#.#...##...##..#......#.######..###.........#######.#.
+.#.#.#.######..#...#..##.....###.......#..###.##..##..#..#..#.....##.#...#....##.######.#..#.######.
+#.#.#.#..##.##.#.##..##.#..#..##.#.....##...#.#.#.#..#...#..#.#..#..#...##.##..#..#.###..#.##.#..#.#
+#.#..###..#.....###..#.#..#.####...#.#####..##.##.###.#...#....#####......#.#..#.###..####.#..##.#..
+....###.###.#...#..#...#.###.....####..###.#.............##....#.....##.###.#.#.#.###.###.##.##.###.
+...#.#...##.###....#....##.#.#.##.#..#.#...#.##.#.#.#..#.#....##.###.#...#..##.#..#..#..#...#.##.###
+.#..#.####.#####..###....#.##.#..#...###..##.#.###.###..##.#####..###...#######.....##.#..##..#.....
+.##..##.#.#.#.....#.#...##.###.#.#.##..#.#.#.#.#...##.#.#..####....#....#.######.####.##.#...##..#..
+##.#...###.##...###.##.##....##..##.##...#####..###.#..##.###..#..#....######.###...###.#.####..#...
+..####.#.#..####....#..#......#.##.#..##..#.####.#.##.##.##.#####..#.#.#.....#.#######..###.#.#.##.#
+#.###.##.#.....####.###.#..##..#.#.##....#.#####.##.#...#..#.##.#..####.##..##........###.####.##..#
+....###....#..#.##..##..#.####...#..####.#.##.........##....#.#......#.###.###..#.#.####.##..#.###..
+.##.#........#.##.#..#.#.###.#########...##.##.#..#.#.#.#.##...####..#...###.#.#...#.#.##..##..#####
+####.###.##.#.######..##.#.##.....##.##.####....###.#.#..#.....##.#..#....#.#...#...#.##.#..####.#..
+###.....###.###....#..#...#.#..#.#.###.#.#..##.###.###...###.###.#...#.#.##.#..#..##.#..#####.#.#.##
+.#...#.########..######.#..##.#...##..#.....#..#....#....######.#..#..#.###.#.#..###.##.#.#.#...##..
+#.##.##.#...#....#.######...##.#.#.###......####.###.######.#.#....####.####..##.#.#..###.##..##....
+##.##..####.#.....#....#..###...##.####..###..#####..#......####...#...#.#.##..###..#..#....#...##.#
+#...####.##.#####..##..#.##.#....#######..##.##.#####...###..######..##.###..#.#.#.#..#..#..##..##.#
+#####..####..####.##..###.##...####.#.##.######..#..#.##.#.##..#..#.##....##....####..#######.#.#.##
+......#.#..##.#.###......#..#.#..#..##.#.#...#.#.....###..##.##.###..#.#...#..##.###...#...###.#.#.#
+....##.#..#....###...#.#....##.#...#.###.#######.#....#...#..#####..#.#..#.#.#.##...##.#.#.###.#.###
+..###.#.#.#.##...#####.....##..#.###..##.#......#..##.##...##..#....#.####.##.#.##..######..#.#.#..#
+#....##.#....####......##...##.##....##..######...##...####..#.##...###.##.##.#..#####..#.##.##.#..#
+##.#....##.#...##...#.##..#####.#.#...#.#.#.######.#####...###.....###.####.######..##.#.#..#.#..###
+.#.#....##..#.####.#...####...#.##.####.##...###.###.##...###.#..#..##.##....#.#..#.#.##.#.#.###.##.
+.##.#..#.#.#..#.#######....#.#....#####...#..######..##......#.#####...###.##.#.....###.#.##.#....#.
+.####..##...#.##...#.#.#.....#..##.#....####......#....####.###...##.####.###..#.#..#.#.###.#.######
+.#.#.#.##.....#.###.#..#....##.##..###..###..##.#.#..#.#.#..#.#####.#.#####.##.##.##...##.##.###...#
+.#.#.##.....#.###.#.##.....##.#####..##..##.###....#.....##.#..#.#.##.....#.#.####....#####..#####..
+##.#.#.####.#.###..#...#.##.######.#...##.###..###.#.##.#..#.#######.#.##.#.#.###..###.#.#.#..#.###.
+#.##.#.#...#...#######...###.#..###.###..#.###.......##..#.##....##.##.#####..##...##.###..##.##.#..
+##...#..#.########.#.#.#####.#...#.##.#####.#.##..#.#.#.##.....#.#..#.###.#.....#..#...###..###..#.#
+###..##...#.##....#.#..#.#....#..##.###...##.####.....#..####.#.#.....#.##..##....##...#.##.#.#.##.#
+#.##.###..####.##....######..#....#.....####.#.#..###.#.###.#......##......#..####...###..######..#.
+#..####.#..#.#####..##.#.#...#######.#.###..#...######.#....#.#.####.##...#.#####..#......##..#.##..
+..####.###....#...##......########..###.#.#.#.#####.#.###.#.##.###.#..##..#.####..##..#.##.#.###...#
+######.##.#..#.#..#..##.#####..#.##....#.#..#......##..#..#..#.###.#########.####.#.##.##..#########
+###.#.##..#..####..#...###.#.####..#.##.##..#..##.#.#.#####.#...######.....#..#.##.##.##.#.#..###.#.
+##.####.#..#.###..#..#.#...##.#...#.#...###.......#..###...#.#####..#####.....##.....###..##.#....##
+#####.#.#..###.##.....#.##..#.#..#.##...#.....#....#....###########.###.....#..##.#.####.#.#####.##.
+.###.#......#.#.#.####..#.#.#####.#...##..##.##..###...##.##.##.####.#.##.#.##......####...#.#######
+#.##.#.##.#.####..#.#.####..#...#.#.#.....#..##...####.#..###...#...#..#..#..#.##.#####..#.###.##..#
+.#..###.#...###.#.##...#.##...####.#.#..####.##.#..##.....#.##.#......##..####.##....##....#..#...##
+####.#..#.#..###.##.#####.##..##.####.#.##..#..####....##...#..#..#.#.....##.###............#......#
+....##.#####...###..#...#...#.#.##...##..###.##.#...#.#...##...##....#..###..##....##.#.#..#.#..#..#
+####.#.###...##...#..#...#.#.#..##..##.###..##.##.###.....##.#.###..####.##.#####.#######..##..#....
+##..###..#.....#..#...#.#..#..##...##..##.#.##..#.###########...###..#.##...##...###.#######.##.#...
+#..#.#..#.#..#....#.#...#.#..##.#####..#.##..##...####...#.#....#....###.####.####.#..##..#.###....#
+..#..#.#.#.#.##.#.##.#.##..#.#.#...#...####.####..#..####...##..#.#..#.###...#..#.#....##....#..####
+#.#.#...#..........#.##..#.#..#...#.#..#.#.#.#####...##.#.#####.######......#.#.#...#...###.#.......
+..###.#.#.....##..##.###...#####....###.#..###..#....#.#####..##.##.#..#.....##..####.....##.#..#...
+#.#.##...####......#...##..#.#.#...#.....##...#..............#...##.#.#.#...#...#.###.##.#..###.#...
+.###...#..#.#....#.....#.##..#.....#.#...###.#...##.#....##.##.#..#..##.#....##.##..####....#.#.##..
+..#.####..##.#..#....###.#.....###.#.#.#...##.#.#....###...#...#.#..###.#.##.##.###.#..####.########
+.#.##...#.#..#.#.##...#.#..##......#.#..#...#.#.###.#..###.#####.#.....####...#..##.#...#.#.#..###..
+.####.#..##.#####.##.###..###.##..##..##..#..###...##.##.....###..#.##..###...###...#...######..##..
+##.##..########.#########.######.#.###..##.#.####...#....#.###.###.#.###.#...#......##..#.#######.##
+..#.##.####....####.#####.##.##..#...#####.#.....##........###..#...#..#.###..#...#.#########.###.#.
+..#.#..####...##.##...#.##.#...##........###.#......##.####...###..#..#..##.#.#...#.########..#.....
+#....##....###..#..#####...#..#..#.##..#######..#...#.##.#...#..#..#...##.##.##.#....###..###...#.##
+###..##########.#.##.....##.##..#....#.....##......####...#..#.#.....##.##..#.#.#.######..###....#..
+..#.#.##..#..#.##..#..#.#####.##..#.####...#...####..####.##...##...##.#.....#.##..##.##...#..#.#..#
+.##.#....#.#..###.####.#...##.#.....###.#...#..#.###...####.##.....####.#..#..#.#..######.##..##...#
+..##.#...##..#.#..##.#.#..###...#.##..#..###..###.#.###...#.......###.##.###..##.#.#..#.#..#.....#..
+###...###.###.#..#...#.##.....#.#..#...#####....#.##.#..##.#..#.#.#..##..##...#..###.###.##.####.###
+##.####..##.###.##.##.#...##.##.#.....#.#.###...#....#.#.####....#.##..#....#..#..#..###....##.#####
+#..##.#..#..####.#.##.#.##..#..##......##....#####.#.#......##.#.#######.#######.####..#...#.#...#..
+####..........#..##....##.######.#.#.##.#..#.####....##......##..######.###.#..#.##..#.##...#####...
+.#####.#.##.####...##.#...########..##.#..##.##.##.####.#...#.#.#.#..#...#......#....#######...##..#
+#.##.####..##.........#.#.#...#.###..##...###....########.#.##..#.#....##.##.##...##..#.....#..#....
+.#...#####.#....#.##....#...####..####.#.##...##....#.#.##..#.##...#....##..#..######...#####.###.#.
+.#...##.....#.#...#...#.##.##..##.#.##.###.###.##.#..##.#.#.#.#..#.#.....####..#.....###.#.#########
diff --git a/20/small b/20/small
new file mode 100644
index 0000000..4e496e9
--- /dev/null
+++ b/20/small
@@ -0,0 +1,136 @@
+--- scanner 0 ---
+404,-588,-901
+528,-643,409
+-838,591,734
+390,-675,-793
+-537,-823,-458
+-485,-357,347
+-345,-311,381
+-661,-816,-575
+-876,649,763
+-618,-824,-621
+553,345,-567
+474,580,667
+-447,-329,318
+-584,868,-557
+544,-627,-890
+564,392,-477
+455,729,728
+-892,524,684
+-689,845,-530
+423,-701,434
+7,-33,-71
+630,319,-379
+443,580,662
+-789,900,-551
+459,-707,401
+
+--- scanner 1 ---
+686,422,578
+605,423,415
+515,917,-361
+-336,658,858
+95,138,22
+-476,619,847
+-340,-569,-846
+567,-361,727
+-460,603,-452
+669,-402,600
+729,430,532
+-500,-761,534
+-322,571,750
+-466,-666,-811
+-429,-592,574
+-355,545,-477
+703,-491,-529
+-328,-685,520
+413,935,-424
+-391,539,-444
+586,-435,557
+-364,-763,-893
+807,-499,-711
+755,-354,-619
+553,889,-390
+
+--- scanner 2 ---
+649,640,665
+682,-795,504
+-784,533,-524
+-644,584,-595
+-588,-843,648
+-30,6,44
+-674,560,763
+500,723,-460
+609,671,-379
+-555,-800,653
+-675,-892,-343
+697,-426,-610
+578,704,681
+493,664,-388
+-671,-858,530
+-667,343,800
+571,-461,-707
+-138,-166,112
+-889,563,-600
+646,-828,498
+640,759,510
+-630,509,768
+-681,-892,-333
+673,-379,-804
+-742,-814,-386
+577,-820,562
+
+--- scanner 3 ---
+-589,542,597
+605,-692,669
+-500,565,-823
+-660,373,557
+-458,-679,-417
+-488,449,543
+-626,468,-788
+338,-750,-386
+528,-832,-391
+562,-778,733
+-938,-730,414
+543,643,-506
+-524,371,-870
+407,773,750
+-104,29,83
+378,-903,-323
+-778,-728,485
+426,699,580
+-438,-605,-362
+-469,-447,-387
+509,732,623
+647,635,-688
+-868,-804,481
+614,-800,639
+595,780,-596
+
+--- scanner 4 ---
+727,592,562
+-293,-554,779
+441,611,-461
+-714,465,-776
+-743,427,-804
+-660,-479,-426
+832,-632,460
+927,-485,-438
+408,393,-506
+466,436,-512
+110,16,151
+-258,-428,682
+-393,719,612
+-211,-452,876
+808,-476,-593
+-575,615,604
+-485,667,467
+-680,325,-822
+-627,-443,-432
+872,-547,-609
+833,512,582
+807,604,487
+839,-516,451
+891,-625,532
+-652,-548,-490
+30,-46,-14
diff --git a/20/src/main.rs b/20/src/main.rs
new file mode 100644
index 0000000..1b4fc51
--- /dev/null
+++ b/20/src/main.rs
@@ -0,0 +1,128 @@
+use advent_lib::prelude::*;
+
+
+fn main() -> Result<()> {
+  let mut args = std::env::args();
+  if args.len() != 2 {
+    eprintln!("Usage: advent input");
+  }
+  let _ = args.next();
+  let filename = args.next().unwrap();
+
+  let input = advent_lib::read_lines_file(&filename)?;
+
+  let mut iterator = input.iter();
+
+  let mut dictionary = Vec::new();
+  for c in iterator.next().unwrap().chars() {
+    dictionary.push(match c {
+      '#' => 1,
+      '.' => 0,
+      _ => panic!("unexpected character"),
+    });
+  }
+
+  let _ = iterator.next();
+
+  let mut image = Vec::new();
+  for line in iterator {
+    let mut row = Vec::new();
+    for c in line.chars() {
+      row.push(match c {
+        '#' => 1,
+        '.' => 0,
+        _ => panic!("unexpected character"),
+      });
+    }
+    image.push(row);
+  }
+
+  let mut background = 0;
+  for _ in 0 .. 2 {
+    let (new_image, new_background) =
+      iterate(&image, background, &dictionary);
+    image = new_image;
+    background = new_background;
+  }
+  println!("{}", count_pixels(&image));
+
+  for _ in 2 .. 50 {
+    let (new_image, new_background) =
+      iterate(&image, background, &dictionary);
+    image = new_image;
+    background = new_background;
+  }
+  println!("{}", count_pixels(&image));
+
+  Ok(())
+}
+
+
+fn iterate(input: &Vec<Vec<u8>>, background: u8, dictionary: &Vec<u8>)
+  -> (Vec<Vec<u8>>, u8)
+{
+  let height = input.len() as i64;
+  let width = input[0].len() as i64;
+
+  let mut output = Vec::new();
+  for y in -2 .. height+2 {
+    let mut row = Vec::new();
+    for x in -2 .. width+2 {
+      let mut index: usize = 0;
+
+      for offset_y in -1 .. 2 {
+        for offset_x in -1 .. 2 {
+          let computed_y: i64 = y + offset_y;
+          let computed_x: i64 = x + offset_x;
+
+
+          index *= 2;
+          if (computed_y >= 0) && (computed_y < height)
+            && (computed_x >= 0) && (computed_x < width)
+          {
+            index += input[computed_y as usize][computed_x as usize] as usize;
+          } else {
+            index += background as usize;
+          }
+        }
+      }
+
+      row.push(dictionary[index]);
+    }
+    output.push(row);
+  }
+
+  let output_background = dictionary[background as usize];
+
+  (output, output_background)
+}
+
+
+fn count_pixels(image: &Vec<Vec<u8>>) -> i64 {
+  let mut count = 0;
+
+  for row in image {
+    for cell in row {
+      count += *cell as i64;
+    }
+  }
+
+  count
+}
+
+
+#[allow(dead_code)]
+fn debug_image(image: &Vec<Vec<u8>>) {
+  for row in image {
+    for cell in row {
+      if *cell == 1 {
+        print!("#");
+      } else {
+        print!(".");
+      }
+    }
+    println!("");
+  }
+  println!("");
+}
+
diff --git a/20/tests/main.rs b/20/tests/main.rs
new file mode 100644
index 0000000..826e079
--- /dev/null
+++ b/20/tests/main.rs
@@ -0,0 +1,19 @@
+use assert_cmd::prelude::*;
+//use predicates::prelude::*;
+use std::process::Command;
+
+
+#[test]
+fn personal_input() -> Result<(), Box<dyn std::error::Error>> {
+  let mut command = Command::cargo_bin("advent_20")?;
+
+  command.arg("small");
+  command.assert().success().stdout(
+      "5573\n\
+      20097\n");
+
+
+
+  Ok(())
+}
+
diff --git a/Cargo.lock b/Cargo.lock
index 747dfba..12d658b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -155,6 +155,14 @@ dependencies = [
 ]
 
 [[package]]
+name = "advent_20"
+version = "0.1.0"
+dependencies = [
+ "advent_lib",
+ "assert_cmd",
+]
+
+[[package]]
 name = "advent_lib"
 version = "0.1.0"
 dependencies = [
diff --git a/Cargo.toml b/Cargo.toml
index 8e0da29..154b354 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -20,4 +20,5 @@ members = [
   "17",
   "18",
   "19",
+  "20",
 ]