Advertisement
Guest User

Untitled

a guest
Dec 14th, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.29 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "os"
  7.     "strconv"
  8.     "strings"
  9. )
  10.  
  11. type disc struct {
  12.     positions, initial int
  13. }
  14.  
  15. func main() {
  16.     fmt.Println("Day 15 of Advent of Code 2016")
  17.     f, err := os.Open("input")
  18.     check(err)
  19.  
  20.     input := read_input(f)
  21.  
  22.     var discs []disc
  23.     for _, line := range input {
  24.         fields := strings.Fields(line)
  25.         positions, err := strconv.Atoi(fields[3])
  26.         check(err)
  27.         initial, err := strconv.Atoi(fields[11][:len(fields[11])-1])
  28.         check(err)
  29.         discs = append(discs, disc{positions, initial})
  30.     }
  31.  
  32.     var time int
  33.     for time = 0; !is_success(discs, time); time++ {
  34.     }
  35.  
  36.     fmt.Printf("Part 1: %d\n", time)
  37.  
  38.     discs = append(discs, disc{11, 0})
  39.  
  40.     var new_time int
  41.     for new_time = 0; !is_success(discs, new_time); new_time++ {
  42.     }
  43.  
  44.     fmt.Printf("Part 2: %d\n", new_time)
  45. }
  46.  
  47. func is_success(ds []disc, initial_time int) bool {
  48.     for i, disc := range ds {
  49.         if !(rotate_disc(disc, i+initial_time+1) == 0) {
  50.             return false
  51.         }
  52.     }
  53.     return true
  54. }
  55.  
  56. func rotate_disc(d disc, t int) int {
  57.     return (d.initial + t) % d.positions
  58. }
  59.  
  60. func check(e error) {
  61.     if e != nil {
  62.         panic(e)
  63.     }
  64. }
  65.  
  66. func read_input(f *os.File) []string {
  67.     scanner := bufio.NewScanner(f)
  68.     data := []string{}
  69.     for scanner.Scan() {
  70.         data = append(data, scanner.Text())
  71.     }
  72.     return data
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement