add day 8 through 10
This commit is contained in:
Generated
+54
@@ -0,0 +1,54 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day08"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "88904434abc2901f197fe8cc55f0445e7ded921dba5911dad2e2b39b48e663c4"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.12.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1292b7759ae1cb9ec195452d1390a074f0cd8541ab7a5a8c31cd6db45d4a6ba"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"
|
||||
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day08"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
regex = "1.12.4"
|
||||
+300
@@ -0,0 +1,300 @@
|
||||
"qxfcsmh"
|
||||
"ffsfyxbyuhqkpwatkjgudo"
|
||||
"byc\x9dyxuafof\\\xa6uf\\axfozomj\\olh\x6a"
|
||||
"jtqvz"
|
||||
"uzezxa\"jgbmojtwyfbfguz"
|
||||
"vqsremfk\x8fxiknektafj"
|
||||
"wzntebpxnnt\"vqndz\"i\x47vvjqo\""
|
||||
"higvez\"k\"riewqk"
|
||||
"dlkrbhbrlfrp\\damiauyucwhty"
|
||||
"d\""
|
||||
"qlz"
|
||||
"ku"
|
||||
"yy\"\"uoao\"uripabop"
|
||||
"saduyrntuswlnlkuppdro\\sicxosted"
|
||||
"tj"
|
||||
"zzphopswlwdhebwkxeurvizdv"
|
||||
"xfoheirjoakrpofles\"nfu"
|
||||
"q\xb7oh\"p\xce\"n"
|
||||
"qeendp\"ercwgywdjeylxcv"
|
||||
"dcmem"
|
||||
"\"i\x13r\"l"
|
||||
"ikso\xdcbvqnbrjduh\"uqudzki\xderwk"
|
||||
"wfdsn"
|
||||
"pwynglklryhtsqbno"
|
||||
"hcoj\x63iccz\"v\"ttr"
|
||||
"zf\x23\\hlj\\kkce\\d\\asy\"yyfestwcdxyfj"
|
||||
"xs"
|
||||
"m\"tvltapxdvtrxiy"
|
||||
"bmud"
|
||||
"k\"a"
|
||||
"b\"oas"
|
||||
"\"yexnjjupoqsxyqnquy\"uzfdvetqrc"
|
||||
"vdw\xe3olxfgujaj"
|
||||
"qomcxdnd\"\\cfoe\""
|
||||
"fpul"
|
||||
"m\"avamefphkpv"
|
||||
"vvdnb\\x\\uhnxfw\"dpubfkxfmeuhnxisd"
|
||||
"hey\\"
|
||||
"ldaeigghlfey"
|
||||
"eure\"hoy\xa5iezjp\\tm"
|
||||
"yygb\"twbj\\r\"\x10gmxuhmp\""
|
||||
"weirebp\x39mqonbtmfmd"
|
||||
"ltuz\\hs\"e"
|
||||
"ysvmpc"
|
||||
"g\x8amjtt\"megl\"omsaihifwa"
|
||||
"yimmm"
|
||||
"iiyqfalh"
|
||||
"cwknlaaf"
|
||||
"q\x37feg\xc6s\"xx"
|
||||
"uayrgeurgyp\\oi"
|
||||
"xhug\"pt\"axugllbdiggzhvy"
|
||||
"kdaarqmsjfx\xc3d"
|
||||
"\"vkwla"
|
||||
"d\""
|
||||
"tmroz\"bvfinxoe\\mum\"wmm"
|
||||
"\"n\"bbswxne\\p\\yr\"qhwpdd"
|
||||
"skzlkietklkqovjhvj\xfe"
|
||||
"pbg\\pab\"bubqaf\"obzcwxwywbs\\dhtq"
|
||||
"xxjidvqh\"lx\\wu\"ij"
|
||||
"daef\x5fe\x5b\\kbeeb\x13qnydtboof"
|
||||
"ogvazaqy\"j\x73"
|
||||
"y"
|
||||
"n\"tibetedldy\\gsamm\"nwu"
|
||||
"wldkvgdtqulwkad"
|
||||
"dpmxnj"
|
||||
"twybw\"cdvf\"mjdajurokbce"
|
||||
"ru\"\\lasij\"i"
|
||||
"roc\\vra\\lhrm"
|
||||
"pbkt\x60booz\"fjlkc"
|
||||
"j\x4dytvjwrzt"
|
||||
"\\uiwjkniumxcs"
|
||||
"cbhm\"nexccior\"v\"j\"nazxilmfp\x47"
|
||||
"qdxngevzrlgoq"
|
||||
"\"lrzxftytpobsdfyrtdqpjbpuwmm\x9e"
|
||||
"mdag\x0asnck\xc2ggj\"slb\"fjy"
|
||||
"wyqkhjuazdtcgkcxvjkpnjdae"
|
||||
"aixfk\xc0iom\x21vueob"
|
||||
"dkiiakyjpkffqlluhaetires"
|
||||
"ysspv\"lysgkvnmwbbsy"
|
||||
"gy\"ryexcjjxdm\"xswssgtr"
|
||||
"s"
|
||||
"ddxv"
|
||||
"qwt\"\x27puilb\"pslmbrsxhrz"
|
||||
"qdg\xc9e\\qwtknlvkol\x54oqvmchn\\"
|
||||
"lvo"
|
||||
"b"
|
||||
"fk\"aa\"\"yenwch\\\\on"
|
||||
"srig\x63hpwaavs\\\x80qzk\"xa\"\xe6u\\wr"
|
||||
"yxjxuj\"ghyhhxfj\"\xa6qvatre"
|
||||
"yoktqxjxkzrklkoeroil"
|
||||
"\"jfmik\""
|
||||
"smgseztzdwldikbqrh\""
|
||||
"jftahgctf\"hoqy"
|
||||
"tcnhicr\"znpgckt\"ble"
|
||||
"vqktnkodh\"lo\"a\\bkmdjqqnsqr"
|
||||
"ztnirfzqq"
|
||||
"s"
|
||||
"xx"
|
||||
"iqj\"y\\hqgzflwrdsusasekyrxbp\\ad"
|
||||
"\\xzjhlaiynkioz\"\"bxepzimvgwt"
|
||||
"s\x36rbw"
|
||||
"mniieztwrisvdx"
|
||||
"atyfxioy\x2b\\"
|
||||
"irde\x85\x5cvbah\\jekw\"ia"
|
||||
"bdmftlhkwrprmpat\"prfaocvp"
|
||||
"w\\k"
|
||||
"umbpausy"
|
||||
"zfauhpsangy"
|
||||
"p\"zqyw"
|
||||
"wtztypyqvnnxzvlvipnq\"zu"
|
||||
"deicgwq\\oqvajpbov\\or\"kgplwu"
|
||||
"mbzlfgpi\\\\zqcidjpzqdzxityxa"
|
||||
"lfkxvhma"
|
||||
"\xf2yduqzqr\"\\fak\"p\"n"
|
||||
"mpajacfuxotonpadvng"
|
||||
"anb\\telzvcdu\\a\xf2flfq"
|
||||
"lrs\"ebethwpmuuc\"\x86ygr"
|
||||
"qmvdbhtumzc\"ci"
|
||||
"meet"
|
||||
"yopg\x0fdxdq\"h\\ugsu\xffmolxjv"
|
||||
"uhy"
|
||||
"fzgidrtzycsireghazscvmwcfmw\\t"
|
||||
"cqohkhpgvpru"
|
||||
"bihyigtnvmevx\"xx"
|
||||
"xz"
|
||||
"zofomwotzuxsjk\"q\"mc\"js\"dnmalhxd"
|
||||
"\\ktnddux\\fqvt\"ibnjntjcbn"
|
||||
"ia"
|
||||
"htjadnefwetyp\xd5kbrwfycbyy"
|
||||
"\"\\hkuxqddnao"
|
||||
"meqqsz\x83luecpgaem"
|
||||
"cvks\x87frvxo\"svqivqsdpgwhukmju"
|
||||
"sgmxiai\\o\"riufxwjfigr\xdf"
|
||||
"fgywdfecqufccpcdn"
|
||||
"faghjoq\x28abxnpxj"
|
||||
"zuppgzcfb\"dctvp\"elup\"zxkopx"
|
||||
"xqs\x45xxdqcihbwghmzoa"
|
||||
"anbnlp\\cgcvm\"hc"
|
||||
"xf\"fgrngwzys"
|
||||
"nrxsjduedcy\x24"
|
||||
"\x71sxl\"gj\"sds\"ulcruguz\\t\\ssvjcwhi"
|
||||
"jhj\"msch"
|
||||
"qpovolktfwyiuyicbfeeju\x01"
|
||||
"nkyxmb\"qyqultgt\"nmvzvvnxnb"
|
||||
"ycsrkbstgzqb\"uv\\cisn"
|
||||
"s"
|
||||
"ueptjnn\"\"sh"
|
||||
"lp\"z\"d\"mxtxiy"
|
||||
"yzjtvockdnvbubqabjourf\"k\"uoxwle"
|
||||
"\x82\"wqm\""
|
||||
"\xb5cwtuks\x5fpgh"
|
||||
"wd"
|
||||
"tbvf"
|
||||
"ttbmzdgn"
|
||||
"vfpiyfdejyrlbgcdtwzbnm"
|
||||
"uc"
|
||||
"otdcmhpjagqix"
|
||||
"\\\xb1qso\"s"
|
||||
"scowax"
|
||||
"behpstjdh\xccqlgnqjyz\"eesn"
|
||||
"r\xe1cbnjwzveoomkzlo\\kxlfouhm"
|
||||
"jgrl"
|
||||
"kzqs\\r"
|
||||
"ctscb\x7fthwkdyko\"\x62pkf\"d\xe6knmhurg"
|
||||
"tc\"kw\x3ftt"
|
||||
"bxb\x5ccl"
|
||||
"jyrmfbphsldwpq"
|
||||
"jylpvysl\"\"juducjg"
|
||||
"en\\m\"kxpq\"wpb\\\""
|
||||
"madouht\"bmdwvnyqvpnawiphgac\""
|
||||
"vuxpk\"ltucrw"
|
||||
"aae\x60arr"
|
||||
"ttitnne\"kilkrgssnr\xfdurzh"
|
||||
"oalw"
|
||||
"pc\"\"gktkdykzbdpkwigucqni\"nxiqx"
|
||||
"dbrsaj"
|
||||
"bgzsowyxcbrvhtvekhsh\"qgd"
|
||||
"kudfemvk\"\"\"hkbrbil\"chkqoa"
|
||||
"zjzgj\\ekbhyfzufy"
|
||||
"\\acos\"fqekuxqzxbmkbnn\x1ejzwrm"
|
||||
"elxahvudn\"txtmomotgw"
|
||||
"\x2eoxmwdhelpr\"cgi\xf7pzvb"
|
||||
"eapheklx"
|
||||
"hfvma\"mietvc\"tszbbm\"czex"
|
||||
"h\"iiockj\\\xc1et"
|
||||
"d\"rmjjftm"
|
||||
"qlvhdcbqtyrhlc\\"
|
||||
"yy\"rsucjtulm\"coryri\"eqjlbmk"
|
||||
"tv"
|
||||
"r\"bfuht\\jjgujp\""
|
||||
"kukxvuauamtdosngdjlkauylttaokaj"
|
||||
"srgost\"\"rbkcqtlccu\x65ohjptstrjkzy"
|
||||
"yxwxl\\yjilwwxffrjjuazmzjs"
|
||||
"dxlw\\fkstu\"hjrtiafhyuoh\"sewabne"
|
||||
"\x88sj\"v"
|
||||
"rfzprz\xec\"oxqclu\"krzefp\\q"
|
||||
"cfmhdbjuhrcymgxpylllyvpni"
|
||||
"ucrmjvmimmcq\x88\xd9\"lz"
|
||||
"lujtt\""
|
||||
"gvbqoixn\"pmledpjmo\"flydnwkfxllf"
|
||||
"dvxqlbshhmelsk\x8big\"l"
|
||||
"mx\x54lma\x8bbguxejg"
|
||||
"\x66jdati\xeceieo"
|
||||
"\"iyyupixei\x54ff"
|
||||
"xohzf\"rbxsoksxamiu"
|
||||
"vlhthspeshzbppa\x4drhqnohjop\"\"mfjd"
|
||||
"f\"tvxxla\"vurian\"\"idjq\x3aptm\xc3olep"
|
||||
"gzqz"
|
||||
"kbq\\wogye\\altvi\\hbvmodny"
|
||||
"j\xd8"
|
||||
"ofjozdhkblvndl"
|
||||
"hbitoupimbawimxlxqze"
|
||||
"ypeleimnme"
|
||||
"xfwdrzsc\\oxqamawyizvi\\y"
|
||||
"enoikppx\xa1ixe\"yo\"gumye"
|
||||
"fb"
|
||||
"vzf"
|
||||
"zxidr"
|
||||
"cu\x31beirsywtskq"
|
||||
"lxpjbvqzztafwezd"
|
||||
"\\jyxeuo\x18bv"
|
||||
"b\"vawc\"p\\\\giern\"b"
|
||||
"odizunx\"\"t\\yicdn\"x\"sdiz"
|
||||
"\"\"tebrtsi"
|
||||
"ctyzsxv\xa6pegfkwsi\"tgyltaakytccb"
|
||||
"htxwbofchvmzbppycccliyik\xe5a"
|
||||
"ggsslefamsklezqkrd"
|
||||
"rcep\"fnimwvvdx\"l"
|
||||
"zyrzlqmd\x12egvqs\\llqyie"
|
||||
"\x07gsqyrr\\rcyhyspsvn"
|
||||
"butg\""
|
||||
"gb"
|
||||
"gywkoxf\"jsg\\wtopxvumirqxlwz"
|
||||
"rj\"ir\"wldwveair\x2es\"dhjrdehbqnzl"
|
||||
"ru\"elktnsbxufk\\ejufjfjlevt\\lrzd"
|
||||
"\"widsvok"
|
||||
"oy\"\x81nuesvw"
|
||||
"ay"
|
||||
"syticfac\x1cfjsivwlmy\"pumsqlqqzx"
|
||||
"m"
|
||||
"rjjkfh\x78cf\x2brgceg\"jmdyas\"\\xlv\xb6p"
|
||||
"tmuvo\"\x3ffdqdovjmdmkgpstotojkv\"as"
|
||||
"jd\\ojvynhxllfzzxvbn\"wrpphcvx"
|
||||
"pz"
|
||||
"\"twr"
|
||||
"n\\hdzmxe\"mzjjeadlz"
|
||||
"fb\"rprxuagvahjnri"
|
||||
"rfmexmjjgh\\xrnmyvnatrvfruflaqjnd"
|
||||
"obbbde\"co\"qr\"qpiwjgqahqm\\jjp\""
|
||||
"vpbq\"\"y\"czk\\b\x52ed\"lnzepobp"
|
||||
"syzeajzfarplydipny\"y\"\xe8ad"
|
||||
"mpyodwb"
|
||||
"\x47rakphlqqptd"
|
||||
"wa\"oj\"aiy"
|
||||
"a"
|
||||
"ropozx"
|
||||
"q\x51nbtlwa"
|
||||
"etukvgx\\jqxlkq"
|
||||
"\"tp\"rah\"pg\"s\"bpdtes\\tkasdhqd"
|
||||
"dn\"qqpkikadowssb\xcah\"dzpsf\\ect\"jdh"
|
||||
"pxunovbbrrn\\vullyn\"bno\"\"\"myfxlp\""
|
||||
"qaixyazuryvkmoulhcqaotegfj\\mpzm"
|
||||
"bvfrbicutzbjwn\\oml\"cf\"d\"ezcpv\"j"
|
||||
"rmbrdtneudemigdhelmb"
|
||||
"aq\\aurmbhy"
|
||||
"wujqvzw"
|
||||
"gf\"tssmvm\"gm\"hu\x9a\xb7yjawsa"
|
||||
"hrhqqxow\xe2gsydtdspcfqy\"zw\\ou"
|
||||
"ianwwf\\yko\\tdujhhqdi"
|
||||
"xylz\"zpvpab"
|
||||
"lwuopbeeegp"
|
||||
"aoop\x49jhhcexdmdtun"
|
||||
"\\\\mouqqcsgmz"
|
||||
"tltuvwhveau\x43b\"ymxjlcgiymcynwt"
|
||||
"gsugerumpyuhtjljbhrdyoj"
|
||||
"lnjm\xb8wg\"ajh"
|
||||
"zmspue\"nfttdon\\b\"eww"
|
||||
"\"w\x67jwaq\x7ernmyvs\\rmdsuwydsd\"th"
|
||||
"ogtgvtlmcvgllyv"
|
||||
"z\"fqi\"rvddoehrciyl"
|
||||
"yustxxtot\"muec\"xvfdbzunzvveq"
|
||||
"mqslw"
|
||||
"txqnyvzmibqgjs\xb6xy\x86nfalfyx"
|
||||
"kzhehlmkholov"
|
||||
"plpmywcnirrjutjguosh\\"
|
||||
"pydbnqofv\"dn\\m"
|
||||
"aegqof"
|
||||
"eambmxt\\dxagoogl\\zapfwwlmk"
|
||||
"afbmqitxxqhddlozuxcpjxgh"
|
||||
"vgts"
|
||||
"bfdpqtoxzzhmzcilehnflna"
|
||||
"s\"idpz"
|
||||
"\xcfhgly\"nlmztwybx\"ecezmsxaqw"
|
||||
"aackfgndqcqiy"
|
||||
"\x22unqdlsrvgzfaohoffgxzfpir\"s"
|
||||
"abh\"ydv\"kbpdhrerl"
|
||||
"bdzpg"
|
||||
"ekwgkywtmzp"
|
||||
"wtoodejqmrrgslhvnk\"pi\"ldnogpth"
|
||||
"njro\x68qgbx\xe4af\"\\suan"
|
||||
@@ -0,0 +1,49 @@
|
||||
use regex::Regex;
|
||||
|
||||
fn main() {
|
||||
let content = std::fs::read_to_string("input.txt").unwrap();
|
||||
let data: Vec<_> = content.lines().collect();
|
||||
|
||||
part1(&data);
|
||||
part2(&data);
|
||||
}
|
||||
|
||||
fn part2(data: &[&str]) {
|
||||
let mut new_length = 0;
|
||||
let mut old_length = 0;
|
||||
|
||||
let re_quote = Regex::new("\"").unwrap();
|
||||
let re_backslash = Regex::new(r"\\").unwrap();
|
||||
|
||||
for line in data {
|
||||
old_length += line.len();
|
||||
let found = re_quote.replace_all(line, "AA");
|
||||
let found = re_backslash.replace_all(found.as_ref(), "BB");
|
||||
|
||||
new_length += found.len() + 2; // add first and last quote
|
||||
}
|
||||
let result = new_length - old_length;
|
||||
println!("{result}");
|
||||
}
|
||||
|
||||
fn part1(data: &[&str]) {
|
||||
let mut literal_length = 0;
|
||||
let mut memory_length = 0;
|
||||
|
||||
let re_hex = Regex::new(r"\\x[a-f0-9]{2}").unwrap();
|
||||
let re_backslash = Regex::new(r"\\\\").unwrap();
|
||||
let re_doublequote = Regex::new(r"\x5c\x22").unwrap();
|
||||
|
||||
for line in data {
|
||||
literal_length += line.len();
|
||||
let found = re_hex.replace_all(line, "A");
|
||||
let found = re_backslash.replace_all(found.as_ref(), "B");
|
||||
let found = re_doublequote.replace_all(found.as_ref(), "C");
|
||||
|
||||
memory_length += found.len() - 2; // remove first and last quote
|
||||
}
|
||||
let result = literal_length - memory_length;
|
||||
println!("{result}");
|
||||
}
|
||||
|
||||
// https://adventofcode.com/2015/day/8
|
||||
Generated
+25
@@ -0,0 +1,25 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "day09"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b4baf93f58d4425749ca49a51c50ebab072c5df6994d08fed93541c331481dc"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day09"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.15.0"
|
||||
@@ -0,0 +1,28 @@
|
||||
AlphaCentauri to Snowdin = 66
|
||||
AlphaCentauri to Tambi = 28
|
||||
AlphaCentauri to Faerun = 60
|
||||
AlphaCentauri to Norrath = 34
|
||||
AlphaCentauri to Straylight = 34
|
||||
AlphaCentauri to Tristram = 3
|
||||
AlphaCentauri to Arbre = 108
|
||||
Snowdin to Tambi = 22
|
||||
Snowdin to Faerun = 12
|
||||
Snowdin to Norrath = 91
|
||||
Snowdin to Straylight = 121
|
||||
Snowdin to Tristram = 111
|
||||
Snowdin to Arbre = 71
|
||||
Tambi to Faerun = 39
|
||||
Tambi to Norrath = 113
|
||||
Tambi to Straylight = 130
|
||||
Tambi to Tristram = 35
|
||||
Tambi to Arbre = 40
|
||||
Faerun to Norrath = 63
|
||||
Faerun to Straylight = 21
|
||||
Faerun to Tristram = 57
|
||||
Faerun to Arbre = 83
|
||||
Norrath to Straylight = 9
|
||||
Norrath to Tristram = 50
|
||||
Norrath to Arbre = 60
|
||||
Straylight to Tristram = 27
|
||||
Straylight to Arbre = 81
|
||||
Tristram to Arbre = 90
|
||||
@@ -0,0 +1,68 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
fn main() {
|
||||
let data = std::fs::read_to_string("input.txt").unwrap();
|
||||
let legs: Vec<_> = data.lines().map(Leg::from_str).collect();
|
||||
|
||||
part1(&legs);
|
||||
}
|
||||
|
||||
fn part1(legs: &[Leg]) {
|
||||
let mut locations = HashSet::new();
|
||||
for (s, e) in legs.iter().map(|l| (l.from.as_str(), l.to.as_str())) {
|
||||
locations.insert(s);
|
||||
locations.insert(e);
|
||||
}
|
||||
|
||||
let length = locations.len();
|
||||
|
||||
let routes = locations.into_iter().permutations(length).collect_vec();
|
||||
|
||||
let mut sums = vec![];
|
||||
|
||||
for route in routes {
|
||||
let sum: usize = route
|
||||
.iter()
|
||||
.tuple_windows()
|
||||
.map(|(l, r)| find_length(legs, l, r))
|
||||
.sum();
|
||||
sums.push(sum);
|
||||
}
|
||||
sums.sort_unstable();
|
||||
println!("{}\n{}", sums[0], sums.last().unwrap());
|
||||
}
|
||||
|
||||
fn find_length(legs: &[Leg], a: &str, b: &str) -> usize {
|
||||
for leg in legs {
|
||||
if (leg.from == a || leg.to == a) && (leg.from == b || leg.to == b) {
|
||||
return leg.distance;
|
||||
}
|
||||
}
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Leg {
|
||||
pub from: String,
|
||||
pub to: String,
|
||||
pub distance: usize,
|
||||
}
|
||||
|
||||
impl Leg {
|
||||
fn new(from: String, to: String, distance: usize) -> Self {
|
||||
Self { from, to, distance }
|
||||
}
|
||||
|
||||
fn from_str(s: &str) -> Self {
|
||||
let mut segments = s.split_whitespace();
|
||||
let from = segments.next().unwrap().to_string();
|
||||
let to = segments.nth(1).unwrap().to_string();
|
||||
let distance = segments.next_back().unwrap().parse().unwrap();
|
||||
|
||||
Self::new(from, to, distance)
|
||||
}
|
||||
}
|
||||
|
||||
// https://adventofcode.com/2015/day/9
|
||||
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "day10"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day10"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,96 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn main() {
|
||||
let input = "3113322113";
|
||||
|
||||
part1(input);
|
||||
}
|
||||
|
||||
fn part1(input: &str) {
|
||||
let mut input: Vec<_> = input.to_string().chars().collect();
|
||||
|
||||
for _ in 0..34 {
|
||||
input = recurse(input);
|
||||
}
|
||||
for c in input {
|
||||
print!("{c}");
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
||||
fn recurse(input: Vec<char>) -> Vec<char> {
|
||||
if input.is_empty() {
|
||||
return input;
|
||||
}
|
||||
|
||||
let mut iter = input.iter();
|
||||
let first_digit = *iter.next().unwrap();
|
||||
|
||||
let s = 0;
|
||||
let mut e = 0;
|
||||
|
||||
for next_digit in iter {
|
||||
if first_digit == *next_digit {
|
||||
e += 1
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let mut result = look_and_say(&input[s..=e]);
|
||||
|
||||
result.append(&mut recurse(input[e + 1..].to_vec()));
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn look_and_say(s: &[char]) -> Vec<char> {
|
||||
if s.is_empty() {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
let binding = s.len().to_string();
|
||||
let length_chars = binding.chars();
|
||||
let mut result = vec![];
|
||||
|
||||
for c in length_chars {
|
||||
result.push(c);
|
||||
}
|
||||
|
||||
result.push(s[0]);
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{look_and_say, recurse};
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
let x = vec!['1'];
|
||||
let result = look_and_say(&x);
|
||||
assert_eq!(result, vec!['1', '1']);
|
||||
|
||||
let x = vec!['1', '1', '1'];
|
||||
let result = look_and_say(&x);
|
||||
assert_eq!(result, vec!['3', '1']);
|
||||
|
||||
let x = vec!['2'];
|
||||
let result = look_and_say(&x);
|
||||
assert_eq!(result, vec!['1', '2']);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_recurse() {
|
||||
// let x = vec!['1'];
|
||||
// let result = recurse(x);
|
||||
// assert_eq!(result, vec!['1', '1']);
|
||||
|
||||
let result = vec!['1', '1', '1', '2', '2', '1'];
|
||||
let result = recurse(result);
|
||||
assert_eq!(result, vec!['3', '1', '2', '2', '1', '1']);
|
||||
}
|
||||
}
|
||||
|
||||
// https://adventofcode.com/2015/day/10
|
||||
Reference in New Issue
Block a user