/home/cympfh/

はてなブログはクソなのでやめます => https://hackmd.io/s/SyQBwu6Kg#

Advent of Code (Day l-7): Part 2

Thu Dec 24 19:26:29 JST 2015

Advent of Code (Day 1-7): Part 2

Day 1: Part 2

シミュレーション

c=1
gets.chars.each_with_index{|p,i|
  if p == '('
    c += 1
  else
    c -= 1
    if c == 0
      p (i+1)
      exit 0
    end
  end
}

Day 2: Part 2

算数

p open('./input').readlines.map{|line|
  x,y,z=line.chomp.split('x').map(&:to_i)
  (x+y+z-[x,y,z].max)*2+x*y*z
}.reduce :+

Day 3: Part 2

シミュレーション

require 'set'
visited = Set.new [[0,0]]

z = [[0, 0]]*2
dz = {'>' => [1,0], '<' => [-1,0], 'v' => [0,1], '^' => [0,-1]}

gets.chomp.chars.each_with_index{|c,i|
  x,y = z[i%2]
  dx,dy = dz[c]
  x += dx
  y += dy
  visited << [x, y]
  z[i%2] = [x, y]
}
p visited.size

Day 4: Part 2

全探索だとすると 16 ** 6 => 16777216 程度掛かるが果たして?

#!/bin/bash

TOKEN=yzbqklnj

for n in `seq $1 $2`; do
  FROM=$(( 10000 * $n ))
  TO=$(( 10000 * ( $n + 1 ) - 1 ))
  echo $FROM to $TO
  for i in `seq $FROM $TO`
  do
    MD5=$(echo $TOKEN$i | tr -d '\n' | md5sum)
    case $MD5 in
      000000* )
        echo HIT: $i $MD5
        exit 0
        ;;
    esac
  done
  sleep 0.1 # 念の為
done

結果 (とても時間がかかる):

   bash test.sh 60 3000
0 to 9999
10000 to 19999
  :
9900000 to 9909999
9910000 to 9919999
9920000 to 9929999
9930000 to 9939999
9940000 to 9949999
9950000 to 9959999
9960000 to 9969999
HIT: 9962624 0000004b347bf4b398b3f62ace7cd301 -

Day 5: Part 2

実装

p open('./input').readlines.select{|line|
  w = line.chomp
  n = w.size

  p = false
  for i in 0...n
    for j in (i+2)...n
      p = true if (w[i]==w[j] and w[i+1]==w[j+1])
    end
  end

  q = false
  for i in 0...(n-2)
    q = true if (w[i] == w[i+2])
  end

  p and q
}.length

Day 6: Part 2

N=1000
f = N.times.map{[0]*N}

readlines.each{|line|
  ts = line.split
  dl = 0
  if ts[0] == 'toggle'
    dl = 2
    a,b = line.split[1].split(',').map(&:to_i)
    c,d = line.split[3].split(',').map(&:to_i)
  else
    dl = 1
    dl = -1 if ts[1] == 'off'
    a,b = line.split[2].split(',').map(&:to_i)
    c,d = line.split[4].split(',').map(&:to_i)
  end

  for i in a..c
    for j in b..d
      f[i][j] += dl
      f[i][j] = 0 if f[i][j] < 0
    end
  end
}

p f.map{|v|v.reduce :+}.reduce :+
:!time ruby test.rb < input
15343601
ruby test.rb < input  2.95s user 0.00s system 99% cpu 2.953 total

Day 7: Part 2

96c95
<     b = 14146 :: Word16
---
>     b = 956 :: Word16

に書き換えてコンパイルし直すだけ.

Advent of Code (Day l-7): Part 2

Day 1: Part 2

シミュレーション

c=1
gets.chars.each_with_index{|p,i|
  if p == '('
    c += 1
  else
    c -= 1
    if c == 0
      p (i+1)
      exit 0
    end
  end
}

Day 2: Part 2

算数

p open('./input').readlines.map{|line|
  x,y,z=line.chomp.split('x').map(&:to_i)
  (x+y+z-[x,y,z].max)*2+x*y*z
}.reduce :+

Day 3: Part 2

シミュレーション

require 'set'
visited = Set.new [[0,0]]

z = [[0, 0]]*2
dz = {'>' => [1,0], '<' => [-1,0], 'v' => [0,1], '^' => [0,-1]}

gets.chomp.chars.each_with_index{|c,i|
  x,y = z[i%2]
  dx,dy = dz[c]
  x += dx
  y += dy
  visited << [x, y]
  z[i%2] = [x, y]
}
p visited.size

Day 4: Part 2

全探索だとすると 16 ** 6 => 16777216 程度掛かるが果たして?

Day 5: Part 2

実装

p open('./input').readlines.select{|line|
  w = line.chomp
  n = w.size

  p = false
  for i in 0...n
    for j in (i+2)...n
      p = true if (w[i]==w[j] and w[i+1]==w[j+1])
    end
  end

  q = false
  for i in 0...(n-2)
    q = true if (w[i] == w[i+2])
  end

  p and q
}.length

Day 6: Part 2

N=1000
f = N.times.map{[0]*N}

readlines.each{|line|
  ts = line.split
  dl = 0
  if ts[0] == 'toggle'
    dl = 2
    a,b = line.split[1].split(',').map(&:to_i)
    c,d = line.split[3].split(',').map(&:to_i)
  else
    dl = 1
    dl = -1 if ts[1] == 'off'
    a,b = line.split[2].split(',').map(&:to_i)
    c,d = line.split[4].split(',').map(&:to_i)
  end

  for i in a..c
    for j in b..d
      f[i][j] += dl
      f[i][j] = 0 if f[i][j] < 0
    end
  end
}

p f.map{|v|v.reduce :+}.reduce :+
:!time ruby test.rb < input
15343601
ruby test.rb < input  2.95s user 0.00s system 99% cpu 2.953 total

Day 7: Part 2

96c95
<     b = 14146 :: Word16
---
>     b = 956 :: Word16

に書き換えてコンパイルし直すだけ.