Day 09.
This commit is contained in:
1 parent
3cce4256cc
commit
f84401e805
6 files changed
+169
No files matched your search
@@ -0,0 +1 @@
|
||||
7839105685433013714577744422503527278971505960753846696142926278672617237575485286677159338235399035306979223432754870913858965422187978297379842572204980878417875562906234774960549491579742393512183257495060648682356621595018634429939318405794271814717662329720599531302316755296303995988543326927224329169957141263633919324882224314721347533064639375165479309157262071153341435732918424503932471314108574909083829766577763175247705413567394652672118187596212279819758451558513692673402145123711898026129483689247563242821021733040132522687330439917367417553663609237257223538680281592707814918497496773152659383451932774174559672724949594213081236880261724636974425633552750629736879539994675221955887418931699753581581625321530364341189872394015688641897832167293342622982682415256336063291239121360461975569041888929119459292038345383958275392359737155865037791127437255669431852199437591536519699273484919877973377373771967696494864110153921118347477147594892719528235594971017557741814035182461917119314887771770385346504673475061132522694748631198291744938459292839603433946825465719524149911376146210113573315090363147272534972274871296285514191583127929482063874169388078574876895771549855201897906833969957776092121911863914186316972578114526974158707146665749396479115738963891775338993687274533772264824993923096582698405591146644647099989851996621334922766854385459784936626149316425786198395990238669779234529248638835248331987590398573791520884710848039695396764650987113267191137583724588544878897590538183862178414279484843336981339686471186652678464985222344727450435988379998699270659914498884565960723116827050583828832572871119339424803779234819926661485554783290727277179457692161962425681797863440304028269410575321234268542426976635527775273218529215762364477999175231879657407369456955152215236084643314358547272335158866504915397085905911545330607760845375681929543868375513593970925881729931815076892210402484958022493257941294288081908529975539864430326866141564627356605780275899771737632749419111228534799039769054937027999727351847738997689874497144349063184093825922178377231720912945529120709130767456324587833267149448676693759915857877777112294160583057824875156419556957287229695632269554189769297870581486409117283829563563896935695024569376538518665899747665567158607359283648482242157270442133957826242013311168194339253845216157991636375428721194519315213275789263819278188142513466209775767192356132654838177757871942465477999563344873162156219599983553771583598980346457618790411022587457269398538845912333382369187259936354635819546611148921482036965655991999529350432686252767123366548372679824885658225621626072847134778373121577544366901366245723289627511644974997251296645285689567522794692686808754964123725815884969643248921434886429727945758228894389425858894015511699197767599067162138291792723422713021443650434337881214409445722377271642301711921285375919508542722998219454114513235770742697564244492741794018865960159554504591799678769698681857236429521815203183556423523959342330347024705434201018499723963112954281714675185877441715723989824960137175322893294164897060262382291798356062813912494639783142336950936511444648852550397092749658683864307097696872236264576584134758499046475516574830369038207920296538573246815619164888362920277876413781806164749258957565808297321177704962207874188219703898141592723943739855864738969097677044381425488828433057759248436898454161161266558743913331747737547193249154998019111925292369425190525179107930136023444217287461918427658049864567993160938795338827114018342697618083807579891728738176149644242170951584915633769635441034659172919081481936628794494855978077357359131787814746748169692291261794895284538669375158616296317945413678373847528866755569949561798114701025405728541288333514608627149157611916245818771612276683145871894033372986761461419871545898399666761382693770907823527271113728966261491813481887199880561116141755123544512497519254698759947289739318729464843574946537716424801470753411895430599515688671613945315556473988958195731285571525954751964731146155602495181333448274858587323061348443905692585079615880495229198863633236615165516997176694193370694715175642676371406146241038571666264052391691338366214251723982562011204254941450497071234968585722267832846346299066343834574016446924498584257264578181457011417552628142477713766620937261559582192639241660889841494452502568695549255247118850277245238431863837959313448820459757683184147030263814594971146369664985867668491665539314709374956293334381364518881954165311224719153498569832186175603240466330881929529136394358115668965247464252393562529510801438445254676434924652526693319527309328311015212496733751226139481627917838747894126382147522681537421954442373705139283669959548277983335645872960972240606062137053431847946119172484594357858314396210451973164722842498463986988674278315916250587466358591954578866927971277929013309377957140477324697346519787466651932967202094281833573739607555987627151899937339327Line truncated
|
||||
@@ -0,0 +1 @@
|
||||
2333133121414131402
|
||||
@@ -0,0 +1,135 @@
|
||||
package day_09
|
||||
|
||||
const Empty = -1
|
||||
|
||||
type State int
|
||||
|
||||
type Chunk struct {
|
||||
start, length int
|
||||
}
|
||||
|
||||
const (
|
||||
ReadingValue = iota
|
||||
ReadingSpace = iota
|
||||
)
|
||||
|
||||
// task:https://adventofcode.com/2024/day/9
|
||||
func SolveBasic(input string) int {
|
||||
diagram := loadDiagram(input)
|
||||
|
||||
diagram = compress(diagram)
|
||||
|
||||
return hashSum(diagram)
|
||||
}
|
||||
|
||||
// task:https://adventofcode.com/2024/day/9#part2
|
||||
func SolveComplex(input string) int {
|
||||
diagram := loadDiagram(input)
|
||||
|
||||
diagram = safeCompress(diagram)
|
||||
|
||||
return hashSum(diagram)
|
||||
}
|
||||
|
||||
func hashSum(diagram []int) int {
|
||||
sum := 0
|
||||
for i, val := range diagram {
|
||||
if val != Empty {
|
||||
sum += i * val
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func compress(diagram []int) []int {
|
||||
startingCursor := 0
|
||||
endingCursor := len(diagram) - 1
|
||||
|
||||
for {
|
||||
for diagram[startingCursor] != Empty && startingCursor < endingCursor {
|
||||
startingCursor++
|
||||
}
|
||||
for diagram[endingCursor] == Empty && startingCursor < endingCursor {
|
||||
endingCursor--
|
||||
}
|
||||
if startingCursor >= endingCursor {
|
||||
break
|
||||
}
|
||||
diagram[startingCursor] = diagram[endingCursor]
|
||||
diagram[endingCursor] = Empty
|
||||
}
|
||||
return diagram
|
||||
}
|
||||
|
||||
func safeCompress(diagram []int) []int {
|
||||
value := diagram[len(diagram)-1]
|
||||
|
||||
for value >= 0 {
|
||||
file := locateFile(diagram, value)
|
||||
if hole, found := findHole(diagram, file.start, file.length); found {
|
||||
copyFile(diagram, file, value, hole)
|
||||
}
|
||||
value--
|
||||
}
|
||||
|
||||
return diagram
|
||||
}
|
||||
|
||||
func copyFile(diagram []int, file Chunk, value int, hole Chunk) {
|
||||
for i := 0; i < file.length; i++ {
|
||||
diagram[hole.start+i] = value
|
||||
diagram[file.start+i] = Empty
|
||||
}
|
||||
}
|
||||
|
||||
func findHole(diagram []int, end int, length int) (Chunk, bool) {
|
||||
for i := 0; i < end; i++ {
|
||||
if diagram[i] == Empty {
|
||||
size := 0
|
||||
for i+size < end && diagram[i+size] == Empty {
|
||||
size++
|
||||
}
|
||||
if size >= length {
|
||||
return Chunk{i, size}, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return Chunk{}, false
|
||||
}
|
||||
|
||||
func locateFile(diagram []int, value int) Chunk {
|
||||
length := 0
|
||||
for i := 0; i < len(diagram); i++ {
|
||||
if diagram[i] == value {
|
||||
start := i
|
||||
for i+length < len(diagram) && diagram[i+length] == value {
|
||||
length++
|
||||
}
|
||||
return Chunk{start, length}
|
||||
}
|
||||
}
|
||||
panic("couldn't find the value")
|
||||
}
|
||||
|
||||
func loadDiagram(input string) []int {
|
||||
diagram := make([]int, 0, len(input)*5) // we reserve about the average we need
|
||||
state := ReadingValue
|
||||
currentValue := 0
|
||||
|
||||
for _, r := range input {
|
||||
lenght := int(r - '0')
|
||||
if state == ReadingValue {
|
||||
for i := 0; i < lenght; i++ {
|
||||
diagram = append(diagram, currentValue)
|
||||
}
|
||||
currentValue++
|
||||
} else {
|
||||
for i := 0; i < lenght; i++ {
|
||||
diagram = append(diagram, Empty)
|
||||
}
|
||||
}
|
||||
state = (state + 1) % 2
|
||||
}
|
||||
|
||||
return diagram
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package day_09_test
|
||||
|
||||
import (
|
||||
"advent_of_code_2024/day_09"
|
||||
_ "embed"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//go:embed example_data.txt
|
||||
var ExampleData string
|
||||
|
||||
func TestBasicSolutionExample(t *testing.T) {
|
||||
result := day_09.SolveBasic(ExampleData)
|
||||
if result != 1928 {
|
||||
t.Fatalf("Expected 1928 received %d", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComplexSolutionExample(t *testing.T) {
|
||||
result := day_09.SolveComplex(ExampleData)
|
||||
if result != 2858 {
|
||||
t.Fatalf("Expected 2858 received %d", result)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user