diff --git a/2018/day_07/input b/2018/day_07/input new file mode 100644 index 0000000..399cfcf --- /dev/null +++ b/2018/day_07/input @@ -0,0 +1,101 @@ +Step Q must be finished before step I can begin. +Step B must be finished before step M can begin. +Step R must be finished before step F can begin. +Step G must be finished before step S can begin. +Step M must be finished before step A can begin. +Step Z must be finished before step W can begin. +Step J must be finished before step C can begin. +Step K must be finished before step O can begin. +Step C must be finished before step I can begin. +Step Y must be finished before step L can begin. +Step N must be finished before step P can begin. +Step S must be finished before step X can begin. +Step E must be finished before step U can begin. +Step U must be finished before step V can begin. +Step D must be finished before step F can begin. +Step W must be finished before step H can begin. +Step T must be finished before step I can begin. +Step H must be finished before step V can begin. +Step L must be finished before step O can begin. +Step P must be finished before step A can begin. +Step A must be finished before step I can begin. +Step F must be finished before step O can begin. +Step V must be finished before step X can begin. +Step I must be finished before step O can begin. +Step X must be finished before step O can begin. +Step F must be finished before step V can begin. +Step L must be finished before step P can begin. +Step Y must be finished before step P can begin. +Step Y must be finished before step X can begin. +Step Y must be finished before step O can begin. +Step D must be finished before step A can begin. +Step T must be finished before step F can begin. +Step W must be finished before step X can begin. +Step R must be finished before step A can begin. +Step E must be finished before step F can begin. +Step H must be finished before step I can begin. +Step K must be finished before step Y can begin. +Step W must be finished before step P can begin. +Step V must be finished before step O can begin. +Step N must be finished before step E can begin. +Step L must be finished before step I can begin. +Step B must be finished before step G can begin. +Step D must be finished before step T can begin. +Step J must be finished before step L can begin. +Step M must be finished before step Y can begin. +Step T must be finished before step A can begin. +Step K must be finished before step D can begin. +Step H must be finished before step P can begin. +Step P must be finished before step I can begin. +Step T must be finished before step L can begin. +Step J must be finished before step N can begin. +Step U must be finished before step F can begin. +Step U must be finished before step I can begin. +Step A must be finished before step F can begin. +Step U must be finished before step P can begin. +Step R must be finished before step H can begin. +Step G must be finished before step V can begin. +Step P must be finished before step F can begin. +Step B must be finished before step D can begin. +Step U must be finished before step X can begin. +Step K must be finished before step A can begin. +Step G must be finished before step D can begin. +Step N must be finished before step U can begin. +Step U must be finished before step L can begin. +Step M must be finished before step J can begin. +Step I must be finished before step X can begin. +Step H must be finished before step L can begin. +Step M must be finished before step S can begin. +Step E must be finished before step O can begin. +Step Q must be finished before step F can begin. +Step A must be finished before step O can begin. +Step T must be finished before step P can begin. +Step F must be finished before step X can begin. +Step D must be finished before step P can begin. +Step A must be finished before step X can begin. +Step G must be finished before step Z can begin. +Step W must be finished before step F can begin. +Step Q must be finished before step X can begin. +Step C must be finished before step V can begin. +Step L must be finished before step V can begin. +Step E must be finished before step L can begin. +Step B must be finished before step X can begin. +Step M must be finished before step V can begin. +Step F must be finished before step I can begin. +Step P must be finished before step X can begin. +Step C must be finished before step A can begin. +Step Z must be finished before step H can begin. +Step Q must be finished before step S can begin. +Step G must be finished before step X can begin. +Step T must be finished before step O can begin. +Step P must be finished before step O can begin. +Step T must be finished before step V can begin. +Step N must be finished before step V can begin. +Step Z must be finished before step X can begin. +Step L must be finished before step X can begin. +Step Z must be finished before step Y can begin. +Step N must be finished before step T can begin. +Step S must be finished before step T can begin. +Step G must be finished before step K can begin. +Step T must be finished before step X can begin. +Step R must be finished before step X can begin. diff --git a/2018/day_07/input_less b/2018/day_07/input_less new file mode 100644 index 0000000..1dfd2ea --- /dev/null +++ b/2018/day_07/input_less @@ -0,0 +1,7 @@ +Step C must be finished before step A can begin. +Step C must be finished before step F can begin. +Step A must be finished before step B can begin. +Step A must be finished before step D can begin. +Step B must be finished before step E can begin. +Step D must be finished before step E can begin. +Step F must be finished before step E can begin. \ No newline at end of file diff --git a/2018/day_07/main.go b/2018/day_07/main.go new file mode 100644 index 0000000..56e0018 --- /dev/null +++ b/2018/day_07/main.go @@ -0,0 +1,176 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "sort" + "strings" +) + +type step struct { + ID string + time int + next []*step + prev []*step +} + +func alreadyExecuted(executed []string, steps []*step) bool { + for _, s := range steps { + found := false + for _, e := range executed { + if e == s.ID { + found = true + } + } + if !found { + return false + } + } + return true +} + +func alreadyInQueue(queue []string, s *step) bool { + for _, q := range queue { + if s.ID == q { + return true + } + } + return false +} + +func main() { + var queue []string + var executionOrder []string + stepAddr := make(map[string]*step) + + file, err := os.Open("./input_less") + if err != nil { + log.Fatal(err) + } + defer file.Close() + + scan := bufio.NewScanner(file) + for scan.Scan() { + line := scan.Text() + + splittedLine := strings.Split(line, " ") + currentID := splittedLine[1] + nextID := splittedLine[7] + + currentStep, csExists := stepAddr[currentID] + nextStep, nsExists := stepAddr[nextID] + + if !csExists { + cs := step{ID: currentID, time: int(currentID[0]) - 64} + stepAddr[currentID] = &cs + currentStep = &cs + } + if !nsExists { + ns := step{ID: nextID, time: int(nextID[0]) - 64} + stepAddr[nextID] = &ns + nextStep = &ns + } + + currentStep.next = append(currentStep.next, nextStep) + nextStep.prev = append(nextStep.prev, currentStep) + } + + for _, s := range stepAddr { + if len(s.prev) == 0 { + queue = append(queue, s.ID) + } + } + sort.Strings(queue) + i := 0 + for i < len(queue) { + sPrev := stepAddr[queue[i]].prev + if len(sPrev) == 0 || alreadyExecuted(executionOrder, sPrev) { + executionOrder = append(executionOrder, queue[i]) + for _, n := range stepAddr[queue[i]].next { + if !alreadyInQueue(queue, n) { + queue = append(queue, n.ID) + } + } + + queue = append(queue[:i], queue[i+1:]...) + + sort.Strings(queue) + i = 0 + } else { + i++ + } + } + + fmt.Printf("Part One: %v\n", strings.Join(executionOrder, "")) + + for _, s := range stepAddr { + if len(s.prev) == 0 { + queue = append(queue, s.ID) + fmt.Printf("Added in queue: %v\n", s.ID) + } + } + sort.Strings(queue) + + currentTime := 0 + var times [2]int + var workers [2]string + var availableSteps []*step + working := false + executionOrder = make([]string, 0) + for len(queue) > 0 || len(availableSteps) > 0 || working { + if len(queue) > 0 { + for i := 0; i < len(queue); i++ { + fmt.Printf("Analyzing from queue step %v\t i:%v\n", queue[i], i) + sPrev := stepAddr[queue[i]].prev + if len(sPrev) == 0 || alreadyExecuted(executionOrder, sPrev) { + availableSteps = append(availableSteps, stepAddr[queue[i]]) + for _, n := range stepAddr[queue[i]].next { + if !alreadyInQueue(queue, n) { + queue = append(queue, n.ID) + fmt.Printf("Added in queue: %v\n", n.ID) + } + } + fmt.Printf("Added to availableSteps: %v\n", queue[i]) + fmt.Printf("Removed from queue: %v\n", queue[i]) + queue = append(queue[:i], queue[i+1:]...) + i = -1 + } + } + } + sort.Slice(availableSteps, func(i, j int) bool { + return availableSteps[i].ID < availableSteps[j].ID + }) + if len(availableSteps) > 0 || working { + for w := 0; w < len(workers); w++ { + if workers[w] == "" && len(availableSteps) > 0 { + workers[w] = availableSteps[0].ID + times[w] = availableSteps[0].time + working = true + fmt.Printf("Assigned to worker %v step %v\n", w, availableSteps[0].ID) + fmt.Printf("Removed from availableSteps: %v\n", availableSteps[0].ID) + availableSteps = availableSteps[1:] + } else if workers[w] != "" { + times[w]-- + fmt.Printf("Decreased time of worker %v to %v\n", w, times[w]) + if times[w] == 0 { + executionOrder = append(executionOrder, workers[w]) + fmt.Printf("Worker %v has completed step %v\n", w, workers[w]) + workers[w] = "" + times[w] = 0 + //if times[0]+times[1]+times[2]+times[3]+times[4] == 0 { + if times[0]+times[1] == 0 { + working = false + } + } + } + } + if working { + currentTime++ + fmt.Printf("Increaset currentTime to %v\n", currentTime) + } + } + } + fmt.Printf("Part Two: %v\t%v\n", currentTime, executionOrder) +} diff --git a/2018/day_07/prova.go b/2018/day_07/prova.go new file mode 100644 index 0000000..8b9dcb8 --- /dev/null +++ b/2018/day_07/prova.go @@ -0,0 +1,170 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "sort" +) + +type graph map[string][]string + +type task struct { + time int + task string +} + +func popMinimumTask(tasks []task) (int, string, []task) { + var minTime = 2147483647 + var minTasks = make([]task, 0) + for _, t := range tasks { + if t.time < minTime { + minTasks = []task{t} + minTime = t.time + } else if t.time == minTime { + minTasks = append(minTasks, t) + } + } + + var minTask task + var minID = string("Z") + + for _, t := range minTasks { + if t.task <= minID { + minID = t.task + minTask = t + } + } + + var removed = make([]task, 0) + for _, val := range tasks { + if !(val.task == minTask.task && val.time == minTask.time) { + removed = append(removed, val) + } + } + + return minTask.time, minTask.task, removed +} + +func popMinimumString(strs []string) (string, []string) { + if len(strs) == 0 { + return "", nil + } + min := strs[0] + for _, str := range strs { + if str < min { + min = str + } + } + var removed = make([]string, 0) + for _, str := range strs { + if str != min { + removed = append(removed, str) + } + } + return min, removed +} + +func main() { + + var gr = make(graph) + var incoming = make(map[string]int) + var incomingSaved = make(map[string]int) // for Part 2 + + file, err := os.Open("input") + if err != nil { + log.Fatal(err) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + + var start, end string + fmt.Sscanf(line, "Step %s must be finished before step %s can begin.", &start, &end) + + if _, ok := gr[start]; ok { + gr[start] = append(gr[start], end) + } else { + gr[start] = []string{end} + } + + if _, ok := incoming[end]; ok { + incoming[end]++ + incomingSaved[end]++ + } else { + incoming[end] = 1 + incomingSaved[end] = 1 + } + } + + var queue, taskQueue = make([]string, 0), make([]string, 0) + + for key, value := range gr { + sort.Strings(value) + gr[key] = value + if incoming[key] == 0 { + queue = append(queue, key) + } + } + + taskQueue = append(taskQueue, queue...) + + var result string + for len(queue) != 0 { + sort.Strings(queue) + init := queue[0] + var temp []string + for _, v := range queue { + if v != init { + temp = append(temp, v) + } + } + queue = temp + result += init + for _, key := range gr[init] { + incoming[key]-- + if incoming[key] == 0 { + queue = append(queue, key) + } + } + } + + fmt.Printf("Part 1: %s\n", result) + + // Part 2 + var currentTime int + var currentTask string + var tasks = make([]task, 0) + + for len(tasks) < 5 && len(taskQueue) > 0 { + var next string + if nxt, q := popMinimumString(taskQueue); q != nil { + next = nxt + taskQueue = q + tasks = append(tasks, task{time: currentTime + int([]byte(next)[0]) - 4, task: next}) + } + } + + for len(tasks) > 0 || len(taskQueue) > 0 { + currentTime, currentTask, tasks = popMinimumTask(tasks) + for _, key := range gr[currentTask] { + incomingSaved[key]-- + if incomingSaved[key] == 0 { + taskQueue = append(taskQueue, key) + } + } + for len(tasks) < 5 && len(taskQueue) > 0 { + var next string + if nxt, q := popMinimumString(taskQueue); q != nil { + next = nxt + taskQueue = q + tasks = append(tasks, task{time: currentTime + int([]byte(next)[0]) - 4, task: next}) + } + } + } + + fmt.Printf("Part 2: %v\n", currentTime) +} diff --git a/2018/day_10/input b/2018/day_10/input new file mode 100644 index 0000000..19789be --- /dev/null +++ b/2018/day_10/input @@ -0,0 +1,367 @@ +position=< 52484, -20780> velocity=<-5, 2> +position=<-52068, 31483> velocity=< 5, -3> +position=< 21120, 52398> velocity=<-2, -5> +position=<-10264, -31236> velocity=< 1, 3> +position=< 52501, 52392> velocity=<-5, -5> +position=<-31144, 52394> velocity=< 3, -5> +position=<-41603, 21031> velocity=< 4, -2> +position=<-41630, -31239> velocity=< 4, 3> +position=< 21126, -20780> velocity=<-2, 2> +position=<-31150, -10326> velocity=< 3, 1> +position=<-41598, 21033> velocity=< 4, -2> +position=<-10252, -20787> velocity=< 1, 2> +position=<-20677, -52149> velocity=< 2, 5> +position=< 21102, -31236> velocity=<-2, 3> +position=<-20734, 41942> velocity=< 2, -4> +position=< 42018, -41696> velocity=<-4, 4> +position=<-52071, -41691> velocity=< 5, 4> +position=<-20720, -41693> velocity=< 2, 4> +position=<-20710, 10574> velocity=< 2, -1> +position=< 31540, 10578> velocity=<-3, -1> +position=< 31581, -41693> velocity=<-3, 4> +position=< 31540, 10576> velocity=<-3, -1> +position=<-31184, 31490> velocity=< 3, -3> +position=<-31168, -41689> velocity=< 3, 4> +position=<-41641, 10575> velocity=< 4, -1> +position=<-41590, 41940> velocity=< 4, -4> +position=<-31179, -41695> velocity=< 3, 4> +position=<-10232, 31481> velocity=< 1, -3> +position=< 42012, 31481> velocity=<-4, -3> +position=<-31192, 31486> velocity=< 3, -3> +position=< 10685, 21029> velocity=<-1, -2> +position=< 31535, -41692> velocity=<-3, 4> +position=< 31561, 41942> velocity=<-3, -4> +position=<-20719, 31481> velocity=< 2, -3> +position=< 42007, 10581> velocity=<-4, -1> +position=<-20726, 21027> velocity=< 2, -2> +position=<-31136, 21034> velocity=< 3, -2> +position=< 10680, -31241> velocity=<-1, 3> +position=< 31559, 21027> velocity=<-3, -2> +position=< 21088, -20780> velocity=<-2, 2> +position=< 52472, 41939> velocity=<-5, -4> +position=< 21126, -41692> velocity=<-2, 4> +position=<-10268, 41937> velocity=< 1, -4> +position=< 31551, 52393> velocity=<-3, -5> +position=<-52097, 10578> velocity=< 5, -1> +position=<-52087, -52145> velocity=< 5, 5> +position=<-10223, 21034> velocity=< 1, -2> +position=< 52483, -20785> velocity=<-5, 2> +position=< 10660, -20780> velocity=<-1, 2> +position=< 10648, -41694> velocity=<-1, 4> +position=< 41988, -31239> velocity=<-4, 3> +position=< 42010, -52149> velocity=<-4, 5> +position=< 52440, -10327> velocity=<-5, 1> +position=< 41991, 41943> velocity=<-4, -4> +position=< 10636, -52142> velocity=<-1, 5> +position=<-52071, -52146> velocity=< 5, 5> +position=< 10672, -31237> velocity=<-1, 3> +position=<-52095, 31484> velocity=< 5, -3> +position=< 31568, -41688> velocity=<-3, 4> +position=<-10236, -10331> velocity=< 1, 1> +position=<-52095, 21031> velocity=< 5, -2> +position=<-31187, 21027> velocity=< 3, -2> +position=< 31577, 21036> velocity=<-3, -2> +position=<-10284, -20784> velocity=< 1, 2> +position=<-10271, -20783> velocity=< 1, 2> +position=< 10650, -10335> velocity=<-1, 1> +position=<-20706, -41694> velocity=< 2, 4> +position=< 10636, 31490> velocity=<-1, -3> +position=<-41590, 21028> velocity=< 4, -2> +position=< 52480, -52142> velocity=<-5, 5> +position=<-52050, -31239> velocity=< 5, 3> +position=<-31183, -10326> velocity=< 3, 1> +position=< 42038, 52393> velocity=<-4, -5> +position=<-31176, 31486> velocity=< 3, -3> +position=<-10276, 10581> velocity=< 1, -1> +position=<-31168, -31236> velocity=< 3, 3> +position=< 41986, -52151> velocity=<-4, 5> +position=< 10656, 52398> velocity=<-1, -5> +position=< 42046, 52393> velocity=<-4, -5> +position=<-31139, 41936> velocity=< 3, -4> +position=< 41991, -20786> velocity=<-4, 2> +position=<-10243, 21033> velocity=< 1, -2> +position=<-41641, 31488> velocity=< 4, -3> +position=<-52095, 21032> velocity=< 5, -2> +position=<-20728, 31490> velocity=< 2, -3> +position=<-31163, -31237> velocity=< 3, 3> +position=< 41999, 52392> velocity=<-4, -5> +position=<-10279, 31485> velocity=< 1, -3> +position=<-10275, 31481> velocity=< 1, -3> +position=< 52484, 21030> velocity=<-5, -2> +position=< 31537, -20785> velocity=<-3, 2> +position=< 52493, -52148> velocity=<-5, 5> +position=< 21129, -52147> velocity=<-2, 5> +position=<-10241, 52398> velocity=< 1, -5> +position=<-10279, 31489> velocity=< 1, -3> +position=< 52496, -10330> velocity=<-5, 1> +position=< 10651, -41692> velocity=<-1, 4> +position=< 52480, -10326> velocity=<-5, 1> +position=<-41596, 41939> velocity=< 4, -4> +position=<-31174, -52151> velocity=< 3, 5> +position=< 42014, -41692> velocity=<-4, 4> +position=< 41986, -10334> velocity=<-4, 1> +position=< 10656, -20781> velocity=<-1, 2> +position=< 31564, 41935> velocity=<-3, -4> +position=<-20717, -20787> velocity=< 2, 2> +position=< 31540, -20789> velocity=<-3, 2> +position=<-10265, -41692> velocity=< 1, 4> +position=<-10260, 41940> velocity=< 1, -4> +position=<-10247, -10326> velocity=< 1, 1> +position=< 21094, -10328> velocity=<-2, 1> +position=<-41646, -52142> velocity=< 4, 5> +position=<-10284, 52395> velocity=< 1, -5> +position=< 21086, 52394> velocity=<-2, -5> +position=< 52500, -31239> velocity=<-5, 3> +position=<-20689, -41697> velocity=< 2, 4> +position=<-41636, 52389> velocity=< 4, -5> +position=< 42026, 41943> velocity=<-4, -4> +position=< 31549, -31239> velocity=<-3, 3> +position=< 52488, 52398> velocity=<-5, -5> +position=< 21086, -52145> velocity=<-2, 5> +position=< 31573, -31237> velocity=<-3, 3> +position=< 21139, 10573> velocity=<-2, -1> +position=<-31188, -20781> velocity=< 3, 2> +position=< 52488, 21035> velocity=<-5, -2> +position=<-10273, 52398> velocity=< 1, -5> +position=<-41642, 10581> velocity=< 4, -1> +position=< 42034, -10328> velocity=<-4, 1> +position=<-31187, 31489> velocity=< 3, -3> +position=< 41994, -10328> velocity=<-4, 1> +position=< 21110, -41692> velocity=<-2, 4> +position=< 41994, -31236> velocity=<-4, 3> +position=< 31532, -41692> velocity=<-3, 4> +position=<-41645, -41696> velocity=< 4, 4> +position=<-41617, -52148> velocity=< 4, 5> +position=< 42027, 21036> velocity=<-4, -2> +position=<-10268, 21029> velocity=< 1, -2> +position=<-41614, -20785> velocity=< 4, 2> +position=< 21097, -31238> velocity=<-2, 3> +position=< 10645, 10575> velocity=<-1, -1> +position=<-10243, 41944> velocity=< 1, -4> +position=< 21118, 21034> velocity=<-2, -2> +position=< 41994, -41691> velocity=<-4, 4> +position=< 21083, -31240> velocity=<-2, 3> +position=<-41596, 52389> velocity=< 4, -5> +position=<-31172, -10328> velocity=< 3, 1> +position=< 52464, 41944> velocity=<-5, -4> +position=<-31176, 52398> velocity=< 3, -5> +position=< 10669, -10326> velocity=<-1, 1> +position=<-31172, -10328> velocity=< 3, 1> +position=< 10653, 41940> velocity=<-1, -4> +position=<-10242, -20784> velocity=< 1, 2> +position=<-10279, -31234> velocity=< 1, 3> +position=< 21134, 10573> velocity=<-2, -1> +position=<-31160, -31239> velocity=< 3, 3> +position=< 21126, 41935> velocity=<-2, -4> +position=<-41617, 31485> velocity=< 4, -3> +position=<-10279, 10574> velocity=< 1, -1> +position=< 10672, 21027> velocity=<-1, -2> +position=< 42022, -20780> velocity=<-4, 2> +position=<-52100, 10573> velocity=< 5, -1> +position=<-52052, -20788> velocity=< 5, 2> +position=< 52461, 31489> velocity=<-5, -3> +position=< 10672, 10577> velocity=<-1, -1> +position=< 21094, -10334> velocity=<-2, 1> +position=<-52074, -31238> velocity=< 5, 3> +position=<-10249, 10582> velocity=< 1, -1> +position=< 52480, -31235> velocity=<-5, 3> +position=< 42042, 10582> velocity=<-4, -1> +position=< 52496, 31481> velocity=<-5, -3> +position=<-31136, 10578> velocity=< 3, -1> +position=<-52095, -41695> velocity=< 5, 4> +position=< 52460, 21027> velocity=<-5, -2> +position=< 21107, -41695> velocity=<-2, 4> +position=<-52051, -52147> velocity=< 5, 5> +position=< 31564, 31487> velocity=<-3, -3> +position=< 52481, -31237> velocity=<-5, 3> +position=<-20722, -31241> velocity=< 2, 3> +position=<-20735, -41692> velocity=< 2, 4> +position=<-52079, 52390> velocity=< 5, -5> +position=< 41994, -52142> velocity=<-4, 5> +position=< 52497, -20785> velocity=<-5, 2> +position=<-41638, 10577> velocity=< 4, -1> +position=<-31175, -20789> velocity=< 3, 2> +position=<-20719, 21031> velocity=< 2, -2> +position=< 52460, 52389> velocity=<-5, -5> +position=<-31136, -31237> velocity=< 3, 3> +position=< 21086, 52397> velocity=<-2, -5> +position=<-52084, 21027> velocity=< 5, -2> +position=< 41994, -41694> velocity=<-4, 4> +position=<-20706, 31486> velocity=< 2, -3> +position=<-20682, -52147> velocity=< 2, 5> +position=< 41999, 41942> velocity=<-4, -4> +position=< 41994, -52151> velocity=<-4, 5> +position=< 41986, 41943> velocity=<-4, -4> +position=<-20738, -20788> velocity=< 2, 2> +position=< 52464, -20780> velocity=<-5, 2> +position=< 31543, 31485> velocity=<-3, -3> +position=< 52448, -10334> velocity=<-5, 1> +position=<-20713, 31486> velocity=< 2, -3> +position=< 21099, 10576> velocity=<-2, -1> +position=< 52440, 31490> velocity=<-5, -3> +position=< 52441, -31241> velocity=<-5, 3> +position=< 42002, -52147> velocity=<-4, 5> +position=< 52469, 31483> velocity=<-5, -3> +position=< 10658, -41688> velocity=<-1, 4> +position=<-10273, 21031> velocity=< 1, -2> +position=<-52083, -10331> velocity=< 5, 1> +position=<-52044, -31234> velocity=< 5, 3> +position=< 21110, 31482> velocity=<-2, -3> +position=<-41590, -52145> velocity=< 4, 5> +position=<-20689, 10577> velocity=< 2, -1> +position=< 31593, -41697> velocity=<-3, 4> +position=< 10673, 41935> velocity=<-1, -4> +position=<-20677, -10331> velocity=< 2, 1> +position=< 52469, 41940> velocity=<-5, -4> +position=< 31593, -31242> velocity=<-3, 3> +position=< 52467, -41697> velocity=<-5, 4> +position=< 42007, 10582> velocity=<-4, -1> +position=<-31173, -41692> velocity=< 3, 4> +position=< 21083, 21032> velocity=<-2, -2> +position=<-20730, 41939> velocity=< 2, -4> +position=< 31574, -10335> velocity=<-3, 1> +position=< 21139, -52143> velocity=<-2, 5> +position=<-52099, -52150> velocity=< 5, 5> +position=<-10252, 52398> velocity=< 1, -5> +position=< 42047, 21029> velocity=<-4, -2> +position=< 42030, 31484> velocity=<-4, -3> +position=<-10268, -41697> velocity=< 1, 4> +position=< 21080, 31484> velocity=<-2, -3> +position=< 41997, 41935> velocity=<-4, -4> +position=< 10653, -41693> velocity=<-1, 4> +position=< 21089, -52142> velocity=<-2, 5> +position=< 10652, 31486> velocity=<-1, -3> +position=< 21107, -52145> velocity=<-2, 5> +position=<-20718, -10335> velocity=< 2, 1> +position=< 21094, 41941> velocity=<-2, -4> +position=< 21097, 41935> velocity=<-2, -4> +position=< 10624, -20785> velocity=<-1, 2> +position=< 21078, -10328> velocity=<-2, 1> +position=< 52464, 41938> velocity=<-5, -4> +position=< 42006, 21031> velocity=<-4, -2> +position=< 10675, -20789> velocity=<-1, 2> +position=<-20677, 52396> velocity=< 2, -5> +position=<-52095, 52396> velocity=< 5, -5> +position=< 21079, -10333> velocity=<-2, 1> +position=<-41589, 10577> velocity=< 4, -1> +position=< 31556, -41691> velocity=<-3, 4> +position=< 31545, 31482> velocity=<-3, -3> +position=< 10644, 31487> velocity=<-1, -3> +position=<-20706, -41690> velocity=< 2, 4> +position=<-10266, 31481> velocity=< 1, -3> +position=< 21087, -41693> velocity=<-2, 4> +position=<-10239, -41695> velocity=< 1, 4> +position=<-52090, 21031> velocity=< 5, -2> +position=<-10236, 10578> velocity=< 1, -1> +position=<-10236, 41942> velocity=< 1, -4> +position=< 21086, 31483> velocity=<-2, -3> +position=<-31176, -10330> velocity=< 3, 1> +position=< 31574, -10330> velocity=<-3, 1> +position=<-41598, 31482> velocity=< 4, -3> +position=<-52087, -52144> velocity=< 5, 5> +position=< 21090, -31243> velocity=<-2, 3> +position=<-20695, -41693> velocity=< 2, 4> +position=< 31574, -41692> velocity=<-3, 4> +position=<-10240, -52148> velocity=< 1, 5> +position=< 42042, 31481> velocity=<-4, -3> +position=< 41986, 31481> velocity=<-4, -3> +position=< 52484, -10326> velocity=<-5, 1> +position=< 31593, -10330> velocity=<-3, 1> +position=< 31548, 52398> velocity=<-3, -5> +position=<-20719, 21027> velocity=< 2, -2> +position=<-20697, -31243> velocity=< 2, 3> +position=< 10680, -20781> velocity=<-1, 2> +position=<-52088, 21031> velocity=< 5, -2> +position=<-31176, 41943> velocity=< 3, -4> +position=< 41996, -52147> velocity=<-4, 5> +position=<-31176, 41944> velocity=< 3, -4> +position=<-52056, -41697> velocity=< 5, 4> +position=< 31593, 41944> velocity=<-3, -4> +position=< 21099, -41695> velocity=<-2, 4> +position=<-20697, -31243> velocity=< 2, 3> +position=< 31588, 41938> velocity=<-3, -4> +position=<-20728, 21031> velocity=< 2, -2> +position=<-31149, 21027> velocity=< 3, -2> +position=<-10223, -10329> velocity=< 1, 1> +position=<-10236, 21034> velocity=< 1, -2> +position=< 10632, -10333> velocity=<-1, 1> +position=< 10680, 10581> velocity=<-1, -1> +position=<-41641, -41688> velocity=< 4, 4> +position=< 31545, -41696> velocity=<-3, 4> +position=<-31139, 41937> velocity=< 3, -4> +position=<-31189, 52395> velocity=< 3, -5> +position=< 42036, -10335> velocity=<-4, 1> +position=< 41998, 31485> velocity=<-4, -3> +position=<-41637, -10326> velocity=< 4, 1> +position=<-20682, -20787> velocity=< 2, 2> +position=<-10241, 31490> velocity=< 1, -3> +position=< 21135, -31239> velocity=<-2, 3> +position=< 31561, -31240> velocity=<-3, 3> +position=<-31192, -20786> velocity=< 3, 2> +position=<-10236, 41944> velocity=< 1, -4> +position=<-52059, -41697> velocity=< 5, 4> +position=<-20733, -20787> velocity=< 2, 2> +position=< 31540, -20781> velocity=<-3, 2> +position=<-52055, 10573> velocity=< 5, -1> +position=<-31152, -31236> velocity=< 3, 3> +position=< 52496, -31240> velocity=<-5, 3> +position=<-52052, 31484> velocity=< 5, -3> +position=< 52453, 41940> velocity=<-5, -4> +position=<-41646, -10333> velocity=< 4, 1> +position=< 42042, 41941> velocity=<-4, -4> +position=<-31131, 21036> velocity=< 3, -2> +position=<-10281, -52145> velocity=< 1, 5> +position=<-41598, 31485> velocity=< 4, -3> +position=<-31179, -10327> velocity=< 3, 1> +position=< 41988, -10332> velocity=<-4, 1> +position=< 52456, 41936> velocity=<-5, -4> +position=<-41595, -10335> velocity=< 4, 1> +position=< 52445, 52390> velocity=<-5, -5> +position=<-20693, 31483> velocity=< 2, -3> +position=<-41630, 31484> velocity=< 4, -3> +position=< 41986, 10581> velocity=<-4, -1> +position=<-20734, 21035> velocity=< 2, -2> +position=<-10223, 52395> velocity=< 1, -5> +position=< 42012, -31238> velocity=<-4, 3> +position=< 52445, -31237> velocity=<-5, 3> +position=< 21110, -20784> velocity=<-2, 2> +position=<-31179, -10328> velocity=< 3, 1> +position=<-10251, 41944> velocity=< 1, -4> +position=< 31593, 21028> velocity=<-3, -2> +position=<-31167, -52150> velocity=< 3, 5> +position=< 42003, -52151> velocity=<-4, 5> +position=< 52485, -10334> velocity=<-5, 1> +position=<-20690, -20786> velocity=< 2, 2> +position=< 10625, -31242> velocity=<-1, 3> +position=<-52087, -10332> velocity=< 5, 1> +position=< 10672, -20783> velocity=<-1, 2> +position=< 42026, 31489> velocity=<-4, -3> +position=< 10672, 41937> velocity=<-1, -4> +position=< 41995, -41697> velocity=<-4, 4> +position=< 31572, 31481> velocity=<-3, -3> +position=<-20688, 52389> velocity=< 2, -5> +position=<-20725, 10574> velocity=< 2, -1> +position=<-10260, -10328> velocity=< 1, 1> +position=< 10685, 21034> velocity=<-1, -2> +position=< 52499, 10577> velocity=<-5, -1> +position=< 31536, 31488> velocity=<-3, -3> +position=<-10260, 41941> velocity=< 1, -4> +position=<-52089, -20789> velocity=< 5, 2> +position=< 41994, 21034> velocity=<-4, -2> +position=< 31549, -52147> velocity=<-3, 5> +position=< 31564, -10328> velocity=<-3, 1> +position=< 10645, 41943> velocity=<-1, -4> +position=<-31168, -52147> velocity=< 3, 5> +position=<-31187, 21034> velocity=< 3, -2> +position=<-41598, -31241> velocity=< 4, 3> +position=< 10653, 21035> velocity=<-1, -2> +position=< 31576, 52398> velocity=<-3, -5> +position=<-10249, 10582> velocity=< 1, -1> +position=<-31136, -31240> velocity=< 3, 3> +position=<-20712, -52151> velocity=< 2, 5> +position=<-10279, 10579> velocity=< 1, -1> +position=< 52469, -10326> velocity=<-5, 1> +position=< 21103, -20784> velocity=<-2, 2> +position=< 21136, -20785> velocity=<-2, 2> +position=<-41605, -52142> velocity=< 4, 5> +position=< 31551, -52147> velocity=<-3, 5> diff --git a/2018/day_10/main.go b/2018/day_10/main.go new file mode 100644 index 0000000..4e1763a --- /dev/null +++ b/2018/day_10/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" +) + +type point struct { + x, y int + Vx, Vy int +} + +func main() { + var points []*point + + file, err := os.Open("./input") + if err != nil { + log.Fatal(err) + } + defer file.Close() + + scan := bufio.NewScanner(file) + for scan.Scan() { + var p *point + + line := scan.Text() + fmt.Sscanf(line, "position=<%d, %d> velocity=<%d, %d>", &p.x, &p.y, &p.Vx, &p.Vy) + + points = append(points, p) + } +} diff --git a/2018/day_10/prova.go b/2018/day_10/prova.go new file mode 100644 index 0000000..328ed3b --- /dev/null +++ b/2018/day_10/prova.go @@ -0,0 +1,110 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" +) + +type point struct { + x, y int + vx, vy int +} + +func print(points []*point, xrange, yrange, x, y int) { + var res = make([][]string, yrange+1) + for i := range res { + res[i] = make([]string, xrange+1) + } + for i := 0; i < xrange+1; i++ { + for j := 0; j < yrange+1; j++ { + res[j][i] = " " + } + } + for _, p := range points { + res[(*p).y-y][(*p).x-x] = "*" + } + fmt.Println("Part 1: ") + for _, row := range res { + fmt.Println(row) + } +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} + +func move(points []*point, x, X, y, Y *int) { + var tx, tX, ty, tY int = 2147483647, -2147483648, 2147483647, -2147483648 + for _, val := range points { + (*val).x += (*val).vx + (*val).y += (*val).vy + tx = min(tx, (*val).x) + tX = max(tX, (*val).x) + ty = min(ty, (*val).y) + tY = max(tY, (*val).y) + } + *x, *X, *y, *Y = tx, tX, ty, tY +} + +func main() { + + file, err := os.Open("input") + if err != nil { + log.Fatal(err) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + + var points []*point + + var x, X, y, Y int = 2147483647, -2147483648, 2147483647, -2147483648 + + for scanner.Scan() { + p := new(point) + line := scanner.Text() + fmt.Sscanf(line, "position=<%d, %d> velocity=<%d, %d>", &p.x, &p.y, &p.vx, &p.vy) + points = append(points, p) + x = min(x, (*p).x) + X = max(X, (*p).x) + y = min(y, (*p).y) + Y = max(Y, (*p).y) + } + + var minAt, minVal int + minVal = (X - x) + (Y - y) + + for i := 1; i <= 100000; i++ { + move(points, &x, &X, &y, &Y) + current := (X - x) + (Y - y) + if current < minVal { + minVal = current + minAt = i + } + } + + var tx, tX, ty, tY int = 2147483647, -2147483648, 2147483647, -2147483648 + for _, p := range points { + (*p).x = (*p).x - (100000-minAt)*(*p).vx + (*p).y = (*p).y - (100000-minAt)*(*p).vy + tx = min(tx, (*p).x) + tX = max(tX, (*p).x) + ty = min(ty, (*p).y) + tY = max(tY, (*p).y) + } + + print(points, tX-tx, tY-ty, tx, ty) + fmt.Printf("Part 2: %v\n", minAt) +} diff --git a/2018/day_11/main.go b/2018/day_11/main.go new file mode 100644 index 0000000..c42e6f0 --- /dev/null +++ b/2018/day_11/main.go @@ -0,0 +1,98 @@ +package main + +import ( + "fmt" + "math" +) + +const gridSize int = 300 +const gridSerial int = 7347 + +func cellPower(x int, y int, serial int) int { + rackID := x + 1 + 10 + powerLevel := rackID * (y + 1) + powerLevel += serial + powerLevel *= rackID + powerLevel %= int(math.Pow(10, 3)) + powerLevel /= int(math.Pow(10, 2)) + + return powerLevel +} + +func maxPowerSquare(powerGrid *[gridSize][gridSize]int, powerGridCheck *[gridSize][gridSize]bool, size int, res chan<- [4]int) { + maxSum := 0 + maxSumCheck := false + maxX, maxY := 0, 0 + + for x := 0; x < gridSize-size+1; x++ { + for y := 0; y < gridSize-size+1; y++ { + powerSum := 0 + for xi := x; xi < x+size; xi++ { + for yi := y; yi < y+size; yi++ { + if !powerGridCheck[xi][yi] { + powerGrid[xi][yi] = cellPower(xi, yi, gridSerial) + powerGridCheck[xi][yi] = true + } + powerSum += powerGrid[xi][yi] + } + } + if powerSum > maxSum || !maxSumCheck { + maxSum = powerSum + maxSumCheck = true + maxX = x + maxY = y + } + } + } + res <- [4]int{size, maxSum, maxX + 1, maxY + 1} +} + +func main() { + + var powerGrid [gridSize][gridSize]int + var powerGridCheck [gridSize][gridSize]bool + var res = make(chan [4]int) + + go func() { + maxPowerSquare(&powerGrid, &powerGridCheck, 3, res) + close(res) + }() + p1 := <-res + fmt.Printf("Part One: %v,%v\n", p1[2], p1[3]) + + res = make(chan [4]int) + for i := 0; i < gridSize; i++ { + go func(i int) { + maxPowerSquare(&powerGrid, &powerGridCheck, i, res) + if i == gridSize-1 { + close(res) + } + }(i) + } + + maxSum := 0 + maxSumCheck := false + maxX, maxY := 0, 0 + maxSize := 0 + + for r := range res { + fmt.Printf("Size: %v\t Sum: %v\t x: %v\t y: %v\n", r[0], r[1], r[2], r[3]) + if r[1] > maxSum || !maxSumCheck { + maxSize = r[0] + maxSum = r[1] + maxSumCheck = true + maxX = r[2] + maxY = r[3] + } + } + fmt.Printf("Part Two: %v,%v,%v\n", maxX, maxY, maxSize) + + res = make(chan [4]int) + go func() { + maxPowerSquare(&powerGrid, &powerGridCheck, 299, res) + close(res) + }() + p1 = <-res + fmt.Printf("Part Two 299: %v,%v,%v\n", p1[1], p1[2], p1[3]) + +} diff --git a/2018/day_11/prova.go b/2018/day_11/prova.go new file mode 100644 index 0000000..400ffcf --- /dev/null +++ b/2018/day_11/prova.go @@ -0,0 +1,77 @@ +package main + +import ( + "fmt" + "strconv" +) + +func calculate(i int, j int, memo *[][]int, ps *[][]int, input int) int { + t := 0 + for x := i; x < i+3; x++ { + for y := j; y < j+3; y++ { + if (*memo)[y][x] != 0 { + t += (*memo)[y][x] + } else { + id := x + 10 + serial := (id*y + input) + hundreds := strconv.Itoa(serial * id) + var result int + if len(hundreds) >= 3 { + result, _ = strconv.Atoi(string(hundreds[len(hundreds)-3])) + } + result -= 5 + t += result + (*memo)[y][x] = result + (*ps)[y][x] = result + (*ps)[y-1][x] + (*ps)[y][x-1] - (*ps)[y-1][x-1] + } + } + } + return t +} + +func main() { + + input := 7347 + + var res, X, Y int + + // technically memo isn't needed anymore but I added partial sums after + // part 2 and didn't wanna clean up my code after for part 1 + memo := make([][]int, 301) + partialSums := make([][]int, 301) + for i := range memo { + memo[i] = make([]int, 301) + partialSums[i] = make([]int, 301) + } + + for i := 1; i <= 298; i++ { + for j := 1; j <= 298; j++ { + if c := calculate(i, j, &memo, &partialSums, input); c > res { + res = c + X = i + Y = j + } + } + } + + fmt.Printf("Part 1: %v, %v, %v\n", res, X, Y) + + res = 0 + var sz int + + for size := 1; size < 300; size++ { + for i := size; i <= 300; i++ { + for j := size; j <= 300; j++ { + if c := partialSums[i][j] - partialSums[i-size][j] - + partialSums[i][j-size] + partialSums[i-size][j-size]; c > res { + res = c + Y = i - size + 1 + X = j - size + 1 + sz = size + } + } + } + } + + fmt.Printf("Part 2: %v, %v, %v, %v\n", res, X, Y, sz) +} diff --git a/2019/day_07/day_07.go b/2019/day_07/day_07.go new file mode 100644 index 0000000..898df15 --- /dev/null +++ b/2019/day_07/day_07.go @@ -0,0 +1,161 @@ +package main + +import ( + "bufio" + "log" + "math" + "os" + "strconv" + "strings" + "sync" +) + +const ( + opcodeAdd = 1 + opcodeMultiply = 2 + opcodeInput = 3 + opcodeOutput = 4 + opcodeJumpIfTrue = 5 + opcodeJumpIfFalse = 6 + opcodeLessThan = 7 + opcodeEquals = 8 + opcodeHalt = 99 + + paramModePosition = 0 + paramModeImmediate = 1 +) + +func readValues(fileName string) []int { + file, err := os.Open(fileName) + if err != nil { + log.Fatal(err) + } + defer file.Close() + + var values []int + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + lineElements := strings.Split(scanner.Text(), ",") + for _, elem := range lineElements { + n, err := strconv.Atoi(elem) + if err != nil { + log.Fatalf("Cannot convert to int: %s\n", elem) + } + values = append(values, n) + } + } + return values +} + +func runIntcode(program []int, input <-chan int, output chan<- int, wg *sync.WaitGroup) { + values := append([]int(nil), program...) + instructionPointer := 0 + for values[instructionPointer] != opcodeHalt { + instruction := values[instructionPointer] + opCode := instruction % 100 + + paramAddress := func(n int) int { + mode := (instruction % int(math.Pow10(n+2))) / int(math.Pow10(n+1)) + switch mode { + case paramModeImmediate: + return instructionPointer + n + default: + return values[instructionPointer+n] + } + } + + switch opCode { + case opcodeInput: + values[paramAddress(1)] = <-input + instructionPointer += 2 + + case opcodeOutput: + output <- values[paramAddress(1)] + instructionPointer += 2 + + case opcodeAdd: + values[paramAddress(3)] = values[paramAddress(1)] + values[paramAddress(2)] + instructionPointer += 4 + + case opcodeMultiply: + values[paramAddress(3)] = values[paramAddress(1)] * values[paramAddress(2)] + instructionPointer += 4 + + case opcodeJumpIfTrue: + if values[paramAddress(1)] != 0 { + instructionPointer = values[paramAddress(2)] + } else { + instructionPointer += 3 + } + + case opcodeJumpIfFalse: + if values[paramAddress(1)] == 0 { + instructionPointer = values[paramAddress(2)] + } else { + instructionPointer += 3 + } + + case opcodeLessThan: + if values[paramAddress(1)] < values[paramAddress(2)] { + values[paramAddress(3)] = 1 + } else { + values[paramAddress(3)] = 0 + } + instructionPointer += 4 + + case opcodeEquals: + if values[paramAddress(1)] == values[paramAddress(2)] { + values[paramAddress(3)] = 1 + } else { + values[paramAddress(3)] = 0 + } + instructionPointer += 4 + + default: + log.Fatalf("Unknown opCode: %d\n", opCode) + } + } + wg.Done() +} + +func emulateAmplifiers(program []int, phases []int) int { + var wg sync.WaitGroup + + fromEtoA := make(chan int, 1) + fromAtoB := make(chan int) + fromBtoC := make(chan int) + fromCtoD := make(chan int) + fromDtoE := make(chan int) + + fromAtoB <- phases[0] + fromBtoC <- phases[1] + fromCtoD <- phases[2] + fromDtoE <- phases[3] + fromEtoA <- phases[4] + + fromEtoA <- 0 + + wg.Add(5) + go runIntcode(program, fromEtoA, fromAtoB, &wg) + go runIntcode(program, fromAtoB, fromBtoC, &wg) + go runIntcode(program, fromBtoC, fromCtoD, &wg) + go runIntcode(program, fromCtoD, fromDtoE, &wg) + go runIntcode(program, fromDtoE, fromEtoA, &wg) + + fromAtoB <- phases[0] + fromBtoC <- phases[1] + fromCtoD <- phases[2] + fromDtoE <- phases[3] + fromEtoA <- phases[4] + + fromEtoA <- 0 + + wg.Wait() + + return <-fromEtoA +} + +func main() { + //values := readValues("./input") +} diff --git a/2019/day_07/input b/2019/day_07/input new file mode 100644 index 0000000..f868832 --- /dev/null +++ b/2019/day_07/input @@ -0,0 +1 @@ +3,8,1001,8,10,8,105,1,0,0,21,34,43,64,85,98,179,260,341,422,99999,3,9,1001,9,3,9,102,3,9,9,4,9,99,3,9,102,5,9,9,4,9,99,3,9,1001,9,2,9,1002,9,4,9,1001,9,3,9,1002,9,4,9,4,9,99,3,9,1001,9,3,9,102,3,9,9,101,4,9,9,102,3,9,9,4,9,99,3,9,101,2,9,9,1002,9,3,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,99