7 Angajamente 01a1057b0b ... 8c08a78806

Autor SHA1 Permisiunea de a trimite mesaje. Dacă este dezactivată, utilizatorul nu va putea trimite nici un fel de mesaj Data
  Piotr Czajkowski 8c08a78806 Added description 1 săptămână în urmă
  Piotr Czajkowski a06d718446 Solved part2 1 săptămână în urmă
  Piotr Czajkowski 9b59b0fcda Still lost 1 săptămână în urmă
  Piotr Czajkowski 85de6e3fc7 Cleaner 1 săptămână în urmă
  Piotr Czajkowski 8b5a5da9ca Solved part1 1 săptămână în urmă
  Piotr Czajkowski 7edf795ae7 Able to compact 1 săptămână în urmă
  Piotr Czajkowski 5e78997998 Able to create a disk from map 1 săptămână în urmă
3 a modificat fișierele cu 243 adăugiri și 0 ștergeri
  1. 166 0
      09/code.go
  2. 77 0
      09/description.txt
  3. 0 0
      09/input

+ 166 - 0
09/code.go

@@ -0,0 +1,166 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"os"
+)
+
+func getDiskMap(data []byte) []int {
+	diskMap := make([]int, len(data))
+	for i, block := range data {
+		if block < 48 || block > 57 {
+			continue
+		}
+
+		diskMap[i] = int(block) - 48
+	}
+
+	return diskMap
+}
+
+func getDisk(diskMap []int) (int, []int) {
+	var disk []int
+	file := true
+	var fileID int
+
+	for _, number := range diskMap {
+		if file {
+			file = false
+			for j := 0; j < number; j++ {
+				disk = append(disk, fileID)
+			}
+
+			fileID++
+		} else {
+			file = true
+			for j := 0; j < number; j++ {
+				disk = append(disk, -1)
+			}
+		}
+	}
+
+	return diskMap[0], disk
+}
+
+func compact(disk []int, free int) []int {
+	end := len(disk) - 1
+	for free < end {
+		if disk[free] != -1 {
+			free++
+			continue
+		}
+
+		if disk[end] == -1 {
+			end--
+			continue
+		}
+
+		disk[free], disk[end] = disk[end], disk[free]
+		free++
+		end--
+	}
+
+	return disk
+}
+
+func calculateChecksum(disk []int) int64 {
+	var checksum int64
+
+	for i := range disk {
+		if disk[i] != -1 {
+			checksum += int64(disk[i] * i)
+		}
+	}
+
+	return checksum
+}
+
+func part1(diskMap []int) int64 {
+	free, disk := getDisk(diskMap)
+	compacted := compact(disk, free)
+
+	return calculateChecksum(compacted)
+}
+
+func findFile(disk []int, id, end int) int {
+	for i := end; i > 0; i-- {
+		if disk[i] != id {
+			return i + 1
+		}
+	}
+
+	return -1
+}
+
+func findFree(disk []int, start, end, goal int) (int, int) {
+	var freeStart, freeEnd int
+	for i := start; i < end; i++ {
+		if disk[i] == -1 {
+			if freeStart <= 0 {
+				freeStart = i
+				for j := freeStart + 1; j < freeStart+goal; j++ {
+					if disk[j] != -1 {
+						freeStart = -1
+						i = j
+						break
+					}
+				}
+
+				if freeStart > 0 {
+					return freeStart, freeStart + goal
+				}
+			}
+		}
+	}
+
+	return freeStart, freeEnd
+}
+
+func defrag(diskMap []int) []int {
+	free, disk := getDisk(diskMap)
+	end := len(disk) - 1
+
+	for i := end; i > free; i-- {
+		if disk[i] > 0 {
+			fileStart := findFile(disk, disk[i], i)
+			if fileStart > 0 {
+				freeStart, freeEnd := findFree(disk, free, fileStart, i-fileStart+1)
+				if freeStart > 0 && freeEnd > 0 && freeStart < freeEnd {
+					for k := freeStart; k < freeEnd; k++ {
+						disk[k] = disk[i]
+					}
+
+					for k := fileStart; k <= i; k++ {
+						disk[k] = -1
+					}
+				}
+
+				i = fileStart
+			}
+		}
+	}
+
+	return disk
+}
+
+func part2(diskMap []int) int64 {
+	defragmented := defrag(diskMap)
+
+	return calculateChecksum(defragmented)
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		log.Fatal("You need to specify a file!")
+	}
+
+	data, err := os.ReadFile(os.Args[1])
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	diskMap := getDiskMap(data)
+	fmt.Println("Part1:", part1(diskMap))
+	fmt.Println("Part2:", part2(diskMap))
+}

+ 77 - 0
09/description.txt

@@ -0,0 +1,77 @@
+--- Day 9: Disk Fragmenter ---
+
+Another push of the button leaves you in the familiar hallways of some friendly amphipods! Good thing you each somehow got your own personal mini submarine. The Historians jet away in search of the Chief, mostly by driving directly into walls.
+
+While The Historians quickly figure out how to pilot these things, you notice an amphipod in the corner struggling with his computer. He's trying to make more contiguous free space by compacting all of the files, but his program isn't working; you offer to help.
+
+He shows you the disk map (your puzzle input) he's already generated. For example:
+
+2333133121414131402
+
+The disk map uses a dense format to represent the layout of files and free space on the disk. The digits alternate between indicating the length of a file and the length of free space.
+
+So, a disk map like 12345 would represent a one-block file, two blocks of free space, a three-block file, four blocks of free space, and then a five-block file. A disk map like 90909 would represent three nine-block files in a row (with no free space between them).
+
+Each file on disk also has an ID number based on the order of the files as they appear before they are rearranged, starting with ID 0. So, the disk map 12345 has three files: a one-block file with ID 0, a three-block file with ID 1, and a five-block file with ID 2. Using one character for each block where digits are the file ID and . is free space, the disk map 12345 represents these individual blocks:
+
+0..111....22222
+
+The first example above, 2333133121414131402, represents these individual blocks:
+
+00...111...2...333.44.5555.6666.777.888899
+
+The amphipod would like to move file blocks one at a time from the end of the disk to the leftmost free space block (until there are no gaps remaining between file blocks). For the disk map 12345, the process looks like this:
+
+0..111....22222
+02.111....2222.
+022111....222..
+0221112...22...
+02211122..2....
+022111222......
+
+The first example requires a few more steps:
+
+00...111...2...333.44.5555.6666.777.888899
+009..111...2...333.44.5555.6666.777.88889.
+0099.111...2...333.44.5555.6666.777.8888..
+00998111...2...333.44.5555.6666.777.888...
+009981118..2...333.44.5555.6666.777.88....
+0099811188.2...333.44.5555.6666.777.8.....
+009981118882...333.44.5555.6666.777.......
+0099811188827..333.44.5555.6666.77........
+00998111888277.333.44.5555.6666.7.........
+009981118882777333.44.5555.6666...........
+009981118882777333644.5555.666............
+00998111888277733364465555.66.............
+0099811188827773336446555566..............
+
+The final step of this file-compacting process is to update the filesystem checksum. To calculate the checksum, add up the result of multiplying each of these blocks' position with the file ID number it contains. The leftmost block is in position 0. If a block contains free space, skip it instead.
+
+Continuing the first example, the first few blocks' position multiplied by its file ID number are 0 * 0 = 0, 1 * 0 = 0, 2 * 9 = 18, 3 * 9 = 27, 4 * 8 = 32, and so on. In this example, the checksum is the sum of these, 1928.
+
+Compact the amphipod's hard drive using the process he requested. What is the resulting filesystem checksum? (Be careful copy/pasting the input for this puzzle; it is a single, very long line.)
+
+Your puzzle answer was 6340197768906.
+--- Part Two ---
+
+Upon completion, two things immediately become clear. First, the disk definitely has a lot more contiguous free space, just like the amphipod hoped. Second, the computer is running much more slowly! Maybe introducing all of that file system fragmentation was a bad idea?
+
+The eager amphipod already has a new plan: rather than move individual blocks, he'd like to try compacting the files on his disk by moving whole files instead.
+
+This time, attempt to move whole files to the leftmost span of free space blocks that could fit the file. Attempt to move each file exactly once in order of decreasing file ID number starting with the file with the highest file ID number. If there is no span of free space to the left of a file that is large enough to fit the file, the file does not move.
+
+The first example from above now proceeds differently:
+
+00...111...2...333.44.5555.6666.777.888899
+0099.111...2...333.44.5555.6666.777.8888..
+0099.1117772...333.44.5555.6666.....8888..
+0099.111777244.333....5555.6666.....8888..
+00992111777.44.333....5555.6666.....8888..
+
+The process of updating the filesystem checksum is the same; now, this example's checksum would be 2858.
+
+Start over, now compacting the amphipod's hard drive using this new method instead. What is the resulting filesystem checksum?
+
+Your puzzle answer was 6363913128533.
+
+Both parts of this puzzle are complete! They provide two gold stars: **

Fișier diff suprimat deoarece este prea mare
+ 0 - 0
09/input


Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff