1
0
Fork 0

day 12 solution

main
Andrew Coleman 2018-12-18 17:08:23 -05:00
parent 3f9da45a71
commit 8fedd15dc8
4 changed files with 229 additions and 0 deletions

95
2018/day12/Cargo.lock generated Normal file
View File

@ -0,0 +1,95 @@
[[package]]
name = "aho-corasick"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cfg-if"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "day12"
version = "0.1.0"
dependencies = [
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ucd-util"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "utf8-ranges"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
"checksum libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)" = "2d2857ec59fadc0773853c664d2d18e7198e83883e7060b63c924cb077bd5c74"
"checksum memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "db4c41318937f6e76648f42826b1d9ade5c09cafb5aef7e351240a70f39206e9"
"checksum regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "37e7cbbd370869ce2e8dff25c7018702d10b21a20ef7135316f8daecd6c25b7f"
"checksum regex-syntax 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4e47a2ed29da7a9e1960e1639e7a982e6edc6d49be308a3b02daf511504a16d1"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86"
"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737"
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"

8
2018/day12/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day12"
version = "0.1.0"
authors = ["Andrew Coleman <penguincoder@gmail.com>"]
edition = "2018"
[dependencies]
regex = "1"

34
2018/day12/input Normal file
View File

@ -0,0 +1,34 @@
initial state: ##.#...#.#.#....###.#.#....##.#...##.##.###..#.##.###..####.#..##..#.##..#.......####.#.#..#....##.#
#.#.# => #
..#.# => .
.#.## => #
.##.. => .
##... => #
##..# => #
#.##. => #
.#..# => #
.#### => .
....# => .
#.... => .
#.### => .
###.# => #
.#.#. => .
#...# => .
.#... => #
##.#. => #
#..## => #
..##. => .
####. => #
.###. => .
##### => .
#.#.. => .
...#. => .
..#.. => .
###.. => #
#..#. => .
.##.# => .
..... => .
##.## => #
..### => #
...## => #

92
2018/day12/src/main.rs Normal file
View File

@ -0,0 +1,92 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::iter::Iterator;
const MAP_SIZE: usize = 500;
const MAP_ZERO_INDEX: usize = 200;
fn get_score(state: &[char; MAP_SIZE]) -> i64 {
let mut score = 0;
for i in 0..MAP_SIZE {
if state[i] == '#' {
score += (i - MAP_ZERO_INDEX) as i64;
}
}
score
}
fn run_game(initial_state: String, states: HashMap<String, char>, generations: i64) -> (i64, [i64; MAP_SIZE]) {
let mut last_state = ['.'; MAP_SIZE];
for (i, c) in initial_state.char_indices() {
if c == '#' {
last_state[MAP_ZERO_INDEX + i] = '#';
}
}
let mut scores = [0i64; MAP_SIZE];
for gen in 0..generations {
let mut next_state = ['.'; MAP_SIZE];
for i in 2..(MAP_SIZE - 2) {
let mut key = String::new();
for j in i-2..=i+2 {
key.push(last_state[j]);
}
if let Some(dest_state) = states.get(&key) {
next_state[i] = *dest_state;
}
}
last_state = next_state;
scores[gen as usize] = get_score(&last_state);
}
(scores[generations as usize - 1], scores)
}
fn find_big_score(initial_state: String, states: HashMap<String, char>, iterations: i64) -> i64 {
let (_, scores) = run_game(initial_state, states, MAP_SIZE as i64);
for i in 0..(MAP_SIZE - 3) {
let diff1 = scores[i + 1] - scores[i];
let diff2 = scores[i + 2] - scores[i + 1];
let diff3 = scores[i + 3] - scores[i + 2];
if diff1 == diff2 {
if diff2 == diff3 {
let sum = ((iterations - i as i64 - 1) * diff1) + scores[i];
println!("({} - {}) * {} + {} = {}", iterations - 1, i as i64, diff1, scores[i], sum);
return sum;
}
}
}
-1
}
fn main() {
let mut lines: Vec<String> = BufReader::new(File::open("input").unwrap())
.lines()
.map(|line| line.unwrap())
.collect();
let mut default_state = lines.remove(0);
default_state.replace_range(0..15, "");
lines.remove(0);
let mut states: HashMap<String, char> = HashMap::new();
for line in lines.iter() {
let mut chars = line.chars();
let mut state = String::with_capacity(5);
state.push(chars.nth(0).unwrap());
state.push(chars.nth(0).unwrap());
state.push(chars.nth(0).unwrap());
state.push(chars.nth(0).unwrap());
state.push(chars.nth(0).unwrap());
let dest = chars.nth(4).unwrap();
states.insert(state, dest);
}
let (part1score, _) = run_game(default_state.clone(), states.clone(), 20);
println!("part 1 score after 20 generations: {}", part1score);
let iterations = 50000000000;
let part2score = find_big_score(default_state, states, iterations);
println!("part 2 score after {} is {}", iterations, part2score);
}