Browse Source

2016 days 5-7

master
Terrana Ninetailed 2 years ago
parent
commit
e062341287
  1. 1
      2016/Cargo.toml
  2. 572
      2016/resources/day06.input
  3. 2000
      2016/resources/day07.input
  4. 2
      2016/src/day03.rs
  5. 2
      2016/src/day04.rs
  6. 72
      2016/src/day05.rs
  7. 59
      2016/src/day06.rs
  8. 98
      2016/src/day07.rs
  9. 14
      2016/src/main.rs

1
2016/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]
md-5 = "0.9.1"
regex = "1"

572
2016/resources/day06.input

@ -0,0 +1,572 @@
cmezkqgn
nmzrgcft
ydpndcps
zjihhows
kvptxsrx
ubbvugwq
pclcquhl
rtddzpes
gfkylkvo
cpxpjjme
qqntjofm
tnvmqrik
cczmxxag
ikbrgpjh
lpeohbro
sgdidbgw
apjhovfs
miwqgpmr
igkccbxe
dcfpfkdv
neaxgnpr
xjlnhgwz
hbwdbtmt
jaahaztu
xdhkxiwj
kbcnydre
zygzcjxg
pnhlsbyu
gpkfcakg
vlpebsme
fhivcwnn
avscujyu
tckpnxnn
vhtaizda
vghhmhuy
dtzhrwcw
qhbcdaxx
kdoadrvh
yrjzipbd
weqfqmqr
zlkaiefc
zziwfitz
hfdvzpol
opialtmr
wgbarxig
gguytyxk
gwvaqisb
vybedyip
cbcdebwm
twoqbnis
itrspsmt
cqvjpfou
avhpvkbz
xozehrwd
qizmzubk
hpyiulwy
clmrwgdt
uruutjhx
pyvkmpxk
wpjfzzst
hjxjjkup
mdtlnvab
tqwnjufv
nlaxmbxc
nyetqfpn
nmapoequ
aozqvnbx
awuopxxj
jjamjzdr
xsgnpwrv
odpbdulf
nnpddykk
fwkqbeeq
rmpyqcrr
nnrbqymd
advolplo
xfwzojqb
dlxozmgp
mehtypai
qgxmpmza
cyflmzcf
drilfbik
hsrkwohm
lzdcksvs
xtqiuyon
aatvfuvn
tgdwdznm
srlndtlz
kcdtqqov
rjjwcfpr
sqmwnyjj
spfagdkw
ffqrocvz
fdncyaef
doymrkhy
nagivkzc
ylvmvlvo
yqnpiqnx
yqiuccji
swugswxs
wlfcvtms
bplwnlqh
dyqqbiop
ugxdfwnu
actfbdnl
hafvcdjm
uxlvddgb
jimpqraf
oovjqvmc
niixikhh
uamcczvl
iqyhtphk
hmgnaqfa
anptkatn
taslmdqh
hrsdlgth
tidxkojm
bozyplbl
viyiykes
bqttiowc
fdygoexj
yxiqrabo
hoqmzyap
qrdjlssb
kpoknmcl
wmfbbpoz
xyfmwzrc
ekgikzyt
furxwelu
gtfoyquj
xhtkpgnb
pqwfaoeh
kgutwopd
gmsrhxhp
yfriofga
kjulfqdc
anyrvwxv
reuufyff
rhhuhyku
muwxqimh
lmmesfgq
buklvija
nrqemlud
waggxokb
dmmtiifd
kgawgnsa
pvwrwdhz
mboaagdf
tugpycjc
yrrurffl
xnpptcxi
wynqznnj
pecxtzem
qsmjkvvd
gbosyfyx
dckxdlle
oyuucewm
rvzinbwp
bwdsapew
qacnmkst
dunstuov
gfrmztat
psehmndx
krhyzbag
trxayqjv
ddhrarzx
msnjiwaf
znjklkrs
gzhgcuqn
eoivvakl
ekjbelae
oxvbtsmk
mwfqyskr
tihtgxtf
hldkxeuc
nnawdxvy
euemeepz
ibnuhhex
ojwihmnv
cfpezewj
vrxjrwia
wgmyafnj
pnrsmxka
ksuwbzlt
uwkupngv
jdajpsal
tbufcuza
jjgptlxn
hxoulqig
gieqsttk
fwjyxnaq
pmfdifiq
qcgjfmsh
bnzqevtw
zlosluzk
pyfrslkb
ivzxjsgx
wahqmige
uhvsplzs
qaatujkd
taryjkox
jwdwisfv
dtwhlvuv
lwlwbjee
wopsiktn
iojihkrw
pwmqgwpk
kepvgmcd
dqgupbhg
srofdewh
ntijingz
osixtaku
isacbsnl
txtaxccj
uuqanmcw
nsuogfzt
yktybcsy
csqjvxog
rrjygfmc
eftdwemr
uxbswaep
zghswtrf
fhlxbray
julloyea
bsxwmvfv
kzatuvcu
mnymrdpq
idejsnhx
kdbpzapz
tzjefanj
ottzlwxh
mifokhqj
lxxbtzjr
wjcblnsd
siiozsqc
iujapalx
ofsvvyuy
zbgpxvrb
aqbilxlp
ncobthcc
sflihopk
pxwtiwam
nmgzdpyj
nhjhaezr
weihbqyp
pkpnbhxp
dlrelmop
mjbvnjuq
qntmdrey
htiluzbi
fingzxbe
mnekisyu
ynfcmhzd
vdzoljfg
wfmscpvw
efvyjhux
gvfkaxjq
rkmkahxl
vhqijllu
kkjpwxlq
londfadk
ohsxywdq
znstqcbb
qtazxfoi
jdqwiadz
mumicrid
uhwfytgm
srqofgqp
gtlqqspw
kxnkrcln
aycqjkay
yvangrcm
tpokdbwt
hmfqugbw
qoymvotr
icjendxu
uqsvumij
bqkqoeul
riarnbdv
zwlltddu
izcmngof
lawuhjjj
fdtnicju
iizykequ
lwrfolub
rknrbikc
yvogoydm
bogzdkiw
obnhuoxn
lzzpupsk
nuefyzzr
azghigtg
mkyduyug
mnteeioi
yhqbtwyx
eaojxpwy
hbbxehvr
omdkihmb
hbcijcio
settptzw
babyhhhe
cdlexgrs
cwrdtzjk
xvtwjacw
lxeykife
szogbxgb
ggxlgisl
kbmrnfro
ioervjsx
pfkodypz
ojgbokwc
jvykzhzc
cmigvhio
wwiowvyo
igwtrxhe
obawztja
yyazfxks
gfqqttue
czmvgttl
aljlhlyo
zczpqnzb
ruofwgrx
bhemgvlr
yzsulgck
eixzpfkh
cbejkdrs
qcsnnfht
ryvlmbiz
nfswleyf
xtoxoitk
ysfgwpmy
zsnapbrq
olqagygt
zmtyqfvd
ztybusgn
zsydzdnl
fkbvfvsq
gwdjudok
juzbnhfe
apivbufk
ozxgeeqa
yvyvuvxh
kexcesza
gqefjmed
hqyolehg
mluggzqh
gkpjfkhg
bmvxtrci
euyduveo
avwdogys
jnserfgo
iysfpsns
nxilicng
rpclnuwl
anxroxpu
fjmenahn
xngxqxxt
ziwltmcm
rdizrucj
wvvwldvq
blyiqvpw
iklfxllo
txueozfv
wapwemje
bztthavf
fkfejluf
iwynejes
mkwpylhy
pmndxgby
vhgdvrbv
fizshysy
phqddggq
bosaehqz
kwsoncrz
pmaethwo
valgeqbq
rcjuatfg
ryaujqvn
urpgwdyv
gdefrqbu
jcpfzans
eywcyjer
xpkacpyo
xqdukuff
lmbaxfqi
tzvnhfms
osqfwpss
ltgvoipl
bcorqrzk
wgccrykp
aaaoczvn
jpbsehyo
qtfzphwh
bpiiwzib
tnxbnwyg
xruheaca
eoxvahaq
dzhcleaw
vwcgptbp
mmqzjwte
gpxrndsm
kdgwktpb
roqqxgvt
tceymtaf
pkelkvvi
jqfguroe
xbrhyuai
jvbizlbh
hhujmghp
xxtagkzc
pxtzfvsy
vlopcrko
lorhgtfj
eyuzxpjt
jxjbdzrs
jfcuqypt
dcmbqqln
stdmubrl
fkvvwbue
mqqhkoqd
lvmnavnr
gtxksotd
dyjdydhj
rknodxpp
nkrbeqgp
lzzlxjub
hfhycqag
zrhtmjcz
tetkoiki
aeicawds
kvverwcb
vkkmanit
ozzoauql
eqjceipv
vjeajvzj
rfbyfkdt
ayudrwvi
ozlumnku
bbmgldja
dwpjacmb
ddyqbnzl
jlrdfzef
quovmsbh
utposqki
howsfhba
rdddsgwx
fcdtcqni
kbhnvmah
cgpbjquu
qjhmpyff
wxkytidy
ssefidnf
opswmrqz
zhcskfsp
hhkqbfon
uvgdhifc
eoewusji
xjmylrdx
fabeoujy
gzrceopo
fxsivztv
veqxwblf
sacoxlhm
xongcuef
lufmhuoi
juzgavxq
jjwlcfjq
egmnqjqn
ryhlipod
uagzcjur
epjngrwa
fijrzmww
zihnvpgp
zjurrctz
irhnbjjr
mlrfavaa
cokssyim
auwsrcsm
wrkkttyo
cmskryli
mrkpezgq
ehefyaqv
ivsuxdll
gscbkguh
bfxberbd
vihesdxg
vdbxzltv
lkoiranw
qcnefblb
cfftjwud
xqpieetw
crnrywvn
eepxytfc
cacfhgnf
bakhanwy
lsnlnmrj
usaurokx
sjqbyile
lvcgmrte
vesupotm
yeusftiz
clnjmcit
jhexzuyh
wtbiuozi
fsnqljcg
fxretbsa
lsagjnhx
jjknskzr
dllskstv
vgxhdbyw
yryqoqgz
ycilkokz
vfdcsamh
oedmwosl
vzwfymbu
eqrznqgp
fevhvwom
qextbmed
ubdsfkiu
stvuqrka
nmcrshqw
zlfzaxmw
qzcagqcq
djudatbg
usknomtt
busciicd
wyugburo
qblpvrxc
shzawivm
ztgzrklm
ahpxtdmz
obvuhnlj
uihsumey
mircsnyv
ijjhkyjw
dgxmzhgq
rqavgasa
lelkschr
svzzvroa
sevzfvbh
kgzcpbdj
wvctsjcp
kgdrxolj
tlsksbdi
ycqvhidx
epcaeqir
xcrgjgzi
snuvvmmy
cxbxoxvb
leykoxno
ppvysjob
eubrylie
pxspjeqg
xbdesmuq
bfcpktpy
elyounyn
niwhwuak
hukkheui
ueojrjoc
mktpkpsk
uxljxoei
hymwnsrf
sgyywcqt
yznoeeft
puvcmnpe
domsvurc
ukbhxndd
qwlzklcm
qttwpwdc
vxljmley
sjlbsszg
iqobsomn

2000
2016/resources/day07.input

File diff suppressed because it is too large

2
2016/src/day03.rs

@ -19,7 +19,7 @@ pub fn part1() {
}
pub fn part2() {
println!("{}", "Day 3 part 1");
println!("{}", "Day 3 part 2");
let data: Vec<Vec<i32>> = read_input_file().split_terminator('\n').map(|line| line.split_whitespace().map(|s| i32::from_str(s).unwrap()).collect()).collect();
let mut valid_triangles = 0;
let len = data.len();

2
2016/src/day04.rs

@ -5,6 +5,7 @@ use std::collections::HashMap;
use std::str::FromStr;
pub fn part1() {
println!("{}", "Day 4 part 1");
let re: Regex = Regex::new(r"([a-z\-]+)-([0-9]+)\[([a-z]+)]").unwrap();
let mut sectors = 0;
for line in read_input_file("day04.input").split_terminator('\n') {
@ -22,6 +23,7 @@ pub fn part1() {
const Z: u8 = 'z' as u8;
pub fn part2() {
println!("{}", "Day 4 part 2");
let re: Regex = Regex::new(r"([a-z\-]+)-([0-9]+)\[([a-z]+)]").unwrap();
for line in read_input_file("day04.input").split_terminator('\n') {
if let Some(groups) = re.captures(line) {

72
2016/src/day05.rs

@ -0,0 +1,72 @@
use md5::{Digest, Md5};
use std::collections::HashSet;
use std::io::{stdout, Write};
use std::sync::mpsc::channel;
use std::thread;
const INPUT: &str = "uqwqemis";
const THREADS: i32 = 7;
pub fn bothparts() {
println!("{}", "Day 5");
let (sender, receiver) = channel();
for start in 0..THREADS {
let mut i = start;
let send = sender.clone();
thread::spawn(move || {
loop {
let mut hasher = Md5::new();
let mut id = String::new();
id.push_str(INPUT);
id.push_str(i.to_string().as_str());
hasher.update(&id);
let hash = hasher.finalize();
if hash[0] == 0 && hash[1] == 0 && hash[2] & 0xf0 == 0 {
let hex_string: String = hash.iter().map(|b| format!("{:02X}", b)).collect();
println!("{:08X} {}", i, hex_string);
match send.send((i, hash)) {
Ok(_) => {},
Err(_) => {
println!("Thread {} wrapping up", start);
return;
},
}
}
i += THREADS;
}
});
}
let mut results = Vec::new();
let mut indices_filled: HashSet<u8> = HashSet::new();
while indices_filled.len() < 8 {
if let Ok(msg) = receiver.recv() {
stdout().flush().expect("Flush");
results.push(msg);
let index = &msg.1[2] & 0x0f;
//hash[2] & 0x0f, hash[3] & 0xf0 >> 4
if index < 8 {
indices_filled.insert(index);
}
}
}
results.sort();
println!();
println!("Part 1: ");
for r in &results[0..8] {
print!("{:X}", &r.1[2] & 0x0f);
}
println!();
println!("Part 2: ");
indices_filled.clear();
let mut part_2_result = [0;8];
for r in &results {
let index = &r.1[2] & 0x0f;
if index < 8 && !indices_filled.contains(&index) {
part_2_result[index as usize] = (&r.1[3] & 0xf0) >> 4;
indices_filled.insert(index);
}
}
let hex_string: String = part_2_result.iter().map(|b| format!("{:X}", b)).collect();
println!("{}", hex_string);
}

59
2016/src/day06.rs

@ -0,0 +1,59 @@
use crate::file_input::read_input_file;
use std::collections::HashMap;
use std::i32::MAX;
pub fn part1() {
println!("{}", "Day 6 part 1");
let counts = count_letters();
for count_map in counts {
let mut max = 0 as char;
let mut max_count = 0;
for (letter, count) in count_map {
if count > max_count {
max = letter;
max_count = count;
}
}
print!("{}", max);
}
println!();
}
pub fn part2() {
println!("{}", "Day 6 part 2");
let counts = count_letters();
for count_map in counts {
let mut min = 0 as char;
let mut min_count = MAX;
for (letter, count) in count_map {
if count < min_count {
min = letter;
min_count = count;
}
}
print!("{}", min);
}
println!();
}
fn count_letters() -> Vec<HashMap<char, i32>> {
let input = read_input_file("day06.input");
// This assumes that the input file contains only single-byte characters and that all lines are
// the same length.
let line_length = input.find('\n').unwrap();
let mut counts = Vec::new();
for _ in 0..line_length {
counts.push(HashMap::new());
}
for line in input.split_terminator('\n') {
let mut counts_iter = counts.iter_mut();
for c in line.chars() {
let count_map = counts_iter.next().unwrap();
count_map.insert(c, match count_map.get(&c) {
Some(n) => n + 1,
None => 1
});
}
}
counts
}

98
2016/src/day07.rs

@ -0,0 +1,98 @@
use crate::file_input::read_input_file;
use std::collections::HashSet;
pub fn part1() {
println!("{}", "Day 7 part 1");
let mut matching = 0;
for line in read_input_file("day07.input").split_terminator('\n') {
let mut brackets = false;
let mut matched = false;
for chunk in line.split('[').flat_map(|sub| sub.split(']')) {
if brackets {
if detect_abba(chunk) {
matched = false;
break
}
} else if !matched && detect_abba(chunk) {
matched = true;
}
brackets = !brackets;
}
if matched {
matching += 1;
// println!("{}", line);
}
}
println!("{}", matching);
}
pub fn part2() {
println!("{}", "Day 7 part 2");
let mut matching = 0;
for line in read_input_file("day07.input").split_terminator('\n') {
let mut abas = HashSet::new();
let mut babs = HashSet::new();
let mut brackets = false;
for chunk in line.split('[').flat_map(|sub| sub.split(']')) {
if brackets && detect_aba(chunk, &mut abas, &babs) ||
!brackets && detect_aba(chunk, &mut babs, &abas) {
matching += 1;
break;
}
brackets = !brackets;
}
}
println!("{}", matching);
}
fn detect_abba(string: &str) -> bool {
let chars: Vec<char> = string.chars().collect();
for i in 0..(chars.len() - 3) {
if chars[i] == chars[i+3] && chars[i+1] == chars[i+2] && chars[i] != chars[i+1] {
return true;
}
}
false
}
fn detect_aba(chunk: &str, abas: &mut HashSet<(char, char)>, babs: &HashSet<(char, char)>) -> bool {
for aba in ABAs::new(chunk) {
let (a, b) = aba;
if babs.contains(&(b, a)) {
return true;
} else {
abas.insert((a, b));
}
}
false
}
struct ABAs {
counter: usize,
chars: Vec<char>
}
impl ABAs {
fn new(string: &str) -> ABAs {
ABAs {
counter: 0,
chars: string.chars().collect()
}
}
}
impl Iterator for ABAs {
type Item = (char, char);
fn next(&mut self) -> Option<Self::Item> {
while self.counter < self.chars.len() - 2 {
if self.chars[self.counter] == self.chars[self.counter + 2]
&& self.chars[self.counter] != self.chars[self.counter + 1] {
self.counter += 1;
return Some((self.chars[self.counter], self.chars[self.counter + 1]));
}
self.counter += 1;
}
None
}
}

14
2016/src/main.rs

@ -5,6 +5,9 @@ mod day01;
mod day02;
mod day03;
mod day04;
mod day05;
mod day06;
mod day07;
mod file_input;
fn main() {
@ -40,6 +43,17 @@ fn main() {
Some("2") => day04::part2(),
_ => ()
},
Some("5") => day05::bothparts(),
Some("6") => match choices.next() {
Some("1") => day06::part1(),
Some("2") => day06::part2(),
_ => ()
},
Some("7") => match choices.next() {
Some("1") => day07::part1(),
Some("2") => day07::part2(),
_ => ()
},
_ => ()
}
}

Loading…
Cancel
Save