1
0
Fork 0

day 9 solution, using deque

main
Andrew Coleman 2018-12-13 18:35:31 -05:00
parent 92cdd760e2
commit 93e8aef6c0
4 changed files with 157 additions and 0 deletions

95
2018/day9/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 = "day9"
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/day9/Cargo.toml Normal file
View File

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

1
2018/day9/input Normal file
View File

@ -0,0 +1 @@
486 players; last marble is worth 70833 points

53
2018/day9/src/main.rs Normal file
View File

@ -0,0 +1,53 @@
extern crate regex;
use regex::Regex;
use std::collections::VecDeque;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::iter::Iterator;
fn play_game_deque(num_players: usize, max_marbles: u64) -> u64 {
let mut game: VecDeque<u64> = VecDeque::with_capacity(max_marbles as usize);
game.push_back(0);
let mut scores = vec![0; num_players];
for current_marble in 1..=max_marbles {
if current_marble % 23 == 0 {
for _ in 0..7 {
let val = game.pop_back().unwrap();
game.push_front(val);
}
let player = (current_marble as usize % num_players) as usize;
let val = game.pop_back().unwrap();
scores[player] += current_marble as u64 + val;
let val2 = game.pop_front().unwrap();
game.push_back(val2);
} else {
let val = game.pop_front().unwrap();
game.push_back(val);
game.push_back(current_marble);
}
}
*scores.iter().max().unwrap()
}
fn main() {
let lines: Vec<String> = BufReader::new(File::open("input").unwrap())
.lines()
.map(|line| line.unwrap())
.collect();
let re = Regex::new(r"^(\d+) players; last marble is worth (\d+) points").unwrap();
for line in lines.iter() {
if re.is_match(line) {
let cap = re.captures(line).unwrap();
let num_players = cap[1].parse::<usize>().unwrap();
let max_marble = cap[2].parse::<u64>().unwrap();
let score = play_game_deque(num_players, max_marble);
println!("{}: {}", line, score);
let new_max_marble = max_marble * 100;
let score2 = play_game_deque(num_players, new_max_marble);
println!("new_max_marble {}: score: {}", new_max_marble, score2);
}
}
}