Skip to content

Commit 8ad161a

Browse files
committed
Some 2015 puzzles and cleanup
1 parent 0bbc355 commit 8ad161a

22 files changed

+1716
-363
lines changed

src/2015/aoc.js

Lines changed: 0 additions & 290 deletions
This file was deleted.

src/2015/day01.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
AdventOfCode.day01 = {
2+
3+
part1: input => {
4+
let up = input.replace(/\)/g, '').length
5+
let down = input.replace(/\(/g, '').length
6+
return up - down
7+
},
8+
9+
part2: input => {
10+
let floor = 0
11+
let cmds = input.split('')
12+
for (let i = 0; i < cmds.length; i++) {
13+
floor += cmds[i] === '(' ? 1 : -1
14+
if (floor === -1) return i + 1
15+
}
16+
}
17+
18+
}
File renamed without changes.

src/2015/day02.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
AdventOfCode.day02 = {
2+
3+
part1: input => {
4+
let presents = input.split('\n').map(line => line.split('x').map(i => parseInt(i)))
5+
let size = 0
6+
presents.forEach(present => {
7+
let w = present[0]
8+
let h = present[1]
9+
let l = present[2]
10+
size += 2*l*w + 2*w*h + 2*h*l + Math.min(l*w, w*h, h*l)
11+
})
12+
return size
13+
},
14+
15+
part2: input => {
16+
let presents = input.split('\n').map(line => line.split('x').map(i => parseInt(i)))
17+
let length = 0
18+
presents.forEach(present => {
19+
let w = present[0]
20+
let h = present[1]
21+
let l = present[2]
22+
let o = [w, h, l].sort((a, b) => a > b)
23+
length += o[0]*2 + o[1]*2 + w*h*l
24+
})
25+
return length
26+
}
27+
28+
}
File renamed without changes.

src/2015/day03.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
AdventOfCode.day03 = {
2+
3+
part1: input => {
4+
let x = 0, y = 0
5+
let houses = {}
6+
let gift = () => houses[x+','+y] = houses[x+','+y] ? houses[x+','+y] + 1 : 1
7+
gift()
8+
input.split('').forEach(cmd => {
9+
if (cmd === '>') x++
10+
else if (cmd === '<') x--
11+
else if (cmd === 'v') y++
12+
else if (cmd === '^') y--
13+
gift()
14+
})
15+
return Object.keys(houses).length
16+
},
17+
18+
part2: input => {
19+
let sx = 0, sy = 0, rx = 0, ry = 0
20+
let houses = {'0,0': 1}
21+
let gift = (x, y) => houses[x+','+y] = houses[x+','+y] ? houses[x+','+y] + 1 : 1
22+
input.split('').forEach((cmd, i) => {
23+
if (cmd === '>') i%2 ? sx++ : rx++
24+
else if (cmd === '<') i%2 ? sx-- : rx--
25+
else if (cmd === 'v') i%2 ? sy++ : ry++
26+
else if (cmd === '^') i%2 ? sy-- : ry--
27+
gift(i%2 ? sx : rx, i%2 ? sy : ry)
28+
})
29+
return Object.keys(houses).length
30+
}
31+
32+
}
File renamed without changes.

src/2015/day04.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
AdventOfCode.day04 = {
2+
3+
slowWarning: true,
4+
5+
process: (input, start) => {
6+
let hash = ''
7+
let i = 0
8+
do hash = md5(input + i++)
9+
while (hash.substr(0, start.length) !== start)
10+
return i - 1
11+
},
12+
13+
part1: input => AdventOfCode.day04.process(input, '00000'),
14+
part2: input => AdventOfCode.day04.process(input, '000000')
15+
16+
}
File renamed without changes.

0 commit comments

Comments
 (0)