day 2
This commit is contained in:
		
							
								
								
									
										120
									
								
								02/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										120
									
								
								02/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,120 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bufio"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func mustAtoi(line string) int {
 | 
				
			||||||
 | 
						i, _ := strconv.Atoi(line)
 | 
				
			||||||
 | 
						return i
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func main() {
 | 
				
			||||||
 | 
						start := time.Now()
 | 
				
			||||||
 | 
						partOne()
 | 
				
			||||||
 | 
						duration := time.Since(start)
 | 
				
			||||||
 | 
						partTwo()
 | 
				
			||||||
 | 
						duration2 := time.Since(start)
 | 
				
			||||||
 | 
						fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func makeScanner(test bool) *bufio.Scanner {
 | 
				
			||||||
 | 
						var f *os.File
 | 
				
			||||||
 | 
						if test {
 | 
				
			||||||
 | 
							f, _ = os.Open("inputs/testinput")
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							f, _ = os.Open("inputs/input")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						reader := bufio.NewReader(f)
 | 
				
			||||||
 | 
						return bufio.NewScanner(reader)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type game struct {
 | 
				
			||||||
 | 
						red   int
 | 
				
			||||||
 | 
						green int
 | 
				
			||||||
 | 
						blue  int
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func partOne() {
 | 
				
			||||||
 | 
						scanner := makeScanner(false)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						sum := 0
 | 
				
			||||||
 | 
						round := 0
 | 
				
			||||||
 | 
						max := game{
 | 
				
			||||||
 | 
							red:   12,
 | 
				
			||||||
 | 
							green: 13,
 | 
				
			||||||
 | 
							blue:  14,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for scanner.Scan() {
 | 
				
			||||||
 | 
							line := scanner.Text()
 | 
				
			||||||
 | 
							ok := true
 | 
				
			||||||
 | 
							round++
 | 
				
			||||||
 | 
							_, draws, _ := strings.Cut(line, ": ")
 | 
				
			||||||
 | 
							for _, draws := range strings.Split(draws, "; ") {
 | 
				
			||||||
 | 
								iter := strings.Split(draws, " ")
 | 
				
			||||||
 | 
								g := game{}
 | 
				
			||||||
 | 
								for i := 0; i < len(iter); i++ {
 | 
				
			||||||
 | 
									seen := mustAtoi(iter[i])
 | 
				
			||||||
 | 
									i++
 | 
				
			||||||
 | 
									switch iter[i][0] {
 | 
				
			||||||
 | 
									case 'r':
 | 
				
			||||||
 | 
										g.red = seen
 | 
				
			||||||
 | 
									case 'g':
 | 
				
			||||||
 | 
										g.green = seen
 | 
				
			||||||
 | 
									case 'b':
 | 
				
			||||||
 | 
										g.blue = seen
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if g.red > max.red || g.blue > max.blue || g.green > max.green {
 | 
				
			||||||
 | 
									ok = false
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if ok {
 | 
				
			||||||
 | 
								sum += round
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Println(sum)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func partTwo() {
 | 
				
			||||||
 | 
						scanner := makeScanner(false)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						sum := 0
 | 
				
			||||||
 | 
						for scanner.Scan() {
 | 
				
			||||||
 | 
							line := scanner.Text()
 | 
				
			||||||
 | 
							_, draws, _ := strings.Cut(line, ": ")
 | 
				
			||||||
 | 
							max := game{}
 | 
				
			||||||
 | 
							for _, draws := range strings.Split(draws, "; ") {
 | 
				
			||||||
 | 
								iter := strings.Split(draws, " ")
 | 
				
			||||||
 | 
								g := game{}
 | 
				
			||||||
 | 
								for i := 0; i < len(iter); i++ {
 | 
				
			||||||
 | 
									seen := mustAtoi(iter[i])
 | 
				
			||||||
 | 
									i++
 | 
				
			||||||
 | 
									switch iter[i][0] {
 | 
				
			||||||
 | 
									case 'r':
 | 
				
			||||||
 | 
										g.red = seen
 | 
				
			||||||
 | 
									case 'g':
 | 
				
			||||||
 | 
										g.green = seen
 | 
				
			||||||
 | 
									case 'b':
 | 
				
			||||||
 | 
										g.blue = seen
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if g.red > max.red {
 | 
				
			||||||
 | 
									max.red = g.red
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if g.blue > max.blue {
 | 
				
			||||||
 | 
									max.blue = g.blue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if g.green > max.green {
 | 
				
			||||||
 | 
									max.green = g.green
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							sum += max.blue * max.green * max.red
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Println(sum)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -3,14 +3,18 @@
 | 
				
			|||||||
    (chicken format)
 | 
					    (chicken format)
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(define print-line
 | 
				
			||||||
 | 
					    (lambda (line)
 | 
				
			||||||
 | 
					    (printf "~A~%" line)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(call-with-input-file "inputs/input"
 | 
					(call-with-input-file "inputs/input"
 | 
				
			||||||
    (lambda (port)
 | 
					    (lambda (port)
 | 
				
			||||||
        (map
 | 
					        (map
 | 
				
			||||||
            (lambda (line) (printf "~A~%" line))
 | 
					            print-line
 | 
				
			||||||
            (read-lines port))))
 | 
					            (read-lines port))))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(call-with-input-file "inputs/input"
 | 
					(call-with-input-file "inputs/input"
 | 
				
			||||||
    (lambda (port)
 | 
					    (lambda (port)
 | 
				
			||||||
        (map
 | 
					        (map
 | 
				
			||||||
            (lambda (line) (printf "~A~%" line))
 | 
					            print-line
 | 
				
			||||||
            (read-lines port))))
 | 
					            (read-lines port))))
 | 
				
			||||||
							
								
								
									
										6
									
								
								new.sh
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								new.sh
									
									
									
									
									
								
							@@ -14,8 +14,8 @@ if [ "${1}" != "" ]; then
 | 
				
			|||||||
    if [ ! -f "${padded}/main.go" ]; then
 | 
					    if [ ! -f "${padded}/main.go" ]; then
 | 
				
			||||||
        cp -n main.go.tmpl ${padded}/main.go
 | 
					        cp -n main.go.tmpl ${padded}/main.go
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
    if [ ! -f "${padded}/main.scm" ]; then
 | 
					    # if [ ! -f "${padded}/main.scm" ]; then
 | 
				
			||||||
        cp -n main.scm.tmpl ${padded}/main.scm
 | 
					    #     cp -n main.scm.tmpl ${padded}/main.scm
 | 
				
			||||||
    fi
 | 
					    # fi
 | 
				
			||||||
    echo "https://adventofcode.com/${__year}/day/${1}"
 | 
					    echo "https://adventofcode.com/${__year}/day/${1}"
 | 
				
			||||||
fi
 | 
					fi
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user