diff options
| author | Reiner Herrmann <reiner@reiner-h.de> | 2019-12-19 11:26:36 +0100 |
|---|---|---|
| committer | Reiner Herrmann <reiner@reiner-h.de> | 2019-12-19 11:26:36 +0100 |
| commit | 0b2fa13234eb181b3cba2e39db4f177d2d6c0120 (patch) | |
| tree | 8ead3b2a76a894862809ab4c2a7194ec6b04332f | |
| parent | 6f57583eddd8ae302ecaf7c00d650a570255c056 (diff) | |
day19
| -rw-r--r-- | input19 | 1 | ||||
| -rw-r--r-- | src/main.rs | 74 |
2 files changed, 72 insertions, 3 deletions
@@ -0,0 +1 @@ +109,424,203,1,21101,11,0,0,1105,1,282,21101,0,18,0,1106,0,259,1202,1,1,221,203,1,21101,0,31,0,1105,1,282,21102,1,38,0,1106,0,259,20101,0,23,2,22102,1,1,3,21101,1,0,1,21101,0,57,0,1106,0,303,1202,1,1,222,21002,221,1,3,21001,221,0,2,21102,1,259,1,21101,80,0,0,1105,1,225,21102,1,117,2,21102,1,91,0,1105,1,303,1202,1,1,223,20102,1,222,4,21101,0,259,3,21101,0,225,2,21101,225,0,1,21101,118,0,0,1105,1,225,21001,222,0,3,21101,20,0,2,21102,1,133,0,1105,1,303,21202,1,-1,1,22001,223,1,1,21101,0,148,0,1106,0,259,2101,0,1,223,20102,1,221,4,21001,222,0,3,21101,0,16,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21102,195,1,0,105,1,108,20207,1,223,2,21002,23,1,1,21102,-1,1,3,21101,0,214,0,1105,1,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,1201,-4,0,249,22102,1,-3,1,22101,0,-2,2,21202,-1,1,3,21102,1,250,0,1106,0,225,22102,1,1,-4,109,-5,2105,1,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2106,0,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,21202,-2,1,-2,109,-3,2105,1,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,21201,-2,0,3,21101,343,0,0,1105,1,303,1105,1,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,21201,-4,0,1,21101,0,384,0,1105,1,303,1105,1,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,22101,0,1,-4,109,-5,2105,1,0 diff --git a/src/main.rs b/src/main.rs index 439f456..93dbfaa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,7 @@ use std::fs::{self, File}; use std::io::prelude::*; use std::cmp; use std::cmp::Ordering; -use std::collections::VecDeque; -use std::collections::HashMap; +use std::collections::{VecDeque, HashMap, HashSet}; #[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)] struct Point { @@ -1826,8 +1825,77 @@ fn day17() { println!("17b: {}", output); } +fn check_position(program: &[isize], x: isize, y: isize) -> isize { + let mut computer = IntComputer::new(program); + computer.suspend_on_output = true; + let mut output = 0; + computer.input.push_back(x); + computer.input.push_back(y); + loop { + match computer.run_program() { + ProgramState::SUSPENDED => output = computer.output.pop_front().unwrap(), + ProgramState::HALTED => break, + _ => {} + } + } + output +} + +fn check_square_fits(map: &HashSet<(isize, isize)>, size: isize, x: isize, y: isize) -> bool { + for i in 0..size { + for j in 0..size { + if !map.contains(&(x-j, y-i)) { + return false; + } + } + } + true +} + +fn day19() { + let input = read_file("input19"); + let input = input.trim_end(); + let program : Vec<isize> = input.split(',') + .map(|x| x.parse::<isize>().unwrap()) + .collect(); + + let mut sum = 0; + for x in 0..50 { + for y in 0..50 { + sum += check_position(&program, x, y); + } + } + println!("19a: {}", sum); + + let mut map = HashSet::new(); + let mut tractor_begin = 0; + 'outer: for y in 0..1500 { + let mut was_tractor = false; + for x in tractor_begin..1500 { + let state = check_position(&program, x, y); + if state == 0 { + if was_tractor { + /* any further position on the x axis will be space */ + break; + } + } else { + if !was_tractor { + was_tractor = true; + tractor_begin = x; + } + map.insert((x, y)); + if check_square_fits(&map, 100, x, y) { + println!("19b: {}", (x-99)*10_000 + (y-99)); + break 'outer; + } + } + sum += state; + } + } +} + fn main() { - day17(); + day19(); } #[cfg(test)] |
