5 次代碼提交 422f5fa3ae ... 6681232766

作者 SHA1 備註 提交日期
  Piotr Czajkowski 6681232766 Borrowed some code 10 月之前
  Piotr Czajkowski 3177861009 Refactor before part2 10 月之前
  Piotr Czajkowski 3cb9ed5919 Solved part1 10 月之前
  Piotr Czajkowski 66404a27b5 Not there yet 10 月之前
  Piotr Czajkowski 54feaffda2 Able to read input 10 月之前
共有 3 個文件被更改,包括 687 次插入 和 0 次删除
  1. 156 0
      09/code.go
  2. 496 0
      09/input
  3. 35 0
      09/stack.go

+ 156 - 0
09/code.go

@@ -0,0 +1,156 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"log"
+	"os"
+	"sort"
+)
+
+type Point struct {
+	X, Y int
+}
+
+type PointList []Point
+
+func (p PointList) Len() int {
+	return len(p)
+}
+
+func (p PointList) Swap(i, j int) {
+	p[i], p[j] = p[j], p[i]
+}
+
+func Area2(a, b, c Point) int {
+	return (b.X-a.X)*(c.Y-a.Y) - (c.X-a.X)*(b.Y-a.Y)
+}
+
+func abs(a int) int {
+	if a < 0 {
+		return -a
+	}
+
+	return a
+}
+
+func (p PointList) Less(i, j int) bool {
+	area := Area2(p[0], p[i], p[j])
+	if area == 0 {
+		x := abs(p[i].X-p[0].X) - abs(p[j].X-p[0].X)
+		y := abs(p[i].Y-p[0].Y) - abs(p[j].Y-p[0].Y)
+		if x < 0 || y < 0 {
+			return true
+		} else if x > 0 || y > 0 {
+			return false
+		} else {
+			return false
+		}
+	}
+	return area > 0
+}
+
+func (p PointList) FindLowestPoint() {
+	m := 0
+	for i := 1; i < len(p); i++ {
+		//If lowest points are on the same line, take the rightmost point
+		if (p[i].Y < p[m].Y) || ((p[i].Y == p[m].Y) && p[i].X > p[m].X) {
+			m = i
+		}
+	}
+	p[0], p[m] = p[m], p[0]
+}
+
+func isLeft(p0, p1, p2 Point) bool {
+	return Area2(p0, p1, p2) > 0
+}
+
+func (points PointList) Compute() (PointList, bool) {
+	if len(points) < 3 {
+		return nil, false
+	}
+
+	stack := new(Stack)
+	points.FindLowestPoint()
+	sort.Sort(&points)
+
+	stack.Push(points[0])
+	stack.Push(points[1])
+
+	i := 2
+	for i < len(points) {
+		pi := points[i]
+		p1 := stack.top.next.value.(Point)
+		p2 := stack.top.value.(Point)
+		if isLeft(p1, p2, pi) {
+			stack.Push(pi)
+			i++
+		} else {
+			stack.Pop()
+		}
+	}
+
+	//Copy the hull
+	ret := make(PointList, stack.Len())
+	top := stack.top
+	count := 0
+	for top != nil {
+		ret[count] = top.value.(Point)
+		top = top.next
+		count++
+	}
+	return ret, true
+}
+
+func readInput(file *os.File) []Point {
+	scanner := bufio.NewScanner(file)
+	var tiles []Point
+
+	for scanner.Scan() {
+		line := scanner.Text()
+		if line == "" {
+			continue
+		}
+
+		var x, y int
+		n, err := fmt.Sscanf(line, "%d,%d", &x, &y)
+		if n != 2 || err != nil {
+			log.Fatalf("Bad input: %s", line)
+		}
+
+		tiles = append(tiles, Point{X: x, Y: y})
+	}
+
+	return tiles
+}
+
+func part1(tiles []Point) int {
+	end := len(tiles)
+	var maxArea int
+
+	for i := range tiles {
+		for j := i + 1; j < end; j++ {
+			area := (abs(tiles[j].X-tiles[i].X) + 1) * (abs(tiles[j].Y-tiles[i].Y) + 1)
+			if area > maxArea {
+				maxArea = area
+			}
+		}
+	}
+
+	return maxArea
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		log.Fatal("You need to specify a file!")
+	}
+
+	filePath := os.Args[1]
+	file, err := os.Open(filePath)
+	if err != nil {
+		log.Fatalf("Failed to open %s!\n", filePath)
+	}
+
+	tiles := readInput(file)
+	fmt.Println("Part1:", part1(tiles))
+}

+ 496 - 0
09/input

@@ -0,0 +1,496 @@
+97813,50305
+97813,51516
+97783,51516
+97783,52726
+97723,52726
+97723,53925
+97529,53925
+97529,55124
+97383,55124
+97383,56376
+97639,56376
+97639,57596
+97555,57596
+97555,58689
+96759,58689
+96759,60060
+97414,60060
+97414,61189
+96856,61189
+96856,62354
+96490,62354
+96490,63466
+95949,63466
+95949,64751
+95985,64751
+95985,65812
+95312,65812
+95312,66989
+94987,66989
+94987,68037
+94332,68037
+94332,69127
+93798,69127
+93798,70242
+93324,70242
+93324,71482
+93096,71482
+93096,72495
+92402,72495
+92402,73478
+91670,73478
+91670,74710
+91370,74710
+91370,75506
+90344,75506
+90344,76548
+89728,76548
+89728,77514
+88997,77514
+88997,78864
+88786,78864
+88786,79393
+87475,79393
+87475,80380
+86776,80380
+86776,81370
+86073,81370
+86073,82288
+85282,82288
+85282,83613
+84912,83613
+84912,83898
+83467,83898
+83467,84711
+82575,84711
+82575,85781
+81914,85781
+81914,86670
+81076,86670
+81076,87284
+80007,87284
+80007,88497
+79407,88497
+79407,89236
+78427,89236
+78427,89627
+77207,89627
+77207,90199
+76126,90199
+76126,91291
+75372,91291
+75372,91401
+74016,91401
+74016,91983
+72952,91983
+72952,92251
+71727,92251
+71727,93516
+71001,93516
+71001,93679
+69732,93679
+69732,94022
+68559,94022
+68559,95078
+67673,95078
+67673,94873
+66293,94873
+66293,95377
+65187,95377
+65187,95478
+63950,95478
+63950,96455
+62972,96455
+62972,96602
+61748,96602
+61748,96579
+60495,96579
+60495,97372
+59420,97372
+59420,97691
+58233,97691
+58233,97735
+57000,97735
+57000,98001
+55801,98001
+55801,97591
+54532,97591
+54532,98041
+53349,98041
+53349,97790
+52117,97790
+52117,98141
+50912,98141
+50912,97902
+49693,97902
+49693,97770
+48483,97770
+48483,98135
+47250,98135
+47250,97667
+46063,97667
+46063,97599
+44852,97599
+44852,97538
+43636,97538
+43636,97004
+42491,97004
+42491,97602
+41153,97602
+41153,97327
+39958,97327
+39958,96847
+38812,96847
+38812,96442
+37658,96442
+37658,96120
+36483,96120
+36483,95254
+35482,95254
+35482,95468
+34133,95468
+34133,95012
+33001,95012
+33001,94106
+32054,94106
+32054,93769
+30884,93769
+30884,93651
+29604,93651
+29604,92861
+28634,92861
+28634,92406
+27502,92406
+27502,91998
+26336,91998
+26336,91169
+25409,91169
+25409,90232
+24564,90232
+24564,90177
+23150,90177
+23150,89560
+22088,89560
+22088,88650
+21236,88650
+21236,88118
+20102,88118
+20102,86741
+19648,86741
+19648,86162
+18552,86162
+18552,85221
+17767,85221
+17767,84875
+16421,84875
+16421,83824
+15740,83824
+15740,82993
+14843,82993
+14843,81945
+14183,81945
+14183,80857
+13587,80857
+13587,80371
+12262,80371
+12262,79424
+11480,79424
+11480,78153
+11142,78153
+11142,76942
+10758,76942
+10758,76346
+9462,76346
+9462,75317
+8797,75317
+8797,74285
+8134,74285
+8134,73099
+7748,73099
+7748,71829
+7551,71829
+7551,70968
+6553,70968
+6553,69730
+6324,69730
+6324,68735
+5558,68735
+5558,67351
+5741,67351
+5741,66220
+5328,66220
+5328,65210
+4554,65210
+4554,64094
+4051,64094
+4051,62798
+4165,62798
+4165,61656
+3764,61656
+3764,60576
+3060,60576
+3060,59449
+2481,59449
+2481,58187
+2574,58187
+2574,56960
+2538,56960
+2538,55715
+2716,55715
+2716,54590
+1802,54590
+1802,53347
+1991,53347
+1991,52121
+2114,52121
+2114,50911
+1916,50911
+1916,50285
+94619,50285
+94619,48466
+1668,48466
+1668,47274
+2282,47274
+2282,46020
+1813,46020
+1813,44862
+2493,44862
+2493,43617
+2321,43617
+2321,42430
+2611,42430
+2611,41152
+2391,41152
+2391,40031
+3016,40031
+3016,38816
+3168,38816
+3168,37657
+3556,37657
+3556,36624
+4359,36624
+4359,35231
+3961,35231
+3961,34126
+4512,34126
+4512,33010
+5012,33010
+5012,31914
+5549,31914
+5549,30782
+5995,30782
+5995,29877
+6931,29877
+6931,28750
+7371,28750
+7371,27478
+7547,27478
+7547,26548
+8377,26548
+8377,25638
+9214,25638
+9214,24112
+9053,24112
+9053,23078
+9714,23078
+9714,22544
+11086,22544
+11086,21315
+11456,21315
+11456,20475
+12356,20475
+12356,19659
+13271,19659
+13271,18667
+13970,18667
+13970,17259
+14223,17259
+14223,16823
+15542,16823
+15542,15619
+16055,15619
+16055,14805
+16970,14805
+16970,14058
+17942,14058
+17942,13467
+19040,13467
+19040,12958
+20188,12958
+20188,11496
+20587,11496
+20587,11082
+21803,11082
+21803,10241
+22702,10241
+22702,9851
+23906,9851
+23906,8932
+24765,8932
+24765,8868
+26139,8868
+26139,7628
+26834,7628
+26834,7179
+27979,7179
+27979,6608
+29058,6608
+29058,6132
+30182,6132
+30182,5897
+31407,5897
+31407,4952
+32338,4952
+32338,5188
+33728,5188
+33728,4766
+34860,4766
+34860,4099
+35920,4099
+35920,3577
+37037,3577
+37037,3198
+38200,3198
+38200,3484
+39518,3484
+39518,3138
+40681,3138
+40681,3177
+41916,3177
+41916,2437
+43024,2437
+43024,2285
+44232,2285
+44232,1805
+45409,1805
+45409,1997
+46652,1997
+46652,1708
+47860,1708
+47860,1841
+49087,1841
+49087,1594
+50309,1594
+50309,2106
+51519,2106
+51519,2058
+52738,2058
+52738,1966
+53966,1966
+53966,2505
+55136,2505
+55136,2336
+56380,2336
+56380,2580
+57574,2580
+57574,2857
+58760,2857
+58760,2730
+60029,2730
+60029,3421
+61122,3421
+61122,3902
+62250,3902
+62250,4274
+63400,4274
+63400,4367
+64638,4367
+64638,4601
+65842,4601
+65842,5080
+66963,5080
+66963,5654
+68043,5654
+68043,5981
+69223,5981
+69223,6400
+70371,6400
+70371,7257
+71306,7257
+71306,7591
+72498,7591
+72498,8201
+73550,8201
+73550,8781
+74620,8781
+74620,9577
+75555,9577
+75555,10299
+76530,10299
+76530,10753
+77690,10753
+77690,11565
+78602,11565
+78602,12232
+79622,12232
+79622,13007
+80559,13007
+80559,13953
+81346,13953
+81346,14655
+82345,14655
+82345,15256
+83451,15256
+83451,16393
+84038,16393
+84038,17386
+84750,17386
+84750,18236
+85611,18236
+85611,19061
+86508,19061
+86508,20065
+87194,20065
+87194,20596
+88491,20596
+88491,22004
+88639,22004
+88639,22938
+89414,22938
+89414,23814
+90289,23814
+90289,24788
+91029,24788
+91029,26054
+91278,26054
+91278,27083
+91915,27083
+91915,27942
+92892,27942
+92892,28921
+93674,28921
+93674,30316
+93570,30316
+93570,31326
+94293,31326
+94293,32527
+94565,32527
+94565,33724
+94822,33724
+94822,34633
+95912,34633
+95912,35867
+96072,35867
+96072,37131
+96085,37131
+96085,38183
+96868,38183
+96868,39344
+97288,39344
+97288,40571
+97413,40571
+97413,41788
+97564,41788
+97564,43033
+97498,43033
+97498,44296
+97190,44296
+97190,45426
+98016,45426
+98016,46666
+97801,46666
+97801,47894
+97514,47894
+97514,49086
+98205,49086
+98205,50305

+ 35 - 0
09/stack.go

@@ -0,0 +1,35 @@
+// Stack implementation from
+// https://gist.github.com/bemasher/1777766
+package main
+
+type Stack struct {
+	top  *Element
+	size int
+}
+
+type Element struct {
+	value interface{} // All types satisfy the empty interface, so we can store anything here.
+	next  *Element
+}
+
+// Return the stack's length
+func (s *Stack) Len() int {
+	return s.size
+}
+
+// Push a new element onto the stack
+func (s *Stack) Push(value interface{}) {
+	s.top = &Element{value, s.top}
+	s.size++
+}
+
+// Remove the top element from the stack and return it's value
+// If the stack is empty, return nil
+func (s *Stack) Pop() (value interface{}) {
+	if s.size > 0 {
+		value, s.top = s.top.value, s.top.next
+		s.size--
+		return
+	}
+	return nil
+}