Skip to content

Commit

Permalink
fix: really old uncommitted code
Browse files Browse the repository at this point in the history
  • Loading branch information
williamgcampbell committed Nov 14, 2023
1 parent be28ffe commit 3489d52
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 58 deletions.
5 changes: 3 additions & 2 deletions internal/days/01/day.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package _1

import (
_ "embed"
"github.com/williamgcampbell/aoc2022/internal"
"github.com/williamgcampbell/aoc2022/internal/scanner"
"io"
"sort"
"strconv"
"strings"

"github.com/williamgcampbell/aoc2022/internal"
"github.com/williamgcampbell/aoc2022/internal/scanner"
)

//go:embed input.txt
Expand Down
124 changes: 71 additions & 53 deletions internal/days/07/day.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/williamgcampbell/aoc2022/internal"
"github.com/williamgcampbell/aoc2022/internal/scanner"
"io"
"log"
"strings"
)

Expand All @@ -25,52 +26,14 @@ func (d *Solver) SolvePart2() string {
func solve(reader io.Reader, part1 bool) string {
lines := scanner.ScanLines(reader)

var currentDir *Directory
var dirs []*Directory
for i, line := range lines {
if strings.HasPrefix(line, "$ cd ") {
cd := line[5:]
if cd == "sztz" {
fmt.Printf("found")
}
if cd == ".." {
if currentDir.Parent == nil {
fmt.Printf("%s %d\n", currentDir.Name, i)
}
currentDir = currentDir.Parent
} else {
if d, ok := getChildDirectory(dirs, currentDir, cd); ok {
currentDir = d
} else {
newDir := NewDirectory(nil, cd)
dirs = append(dirs, newDir)
currentDir = newDir
}
}
} else if strings.HasPrefix(line, "$ ls") {
continue
} else if strings.HasPrefix(line, "dir ") {
// dir
dirName := line[4:]
if _, ok := getChildDirectory(dirs, currentDir, dirName); !ok {
dir := NewDirectory(currentDir, dirName)
dirs = append(dirs, dir)
currentDir.Directories = append(currentDir.Directories, dir)
}
} else {
lsFile := strings.Split(line, " ")
// file
file := &File{
Name: lsFile[1],
Size: internal.MustAtoI(lsFile[0]),
}
currentDir.AddFile(file)
}
fs := &FileSystem{}
for _, line := range lines {
fs.ReplayLine(line)
}

if part1 {
var totalSize int
for _, dir := range dirs {
for _, dir := range fs.Directories {
size := dir.GetSize()
if size <= 100000 {
totalSize += size
Expand All @@ -79,7 +42,17 @@ func solve(reader io.Reader, part1 bool) string {
return fmt.Sprintf("%d", totalSize)
}

return "24933642" //TODO
unusedSpace := 70000000 - fs.RootDirectory.GetSize()
spaceNeeded := 30000000 - unusedSpace
var minSizeNeeded int
for _, dir := range fs.Directories {
size := dir.GetSize()
fmt.Printf("Directory: %s Size %d\n", dir.Path, size)
if size >= spaceNeeded && (size < minSizeNeeded || minSizeNeeded == 0) {
minSizeNeeded = size
}
}
return fmt.Sprintf("%d", minSizeNeeded)
}

func NewDirectory(current *Directory, name string) *Directory {
Expand All @@ -96,19 +69,51 @@ func NewDirectory(current *Directory, name string) *Directory {
}
}

func getChildDirectory(dd []*Directory, current *Directory, s string) (*Directory, bool) {
var path string
if current != nil {
path = current.Path + s + "/"
} else {
path = s
type FileSystem struct {
CurrentDirectory *Directory
RootDirectory *Directory
Directories []*Directory
}

func (fs *FileSystem) ReplayLine(line string) {
if strings.HasPrefix(line, "$ ls") {
return
}
for _, d := range dd {
if d.Path == path {
return d, true

if strings.HasPrefix(line, "$ cd ") {
cd := line[5:]
if cd == ".." {
fs.CurrentDirectory = fs.CurrentDirectory.Parent
} else if cd == "/" {
// special case for this folder
newDir := NewDirectory(nil, cd)
fs.Directories = append(fs.Directories, newDir)
fs.CurrentDirectory = newDir
fs.RootDirectory = newDir
} else {
if d, ok := fs.CurrentDirectory.GetChildDirectory(cd); ok {
fs.CurrentDirectory = d
} else {
// This should never happen.
// Directories will always be added via the ls command before running cd into them
log.Fatalf("Could not find directory %s", cd)
}
}
} else if strings.HasPrefix(line, "dir ") {
dirName := line[4:]
if _, ok := fs.CurrentDirectory.GetChildDirectory(dirName); !ok {
dir := NewDirectory(fs.CurrentDirectory, dirName)
fs.Directories = append(fs.Directories, dir)
fs.CurrentDirectory.AddDirectory(dir)
}
} else {
lsFile := strings.Split(line, " ")
file := &File{
Name: lsFile[1],
Size: internal.MustAtoI(lsFile[0]),
}
fs.CurrentDirectory.AddFile(file)
}
return nil, false
}

type File struct {
Expand All @@ -128,6 +133,19 @@ func (d *Directory) AddFile(f *File) {
d.Files = append(d.Files, f)
}

func (d *Directory) AddDirectory(dir *Directory) {
d.Directories = append(d.Directories, dir)
}

func (d *Directory) GetChildDirectory(name string) (*Directory, bool) {
for _, dir := range d.Directories {
if dir.Name == name {
return dir, true
}
}
return nil, false
}

func (d *Directory) GetSize() int {
totalSize := 0
for _, file := range d.Files {
Expand Down
2 changes: 1 addition & 1 deletion internal/days/07/day_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestSolvePart1(t *testing.T) {
func TestSolvePart2(t *testing.T) {
t.Parallel()
day := &Solver{}
require.Equal(t, "24933642", day.SolvePart2())
require.Equal(t, "8319096", day.SolvePart2())
}

func TestSolve(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions internal/days/08/day.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ func (d *Solver) SolvePart2() string {
}

func solve(reader io.Reader, part1 bool) string {

return ""
}
8 changes: 6 additions & 2 deletions internal/days/08/day_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
"github.com/stretchr/testify/require"
)

const example = ``
const example = `30373
25512
65332
33549
35390`

func TestSolvePart1(t *testing.T) {
t.Parallel()
Expand All @@ -29,7 +33,7 @@ func TestSolve(t *testing.T) {
}{
"Example": {
input: example,
want: "",
want: "21",
part1: true,
},
"Example part 2": {
Expand Down
99 changes: 99 additions & 0 deletions internal/days/08/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
301201331333030024002151134115545441525510053410321024643223304253251423014345311431413433341211111
210313031411030140245452501003514242514125366210551353634546632015232323540022212401412031113310333
201223331222432221305443412445304401201165033624245311620346242360420521343514132520204104244200322
210003034413133345421153101053132524226234460546142402452466345321440520524311212410303224012242000
002232200024321241105051144044530503201446133504652500410463241436361361002024122340512441243222203
102023303032432341105353554225663326563653250523350302104003635340436625240235531151145343032212033
011432423042054254520503506222252050256315142254016650105015643542660231044302233035322122100032212
034421243321311550201305666136554451350322354144633556333022003664006612152610511132354312443134400
302214114340201552153246320504242003513464536535521715125521314654606653312124014223422135000032024
022203301311105514431426025115566414166144753435463146324664616502342356626446124204455213422314241
133202031100242023236362026113243573426315617627527543647213541517440126016042624554125530550304004
412414223443004100621020255403316757326244275247134627215636656313472021156626214003511121524211201
100020113553530111635403433310123371416566464531664371236763762654672716453255155611041431321301000
040442015403322353635250250156757514552341246114755653427321572644373333764015356451022003122252214
131335400000330152046666616363523347474566725475326875713565751313316433363251655012415235210153134
212205354412516232346215667365363413636143322285687428338237666271455444277511521334662020135104441
400134535100103465544240377332663352346366464426356585587388572154627365567576126334445412551245044
313125111102350042411627156361342341273445272266422466584446423554731131743433165124231111435223052
414211350002631116406722423146474385423846752263457355267246283862347153516722261451645234034250125
142350115212334322146515331713325337468443588225285768685574226644533724475335124115505531225141432
221420101060222256363211452511352288853828648746647256668823328327287747564123275261621500245133411
453113024555202631573132336658242736347325874252333535256677587837657678625214457745232452150220123
555034246226545644223655121622358344733858586769673974337826765554468355524344661464661052120504131
101440210460332606657775163743688753344448398636494466664973768385765272752651367426304666101123130
532232304344031236635141354227547676887398783998657559945898897482268637355733664642700640525354230
254425530336536647234251648333783825963356648666958849583777336977537428866623134526176025236412301
052420045556125743544677352453853565968669799673389736356646485675748435258353372737571166544644253
144212521432144457427473545763356688633446594997933764479494846866798222377556235341461400160564305
414546463341545336547365484645276357553584336774496855639397587938638557263543347554553131065014354
213110564326373147676363536832594994448537796457645847754798668996863976763583886251625241215423451
402441605154657151126587562648764444548399868584444984666447398399475652866688642677426256050111044
030505222044142675156675655568358889949479978645448697784665774359755689243645735162215642560304321
002364341155613641588426677659986477446964688986679878477959859546863555528383865312255533510006644
315603002442216142126278638843367593346876468657757444768587498693659647788784456647117426311553113
534026231247517653336254847934574378679647999777747989757856888763437583487626885465121722135401110
152663134317453637627862365337774534996567697946779555455989744649937645994653657567156245462533302
415120400426463528567443378965654344948544677795568678979865564545598768643876476547177613642314014
212134106151643463482274255887744459499685657799867587677998674896776889393348337484463153571460162
123265543627354585248835586978887467585449979975988897979955857994455783796697728544256417663200436
253156122444754335226888665898754494695989955588988885866899669459797535776733282376417141555433640
105125666421254638626347936375599547788669998799766755958796875664688687698846364536875113743306636
543154323554211424234263689757375946445859555669888666959896575566688549986398632678217421361116016
534446627674264324833537896437597864966965789556679958788689859997595593943745863828822163463613245
456422412772232364425647578537895646785958867897868976696886585987468565967685553265682333333642602
155305566576114247342276876599654498977865685987867897688785559595497686574858683675725765634063131
350133405277354723553294549397478955987876557987698878889985757654986568886863447837277147741536410
013062547146413742675348465764876557565879576696666889866579778749764578444847765768252647361443506
312465621677151875655585995745694878655956799697796697686885558978975449756474748887586542411256255
423204362353178745852254637935464664778869968879677697777878575777556878634856584473864213312622015
534661567344614365768387877864878845767598869677978779767787858784584759476648665773358427645311651
430011475352152575755679953749774454789788589769698678798856669687965675695986644657485514217351666
542102111432167877663467859536777594767895986867898886766676976869846495867599543374583332731266033
625000474732136887874855736749589476996659798967789898789776587976889996948384763734447643736220025
315666062115612845335864885945654758789765698688877769667668867576798855583978955268844425122514014
152600463716132562642488647595685976468985656996977998778955585795596495844759888838286175352560414
006166514645376457676454676698858546456775687876679968965697579748769964686887987774361312361623112
630201542341341448626638885984646765658869666987886976795798598788495887683399383777375346636211550
036646423755734847267336476354564466955596575589776859595968578788654499546534758266652277357366530
453243636357156364866489789657955759776966959655868798956766987954989947943863354645777345476202531
530105612477445332537878834449565659485976667656597697775688857778754844458355277846316112626121162
055455363734131537735589765579697845958456556556686566965687745949576967556763233558753142411453220
106052304147675524263254984449336844955757978879668668576768676787755787574533445667862426312335351
133545504422337378456858864594449878578885567885775668878958786485657683335482267532317234560541422
200255566345515714753376495455776859698895897588685996587586745857468367437854554745116413532363200
350510465672221255867873257334876685756745545667796896497688464848948768947422782828661471761064122
525006435255273146277854349566333878494888868995668459955986944485699959837552728523754132365504644
455465632474322677526353348496893746878784677485547847847759558643378358396575444475242721722332551
341264465466426473255828826967974346556896576764466849697759555548879644597626468762475471232114332
143015600661254143155723453874645479598897489897887869959899767545558694586565824554175726556330040
133114601616545361237744836629659733553959758846995589848895753645437876746537567247223752042644353
434245136445331567724385257642784846854365449776854954774496353883589792252488545733673623365531411
242355400240614257475587442835547333788435758749676988545464355973893552465258322436517445530103424
150522445522362651727687768785836854563533374767758439957544655684349342343244731321332645111541021
552400436500115714126552863645543555378537389683366874478896898884353658447572421457611323114255212
304202302265264467247666485428778363698453577569497785947535758344375242826567265416457044433163541
233353455013125235351275368356247267348486886475363374634485474876565735683724532261524142510630102
440535161152151631635373575637235623878689436669334497565478939626735535482665761716504623034023120
530315220006363246522576534738725655632837547375957696853688457354338383534653311344113001640515414
204313020634510554536253474458753547425233336944493739858844757374686252224513224773652513353305442
303341052510105201513731753724282223364752725537642288866686622588863837445543126664241453441235310
020424153032624323671363111463422353477742627478683844868633432636588536274451572264610340614104555
234413051424523314161146731763467724436346548732688655832363463687352214147256367321403115315443500
012242303516512442500774532142266284382765822428622346384737664242643615627661273552155526054233342
414111403514124460666037555342627216676675426728525487642273658483463277637667635142640425425132342
030230422403061322643446763675767367778368767234652457668385774272636771211474352411225452213412310
032113014010425161346051473351752565267374786865733735338367253243133431751566100563025504520403342
412304104125143243301236345151712624614211575411475821376435722564217622616521260005512052222004343
220120030303233310444012046257231472762352112415164732622531735165124331120326541545011444410002400
014433054545451316643032015010316146277161216552272145522146562717437536450604320052302441034404241
343434423453055414653253445655564632771244123251647272745743671776331230215004615545255055050133340
201000210435340452132116022411356245227731333555165752717373435156310453501204235431002250524221113
132100434242305201202434465065260455543457427521464157317741755225245256645305422324552252031144120
202441110432420500412256514006155406431013256445476676323751251451145064255351541053031130431201234
020212022220112430245130010061216066220264013366625475240550163452521001005224125240541123033121143
132244304123021525001211154544140012602120125510011436101445405650606625263223310430540142302200433
103142142201203130045124150512613540504163320002045004044613203062515015344450101154534004044231012
221234114210331020511522535110011636160005413424224560604634052506210501451443521502544433404042033
001233231400214434040102054514026055053336624361305231213331535242260044242320014551114014133012203
013221201313013142535111451534205341402225520043535003305340330410530234344250234251433044340323003

0 comments on commit 3489d52

Please sign in to comment.